gpt4 book ai didi

node.js - Stripe - 创建或更新订阅时 PaymentIntent 为空

转载 作者:行者123 更新时间:2023-12-04 12:00:24 26 4
gpt4 key购买 nike

我是 Stripe 的新手,所以如果我遗漏了什么,请提供建议。
技术栈:
react (前端)
Node (后端)
我正在尝试创建或更新 Stripe 订阅,在任何一种情况下,我都会得到一个 null 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)
}

在后端,我获取 Stripe 客户,添加付款方式,并将订阅升级到新计划。 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;
}

最佳答案

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

关于node.js - Stripe - 创建或更新订阅时 PaymentIntent 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63805648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com