条带-创建或更新订阅时,{PatentIntent}为空

时间:2020-09-09 06:07:18

标签: node.js reactjs stripe-payments

我是Stripe的新手,所以如果我缺少任何东西,请提出建议。

技术栈: 反应(前端) 节点(后端)

我正在尝试创建或更新Stripe订阅,无论哪种情况,我都将获得一个空的paymentIntent。我以为我读到我应该检查paymentIntent状态,以确保已完成订阅的付款。

我的订阅工作流程,当用户注册时,我创建了Stripe客户并将其添加到订阅中。此订阅是免费套餐,因此不需要付款方式。 payment_intent为空。

//create a Stripe customer code here
...

//create the subscription 
const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{ priceID }],
    expand: ['latest_invoice.payment_intent'],
});

const invoice = subscription.latest_invoice as Stripe.Invoice;
const payment_intent = invoice.payment_intent as Stripe.PaymentIntent;

稍后,当他们想要升级到付费计划后,我要求提供信用卡并将他们当前的订阅升级到付费计划。在前端,我使用Stripe Element并通过此操作创建付款方式。

if (!elements || !stripe) {
     return 
}

const cardElement = elements.getElement(CardElement);

if (!cardElement) {
     return
}

// Confirm Card Setup
const { 
     error, 
     paymentMethod 
} = await stripe.createPaymentMethod({
     type: 'card',
     card: cardElement,
     billing_details:{
          name,
          email: currentUser.email,
          phone,
          address: {
               city,
               line1: address,
               state,
          },
     }
});

if (error) {
     console.log("createPaymentMethod: ", error.message)
} else {
     const paymentMethodId = paymentMethod!.id
     // Switch to new subscription
     await switchSubscription(priceID, paymentMethodId)
}
    

然后在后端,我得到了条纹客户,添加了付款方式,并将订阅升级为新计划。 payment_intent为空。

function switchSubscription(priceID, user, paymentMethod) {
     //get Stripe customer code here ...
     ...

     await stripe.paymentMethods.attach(paymentMethod, { customer: customer.id });
     await stripe.customers.update(customer.id, {
        invoice_settings: { default_payment_method: paymentMethod },
    });

    const currentSubscription = await getSubscriptions(user)

     const updatedSubscription = await stripe.subscriptions.update(
          currentSubscription.id,
     {
          cancel_at_period_end: false,
          proration_behavior: 'always_invoice',
          items: [
               {
                    id: currentSubscription.items.data[0].id,
                    price: priceID,
               },
          ],
          expand: ['latest_invoice.payment_intent'],
     })

     const invoice = updatedSubscription.latest_invoice as Stripe.Invoice;
     const payment_intent = invoice.payment_intent as Stripe.PaymentIntent;
}

1 个答案:

答案 0 :(得分:0)

您的客户的帐户中可能有余额,因此可以从客户的可用余额中取出该金额。我认为对于这种情况,Stripe 不会创建付款意图,因此返回 null

相关问题