#mboras_api
1 messages ยท Page 1 of 1 (latest)
๐ 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!
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.
- mboras_api, 46 minutes ago, 11 messages
- mboras_api, 1 hour ago, 15 messages
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
when the subscription is deleted there is no longer a subscription/plan
I'm missing something obviously
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.
once they switch to monthly, the unused time will be added back to their credit balance
after that the monthly price will be deducted from the credit balance
is this legit code to use on customer.subscription.deleted
and if they cancel without proration
they would still have ~5months of credit balance
Should I send code or you're writing?
you need to specify prorations: none
otherwise the proration will happen automatically
since there's still unused time
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
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.
I am not using API to manage portal configurations
so I have above settings as you see
You need to be looking in the Cancellations section on that page
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
There won't be prorations there, because you're letting the subscriptions come to the end of their billing period before they're cancelled.
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
๐ gotcha
Is this legit from your fast view of this?
There is no one else with whom I can discuss this ๐
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.
It is good
I just wanted to check is there common pattern to return back leftover credit balance to user
in docs
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
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,
);
}
}
}