import app from "../../stripe.app.mjs";
import utils from "../../common/utils.mjs";
export default {
key: "stripe-create-subscription",
name: "Create Subscription",
type: "action",
version: "0.1.4",
description: "Create a subscription. [See the documentation](https://stripe.com/docs/api/subscriptions/create).",
props: {
app,
customer: {
propDefinition: [
app,
"customer",
],
optional: false,
},
items: {
propDefinition: [
app,
"price",
],
optional: false,
type: "string[]",
},
country: {
propDefinition: [
app,
"country",
],
optional: true,
},
currency: {
propDefinition: [
app,
"currency",
({ country }) => ({
country,
}),
],
optional: true,
},
description: {
propDefinition: [
app,
"description",
],
},
collectionMethod: {
description: "Either charge_automatically, or send_invoice. When charging automatically, Stripe will attempt to pay this subscription at the end of the cycle using the default source attached to the customer. When sending an invoice, Stripe will email your customer an invoice with payment instructions and mark the subscription as active. Defaults to charge_automatically.",
propDefinition: [
app,
"collectionMethod",
],
},
daysUntilDue: {
description: "Number of days a customer has to pay invoices generated by this subscription. Valid only for subscriptions where collection_method is set to send_invoice.",
propDefinition: [
app,
"daysUntilDue",
],
},
paymentType: {
type: "string",
propDefinition: [
app,
"paymentMethodTypes",
],
optional: true,
},
defaultPaymentMethod: {
propDefinition: [
app,
"paymentMethod",
(c) => ({
customer: c.customer,
type: c.paymentType,
}),
],
label: "Default Payment Method",
description: "Must belong to the customer associated with the invoice. If not set, " +
"defaults to the subscription’s default payment method, if any, or to the default " +
"payment method in the customer’s invoice settings.",
},
metadata: {
propDefinition: [
app,
"metadata",
],
},
automaticTaxEnabled: {
type: "boolean",
label: "Automatic Tax Enabled",
description: "Enabled automatic tax calculation which will automatically compute tax rates on all invoices generated by the subscription.",
optional: true,
},
automaticTaxLiabilityType: {
type: "string",
label: "Automatic Tax - Liability - Type",
description: "Type of the account referenced in the request.",
optional: true,
options: [
"account",
"self",
],
},
automaticTaxLiabilityAccount: {
type: "string",
label: "Automatic Tax - Liability - Account",
description: "The connected account being referenced when **Automatic Tax - Liability - Type** is account.",
optional: true,
},
paymentBehavior: {
type: "string",
label: "Payment Behavior",
description: "Specifies whether to create the subscription in the incomplete or active state. Only applies to subscriptions when **Collection Method** is set to `charge_automatically`. [See the documentation](https://docs.stripe.com/api/subscriptions/create#create_subscription-payment_behavior).",
optional: true,
options: [
"allow_incomplete",
"default_incomplete",
"error_if_incomplete",
"pending_if_incomplete",
],
},
},
async run({ $ }) {
const {
app,
customer,
items,
currency,
description,
collectionMethod,
daysUntilDue,
defaultPaymentMethod,
metadata,
automaticTaxEnabled,
automaticTaxLiabilityType,
automaticTaxLiabilityAccount,
paymentBehavior,
} = this;
const resp = await app.sdk().subscriptions.create({
customer,
default_payment_method: defaultPaymentMethod,
collection_method: collectionMethod,
days_until_due: daysUntilDue,
payment_behavior: paymentBehavior,
metadata: utils.parseJson(metadata),
description,
currency,
...(automaticTaxEnabled
? {
automatic_tax: {
enabled: automaticTaxEnabled,
...(automaticTaxLiabilityType && {
liability: {
type: automaticTaxLiabilityType,
...(automaticTaxLiabilityAccount && {
account: automaticTaxLiabilityAccount,
}),
},
}),
},
}
: {}),
items: utils.parseArray(items).map((price) => ({
price,
})),
});
$.export("$summary", `Successfully created a new subscription with ID \`${resp.id}\`.`);
return resp;
},
};