#mattddean
1 messages ยท Page 1 of 1 (latest)
I'm attempting to do something like this
const stripeSubscription = await stripe.subscriptions.create({
customer: stripeCustomer.id,
payment_behavior: 'default_incomplete',
payment_settings: {
save_default_payment_method: 'on_subscription',
payment_method_types: ['card'],
},
expand: ['latest_invoice.payment_intent'],
items: [item],
})
// Create a subscription schedule that will auto-cancel the subscription after the user's specified number of months.
const stripeSubscriptionSchedule = await stripe.subscriptionSchedules.create({
from_subscription: stripeSubscription.id,
})
// We cannot provide other params to the subscriptionSchedules.create call when using from_subscription,
// so we have to make two api calls
await stripe.subscriptionSchedules.update(stripeSubscriptionSchedule.id, {
phases: [
{
items: [item],
iterations: requestBody.data.subscription.numMonths,
},
],
end_behavior: 'cancel', // cancel subscription after the number of months the user has specified
})
It seems like what I'm trying to do is directly against what the Stripe sdk wants me to do, but I feel like I have a pretty normal use case. Where have I gone wrong?
Hi ๐
Is there a particular reason you are trying to create the subscription first and then create the schedule?
Yes, that's what I was told to do in this thread
https://discord.com/channels/841573134531821608/1048002850724266075
Basically I want subscriptions to auto-expire if the initial payment intent fails rather than continue to exist on their account and retry charging them over and over
One remaining idea I have is to attach the subscription schedule after the user has completed their payment intent, but that feels dangerous. In our use case, a customer decides exactly how long they want to subscribe up front, so if we create the subscription but something fails and we can't attach a schedule to auto-cancel it after the number of months the customer has specified, that's a pretty bad thing.
but something fails
Sorry, hit send too early
I was going to say, are you referring to the customer's session on your site?
I guess I would be attaching the schedule using a webhook or something like that, but I would like to avoid creating a subscription that isn't set up to auto-cancel because I'd have to trust the webhook processing to work perfectly otherwise
Well, as the error message states, you cannot create a schedule using a Subscription in an incomplete status. So we need to figure out how we are going to get it into one of the usable statuses if we are going to apply the Subscription.
The 'unpaid' status sounds interesting
If I put it in the 'unpaid' status, will it still auto-expire in 23 hours if the payment intent doesn't get completed?
I do not think so. It does not transition to incomplete_expired since an unpaid subscription can be re-activated by receiving payment
Any other ideas?
Maybe there's a way to auto-cancel a subscription after a set number of charges without using schedules?
I think my use case is most similar to installment plans
https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases#installment-plans
I guess I am still not certain why you wouldn't just include the schedule creation in the response to successfully accepting payment. I agree this scenario does sound like what you are referring to.
As for automatically canceling after a number of invoices, I'm not aware of any settings in Stripe that would do that so you would need to build that logic in your integration. However, that does sound messy.
Yeah I guess I'm not opposed to that. I'm sort of relying on the successful processing of payment_intent.succeeded webhook events already to do other important things anyway
Do you think applying the schedule in response to the payment_intent.succeeded webhook event is the way to go?
That would be one approach or you could also listen for invoice.payment_succeeded since that Invoice object will include the invoice.subscription ID
Okay thank you! I'll try payment_intent.succeeded first because I'm already looking up the subscription via the intent's invoice to do other stuff
Okay that makes sense.
This was super helpful, thank you so much!
Happy to do it, that's why we're here ๐