Skip to content

Commit e1fae49

Browse files
committed
Create Subscription adjustments
1 parent f87ab92 commit e1fae49

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

components/chargebee/actions/create-subscription/create-subscription.mjs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ export default {
1515
"customerId",
1616
],
1717
},
18+
itemPriceId: {
19+
propDefinition: [
20+
chargebee,
21+
"itemPriceId"
22+
],
23+
},
24+
unitPrice: {
25+
type: "integer",
26+
label: "Unit Price",
27+
description: "The unit price of the plan item.",
28+
},
29+
quantity: {
30+
type: "integer",
31+
label: "Quantity",
32+
description: "The quantity of the plan item.",
33+
},
1834
id: {
1935
type: "string",
2036
label: "ID",
@@ -41,14 +57,27 @@ export default {
4157
},
4258
},
4359
async run({ $ }) {
44-
const response = await this.chargebee.createCustomer(this.customerId, clearObject({
60+
try {
61+
const response = await this.chargebee.createSubscription(this.customerId, clearObject({
4562
id: this.id,
4663
net_term_days: this.netTermDays,
4764
start_date: this.startDate && (Date.parse(this.startDate) / 1000),
65+
subscription_items: [
66+
{
67+
item_price_id: this.itemPriceId,
68+
item_type: "plan",
69+
unit_price: this.unitPrice,
70+
quantity: this.quantity,
71+
}
72+
],
4873
...this.additionalFields,
4974
}));
5075

5176
$.export("$summary", `Successfully created subscription (ID: ${response?.subscription?.id})`);
5277
return response;
78+
} catch (error) {
79+
$.export("debug", error);
80+
throw new Error("Error creating subscription. Check the debug export for more information.")
81+
}
5382
},
5483
};
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chargebee from "chargebee";
1+
import Chargebee from "chargebee";
22

33
export default {
44
type: "app",
@@ -10,44 +10,58 @@ export default {
1010
description: "The ID of the customer to create the subscription for.",
1111
async options() {
1212
const customers = await this.getCustomers();
13-
return customers.list.map((customer) => ({
13+
return customers.list.map(({ customer }) => ({
1414
label: `${customer.first_name ?? ""} ${customer.last_name ?? ""} (${customer.email ?? customer.id})`,
1515
value: customer.id,
1616
}));
1717
},
1818
},
19+
itemPriceId: {
20+
type: "string",
21+
label: "Item Price ID",
22+
description: "The unique identifier of the plan item price.",
23+
async options() {
24+
const itemPrices = await this.getItemPrices();
25+
return itemPrices.list.map(({ item_price: { name, id } }) => ({
26+
label: name,
27+
value: id,
28+
}));
29+
},
30+
},
1931
},
2032
methods: {
2133
instance() {
22-
chargebee.configure({
34+
return new Chargebee({
2335
site: this.$auth.sub_url,
24-
api_key: this.$auth.api_key,
36+
apiKey: this.$auth.api_key,
2537
});
26-
return chargebee;
2738
},
2839
getSubscriptions(args = {}) {
29-
return this.instance().subscription.list(args).request();
40+
return this.instance().subscription.list(args);
3041
},
3142
getTransactions(args = {}) {
32-
return this.instance().transaction.list(args).request();
43+
return this.instance().transaction.list(args);
3344
},
3445
getCustomers(args = {}) {
35-
return this.instance().customer.list(args).request();
46+
return this.instance().customer.list(args);
3647
},
3748
getInvoices(args = {}) {
38-
return this.instance().invoice.list(args).request();
49+
return this.instance().invoice.list(args);
3950
},
4051
getPaymentSources(args = {}) {
41-
return this.instance().payment_source.list(args).request();
52+
return this.instance().paymentSource.list(args);
4253
},
4354
getEvents(args = {}) {
44-
return this.instance().event.list(args).request();
55+
return this.instance().event.list(args);
4556
},
4657
createCustomer(args = {}) {
47-
return this.instance().customer.create(args).request();
58+
return this.instance().customer.create(args);
4859
},
4960
createSubscription(customerId, args = {}) {
50-
return this.instance().subscription.create_for_customer(customerId, args).request();
61+
return this.instance().subscription.createWithItems(customerId, args);
62+
},
63+
getItemPrices(args = {}) {
64+
return this.instance().itemPrice.list(args);
5165
},
5266
},
5367
};

components/chargebee/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"license": "MIT",
1313
"dependencies": {
1414
"@pipedream/platform": "^3.0.3",
15-
"chargebee": "^2.22.3"
15+
"chargebee": "^3.2.1"
1616
},
1717
"publishConfig": {
1818
"access": "public"

0 commit comments

Comments
 (0)