#mangle8582

1 messages · Page 1 of 1 (latest)

misty swanBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

misty mason
#

When you say 'refund', what exactly do you mean? What are you refunded or what API call are you making then you see that error?

neat girder
#

nextInvoice = await stripe.invoices.finalizeInvoice(
deletedSubscription?.latest_invoice
);

const refund = await stripe.refunds.create({
payment_intent: subscriptionToDeleteAndRefund?.payment_intent,
amount: nextInvoice.ending_balance ? Math.abs(nextInvoice.ending_balance) : 0,
});

    await stripe.customers.createBalanceTransaction(
      subscriptionToDeleteAndRefund?.dataValues?.customer_id,
      {
        amount: -nextInvoice.ending_balance,
        currency: 'usd',
      }
    );

I make these three. I believe stripe.refunds.create throws that error

misty mason
neat girder
#

req_UdYPpeXsF7SAu7 the req Id that failed

#

req_oOSNYz6SWkEZvP

This was the upgrade to Yearly from Monthly

misty mason
#

Seems expected given that the pi_xxx you're trying to refund is for a lesser amount than you're stipulating. You can't refund beyond that payment amount

neat girder
#

Yea, but i'm taking the refund amount from nextInvoice after i finalize it

#

nextInvoice = await stripe.invoices.finalizeInvoice(
deletedSubscription?.latest_invoice
);

#

I'm ok with it to refund the exact amount he did for the upgrade. But i need it to come on finalizeInvoice

#

Also the invoiceUpcoming is still higher than the amount he paid when upgraded

#

{
customer: 'cus_PZKpmcyiaCus0G',
subscription: 'sub_1OmOwjDmVlmqORBIPD2ybaYb',
subscription_proration_behavior: 'create_prorations',
subscription_cancel_now: true
}

fathom fern
#

Hi! I'm taking over from my colleague. Please, give me a moment to catch up.

neat girder
#

Sure, thanks

fathom fern
#

What exactly do you want to refund? Because it seems like you want to refund for part of the monthly Invoice + part of the yearly Invoice, but it needs to be done separately.

neat girder
#

Yea, but i'm fine to refund just the yearly amount for the upgrade and do the refund for monthly directly from dashboard

#

But the invoiceUpcoming and finalizeInvoice doesn't return the correct amount

#

it returns the value that i need to refund + the monthly subscription and Stripe throws error on refund

fathom fern
#

I don't really follow when do you request the upcoming invoice and then when do you refund.

#

Are you requesting the upcoming invoice for the proration?

neat girder
#

Yes, user clicks on Remove & Refund Subscription, a modal comes up. I make the call to invoiceUpcoming with prorated amount and show to the user the amount on the UI. He then can press refund and makes an API call -> that does the above things with finalizeInvoice, createRefund etc

fathom fern
#

But does it happen after the upgrade of Subscription?

neat girder
#

Yeah.
So my issue is only on upgrade.

  1. If user buys monthly directly and i show modal with invoiceUpcoming and do the refund, everything works correctly.
  2. If user buys yearly directly refund and invoiceUpcoming works fine.
  3. If user buys monthly, then upgrades and he then wants to refund. The invoiceUpcoming shows more than the amount for the upgrade and if i want to refund him the amount from finalizeInvoice , stripe throws error that the amount to refund is higher than the user paid for
fathom fern
#

For case 3, upcoming Invoice after the upgrader will show the next period's Invoice. You should instead use the amount paid for the last Invoice.

neat girder
#

How can i do that? Because it needs to prorate from the amount paid for the last invoice

misty swanBOT
fathom fern
#

It will already contain the proration and the total amount.

neat girder
#

On the current subscription right?
I need to take the id of that latest invoice and make a call to see the amount?

fathom fern
neat girder
#

And it really comes prorated? Like if 1 week passed it will be less than the amount paid so i can show to the user or refund?

fathom fern
#

It's best to check for yourself. It's the Invoice that was already paid so it won't change and is exactly the amount that was charged, therefore will be refundable.

neat girder
#

Yea, but if i want to do prorated from that amount? User is using the application and we pay servers, by doing multiple full refunds users can use the app without paying ever. So that's why we use prorated

fathom fern
#

What do you mean by "prorated" here exactly?
You mean you want to issue a partial refund?

neat girder
#

Yea, if user upgrades with 64$ let's say. If 10 days pass, they will get back 58$ or something. But if latest_invoice says 64$ how can i calculate that?

fathom fern
#

Subscription prorations have nothing to do with this.

neat girder
#

Uff.. if i have prorated amount from monthly i need to remember it then substract it, and substract it also on invoicePreview.

#

Is there a flag that i could know if the subscription was upgraded and not directly bought?

peak tendon
#

Hey! Taking over for my colleague.

Is there a flag that i could know if the subscription was upgraded and not directly bought?
You can track that in your integration

neat girder
#

Yea, working with it.
So if i make the API call /upgrade-to-yearly that calls Stripe, i believe i can set the flag in my database that was upgraded.

And then on next webhook to make it false

peak tendon
#

Yes that's an option

neat girder
#

but invoice.paid is also triggered on invoice.paid webhook

#

Maybe i can check the current subscription if it's monthly or smth and next one

#

and set this flag

peak tendon
neat girder
#

When you upgrade invoice.paid is still triggered.
When you wait one year and it automatically pays, the invoice.paid webhook still triggers.

So where i can set the flag or how?

#

Maybe on customer.subscription.updated

peak tendon
#

When you upgrade invoice.paid is still triggered.
When you wait one year and it automatically pays, the invoice.paid webhook still triggers.
Yes correct

#

So where i can set the flag or how?
You need to add some additional checks, not only when receiving a particular webhook event

#

and see if the priceId was changed for example or not...

neat girder
#

can i check this in the webhook customer.subscription.updated? Or on upgrade route? Because i need the id of that event to make the GET

peak tendon
#

Yes any webhook event body, will have the previous_attributesif there is a change on an attribute

neat girder
#

perfect! thanks