stripe - invoice - amountPaid + renaming!
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -249,7 +249,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
// Get billing invoice
const invoiceInBilling = await this.getInvoice(invoiceIDInBilling);
// Get e-Mobility invoice
- const invoice = await BillingStorage.getInvoiceByBillingInvoiceID(this.tenantID, invoiceIDInBilling);
+ const invoice = await BillingStorage.getInvoiceByInvoiceID(this.tenantID, invoiceIDInBilling);
if (!invoiceInBilling && !invoice) {
actionsDone.inError++;
await Logging.logError({

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -220,7 +220,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
// Get Invoice
try {
const stripeInvoice = await this.stripe.invoices.retrieve(id);
- const { id: invoiceID, customer, number, amount_due: amount, amount_paid, status, currency, invoice_pdf: downloadUrl } = stripeInvoice;
+ const { id: invoiceID, customer, number, amount_due: amount, amount_paid: amountPaid, status, currency, invoice_pdf: downloadUrl } = stripeInvoice;
const nbrOfItems: number = this.getNumberOfItems(stripeInvoice);
const customerID = customer as string;
const billingInvoice: BillingInvoice = {
@@ -229,12 +229,13 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
customerID,
number,
amount,
- amount_paid,
+ amountPaid,
status: status as BillingInvoiceStatus,
currency,
createdOn: new Date(stripeInvoice.created * 1000),
nbrOfItems: nbrOfItems,
- downloadUrl
+ downloadUrl,
+ downloadable: !!downloadUrl
};
return billingInvoice;
} catch (e) {
@@ -341,12 +342,25 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
return stripeInvoice;
}
- private async _replicateStripeInvoice(userID: string, stripeInvoiceID: string): Promise<BillingInvoice> {
+ public async synchronizeAsBillingInvoice(userID: string, stripeInvoiceID: string): Promise<BillingInvoice> {
// Make sure to get fresh data !
const stripeInvoice: Stripe.Invoice = await this.getStripeInvoice(stripeInvoiceID);
if (!stripeInvoice) {
throw new BackendError({
message: `Unexpected situation - invoice not found - ${stripeInvoiceID}`,
+ source: Constants.CENTRAL_SERVER, module: MODULE_NAME, action: ServerAction.BILLING_TRANSACTION,
+ method: '_replicateStripeInvoice',
+ });
+ }
+ // Destructuring the STRIPE invoice to extract the required information
+ const { id: invoiceID, customer, number, amount_due: amount, amount_paid: amountPaid, status, currency, invoice_pdf: downloadUrl, metadata } = stripeInvoice;
+ const customerID = customer as string;
+ const createdOn = moment.unix(stripeInvoice.created).toDate(); // epoch to Date!
+ // Check metadata consistency - userID is mandatory!
+ const eMobilityUserID = metadata?.userID;
+ if (userID !== eMobilityUserID) {
+ throw new BackendError({
+ message: `Unexpected situation - userID metadata is not properly set in invoice - ${stripeInvoiceID}`,
source: Constants.CENTRAL_SERVER,
module: MODULE_NAME,
method: '_replicateStripeInvoice',
@@ -354,26 +368,16 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
});
}
// Get the corresponding BillingInvoice (if any)
- const billingInvoice: BillingInvoice = await BillingStorage.getInvoiceByBillingInvoiceID(this.tenantID, stripeInvoice.id);
+ const billingInvoice: BillingInvoice = await BillingStorage.getInvoiceByInvoiceID(this.tenantID, stripeInvoice.id);
const nbrOfItems: number = this.getNumberOfItems(stripeInvoice);
const invoiceToSave: BillingInvoice = {
- id: billingInvoice?.id,
- userID,
- invoiceID: stripeInvoice.id,
- customerID: stripeInvoice.customer as string,
- number: stripeInvoice.number,
- amount: stripeInvoice.amount_due,
- amount_paid: stripeInvoice.amount_paid,
- status: stripeInvoice.status as BillingInvoiceStatus,
- currency: stripeInvoice.currency,
- createdOn: moment.unix(stripeInvoice.created).toDate(), // epoch to Date!
- nbrOfItems,
- downloadUrl: stripeInvoice.invoice_pdf,
- downloadable: !!stripeInvoice.invoice_pdf,
+ id: billingInvoice?.id, // ACHTUNG: billingInvoice is null when creating the Billing Invoice
+ userID, invoiceID, customerID, number, amount, amountPaid, currency, createdOn, nbrOfItems, downloadUrl, downloadable: !!downloadUrl,
+ status: status as BillingInvoiceStatus,
};
// Let's persist the up-to-date data
- const invoiceId = await BillingStorage.saveInvoice(this.tenantID, invoiceToSave);
- const freshBillingInvoice = await BillingStorage.getInvoice(this.tenantID, invoiceId);
+ const freshInvoiceId = await BillingStorage.saveInvoice(this.tenantID, invoiceToSave);
+ const freshBillingInvoice = await BillingStorage.getInvoice(this.tenantID, freshInvoiceId);
if (freshBillingInvoice?.downloadable) {
// Replicate the invoice as a PDF document
const invoiceDocument = await this.downloadInvoiceDocument(freshBillingInvoice);
@@ -489,7 +493,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
await this.checkConnection();
try {
const stripeInvoice = await this._chargeStripeInvoice(invoice.invoiceID);
- const billingInvoice = await this._replicateStripeInvoice(invoice.userID, stripeInvoice.id);
+ const billingInvoice = await this.synchronizeAsBillingInvoice(invoice.userID, stripeInvoice.id);
return billingInvoice;
} catch (error) {
throw new BackendError({
@@ -858,7 +862,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
}
// Let's replicate some information on our side
- const billingInvoice = await this._replicateStripeInvoice(userID, stripeInvoice.id);
+ const billingInvoice = await this.synchronizeAsBillingInvoice(userID, stripeInvoice.id);
// Return the billing invoice
return billingInvoice;
}

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -19,7 +19,7 @@ export default class BillingStorage {
return invoicesMDB.count === 1 ? invoicesMDB.result[0] : null;
}
- public static async getInvoiceByBillingInvoiceID(tenantID: string, id: string): Promise<BillingInvoice> {
+ public static async getInvoiceByInvoiceID(tenantID: string, id: string): Promise<BillingInvoice> {
const invoicesMDB = await BillingStorage.getInvoices(tenantID, {
billingInvoiceID: id
}, Constants.DB_PARAMS_SINGLE_RECORD);
@@ -169,6 +169,7 @@ export default class BillingStorage {
userID: invoiceToSave.userID ? Utils.convertToObjectID(invoiceToSave.userID) : null,
customerID: invoiceToSave.customerID,
amount: Utils.convertToFloat(invoiceToSave.amount),
+ amountPaid: Utils.convertToFloat(invoiceToSave.amountPaid),
status: invoiceToSave.status,
currency: invoiceToSave.currency,
createdOn: Utils.convertToDate(invoiceToSave.createdOn),

MODIFY
src/types/Billing.ts
src/types/Billing.ts
@@ -74,7 +74,7 @@ export interface BillingInvoice {
number?: string;
status?: BillingInvoiceStatus;
amount?: number;
- amount_paid?: number;
+ amountPaid?: number;
currency?: string;
customerID?: string;
createdOn?: Date;

stripe - invoice - amountPaid + renaming!