我正在实现条带订阅,但在 api 文档中找不到如何在创建结账会话时指定订阅持续时间的信息。这是我到目前为止所拥有的:
const customer = await stripe.customers.create({
description: "My First Test Customer (created for API docs)",
email: "some.email@gmail.com",
address: {
city: 'Toronto',
country: 'CA',
line1: "Some Address",
line2: "Some unit number",
postal_code: "M5G0V1",
state: "ON",
},
name: "Some Name",
phone: "Some Number",
});
const price = await stripe.prices.create({
unit_amount: 599,
currency: 'cad',
recurring: {interval: 'month', interval_count: '1'},
product: 'prod_KZfHFK4nfCqlGS',
});
const subscriptionSchedule = await stripe.subscriptionSchedules.create({
customer: customer.id,
start_date: 'now',
end_behavior: 'cancel',
phases: [
{
items: [
{
price: price.id,
quantity: 1,
},
],
iterations: 24,
},
],
});
console.log(subscriptionSchedule);
const session = await stripe.checkout.sessions.create({
success_url: 'http://localhost:8080/stripeSessionSuccessHook',
cancel_url: 'http://localhost:8080/stripeSessionCancelHook',
payment_method_types: ['card'],
customer: customer.id,
line_items: [
{price: price.id, quantity: 1},
],
mode: 'subscription',
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个订阅,客户每月支付 24 个月的费用。我正在使用答案中建议的订阅计划,并且确实正在创建这样的 24 个月订阅。但是,当创建新的结帐时,如果付款成功,它将创建一个跨度为 12 个月的订阅...最终,我有 2 个订阅。一项由订阅计划创建(24 个月,所需),另一项订阅(12 个月)。有没有办法可以将此订阅计划传递给结账,这样就不会创建 2 个订阅。
ale*_*lex 10
如果付费,订阅通常会在每个计费周期后自动续订。recurring.interval在 Price 中设置,最大重复间隔为一年。[注:截至 2024 年 1 月,Stripe 现在支持最多三年的间隔]
如果您想在 2 年后结束订阅,您可以使用订阅计划: https: //stripe.com/docs/billing/subscriptions/subscription-schedules。
对于您的用例,如果价格为recurring.interval=year,您将创建一个包含单个阶段的订阅计划,其中iterations=2和end_behavior=cancel。
您可以参考此示例:https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases#installment-plans
请注意,订阅计划目前不适用于 Checkout 或客户门户。如果您想使用 Checkout,解决方法是:
您不应依赖成功重定向来在客户端上执行任何重要操作。在客户端上,客户可以在重定向发生之前关闭浏览器窗口或退出应用程序。您需要设置一个 Webhook 来侦听事件checkout.session.completed并在收到此事件后创建订阅计划。
您可以在此处阅读有关设置 webhook 的更多信息:
| 归档时间: |
|
| 查看次数: |
3256 次 |
| 最近记录: |