#mboras_api

1 messages ยท Page 1 of 1 (latest)

toxic cloakBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฑ๏ธ We automatically close idle threads, which makes them read-only. Make sure you stick around to chat in realtime!

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1212030673917845595

๐Ÿ“ Have more to share? You can add more detail below, including code, screenshots, videos, etc.

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question. Thank you for your patience!

static loomBOT
#

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.

neat snow
#

๐Ÿ‘‹ happy to help

#

I'm not entirely sure I follow, would you mind elaborating please?

pearl jacinth
#

Is there some usual way to go or I am making up my logic here?

#

Just sanity check thread

#

So I want when users is returning back to free plan on customer.subscription.deleted
to check his balance and return him back money if he has

neat snow
#

when the subscription is deleted there is no longer a subscription/plan

#

I'm missing something obviously

pearl jacinth
#

cus_Pdg0TDCpMkNxfZ
sub_1OoQtRGeBYlVs2NBzSY8DvpU

#

but I'll explain

#

User buys yearly.
Uses it 6months.
Switches to monthly.
Cancels inside of monthly.
After that monthly he is returned back to free.
User needs to pay 7months and he needs to have remaining balance for 5months.
I want to return him back 5months as a refund to his account.

neat snow
#

once they switch to monthly, the unused time will be added back to their credit balance

pearl jacinth
#

Kk

#

Great

#

So final queston

neat snow
#

after that the monthly price will be deducted from the credit balance

pearl jacinth
#

is this legit code to use on customer.subscription.deleted

neat snow
#

and if they cancel without proration

pearl jacinth
#

I have prorations

#

turnedo n

neat snow
#

they would still have ~5months of credit balance

pearl jacinth
#

Should I send code or you're writing?

neat snow
#

otherwise the proration will happen automatically

#

since there's still unused time

pearl jacinth
#

just a sec to catch you

#

I am using customer portal for subscription management

#

Where do you mean to specifiy prorations: none

#

so these are my settings

robust forum
#

Hi there ๐Ÿ‘‹ jumping in as my teammate needs to step away soon. I'll answer the immediate question then work on building context from the previous discussions.

If you're using the API to manage your Portal Configurations, then that's controlled via features.subscription_cancel.proration_behavior
https://docs.stripe.com/api/customer_portal/configurations/update#update_portal_configuration-features-subscription_cancel-proration_behavior

If you're using your default configuration, then that's controlled through the dashboard.

pearl jacinth
#

I am not using API to manage portal configurations

#

so I have above settings as you see

robust forum
#

You need to be looking in the Cancellations section on that page

pearl jacinth
#

User uses yearly for 6months and then switches to monthly.
He paid 6months and on monthly he decides to cancel.
After that month has expired I am checking his current balance and creating refund.

#

these are cancellatiosn settings

#

so when he cancels he has plan till the end

#

of period

robust forum
#

There won't be prorations there, because you're letting the subscriptions come to the end of their billing period before they're cancelled.

pearl jacinth
#

Yeah

#

And on customer.subscription.deleted I am checking balance and returning money left to user if he has

#

This thread was sanity check just

robust forum
#

๐Ÿ‘ gotcha

pearl jacinth
#

Is this legit from your fast view of this?

#

There is no one else with whom I can discuss this ๐Ÿ˜„

robust forum
#

What do you mean by that? Is it working the way you want it to when you're testing? We aren't experts in the business logic you need to implement, but once you know that we can definitely help map that to capabilities of our system.

pearl jacinth
#

It is good
I just wanted to check is there common pattern to return back leftover credit balance to user

#

in docs

robust forum
#

I don't think we mention it in the docs, but typically you'd perform a refund (more specifically sounds like it'd be a partial refund in this case) on a previous payment, and then make a request to create a new Customer Balance Transaction to adjust the customer's balance by the amount you refunded.
https://docs.stripe.com/api/customer_balance_transactions/create

pearl jacinth
#

I am doing this

for (const invoice of invoices.data) {
console.log('Invoice:', invoice);
/**If user has leftover balance on cancel return back balance amount to user.
* NOTE: Currently we are paying Stripe fees for refunding the money back to the user.
* TODO @markoboras0712 check this out with administration
*/
if (
invoice.charge &&
invoice.payment_intent &&
currentCustomer.balance < 0
) {
try {
const amount = Math.abs(currentCustomer.balance);
console.log('Refunding amount:', amount);
const refund = await stripe.refunds.create({
charge: invoice.charge as string,
amount: Math.abs(currentCustomer.balance),
});

          const currentBalance = currentCustomer.balance;
          const newBalance = currentBalance - amount;
          console.log({ currentBalance, newBalance });
          await stripe.customers.update(customer, {
            balance: currentBalance - amount,
          });
          console.log(
            `Refund created for ${refund.amount} on invoice ${invoice.id}`,
          );
          break;
        } catch (error) {
          console.error(
            `Error creating refund for invoice ${invoice.id}:`,
            error,
          );
        }
      }
    }