stripe - finilize step is implicit
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -350,43 +350,6 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
return actionsDone;
}
- public async finalizeInvoices(): Promise<BillingChargeInvoiceAction> {
- const actionsDone: BillingChargeInvoiceAction = {
- inSuccess: 0,
- inError: 0
- };
- await this.checkConnection();
- // Get the draft invoices
- const draftInvoices = await BillingStorage.getDraftInvoices(this.tenantID);
- // Let's first finalize the invoices
- for (const draftInvoice of draftInvoices.result) {
- // Synchronize user
- try {
- await this.finalizeInvoice(draftInvoice);
- // TODO - update the billing invoice state to reflect the change?
- Logging.logInfo({
- tenantID: this.tenantID,
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_CHARGE_INVOICE,
- module: MODULE_NAME, method: 'chargeInvoices',
- message: `Succesfully finalized invoice '${draftInvoice.id}'`
- });
- actionsDone.inSuccess++;
- } catch (error) {
- actionsDone.inError++;
- Logging.logError({
- tenantID: this.tenantID,
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_SYNCHRONIZE_USERS,
- module: MODULE_NAME, method: 'chargeInvoices',
- message: `Failed to finalize invoice '${draftInvoice.id}'`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
- }
- return actionsDone;
- }
-
public async chargeInvoices(): Promise<BillingChargeInvoiceAction> {
const actionsDone: BillingChargeInvoiceAction = {
inSuccess: 0,
@@ -394,7 +357,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
};
await this.checkConnection();
- const openedInvoices = await BillingStorage.getOpenInvoices(this.tenantID);
+ const openedInvoices = await BillingStorage.getInvoicesToPay(this.tenantID);
// Let's now try to pay opened invoices
for (const openInvoice of openedInvoices.result) {
// Synchronize user

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
import { BillingDataTransactionStart, BillingDataTransactionStop, BillingDataTransactionUpdate, BillingInvoice, BillingInvoiceDocument, BillingInvoiceItem, BillingInvoiceStatus, BillingStatus, BillingTax, BillingUser } from '../../../types/Billing';
import { DocumentEncoding, DocumentType } from '../../../types/GlobalType';
-import Stripe, { IResourceObject, customers, invoices, paymentMethods, setupIntents } from 'stripe';
+import Stripe, { IResourceObject, customers, invoices, paymentMethods } from 'stripe';
import AxiosFactory from '../../../utils/AxiosFactory';
import { AxiosInstance } from 'axios';

MODIFY
src/scheduler/tasks/SynchronizeBillingInvoicesTask.ts
src/scheduler/tasks/SynchronizeBillingInvoicesTask.ts
@@ -29,21 +29,8 @@ export default class SynchronizeBillingInvoicesTask extends SchedulerTask {
}
);
}
- // Attempt payment - once a month!
+ // Attempt payment - once a month! - A second task with a dedicated configuration to trigger the payment attempts
if (taskConfig?.attemptPayment) {
- // Attempt to finalize invoices with status DRAFT
- const finalizeActionResults = await billingImpl.finalizeInvoices();
- if (finalizeActionResults.inError > 0) {
- // TODO - dedicated notification type is required here!!!
- await NotificationHandler.sendBillingInvoicesSynchronizationFailed(
- tenant.id,
- {
- nbrInvoicesInError: finalizeActionResults.inError,
- evseDashboardURL: Utils.buildEvseURL(tenant.subdomain),
- evseDashboardBillingURL: Utils.buildEvseBillingSettingsURL(tenant.subdomain)
- }
- );
- }
// Attempt to pay invoices with status OPEN
const chargeActionResults = await billingImpl.chargeInvoices();
if (chargeActionResults.inError > 0) {

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -26,17 +26,10 @@ export default class BillingStorage {
return invoicesMDB.count === 1 ? invoicesMDB.result[0] : null;
}
- public static async getDraftInvoices(tenantID: string): Promise<DataResult<BillingInvoice>> {
+ public static async getInvoicesToPay(tenantID: string): Promise<DataResult<BillingInvoice>> {
const invoicesMDB = await BillingStorage.getInvoices(tenantID, {
- invoiceStatus: [BillingInvoiceStatus.DRAFT]
- }, Constants.DB_PARAMS_SINGLE_RECORD);
- return invoicesMDB;
- }
-
- public static async getOpenInvoices(tenantID: string): Promise<DataResult<BillingInvoice>> {
- const invoicesMDB = await BillingStorage.getInvoices(tenantID, {
- invoiceStatus: [BillingInvoiceStatus.OPEN]
- }, Constants.DB_PARAMS_SINGLE_RECORD);
+ invoiceStatus: [BillingInvoiceStatus.DRAFT, BillingInvoiceStatus.OPEN]
+ }, Constants.DB_PARAMS_MAX_LIMIT);
return invoicesMDB;
}

stripe - finilize step is implicit