#BarrelRider

1 messages ยท Page 1 of 1 (latest)

queen magnetBOT
velvet hearth
#

Also if that helps I can provide entire snippet.

sturdy tinsel
#

on reading the rest, maybe I don't understand what you're doing so perhaps ignore that answer

#

what are you actually trying to do and what is the refund

#

to be clear you can just use prorate=>true on https://stripe.com/docs/api/subscriptions/cancel#cancel_subscription-prorate to have the cancellation calculate proration(the amount paid in the last invoice that is unused proportional to the next billing date) and credit that to a final invoice, which will be automatically added to the customer's balance if the invoice amount is negative; if that's what you're trying to do and it fits your use case, instead of doing anything special in your own code.

velvet hearth
#

my bad I did not explain everything well enough,

if user cancels the subscription e.g. halfway through I want to refund what ever is unused for that billing period.

I am using stripe hosted subscription pages

protected function handleCustomerSubscriptionDeleted(array $payload)
    {
        // Call the parent method to handle the default subscription deletion behavior
        parent::handleCustomerSubscriptionDeleted($payload);

        // Find the subscription in the application using the Stripe subscription ID
        $subscription = Subscription::where('stripe_id', $payload['data']['object']['id'])->first();
        $user = $subscription->user;

        // If the subscription doesn't exist, return false
        if (!$subscription) {
            return false;
        }

        // Check if the user is currently on trial
        if ($user->onTrial()) {
            // The user is on trial, so no need to refund, just return a success response
            return $this->successMethod();
        }

        // Retrieve the proration cancellation invoice
        $latestInvoice = $user->findInvoiceOrFail($payload['data']['object']['latest_invoice']);
        $previousInvoice = $user->findInvoice($latestInvoice->lines->data[0]->proration_details->credited_items->invoice);
        $refundAmount = abs($latestInvoice->lines->data[0]['amount']);

        // Create a refund in Stripe
        $refund = $user->refund($previousInvoice->payment_intent, [
            'amount' => $refundAmount,
            'reason' => Refund::REASON_REQUESTED_BY_CUSTOMER
        ]);

        if ($refund->status === 'succeeded') {
            // Create a customer balance transaction in Stripe
            $user->debitBalance($refundAmount, 'Prorated refund for canceled subscription');
            // TODO: email user and owner about successful refund
        } else {
            // TODO: email user and owner about failed refund
        }

        return $this->successMethod();
    }
#

So what is happening I am just getting prorated refund amount

#

from latest cancellation invoice and and issue a new refund

#

then I adjust balance

#

seems to work out correctly

#

on manual testing

#

I provided the snipped in hopes it will give a clearer picture

sturdy tinsel
velvet hearth
#

Yes my apologies, I mean the actual refund.

On cancelation money goes back to credit balance for the user. And once that happens I am issuing the actual refund to the original payment method.

#

This method

$user->debitBalance($refundAmount, 'Prorated refund for canceled subscription');

charges user balance with the refund amount from the latest (cancellation) invoice

#

am I making any sense at all?

sturdy tinsel
#

when you say "change" do you mean you are zeroing it out

#

like, you cancel the subscription, with prorate=>true

#

that will result in the customer getting e.g -$75 in their balance

#

and then you add +$75 to their balance with $user->debitBalance, and then refund a previous invoice of theirs for $75 to give them that cash?

velvet hearth
#

No sorry, let me write out a step by step what is happening.

#
  1. user subscribes and pays the required amount.
  2. E.g. 15 days passes and then the user decides to cancel.
  3. They cancel via stripe hosted cancelation page and the credit automatically goes into their credit balance. I can see that in customer dashboard, so user has credit that normally would be deducted from next invoice if I didn't touch it.
  4. Now upon subscription deletion event I am getting the prorated amount of that credit in users balance and I am putting a debit for taht amount on user credit balance so effectively i am 0ing it out. So user cancel and get credit into account and then I am issuing refund to original payment method, once that is successful I am adjusting balance.
#

Is that helping at all?

#

To clarify further the refund part.

Cancelation, credits user with prorated amount, so they get a bit of money back. I look at the latest cancelation invoice and take that amount to apply debit to users credit balance.

In my system user can only have one subscription, there is only one plan that they can have therefore I am just grabbing first item from the invoice.

sturdy tinsel
#

I think we're saying the same thing

#

but sure yes, you can find a previous invoice the customer paid and refund that for a given amount

#

that is generally how people implement this sort of flow of giving an actual cash refund for a cancelled subscription, indeed, I've seen it before.

#

we also have a Credit Note API that I think might also fill this use case, but I don't understand that API personally

velvet hearth
#

gotcha that makes sense, so let me just summarise that into neat note so I can just make sure I am on the page (apologies, I am trying to be thorough).

sturdy tinsel
#

I have to run unfortunately but my colleague can help

velvet hearth
#

No problem thanks for all the help!

#

please connect me with your friend

#

This is the summary of what is happening:

  1. User subscribes and pays the required amount via stripe hosted page.

  2. E.g. 15 days passes and then the user decides to cancel via stripe hosted subscription management page.

  3. They cancel via stripe hosted cancelation page and the credit automatically goes into their credit balance. I can see that credit amount in customer dashboard, so user now has a credit in credit balance that normally would be deducted from the next invoice if I didn't refund it.

  4. Now upon subscription deletion event I am retrieving the prorated amount of that credit in users balance. That amount comes from the latest invoice that has been created when subscription was cancelled. So now I have cancellation invoice with prorated amount and invoice with the original full payment. Original full payment - prorated amount = used time.

  5. I am issuing the actual money refund to original payment method. That amount comes from prorated cancellation amount from the cancellation invoice.

  6. Once I have the confirmation that refund has been created sucessfully I am putting a debit for that amount from cancellation invoice on user credit balance so effectively i am 0ing it out because I am debiting the amount that they got in credit for cancelation. I do this manually because this is not automated in stripe.

Is there any waiting time I should apply between cancellation and refund&adjusting balance? I believe it will take an hour to process?

Does all that sound correct?

queen magnetBOT
velvet hearth
#

Hey @viral lichen ,

Nice to talk to you again, hope you are well. Please feel free to ask any additional questions if anything is unclear.

viral lichen
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

velvet hearth
#

No problem please take all the time you need, I am really keen to get this right.

viral lichen
#

Is there any waiting time I should apply between cancellation and refund&adjusting balance? I believe it will take an hour to process?
I think you can listen to the https://stripe.com/docs/api/events/types#event_types-charge.refunded event and once that is done you can go ahead and debit the customer's credit balance

velvet hearth
#

Oh this one is brilliant I will do do that like this and I will just dispatch a job upon that event.

About the rest, just to clarify: I am using PHP and Laravel. Does the presented final explanation looks ok to you? This is just my idea of how to implement it and I want to make sure that my inconsistent explanations did not leave anything in my logic that will cause me issues down the line.

viral lichen
#

yes it's good

#

I would add one thing though

#

when you create the refund I would add some metadata telling you to debit the credit balance and the amount to be debited

#

so that you don't have to think whether this refund should be debited from the customer debit account or not

velvet hearth
#

That is an excellent idea.

Thank you for reviewing my idea. Final followup question - which would be most suitable event to watch in case refund to payment method fails? I am thinking I will have to send myself an email id that happens and just do it manually.

viral lichen
velvet hearth
#

gotcha I think I was looking at the same thing