[英]Stripe - PaymentIntent is null when I create or update a subscription
I'm new to Stripe, so if I'm missing something, please advise.我是 Stripe 的新手,所以如果我遗漏了什么,请提供建议。
Tech Stack: React (front-end) Node (back-end)技术栈:React(前端)节点(后端)
I am trying to create or update a Stripe subscription, and in either case, I get a null paymentIntent.我正在尝试创建或更新 Stripe 订阅,在任何一种情况下,我都会得到一个 null paymentIntent。 I thought I read that I should check the paymentIntent status to make sure the payment for the subscription has gone through.我想我读到我应该检查 paymentIntent 状态以确保订阅的付款已经完成。
My subscription workflow, when a user signs up, I create a Stripe customer and add them to a subscription.我的订阅工作流程,当用户注册时,我创建一个 Stripe 客户并将他们添加到订阅中。 This subscription is a free tier so no payment method is needed.此订阅是免费套餐,因此不需要付款方式。 The payment_intent
is null. 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;
Later after they want to upgrade to a paid plan, I request a credit card and upgrade their current subscription to a paid plan.后来他们想升级到付费计划后,我申请了一张信用卡并将他们当前的订阅升级到付费计划。 On the front-end, I use Stripe Element and create a payment method by doing this.在前端,我使用 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)
}
And on the back-end, I get the stripe customer, add a payment method, and upgrade the subscription to the new plan.在后端,我获取 Stripe 客户,添加付款方式,并将订阅升级到新计划。 The payment_intent
is null. 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;
}
Your customer might have a balance in their account so that the amount might be taken out from the customer's available balance.您的客户的帐户中可能有余额,因此可以从客户的可用余额中取出该金额。 I think for cases like that, Stripe doesn't create a payment intent, and therefore returns null
.我认为对于这样的情况,Stripe 不会创建付款意图,因此返回null
。
I solved this probleme by adding the quantity to items.我通过将数量添加到项目中解决了这个问题。 I think the invoice is not created (null) when the total amount is 0.我认为当总金额为 0 时不会创建发票(空)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.