Skip to content

Commit ace38f7

Browse files
Chargebee new actions (#14906)
* Create Customer action * Create Subscription action * ESLint and pnpm-lock * Source version bumps * ESLint fixes * Create Subscription adjustments * pnpm-lock * ESLint fixes * Adjusting return value and error message * Adding Plan Item Price ID filter * ESLint * Improve error message --------- Co-authored-by: Leo Vu <[email protected]>
1 parent 9b99998 commit ace38f7

File tree

26 files changed

+239
-44
lines changed

26 files changed

+239
-44
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import chargebee from "../../chargebee.app.mjs";
2+
import { clearObject } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "chargebee-create-customer",
6+
name: "Create Customer",
7+
description: "Create a customer in Chargebee. [See the documentation](https://apidocs.chargebee.com/docs/api/customers?lang=node-v3#create_a_customer)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
chargebee,
12+
id: {
13+
type: "string",
14+
label: "ID",
15+
description: "ID for the new customer. If not given, this will be auto-generated.",
16+
optional: true,
17+
},
18+
firstName: {
19+
type: "string",
20+
label: "First Name",
21+
description: "First name of the customer.",
22+
optional: true,
23+
},
24+
lastName: {
25+
type: "string",
26+
label: "Last Name",
27+
description: "Last name of the customer.",
28+
optional: true,
29+
},
30+
email: {
31+
type: "string",
32+
label: "Email",
33+
description: "Email of the customer.",
34+
optional: true,
35+
},
36+
phone: {
37+
type: "string",
38+
label: "Phone",
39+
description: "Phone number of the customer.",
40+
optional: true,
41+
},
42+
company: {
43+
type: "string",
44+
label: "Company",
45+
description: "Company name of the customer.",
46+
optional: true,
47+
},
48+
additionalFields: {
49+
type: "object",
50+
label: "Additional Fields",
51+
description: "Additional fields and values to set for the customer. [See the documentation](https://apidocs.chargebee.com/docs/api/customers?lang=curl#create_a_customer) for all available fields.",
52+
optional: true,
53+
},
54+
},
55+
async run({ $ }) {
56+
const response = await this.chargebee.createCustomer(clearObject({
57+
id: this.id,
58+
first_name: this.firstName,
59+
last_name: this.lastName,
60+
email: this.email,
61+
phone: this.phone,
62+
company: this.company,
63+
...this.additionalFields,
64+
}));
65+
66+
$.export("$summary", `Successfully created customer (ID: ${response?.customer?.id})`);
67+
return response?.customer ?? response;
68+
},
69+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
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+
},
34+
id: {
35+
type: "string",
36+
label: "ID",
37+
description: "A unique and immutable identifier for the subscription. If not provided, it is autogenerated.",
38+
optional: true,
39+
},
40+
netTermDays: {
41+
type: "integer",
42+
label: "Net Term Days",
43+
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.",
44+
optional: true,
45+
},
46+
startDate: {
47+
type: "string",
48+
label: "Start Date",
49+
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.",
50+
optional: true,
51+
},
52+
additionalFields: {
53+
type: "object",
54+
label: "Additional Fields",
55+
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.",
56+
optional: true,
57+
},
58+
},
59+
async run({ $ }) {
60+
try {
61+
const response = await this.chargebee.createSubscription(this.customerId, clearObject({
62+
id: this.id,
63+
net_term_days: this.netTermDays,
64+
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+
],
73+
...this.additionalFields,
74+
}));
75+
76+
$.export("$summary", `Successfully created subscription (ID: ${response?.subscription?.id})`);
77+
return response;
78+
} catch (error) {
79+
$.export("debug", error);
80+
throw new Error(`Error creating subscription: ${error.message}`);
81+
}
82+
},
83+
};
Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,73 @@
1-
import chargebee from "chargebee";
1+
import Chargebee from "chargebee";
22

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+
itemPriceId: {
20+
type: "string",
21+
label: "Plan 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
26+
.filter(({ item_price: { item_type } }) => item_type === "plan")
27+
.map(({
28+
item_price: {
29+
name, id,
30+
},
31+
}) => ({
32+
label: name,
33+
value: id,
34+
}));
35+
},
36+
},
37+
},
638
methods: {
739
instance() {
8-
chargebee.configure({
40+
return new Chargebee({
941
site: this.$auth.sub_url,
10-
api_key: this.$auth.api_key,
42+
apiKey: this.$auth.api_key,
1143
});
12-
return chargebee;
1344
},
1445
getSubscriptions(args = {}) {
15-
return this.instance().subscription.list(args).request();
46+
return this.instance().subscription.list(args);
1647
},
1748
getTransactions(args = {}) {
18-
return this.instance().transaction.list(args).request();
49+
return this.instance().transaction.list(args);
1950
},
2051
getCustomers(args = {}) {
21-
return this.instance().customer.list(args).request();
52+
return this.instance().customer.list(args);
2253
},
2354
getInvoices(args = {}) {
24-
return this.instance().invoice.list(args).request();
55+
return this.instance().invoice.list(args);
2556
},
2657
getPaymentSources(args = {}) {
27-
return this.instance().payment_source.list(args).request();
58+
return this.instance().paymentSource.list(args);
2859
},
2960
getEvents(args = {}) {
30-
return this.instance().event.list(args).request();
61+
return this.instance().event.list(args);
62+
},
63+
createCustomer(args = {}) {
64+
return this.instance().customer.create(args);
65+
},
66+
createSubscription(customerId, args = {}) {
67+
return this.instance().subscription.createWithItems(customerId, args);
68+
},
69+
getItemPrices(args = {}) {
70+
return this.instance().itemPrice.list(args);
3171
},
3272
},
3373
};

components/chargebee/common/utils.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function clearObject(obj) {
2+
return Object.fromEntries(Object.entries(obj).filter(([
3+
,
4+
v,
5+
]) => v !== undefined));
6+
}

components/chargebee/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/chargebee",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Pipedream Chargebee Components",
55
"main": "chargebee.app.mjs",
66
"keywords": [
@@ -11,8 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"license": "MIT",
1313
"dependencies": {
14-
"@pipedream/platform": "^1.4.1",
15-
"chargebee": "^2.22.3"
14+
"@pipedream/platform": "^3.0.3",
15+
"chargebee": "^3.2.1"
1616
},
1717
"publishConfig": {
1818
"access": "public"

components/chargebee/sources/customer-card-expired-instant/customer-card-expired-instant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
name: "Customer Card Expired (Instant)",
99
description: "Emit new event when a customer card has expired. [See the Documentation](https://apidocs.chargebee.com/docs/api/events#card_expired). Please make sure once you deploy this source, you copy/paste the webhook URL to create it in your [Chargebee Webhook settings](https://www.chargebee.com/docs/2.0/webhook_settings.html#configure-webhooks).",
1010
type: "source",
11-
version: "0.0.2",
11+
version: "0.0.3",
1212
dedupe: "unique",
1313
methods: {
1414
...common.methods,

components/chargebee/sources/customer-changed-instant/customer-changed-instant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
name: "Customer Changed (Instant)",
99
description: "Emit new event when a customer is changed. [See the Documentation](https://apidocs.chargebee.com/docs/api/events#customer_changed). Please make sure once you deploy this source, you copy/paste the webhook URL to create it in your [Chargebee Webhook settings](https://www.chargebee.com/docs/2.0/webhook_settings.html#configure-webhooks).",
1010
type: "source",
11-
version: "0.0.2",
11+
version: "0.0.3",
1212
dedupe: "unique",
1313
methods: {
1414
...common.methods,

components/chargebee/sources/new-customer-created-instant/new-customer-created-instant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
name: "New Customer Created (Instant)",
99
description: "Emit new event when a new customer is created. [See the Documentation](https://apidocs.chargebee.com/docs/api/events#customer_created). Please make sure once you deploy this source, you copy/paste the webhook URL to create it in your [Chargebee Webhook settings](https://www.chargebee.com/docs/2.0/webhook_settings.html#configure-webhooks).",
1010
type: "source",
11-
version: "0.0.2",
11+
version: "0.0.3",
1212
dedupe: "unique",
1313
methods: {
1414
...common.methods,

components/chargebee/sources/new-event/new-event.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
name: "New Event",
99
description: "Emit new event when the selected event is triggered. [See the Documentation](https://apidocs.chargebee.com/docs/api/events). Please make sure once you deploy this source, you copy/paste the webhook URL to create it in your [Chargebee Webhook settings](https://www.chargebee.com/docs/2.0/webhook_settings.html#configure-webhooks).",
1010
type: "source",
11-
version: "0.0.1",
11+
version: "0.0.2",
1212
dedupe: "unique",
1313
props: {
1414
...common.props,

components/chargebee/sources/new-invoice-created-instant/new-invoice-created-instant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
name: "New Invoice Created (Instant)",
99
description: "Emit new event when a new invoice is created. [See the Documentation](https://apidocs.chargebee.com/docs/api/events#invoice_generated). Please make sure once you deploy this source, you copy/paste the webhook URL to create it in your [Chargebee Webhook settings](https://www.chargebee.com/docs/2.0/webhook_settings.html#configure-webhooks).",
1010
type: "source",
11-
version: "0.0.2",
11+
version: "0.0.3",
1212
dedupe: "unique",
1313
methods: {
1414
...common.methods,

0 commit comments

Comments
 (0)