billing - repairUser  - to recover from inconsistencies
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -178,7 +178,12 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
// Create or Update the user and its billing data
let billingUser: BillingUser;
if (!exists) {
+ if (forceMode) {
+ // Specific situation where we want to repair inconsistencies
+ billingUser = await this.repairUser(user);
+ } else {
billingUser = await this.createUser(user);
+ }
} else {
billingUser = await this.updateUser(user);
}
@@ -476,6 +481,8 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
abstract updateUser(user: User): Promise<BillingUser>;
+ abstract repairUser(user: User): Promise<BillingUser>;
+
abstract deleteUser(user: User): Promise<void>;
abstract isUserSynchronized(user: User): Promise<boolean>;

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -1107,12 +1107,23 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
public async createUser(user: User): Promise<BillingUser> {
+ return await this._createUser(user, false);
+ }
+
+ public async repairUser(user: User): Promise<BillingUser> {
+ return await this._createUser(user, true);
+ }
+
+ private async _createUser(user: User, forceUserCreation: boolean): Promise<BillingUser> {
// Check connection
await this.checkConnection();
await this.checkIfUserCanBeCreated(user);
if (user.billingData?.customerID) {
+ // The customerID should be preserved - unless the creation is forced
+ if (!forceUserCreation) {
throw new Error('Unexpected situation - the customerID is already set');
}
+ }
// Checks create a new STRIPE customer
const customer: Stripe.Customer = await this.stripe.customers.create({
...this._buildCustomerCommonProperties(user),

billing - repairUser  - to recover from inconsistencies