billing - STRIPE - periodicBillingAllowed
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -177,18 +177,24 @@ export default abstract class BillingIntegration {
inError: 0
};
await this.checkConnection();
- const openedInvoices = await BillingStorage.getInvoicesToPay(this.tenantID);
- // Let's now try to pay opened invoices
- for (const openInvoice of openedInvoices.result) {
+
+ let invoices = await BillingStorage.getInvoicesToPay(this.tenantID);
+ if (this.settings.billing?.periodicBillingAllowed) {
+ invoices = await BillingStorage.getInvoicesToProcess(this.tenantID);
+ } else {
+ invoices = await BillingStorage.getInvoicesToPay(this.tenantID);
+ }
+ // Let's now finalize all invoices and attempt to get it paid
+ for (const invoice of invoices.result) {
try {
- await this.chargeInvoice(openInvoice);
+ await this.chargeInvoice(invoice);
await Logging.logInfo({
tenantID: this.tenantID,
source: Constants.CENTRAL_SERVER,
action: ServerAction.BILLING_CHARGE_INVOICE,
- actionOnUser: openInvoice.user,
+ actionOnUser: invoice.user,
module: MODULE_NAME, method: 'chargeInvoices',
- message: `Successfully charged invoice '${openInvoice.id}'`
+ message: `Successfully charged invoice '${invoice.id}'`
});
actionsDone.inSuccess++;
} catch (error) {
@@ -197,9 +203,9 @@ export default abstract class BillingIntegration {
tenantID: this.tenantID,
source: Constants.CENTRAL_SERVER,
action: ServerAction.BILLING_CHARGE_INVOICE,
- actionOnUser: openInvoice.user,
+ actionOnUser: invoice.user,
module: MODULE_NAME, method: 'chargeInvoices',
- message: `Failed to charge invoice '${openInvoice.id}'`,
+ message: `Failed to charge invoice '${invoice.id}'`,
detailedMessages: { error: error.message, stack: error.stack }
});
}

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -960,7 +960,7 @@ export default class StripeBillingIntegration extends BillingIntegration {
stripeInvoice = await this._createStripeInvoice(customerID, userID, this.buildIdemPotencyKey(idemPotencyKey));
}
let operationResult: BillingOperationResult;
- if (this.settings.billing.immediateBillingAllowed) {
+ if (this.settings.billing?.immediateBillingAllowed) {
// Let's try to bill the stripe invoice using the default payment method of the customer
operationResult = await this._chargeStripeInvoice(stripeInvoice.id);
if (!operationResult?.succeeded && operationResult?.error) {

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -26,13 +26,22 @@ export default class BillingStorage {
return invoicesMDB.count === 1 ? invoicesMDB.result[0] : null;
}
- public static async getInvoicesToPay(tenantID: string): Promise<DataResult<BillingInvoice>> {
+ public static async getInvoicesToProcess(tenantID: string): Promise<DataResult<BillingInvoice>> {
+ // Returns all invoices that are to be finalized and paid
const invoicesMDB = await BillingStorage.getInvoices(tenantID, {
invoiceStatus: [BillingInvoiceStatus.DRAFT, BillingInvoiceStatus.OPEN]
}, Constants.DB_PARAMS_MAX_LIMIT);
return invoicesMDB;
}
+ public static async getInvoicesToPay(tenantID: string): Promise<DataResult<BillingInvoice>> {
+ // Returns invoices that are already finalized and are to be paid
+ const invoicesMDB = await BillingStorage.getInvoices(tenantID, {
+ invoiceStatus: [BillingInvoiceStatus.OPEN]
+ }, Constants.DB_PARAMS_MAX_LIMIT);
+ return invoicesMDB;
+ }
+
public static async getInvoices(tenantID: string,
params: {
invoiceIDs?: string[]; billingInvoiceID?: string; search?: string; userIDs?: string[]; invoiceStatus?: BillingInvoiceStatus[];

billing - STRIPE - periodicBillingAllowed