Switch SMS billing to credit units with IRT top-ups.

Balance is SMS credits (1 per 64 chars); top-ups add floor(IRT/500) credits, track sms_sent_count, and convert existing IRT balances.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Alireza Hassani
2026-09-09 11:10:22 +03:30
co-authored by Cursor
parent 07f3304c07
commit 584c01fcee
9 changed files with 152 additions and 32 deletions
+17 -6
View File
@@ -908,7 +908,11 @@ export class CustomersService {
});
totalCost += cost;
}
charged = await this.smsBilling.chargeAmount(businessId, totalCost);
charged = await this.smsBilling.chargeAmount(
businessId,
totalCost,
personalizedPayloads.length,
);
}
try {
@@ -940,10 +944,13 @@ export class CustomersService {
);
serverIds.push(result.serverId);
} catch (err) {
const refundAmount = personalizedPayloads
.slice(i)
.reduce((sum, item) => sum + item.cost, 0);
await this.smsBilling.refund(businessId, refundAmount);
const remaining = personalizedPayloads.slice(i);
const refundAmount = remaining.reduce((sum, item) => sum + item.cost, 0);
await this.smsBilling.refund(
businessId,
refundAmount,
remaining.length,
);
charged = 0;
throw err;
}
@@ -960,7 +967,11 @@ export class CustomersService {
};
} catch (err) {
if (charged > 0) {
await this.smsBilling.refund(businessId, charged);
await this.smsBilling.refund(
businessId,
charged,
personalized ? personalizedPayloads.length : recipients.length,
);
}
if (
err instanceof BadRequestException ||