periodic billing - refresh data required to get the latest amount
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -1097,6 +1097,7 @@ export default class StripeBillingIntegration extends BillingIntegration {
public async billInvoiceItem(user: User, billingInvoiceItem: BillingInvoiceItem, idemPotencyKey?: string): Promise<BillingInvoice> {
// Let's collect the required information
+ let refreshDataRequired = false;
const userID: string = user.id;
const customerID: string = user.billingData?.customerID;
// Check whether a DRAFT invoice can be used
@@ -1140,6 +1141,9 @@ export default class StripeBillingIntegration extends BillingIntegration {
if (!stripeInvoice) {
// Let's create a new DRAFT invoice (if none has been found)
stripeInvoice = await this._createStripeInvoice(customerID, userID, this.buildIdemPotencyKey(idemPotencyKey));
+ } else {
+ // Here an existing invoice is being reused
+ refreshDataRequired = true;
}
let operationResult: StripeChargeOperationResult;
if (this.settings.billing?.immediateBillingAllowed) {
@@ -1160,10 +1164,14 @@ export default class StripeBillingIntegration extends BillingIntegration {
// Reuse the operation result (for better performance)
stripeInvoice = operationResult.invoice;
} else {
+ // Something went wrong - we need to fetch the latest information from STRIPE again!
+ refreshDataRequired = true;
+ }
+ }
// Get fresh data only when necessary - e.g.: invoice has been finalized, however the payment attempt failed
+ if (refreshDataRequired) {
stripeInvoice = await this.getStripeInvoice(stripeInvoice.id);
}
- }
// Let's replicate some information on our side
const billingInvoice = await this.synchronizeAsBillingInvoice(stripeInvoice, false);
// We have now a Billing Invoice - Let's update it with details about the last operation result

periodic billing - refresh data required to get the latest amount