问题描述
如何一次性支付多个订阅费用。
客户可以选择多个订阅,有些是按月付费,有些是半年付费。
我在服务器端这样做是为了创建一个客户 (stripe.com/docs/billing/subscriptions/elements#create-customer)
$customer = $stripe->customers->create([
'email' => $this->getUser()->getEmail(),]);
然后我创建订阅 (https://stripe.com/docs/billing/subscriptions/multiple)
$sub1 = $stripe->subscriptions->create([
'customer' => 'cus_4fdAW5ftNQow1b','items' => [['price' => 'price_CZB2krKbBDOkTe']],'payment_behavior' => 'default_incomplete','expand' => ['latest_invoice.payment_intent'],]);
$sub2 = $stripe->subscriptions->create([
'customer' => 'cus_4fdAW5ftNQow1b','items' => [['price' => 'price_CZB1AX3KOacNJb']],]);
但是,在文档中引用:
“在后端,使用payment_behavior=default_incomplete 创建状态为不完整的订阅,并将来自支付意图的client_secret 返回给前端以完成支付。”
我遇到的问题是每个订阅都有一个 client_secret。那么,如何以一张信用卡形式付款(第一次,订阅开始)。
在前端我有这个:
let stripePublicKey = "{{ stripePublicKey }}"
let stripe = Stripe(stripePublicKey);
let elements = stripe.elements();
//let clientSecret = {{clientSecret}} // the secret client is requested in the function stripe.confirmCardPayment below
//Create an instance of an Element and mount it to the Element container
let card = elements.create('card');
card.mount('#card-element');
function displayError(event) {
//changeLoadingStatePrices(false);
let displayError = document.getElementById('card-element-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
}
card.addEventListener('change',function (event) {
displayError(event);
});
const btn = document.querySelector('#submit-payment-btn');
btn.addEventListener('click',async (e) => {
e.preventDefault();
const nameInput = document.getElementById('name');
// Create payment method and confirm payment intent.
stripe.confirmCardPayment(clientSecret,{
payment_method: {
card: cardElement,billing_details: {
name: nameInput.value,},}
}).then((result) => {
if(result.error) {
alert(result.error.message);
} else {
// Successful subscription payment
}
});
});
这可能吗?如果不是,我应该考虑什么方法?
谢谢!
更新
如果我循环支付,是不是错了?
const btn = document.querySelector('#submit-payment-btn');
btn.addEventListener('click',async (e) => {
e.preventDefault();
for(const [key,value] of Object.entries(clientSecrets)) {
// Create payment method and confirm payment intent.
await stripe.confirmCardPayment(value.secret,{
payment_method: {
card: card,billing_details: {
name: '{{app.user.fullName}}'
},}
}).then((result) => {
if (result.error) {
alert(result.error.message);
} else {
// Successful subscription payment
console.log('Successful subscription payment');
}
});
}
});
会导致问题吗?
解决方法
您不能按照您尝试的方式执行此操作,因为您注意到这些是单独的付款意图(每个意图都有自己的 client_secret)。
当您遵循 default_incomplete
集成路径时,当您确认第一次订阅的付款意向时,付款方式将附加到客户并准备好供将来使用。成功后,您并使用附加的付款方式创建第二个订阅,方法是明确提供 default_payment_method
(API ref),或首先设置客户 invoice_settings[default_payment_method]
({{3 }})。您可能仍会遇到身份验证请求,具体取决于客户银行的判断,因此请务必检查并处理这些情况。