import { Lava } from '@lavapayments/nodejs';
const lava = new Lava();
async function getBalanceDisplay(customerId: string) {
const result = await lava.customers.getSubscription(customerId);
if (!result.subscription) {
return { status: 'no_subscription', credits: null, plan: null };
}
const { credits, plan, pending_change } = result.subscription;
const total = parseFloat(credits.total_remaining);
const LOW_BALANCE_THRESHOLD = 2.0; // $2.00
return {
status: total <= 0 ? 'empty' : total < LOW_BALANCE_THRESHOLD ? 'low' : 'ok',
credits: {
total: total.toFixed(2),
fromPlan: parseFloat(credits.cycle_remaining).toFixed(2),
fromBundles: parseFloat(credits.bundle_remaining).toFixed(2),
},
plan: {
name: plan.name,
price: `$${plan.period_amount}/${plan.billing_interval}`,
included: `$${plan.included_credit}`,
rollover: plan.rollover_type,
},
pendingChange: pending_change
? {
type: pending_change.type,
effectiveAt: pending_change.effective_at,
newPlan: pending_change.name ?? null,
}
: null,
};
}