如何创建带有试用期的 stripe 订阅

How to create a stripe subscription with a trial period

我今天有一个没有试用期的订阅流程。但是尝试添加 7 天试用版时,我不确定这些步骤。这就是我正在尝试的,通常遵循这个

在后端创建用户:

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

在后端创建一个设置意图:

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");

点击提交后,我不知何故需要确认付款细节。
我在后端试过了

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,
        )