I am implementing stripe subscriptions and I can't find anywhere in the api documentation how to specify the duration of the subscription when creating a checkout session. This is what I have so far:
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',
});
I am trying to create a subscription that the customer pays monthly for 24 months. I am using Subscription Schedule suggested in the answers, and indeed such subscription with 24 months is being created. However, when creating a new checkout, and in case the payment is successful, it will create a Subscription that spans for 12 months... In the end, I have 2 subscriptions. One created by Subscription Schedule (24 months, desired one), and other Subscription (12 months). Is there a way I can somehow pass this subscription schedule to checkout, so 2 subscriptions don't get created.