#harisabbasi12

1 messages · Page 1 of 1 (latest)

teal saffronBOT
tardy haven
#

Or you could have them attempt to pay the latest invoice on the sub

spark crater
#

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
}
);

tardy haven
#

Yeah but only need to end the trial if you're trialing. Which wouldn't make sense if you already had a failed payment

spark crater
#

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?

tardy haven
#

Why do that though? You could just pay the latest invoice on the subscription and it'll resume

spark crater
#

//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 😦

tardy haven
#

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

#

Then you can get that payment method and set it as the sub's default after

spark crater
#

Can't i just cancel current invoice, create new invoice against current subscription?

tardy haven
#

No

#

Just pay the current one

#

Why do you want to cancel it?

spark crater
#

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?

tardy haven
teal saffronBOT
spark crater
#

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);
tardy haven
#

Any reason you don't want to use the payment intent's client secret to pay the invoice though?

spark crater
#

Thank you so much brother ❤️

#

My team lead who is frontend guy, he wants me to just recieve paymentMethodId and pay the subscription. He doesnt want to call payment intent in useEffect of react

tardy haven
#

Hm ok

#

That's not ideal though if card requires 3ds

#

The /pay request might fail

#

That's why we always recommend just using client secret