stripe latest version  - invoice line total_count is deprecated
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -186,6 +186,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
// Get Invoice
try {
const stripeInvoice = await this.stripe.invoices.retrieve(id);
+ const nbrOfItems: number = this.getNumberOfItems(stripeInvoice);
return {
invoiceID: stripeInvoice.id,
customerID: stripeInvoice.customer,
@@ -194,7 +195,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
status: stripeInvoice.status as BillingInvoiceStatus,
currency: stripeInvoice.currency,
createdOn: new Date(stripeInvoice.created * 1000),
- nbrOfItems: stripeInvoice.lines.data.length,
+ nbrOfItems: nbrOfItems,
downloadUrl: stripeInvoice.invoice_pdf
} as BillingInvoice;
} catch (e) {
@@ -202,6 +203,12 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}
}
+ private getNumberOfItems(stripeInvoice: Stripe.Invoice): number {
+ // STRIPE version 8.137.0 - total_count property is deprecated - TODO - find another way to get it!
+ const nbrOfItems: number = stripeInvoice.lines['total_count'];
+ return nbrOfItems;
+ }
+
public async getUpdatedUserIDsInBilling(): Promise<string[]> {
const createdSince = this.settings.usersLastSynchronizedOn ? moment(this.settings.usersLastSynchronizedOn).unix() : 0;
const collectedCustomerIDs: string[] = [];
@@ -297,11 +304,12 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
idempotency_key: idempotencyKey?.toString()
});
// Let's update the data which is replicated on our side
- return this.upsertBillingInvoice(user, stripeInvoice);
+ return this.updateBillingInvoice(user, stripeInvoice);
}
- private async upsertBillingInvoice(billingUser: BillingUser, stripeInvoice: Stripe.Invoice): Promise<BillingInvoice> {
+ private async updateBillingInvoice(billingUser: BillingUser, stripeInvoice: Stripe.Invoice): Promise<BillingInvoice> {
const customerID = stripeInvoice.customer;
+ const nbrOfItems: number = this.getNumberOfItems(stripeInvoice);
const invoice = {
invoiceID: stripeInvoice.id,
customerID,
@@ -310,7 +318,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
status: stripeInvoice.status as BillingInvoiceStatus,
currency: stripeInvoice.currency,
createdOn: new Date(),
- nbrOfItems: stripeInvoice.lines.data.length,
+ nbrOfItems,
} as BillingInvoice;
// Set user
invoice.user = await UserStorage.getUserByBillingID(this.tenantID, billingUser.billingData.customerID);
@@ -595,12 +603,10 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
action: ServerAction.BILLING_TRANSACTION
});
}
-
// Let's get the raw data from stripe!
const stripeInvoice = await this.getStripeInvoice(draftInvoice.invoiceID);
-
// Well ... we need to update the billing invoice to reflect the latest changes
- await this.upsertBillingInvoice(billingUser, stripeInvoice);
+ await this.updateBillingInvoice(billingUser, stripeInvoice);
}
// Return the operation result as a BillingDataTransactionStop
return {

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -193,6 +193,28 @@ describe('Billing Service', function() {
// expect(exists).to.be.true;
});
+ describe('Where admin user (essential)', () => {
+ // eslint-disable-next-line @typescript-eslint/require-await
+ before(async () => {
+ testData.userContext = testData.adminUserContext;
+ assert(testData.userContext, 'User context cannot be null');
+ testData.userService = testData.adminUserService;
+ assert(!!testData.userService, 'User service cannot be null');
+ // await testData.setBillingSystemValidCredentials();
+ });
+
+ 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);
+ const transactionID = await testData.generateTransaction(testData.userContext);
+ expect(transactionID).to.not.be.null;
+ await testData.userService.billingApi.synchronizeInvoices({});
+ const itemsAfter = await testData.getNumberOfItems(testData.userContext);
+ expect(itemsAfter).to.be.eq(itemsBefore + 1);
+ });
+
+ });
+
describe('Where admin user', () => {
// eslint-disable-next-line @typescript-eslint/require-await
before(async () => {

stripe latest version  - invoice line total_count is deprecated