#harisabbasi12
1 messages · Page 1 of 1 (latest)
You could just update the subscription with the new default payment method. If you have automatic retries enabled and they're still going, it will retry with that PM: https://stripe.com/docs/api/subscriptions/update#update_subscription-default_payment_method
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Or you could have them attempt to pay the latest invoice on the sub
You mean like this:
const subscription = await stripe.subscriptions.update(
'sub_123', // the ID of the subscription to update
{
default_payment_method: 'pm_456', // the ID of the new default payment method
trial_end: 'now', // set the trial end to now to renew the subscription immediately
}
);
Yeah but only need to end the trial if you're trialing. Which wouldn't make sense if you already had a failed payment
What do you you say about the approach, where you just delete the current subscription and create the new subscription with same product, so that subscription can be renewed?
Why do that though? You could just pay the latest invoice on the subscription and it'll resume
//Save the PaymentMethod ID in your database
var paymentMethodIdd = paymentMethod.id;
var customerId = subscription.customer
await stripe.paymentMethods.attach(paymentMethodIdd, { customer: customerId });
const subscriptionRetry = await stripe.subscriptions.update(
subscriptionId, // the ID of the subscription to update
{
default_payment_method: paymentMethodIdd, // the ID of the new default payment method
trial_end: 'now', // set the trial end to now to renew the subscription immediately
}
);
console.log("subscriptionRetry ",subscriptionRetry)
i tried this way but status of subscription is still past_due
Invoice is of 14 march still, even i retried payment now 😦
Did you attempt to pay the invoice?
subscriptionId, // the ID of the subscription to update
{
default_payment_method: paymentMethodIdd, // the ID of the new default payment method
trial_end: 'now', // set the trial end to now to renew the subscription immediately
}
);```
That doesn't attempt to pay the subscription invoice
I suggested that only if you wanted automatic retries to take care of paying the sub's invoice later
If you want it paid now, get the latest invoice from the sub: https://stripe.com/docs/api/subscriptions/object#subscription_object-latest_invoice and have the customer pay that invoice (via its payment intent client secret for example)
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Then you can get that payment method and set it as the sub's default after
Can't i just cancel current invoice, create new invoice against current subscription?
Okay one last thing, i dont want to send payment intent client secret to frontend. Can i pay the invoice by just paymentId i recieved from frontend, instead of creating payment intent and sending it to frontend?
i dont want to send payment intent client secret to frontend Any reason why?
Yeah you can: https://stripe.com/docs/api/invoices/pay. But using client secret is easier
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I can't believe it worked:
// Retrieve the latest invoice for the subscription
const latestInvoice = await stripe.invoices.retrieve(subscription.latest_invoice);
// Retrieve the payment method from the frontend
const paymentMethodId = paymentMethod.id; // Replace with the actual parameter name you're using
console.log("latestInvoice ",latestInvoice)
var customerId=latestInvoice.customer;
await stripe.paymentMethods.attach(paymentMethodId, { customer: customerId });
var invoiceId=latestInvoice.id;
//return
// Create the payment for the invoice using the payment method and customer ID
const invoice = await stripe.invoices.pay(invoiceId, {
payment_method: paymentMethodId
});
console.log('Invoice paid:', invoice);
Any reason you don't want to use the payment intent's client secret to pay the invoice though?