Is my Stripe code correct?

Hi, I want to offer my users a fixed-term subscription plan, where they get 30 days for free, and then they pay monthly for 12 months. Am I doing this the correct way? (I am not using a webhook since it gives me errors and I have up) Creating a session
const paymentSession = await stripe.checkout.sessions.create({
customer: paymentId,
mode: 'subscription',
payment_method_types: ['card'],
line_items: lineItems,
subscription_data: {
trial_period_days: 30,
},
success_url: `...`,
cancel_url: `...`,
});
const paymentSession = await stripe.checkout.sessions.create({
customer: paymentId,
mode: 'subscription',
payment_method_types: ['card'],
line_items: lineItems,
subscription_data: {
trial_period_days: 30,
},
success_url: `...`,
cancel_url: `...`,
});
Create subscription after the user went through the Stripe widget:
const subscription = await stripe.subscriptionSchedules.create({
customer: customer.id,
start_date: 'now',
end_behavior: 'release',
phases: [
{
items: [
{ price: process.env[`PRICE_${type.toUpperCase()}`], quantity: 1 },
],
iterations: 12,
},
],
});
const subscription = await stripe.subscriptionSchedules.create({
customer: customer.id,
start_date: 'now',
end_behavior: 'release',
phases: [
{
items: [
{ price: process.env[`PRICE_${type.toUpperCase()}`], quantity: 1 },
],
iterations: 12,
},
],
});
5 Replies
jingleberry
jingleberry15mo ago
Webhook is the right way to do this. Otherwise you’ll need to manually poll the stripe server until you see that the users subscription has been enabled
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
Aviv
Aviv15mo ago
Yea for now my process is that on the success page, I manually fetch Stripe. Its just that webhooks just really didnt work for me. my line items:
const lineItems = [
{
price: price.id,
quantity: 1,
},
];
const lineItems = [
{
price: price.id,
quantity: 1,
},
];
jingleberry
jingleberry15mo ago
Yeah, that does work right now because you're only concerned about the immediate action of 'enabling a subscription' when the user successfully submits payment. Now let's wait a month later and the user fails to make a payment for the subscription for the next month. How will your server know to deactivate and prevent access to your site for this particular user? This is why you need webhooks. Because this is how stripe can let you know in one month's time whether or not a user is still subscribed and is paying you money for said subscription. What about webhooks didn't work for you? Perhaps we can help debug any issues you're having 🙂
Aviv
Aviv14mo ago
yea, you are totally right. I've taken a lot of time away from this problem and came back to it, but still to no avail. I've opened a new question if you want to give it a look 😦 https://discord.com/channels/966627436387266600/1097123974938366113