Merge pull request #6181 from mozilla/6175-sub-download-email-on-success

fix(payments): send subscription download email along with initial invoice
This commit is contained in:
Les Orchard 2020-08-12 09:40:20 -07:00 коммит произвёл GitHub
Родитель 4326307b7f d080dad75c
Коммит 226c677577
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 25 добавлений и 38 удалений

Просмотреть файл

@ -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<T>(
resource: string | T,
resourceType: string
resourceType: typeof VALID_RESOURCE_TYPES[number]
): Promise<T> {
if (typeof resource !== 'string') {
return resource;

Просмотреть файл

@ -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

Просмотреть файл

@ -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(