Skip to content

Commit 141c90f

Browse files
committed
Create Subscription action
1 parent 9a70095 commit 141c90f

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import chargebee from "../../chargebee.app.mjs";
2+
import { clearObject } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "chargebee-create-subscription",
6+
name: "Create Subscription",
7+
description: "Create a new subscription for an existing customer. [See the documentation](https://apidocs.chargebee.com/docs/api/subscriptions?lang=curl#create_subscription_for_items)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
chargebee,
12+
customerId: {
13+
propDefinition: [
14+
chargebee,
15+
"customerId",
16+
]
17+
},
18+
id: {
19+
type: "string",
20+
label: "ID",
21+
description: "A unique and immutable identifier for the subscription. If not provided, it is autogenerated.",
22+
optional: true,
23+
},
24+
netTermDays: {
25+
type: "integer",
26+
label: "Net Term Days",
27+
description: "Defines [Net D](https://www.chargebee.com/docs/net_d.html?_gl=1*1w075xz*_gcl_au*MTU4NzU2NDYzOC4xNzMzODA0OTYw) for the subscription. Net D is the number of days within which any invoice raised for the subscription must be paid.",
28+
optional: true,
29+
},
30+
startDate: {
31+
type: "string",
32+
label: "Start Date",
33+
description: "The date/time at which the subscription is to start, e.g. `2024-08-15T09:30:00Z`. If not provided, the subscription starts immediately.",
34+
optional: true,
35+
},
36+
additionalFields: {
37+
type: "object",
38+
label: "Additional Fields",
39+
description: "Additional fields and values to set for the subscription. [See the documentation](https://apidocs.chargebee.com/docs/api/subscriptions?lang=curl#create_subscription_for_items) for all available fields.",
40+
optional: true,
41+
},
42+
},
43+
async run({ $ }) {
44+
const response = await this.chargebee.createCustomer(this.customerId, clearObject({
45+
id: this.id,
46+
net_term_days: this.netTermDays,
47+
start_date: this.startDate && (Date.parse(this.startDate) / 1000),
48+
...this.additionalFields,
49+
}));
50+
51+
$.export("$summary", `Successfully created subscription (ID: ${response?.subscription?.id})`);
52+
return response;
53+
},
54+
};

components/chargebee/chargebee.app.mjs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import chargebee from "chargebee";
33
export default {
44
type: "app",
55
app: "chargebee",
6+
propDefinitions: {
7+
customerId: {
8+
type: "string",
9+
label: "Customer ID",
10+
description: "The ID of the customer to create the subscription for.",
11+
async options() {
12+
const customers = await this.getCustomers();
13+
return customers.list.map((customer) => ({
14+
label: `${customer.first_name ?? ''} ${customer.last_name ?? ''} (${customer.email ?? customer.id})`,
15+
value: customer.id,
16+
}));
17+
}
18+
}
19+
},
620
methods: {
721
instance() {
822
chargebee.configure({
@@ -31,6 +45,9 @@ export default {
3145
},
3246
createCustomer(args = {}) {
3347
return this.instance().customer.create(args).request();
34-
}
48+
},
49+
createSubscription(customerId, args = {}) {
50+
return this.instance().subscription.create_for_customer(customerId, args).request();
51+
},
3552
},
3653
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function clearObject(obj) {
2+
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== undefined));
3+
}

0 commit comments

Comments
 (0)