billing - api - code cleanup
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -162,7 +162,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
// -------------------------------------------------------------------------------------------
// Regular Situation - CustomerID is set and we trust it!
// -------------------------------------------------------------------------------------------
- exists = await this.userExists(user); // returns false when the customerID is not set
+ exists = await this.isUserSynchronized(user); // returns false when the customerID is not set
} else {
// -------------------------------------------------------------------------------------------
// Specific use-case - Trying to REPAIR inconsistencies
@@ -292,7 +292,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
source: Constants.CENTRAL_SERVER,
action: ServerAction.BILLING_CHARGE_INVOICE,
module: MODULE_NAME, method: 'chargeInvoices',
- message: `Successfully charge invoice '${openInvoice.id}'`
+ message: `Successfully charged invoice '${openInvoice.id}'`
});
actionsDone.inSuccess++;
} catch (error) {
@@ -479,7 +479,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
abstract deleteUser(user: User): Promise<void>;
- abstract userExists(user: User): Promise<boolean>;
+ abstract isUserSynchronized(user: User): Promise<boolean>;
abstract getTaxes(): Promise<BillingTax[]>;
@@ -491,14 +491,12 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
abstract downloadInvoiceDocument(invoice: BillingInvoice): Promise<BillingInvoiceDocument>;
- // abstract finalizeInvoice(invoice: BillingInvoice): Promise<string>;
-
- abstract setupPaymentMethod(user: User, paymentMethodId: string): Promise<BillingOperationResult>;
-
abstract chargeInvoice(invoice: BillingInvoice): Promise<BillingInvoice>;
abstract consumeBillingEvent(req: Request): Promise<boolean>;
+ abstract setupPaymentMethod(user: User, paymentMethodId: string): Promise<BillingOperationResult>;
+
abstract getPaymentMethods(user: User): Promise<BillingPaymentMethodResult>;
abstract deletePaymentMethod(user: User, paymentMethodId: string): Promise<BillingPaymentMethodResult>;

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -166,7 +166,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
return billingUser;
}
- public async userExists(user: User): Promise<boolean> {
+ public async isUserSynchronized(user: User): Promise<boolean> {
// Check Stripe
await this.checkConnection();
if (!user.billingData) {
@@ -418,7 +418,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
public async chargeInvoice(billingInvoice: BillingInvoice): Promise<BillingInvoice> {
await this.checkConnection();
- try {
const billingOperationResult: BillingOperationResult = await this._chargeStripeInvoice(billingInvoice.invoiceID);
billingInvoice = await this.synchronizeAsBillingInvoice(billingInvoice.invoiceID, false);
if (!billingOperationResult.succeeded) {
@@ -427,15 +426,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
await BillingStorage.saveLastPaymentFailure(this.tenantID, billingInvoice.id, billingOperationResult);
}
return billingInvoice;
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'chargeInvoice',
- action: ServerAction.BILLING,
- message: `Stripe Operation Failed: ${error.message as string}`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
private async _chargeStripeInvoice(invoiceID: string): Promise<BillingOperationResult> {
@@ -1013,15 +1003,13 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
return new Decimal(transaction.stop.totalConsumptionWh).dividedBy(10).round().dividedBy(100).toNumber();
}
+ // eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
public async checkIfUserCanBeCreated(user: User): Promise<boolean> {
- // Check
- return this.checkIfUserCanBeUpdated(user);
+ return true;
}
- // eslint-disable-next-line @typescript-eslint/require-await
+ // eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
public async checkIfUserCanBeUpdated(user: User): Promise<boolean> {
- // Check connection
- // await this.checkConnection();
return true;
}
@@ -1085,7 +1073,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
public async createUser(user: User): Promise<BillingUser> {
// Check
- const success = await this.checkIfUserCanBeUpdated(user);
+ const success = await this.checkIfUserCanBeCreated(user);
if (!success) {
throw new BackendError({
source: Constants.CENTRAL_SERVER,

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -237,8 +237,8 @@ describe('Billing Service', function() {
);
testData.createdUsers.push(fakeUser);
// Let's check that the corresponding billing user exists as well (a Customer in the STRIPE DB)
- let exists = await testData.billingImpl.userExists(fakeUser);
- expect(exists).to.be.true;
+ let billingUser = await testData.billingImpl.getUser(fakeUser);
+ expect(billingUser).to.be.not.null;
// Let's update the new user
fakeUser.firstName = 'Test';
fakeUser.name = 'NAME';
@@ -249,7 +249,7 @@ describe('Billing Service', function() {
false
);
// Let's check that the corresponding billing user was updated as well
- const billingUser = await testData.billingImpl.getUser(fakeUser);
+ billingUser = await testData.billingImpl.getUser(fakeUser);
expect(billingUser.name).to.be.eq(fakeUser.firstName + ' ' + fakeUser.name);
// Let's delete the user
await testData.userService.deleteEntity(
@@ -257,13 +257,11 @@ describe('Billing Service', function() {
{ id: testData.createdUsers[0].id }
);
// Verify that the corresponding billing user is gone
- exists = await testData.billingImpl.userExists(testData.createdUsers[0]);
+ const exists = await testData.billingImpl.isUserSynchronized(testData.createdUsers[0]);
expect(exists).to.be.false;
testData.createdUsers.shift();
-
});
-
it('should add an item to the existing invoice after a transaction', async () => {
await testData.userService.billingApi.forceSynchronizeUser({ id: testData.userContext.id });
const itemsBefore = await testData.getNumberOfItems(testData.userContext.id);
@@ -481,7 +479,7 @@ describe('Billing Service', function() {
testData.createdUsers.push(fakeUser);
testData.billingImpl = await testData.setBillingSystemValidCredentials();
await testData.userService.billingApi.synchronizeUser({ id: fakeUser.id });
- const userExists = await testData.billingImpl.userExists(fakeUser);
+ const userExists = await testData.billingImpl.isUserSynchronized(fakeUser);
expect(userExists).to.be.true;
});
});

billing - api - code cleanup