stripe - synch - bug -preserve Billing Data
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -109,7 +109,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
continue;
}
// Get Billing User
- const billingUser = await this.getBillingUserByInternalID(userBillingIDChangedInBilling);
+ const billingUser = await this.getUser(user);
if (!billingUser) {
// Only triggers an error if e-Mobility user is not deleted
actionsDone.inError++;
@@ -638,8 +638,6 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
abstract checkIfUserCanBeDeleted(user: User): Promise<boolean>;
- abstract getBillingUserByInternalID(id: string): Promise<BillingUser>;
-
abstract getUsers(): Promise<BillingUser[]>;
abstract getUser(user: User): Promise<BillingUser>;

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
/* eslint-disable @typescript-eslint/member-ordering */
-import { BillingDataTransactionStart, BillingDataTransactionStop, BillingDataTransactionUpdate, BillingInvoice, BillingInvoiceDocument, BillingInvoiceItem, BillingInvoiceStatus, BillingOperationResult, BillingStatus, BillingTax, BillingUser } from '../../../types/Billing';
+import { BillingDataTransactionStart, BillingDataTransactionStop, BillingDataTransactionUpdate, BillingInvoice, BillingInvoiceDocument, BillingInvoiceItem, BillingInvoiceStatus, BillingOperationResult, BillingStatus, BillingTax, BillingUser, BillingUserData } from '../../../types/Billing';
import { DocumentEncoding, DocumentType } from '../../../types/GlobalType';
import AxiosFactory from '../../../utils/AxiosFactory';
@@ -136,20 +136,26 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
return users;
}
-
- private convertToBillingUser(customer: Stripe.Customer) : BillingUser {
- if (customer) {
+ private convertToBillingUser(customer: Stripe.Customer, user: User) : BillingUser {
+ // Check for the deletion flag
+ const deleted: boolean = customer?.deleted as unknown as boolean; // ACHTUNG - Hack because STRIPE type definition is wrong!
+ if (customer && !deleted) {
+ const previousBillingData = user?.billingData;
+ const newBillingData: BillingUserData = {
+ ...previousBillingData, // Preserve previous values if any
+ customerID: customer.id,
+ lastChangedOn: new Date(),
+ hasSynchroError: false
+ };
+ const userID = customer.metadata?.['userID'];
const billingUser: BillingUser = {
- userID: customer.metadata['userID'],
- // email: customer.email,
+ userID,
name: customer.name,
- billingData: {
- customerID: customer.id
- }
+ billingData: newBillingData
};
return billingUser;
}
-
+ // return null when the customer is marked as deleted in STRIPE
return null;
}
@@ -186,15 +192,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
// Return the corresponding Billing User
- return this.convertToBillingUser(customer);
- }
-
- public async getBillingUserByInternalID(customerID: string): Promise<BillingUser> {
- // Check Stripe
- await this.checkConnection();
- // Get customIDer
- const customer: Stripe.Customer = await this.getStripeCustomer(customerID);
- return this.convertToBillingUser(customer);
+ return this.convertToBillingUser(customer, user);
}
public async getTaxes(): Promise<BillingTax[]> {
@@ -1130,6 +1128,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
customer.id, userDataToUpdate
);
// Let's return the corresponding Billing User
- return this.convertToBillingUser(customer);
+ return this.convertToBillingUser(customer, user);
}
}

MODIFY
test/api/BillingStripeTestData.ts
test/api/BillingStripeTestData.ts
@@ -62,8 +62,8 @@ export default class StripeIntegrationTestData {
public async forceBillingSettings(immediateBilling: boolean): Promise<void> {
// The tests requires some settings to be forced
this.billingImpl = await this.setBillingSystemValidCredentials(immediateBilling);
- // Let's get access to the STRIPE implementation - StripeBillingIntegration instance
- this.billingUser = await this.billingImpl.getBillingUserByInternalID(this.getCustomerID());
+ expect(this.billingImpl, 'Billing implementation should not ber null');
+ this.billingUser = await this.billingImpl.getUser(this.dynamicUser);
expect(this.billingUser, 'Billing user should not ber null');
}

stripe - synch - bug -preserve Billing Data