带订阅和试用期的 Stripe 支付元素

ENx*_*ENx 6 stripe-payments

我正在按照本指南https://stripe.com/docs/billing/subscriptions/build-subscription?ui=elements将 Stripe 卡元素迁移到具有订阅的 Stripe Payment Element ,但是当我将试用期添加到订阅时,api不返回“已用”发票的付款意图,在哪里获取 client_secret 以便与 js elements.create 方法一起使用。
因此,我按照本指南https://stripe.com/docs/ payments/ payment-element/migration?integration-path=future 使用设置意图而不是付款意图,但是在使用elements.create("payment")此代码实例化元素后调用时

const elements = stripe.elements({
    clientSecret: client_secret
});
Run Code Online (Sandbox Code Playgroud)

我从 stripe 收到以下错误:
Uncaught IntegrationError: Missing value for elements.create('payment'): clientSecret should be a client_secret string.
该变量包含从设置意图中获取的client_secret类似字符串。seti_1Jr36EGwrHzEL7YxdKlo86JF_secret_KW5G5nr6Dv1i9fnFhlQk9iA7yhe5tgP

我哪里错了?
如何将 Stripe Payment Element 与有试用期的订阅一起使用?

谢谢ENx

小智 11

要设置带有试用期的描述,请使用需要扩展的pending_setup_intent对象中的client_secret。

这是我让它工作的一个例子:

export const createStripeSubscription = async (customerId, priceId, trial_period) => {

  try {
    const subscription = await stripe.subscriptions.create({
        customer: customerId,
        items: [{
          price: priceId,
        }],
        trial_period_days: trial_period,
        expand: ['pending_setup_intent'],
      });
      return {
          subscriptionId: subscription.id,
          clientSecret: subscription.pending_setup_intent.client_secret,
      }
  } catch (error) {
      return null
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

亚历克斯的回答有效。另外,如果您在付款后收到错误“No such payment_intent: seti_...”,请确保您的结账表单配置为使用条带设置 API,而不是付款 API。IE :

像这样:

const { error } = await stripe.confirmSetup({
  elements,
  confirmParams: {
    return_url: 'url',
  },
});
Run Code Online (Sandbox Code Playgroud)

而不是像这样:

const { error } = await stripe.confirmPayment({
  elements,
  confirmParams: {
    return_url: 'url',
  },
});
Run Code Online (Sandbox Code Playgroud)