如何创建带试用期的分条订阅



我今天有一个订阅流程,没有试用期。然而,试图增加一个7天的试验,我不确定的步骤。这就是我正在尝试的,通常遵循这个SO答案

在后端创建用户:

customer = stripe.Customer.create(email=email)

在后端创建一个setup意图:

setup_intent = stripe.SetupIntent.create(
customer=cust_id,
usage='off_session',
)

显示支付元素在客户端:

this.elements = this.stripe.elements();
this.paymentElement = this.elements.create("payment", {clientSecret: setUpObj.clientSecret});
this.paymentElement.mount("#payment-element");

点击submit后,不知怎么的,我需要确认支付细节。
我在后台试过了

si = stripe.SetupIntent.confirm(
setup_id,
)

然而,它抛出错误You cannot confirm this SetupIntent because it's missing a payment method
我试图确认客户端的支付方式

const {paymentIntent, error} = await this.stripe.confirmCardSetup(
this.setup_intent_cs,
{
payment_method: {
card: this.paymentElement,
}
}
);

但是会抛出错误Invalid value for confirmCardSetup: payment_method.card was `payment` Element, which cannot be used to create card PaymentMethods.

如何确认付款方式&继续创建订阅?

我找到了一个方法。接受付款细节后,您需要确认设置,而不需要重定向

const {confirmError} = await this.stripe.confirmSetup({
element: this.paymentElement,
confirmParams: {
return_url: MyGlobals.THIS_URL+'/newreg',
},
redirect: 'if_required'
});

然后在服务器上创建订阅

subscription = stripe.Subscription.create(
customer=cust_id,
items=[{
'price': price_id,
}],
payment_behavior='default_incomplete',
expand=['latest_invoice.payment_intent'],
trial_period_days=7,
off_session=True,
)

相关内容

最新更新