Added display of credits

This commit is contained in:
Dries Augustyns
2025-12-05 07:59:47 +01:00
parent 8c348d3580
commit 04a64ef83a
4 changed files with 62 additions and 1 deletions
+20
View File
@@ -476,6 +476,25 @@ export class Users {
// This is normal for new subscriptions or if there's no usage yet
}
// Get customer to retrieve balance (credits)
const customer = await stripe.customers.retrieve(project.customer);
let credits = null;
if (!customer.deleted) {
// Stripe stores balance in cents as a negative number (credit) or positive (debit)
// A negative balance means the customer has credits
const balance = customer.balance || 0;
const hasCredits = balance < 0;
const creditAmount = hasCredits ? Math.abs(balance) : 0;
credits = {
balance,
hasCredits,
creditAmount,
currency: customer.currency || 'usd',
};
}
return res.status(200).json({
period: {
start: startOfMonth.toISOString(),
@@ -485,6 +504,7 @@ export class Users {
total: totalUsage,
records: [], // Deprecated in favor of invoice line items
},
credits,
upcomingInvoice: upcomingInvoice
? {
amountDue: upcomingInvoice.amount_due,
+5
View File
@@ -276,6 +276,11 @@ export class Webhooks {
},
});
// add 1 eur/usd in credit in return for onboarding fee
await stripe.customers.update(customerId, {
balance: -100,
});
signale.success(`[WEBHOOK] Checkout completed for project ${projectId}`);
// Send notification about subscription started