saveInvoice - fix createdOn issue (unix epoch to date)
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -348,7 +348,8 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
// Get the corresponding BillingInvoice (if any)
const billingInvoice: BillingInvoice = await BillingStorage.getInvoiceByBillingInvoiceID(this.tenantID, stripeInvoice.id);
const nbrOfItems: number = this.getNumberOfItems(stripeInvoice);
- const invoiceToSave: Partial<BillingInvoice> = {
+ const invoiceToSave: BillingInvoice = {
+ id: billingInvoice?.id,
userID,
invoiceID: stripeInvoice.id,
customerID: stripeInvoice.customer as string,
@@ -356,15 +357,15 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
amount: stripeInvoice.amount_due,
status: stripeInvoice.status as BillingInvoiceStatus,
currency: stripeInvoice.currency,
- createdOn: new Date(), // TODO - This is suspicious - what about the update?
+ createdOn: new Date(stripeInvoice.created * 1000), // epoch to Date!
nbrOfItems,
downloadUrl: stripeInvoice.invoice_pdf,
downloadable: !!stripeInvoice.invoice_pdf,
};
- if (billingInvoice) {
- invoiceToSave.id = billingInvoice.id;
- }
+ // if (billingInvoice) {
+ // invoiceToSave.id = billingInvoice.id;
+ // }
const invoiceId = await BillingStorage.saveInvoice(this.tenantID, invoiceToSave);
return BillingStorage.getInvoice(this.tenantID, invoiceId);
}
@@ -429,6 +430,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
}
+ // eslint-disable-next-line @typescript-eslint/require-await
public async consumeBillingEvent(req: Request): Promise<boolean> {
let event: { data, type: string };
if (process.env.STRIPE_WEBHOOK_SECRET) { // ##CR - to be clarified - where this secret key should come from

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -259,26 +259,6 @@ describe('Billing Service', function() {
});
- it('Should force a user synchronization', async () => {
- const fakeUser = {
- ...Factory.user.build(),
- } as User;
- fakeUser.issuer = true;
- testData.billingImpl = await testData.setBillingSystemValidCredentials();
- await testData.userService.createEntity(
- testData.userService.userApi,
- fakeUser
- );
- testData.createdUsers.push(fakeUser);
- fakeUser.billingData = { customerID: 'cus_utbilling_fake_user' }; // TODO - Updating billing data is not possible anymore - filterUserRequest prevents this to happen!!!
- await testData.userService.updateEntity(
- testData.userService.userApi,
- fakeUser
- );
- await testData.userService.billingApi.forceSynchronizeUser({ id: fakeUser.id });
- const billingUserAfter = await testData.billingImpl.getUser(fakeUser);
- expect(fakeUser.billingData.customerID).to.not.be.eq(billingUserAfter.billingData.customerID);
- });
it('should add an item to the existing invoice after a transaction', async () => {
await testData.userService.billingApi.forceSynchronizeUser({ id: testData.userContext.id });
@@ -324,6 +304,27 @@ describe('Billing Service', function() {
const response = await testData.userService.billingApi.synchronizeInvoices({});
expect(response.data).containSubset(Constants.REST_RESPONSE_SUCCESS);
});
+
+ it('Should force a user synchronization', async () => {
+ const fakeUser = {
+ ...Factory.user.build(),
+ } as User;
+ fakeUser.issuer = true;
+ testData.billingImpl = await testData.setBillingSystemValidCredentials();
+ await testData.userService.createEntity(
+ testData.userService.userApi,
+ fakeUser
+ );
+ testData.createdUsers.push(fakeUser);
+ fakeUser.billingData = { customerID: 'cus_utbilling_fake_user' }; // TODO - not supported anymore
+ await testData.userService.updateEntity(
+ testData.userService.userApi,
+ fakeUser
+ );
+ await testData.userService.billingApi.forceSynchronizeUser({ id: fakeUser.id });
+ const billingUserAfter = await testData.billingImpl.getUser(fakeUser);
+ expect(fakeUser.billingData.customerID).to.not.be.eq(billingUserAfter.billingData.customerID);
+ });
});
describe('Where basic user', () => {

saveInvoice - fix createdOn issue (unix epoch to date)