actions/create-subscription/create-subscription.mjs
import chargebee from "../../chargebee.app.mjs"; import { clearObject } from "../../common/utils.mjs"; export default { key: "chargebee-create-subscription", name: "Create Subscription", 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)", version: "0.0.2", annotations: { destructiveHint: false, openWorldHint: true, readOnlyHint: false, }, type: "action", props: { chargebee, customerId: { propDefinition: [ chargebee, "customerId", ], }, itemPriceId: { propDefinition: [ chargebee, "itemPriceId", ], }, unitPrice: { type: "integer", label: "Unit Price", description: "The unit price of the plan item.", }, quantity: { type: "integer", label: "Quantity", description: "The quantity of the plan item.", }, id: { type: "string", label: "ID", description: "A unique and immutable identifier for the subscription. If not provided, it is autogenerated.", optional: true, }, netTermDays: { type: "integer", label: "Net Term Days", 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.", optional: true, }, startDate: { type: "string", label: "Start Date", 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.", optional: true, }, additionalFields: { type: "object", label: "Additional Fields", 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.", optional: true, }, }, async run({ $ }) { try { const response = await this.chargebee.createSubscription(this.customerId, clearObject({ id: this.id, net_term_days: this.netTermDays, start_date: this.startDate && (Date.parse(this.startDate) / 1000), subscription_items: [ { item_price_id: this.itemPriceId, item_type: "plan", unit_price: this.unitPrice, quantity: this.quantity, }, ], ...this.additionalFields, })); $.export("$summary", `Successfully created subscription (ID: ${response?.subscription?.id})`); return response; } catch (error) { $.export("debug", error); throw new Error(`Error creating subscription: ${error.message}`); } }, };