diff --git a/packages/fxa-auth-server/lib/payments/stripe.ts b/packages/fxa-auth-server/lib/payments/stripe.ts index c773362091..9214283827 100644 --- a/packages/fxa-auth-server/lib/payments/stripe.ts +++ b/packages/fxa-auth-server/lib/payments/stripe.ts @@ -47,7 +47,7 @@ const VALID_RESOURCE_TYPES = [ PLAN_RESOURCE, CHARGES_RESOURCE, INVOICES_RESOURCE, -]; +] as const; export const SUBSCRIPTION_UPDATE_TYPES = { UPGRADE: 'upgrade', @@ -1587,7 +1587,7 @@ export class StripeHelper { */ async expandResource( resource: string | T, - resourceType: string + resourceType: typeof VALID_RESOURCE_TYPES[number] ): Promise { if (typeof resource !== 'string') { return resource; diff --git a/packages/fxa-auth-server/lib/routes/subscriptions.ts b/packages/fxa-auth-server/lib/routes/subscriptions.ts index 1964c5ca9a..2865c000cb 100644 --- a/packages/fxa-auth-server/lib/routes/subscriptions.ts +++ b/packages/fxa-auth-server/lib/routes/subscriptions.ts @@ -151,10 +151,7 @@ class DirectStripeRoutes { idempotencyKey, } = request.payload as any; - // Find the selected plan and get its product ID const selectedPlan = await this.stripeHelper.findPlanById(planId); - const productId = selectedPlan.product_id; - const productMetadata = metadataFromPlan(selectedPlan); const customer = await this.stripeHelper.fetchCustomer(uid, email, [ 'data.subscriptions.data.latest_invoice', @@ -186,19 +183,6 @@ class DirectStripeRoutes { await this.customerChanged(request, uid, email); - const account = await this.db.account(uid); - await this.mailer.sendDownloadSubscriptionEmail(account.emails, account, { - acceptLanguage: account.locale, - productId, - planId, - planName: selectedPlan.plan_name, - productName: selectedPlan.product_name, - planEmailIconURL: productMetadata.emailIconURL, - planDownloadURL: productMetadata.downloadURL, - appStoreLink: productMetadata.appStoreLink, - playStoreLink: productMetadata.playStoreLink, - productMetadata, - }); this.log.info('subscriptions.createSubscription.success', { uid, subscriptionId: subscription.id, @@ -664,6 +648,9 @@ class DirectStripeRoutes { paymentMethodId, retryIdempotencyKey ); + + await this.customerChanged(request, uid, email); + return filterInvoice(invoice); } @@ -707,22 +694,6 @@ class DirectStripeRoutes { await this.customerChanged(request, uid, email); - const account = await this.db.account(uid); - const selectedPlan = await this.stripeHelper.findPlanById(priceId); - const productId = selectedPlan.product_id; - const productMetadata = metadataFromPlan(selectedPlan); - await this.mailer.sendDownloadSubscriptionEmail(account.emails, account, { - acceptLanguage: account.locale, - productId, - planId: priceId, - planName: selectedPlan.plan_name, - productName: selectedPlan.product_name, - planEmailIconURL: productMetadata.emailIconURL, - planDownloadURL: productMetadata.downloadURL, - appStoreLink: productMetadata.appStoreLink, - playStoreLink: productMetadata.playStoreLink, - productMetadata, - }); this.log.info('subscriptions.createSubscriptionWithPMI.success', { uid, subscriptionId: subscription.id, @@ -1108,6 +1079,7 @@ class DirectStripeRoutes { switch (invoice.billing_reason) { case 'subscription_create': await this.mailer.sendSubscriptionFirstInvoiceEmail(...mailParams); + await this.mailer.sendDownloadSubscriptionEmail(...mailParams); break; default: // Other billing reasons should be covered in subsequent invoice email diff --git a/packages/fxa-auth-server/test/local/routes/subscriptions.js b/packages/fxa-auth-server/test/local/routes/subscriptions.js index a4eed226b8..639cf2e197 100644 --- a/packages/fxa-auth-server/test/local/routes/subscriptions.js +++ b/packages/fxa-auth-server/test/local/routes/subscriptions.js @@ -658,7 +658,6 @@ describe('DirectStripeRoutes', () => { TEST_EMAIL ) ); - assert.isTrue(mailer.sendDownloadSubscriptionEmail.calledOnce); assert.isTrue(log.info.calledOnce); assert.deepEqual(actual, expected); @@ -715,7 +714,6 @@ describe('DirectStripeRoutes', () => { TEST_EMAIL ) ); - assert.isTrue(mailer.sendDownloadSubscriptionEmail.calledOnce); assert.isTrue(log.info.calledOnce); assert.deepEqual(actual, expected); @@ -777,7 +775,6 @@ describe('DirectStripeRoutes', () => { UID, TEST_EMAIL ); - assert.isTrue(mailer.sendDownloadSubscriptionEmail.calledOnce); assert.deepEqual( { @@ -849,7 +846,6 @@ describe('DirectStripeRoutes', () => { UID, TEST_EMAIL ); - assert.isTrue(mailer.sendDownloadSubscriptionEmail.calledOnce); }); }); @@ -861,6 +857,7 @@ describe('DirectStripeRoutes', () => { directStripeRoutesInstance.stripeHelper.retryInvoiceWithPaymentId.resolves( expected ); + sinon.stub(directStripeRoutesInstance, 'customerChanged').resolves(); VALID_REQUEST.payload = { invoiceId: 'in_testinvoice', paymentMethodId: 'pm_asdf', @@ -871,6 +868,13 @@ describe('DirectStripeRoutes', () => { VALID_REQUEST ); + sinon.assert.calledWith( + directStripeRoutesInstance.customerChanged, + VALID_REQUEST, + UID, + TEST_EMAIL + ); + assert.deepEqual(filterInvoice(expected), actual); }); @@ -2389,6 +2393,17 @@ describe('DirectStripeRoutes', () => { ...mockInvoiceDetails, } ); + if (expectedMethodName === 'sendSubscriptionFirstInvoiceEmail') { + assert.calledWith( + directStripeRoutesInstance.mailer.sendDownloadSubscriptionEmail, + mockAccount.emails, + mockAccount, + { + acceptLanguage: mockAccount.locale, + ...mockInvoiceDetails, + } + ); + } }; it(