From 718b9a04e6863741f866e04777032c71fa57ce12 Mon Sep 17 00:00:00 2001 From: Ben Bangert Date: Mon, 11 Jul 2022 15:04:06 -0700 Subject: [PATCH] fix(auth): firestore processor didnt ignore proper errors Because: * The firestore processor failed to find existing records for deleted customers nor could it create them as we don't store deleted customers. This commit: * Catches both the stripe customer deleted error and the lack of a firestore record from the same error case. Closes #13591 --- .../fxa-auth-server/lib/payments/stripe.ts | 7 +++++- .../test/local/payments/stripe.js | 23 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/fxa-auth-server/lib/payments/stripe.ts b/packages/fxa-auth-server/lib/payments/stripe.ts index 1493aaf422..6611f5eeb9 100644 --- a/packages/fxa-auth-server/lib/payments/stripe.ts +++ b/packages/fxa-auth-server/lib/payments/stripe.ts @@ -3014,7 +3014,12 @@ export class StripeHelper extends StripeHelperBase { } } } catch (err) { - if (err.name === FirestoreStripeError.STRIPE_CUSTOMER_DELETED) { + if ( + [ + FirestoreStripeError.STRIPE_CUSTOMER_DELETED, + FirestoreStripeError.FIRESTORE_CUSTOMER_NOT_FOUND, + ].includes(err.name) + ) { // We cannot back-fill Firestore with records for deleted customers // as they're missing necessary metadata for us to know which user // the customer belongs to. diff --git a/packages/fxa-auth-server/test/local/payments/stripe.js b/packages/fxa-auth-server/test/local/payments/stripe.js index ec71ee2d6b..8b000b8b43 100644 --- a/packages/fxa-auth-server/test/local/payments/stripe.js +++ b/packages/fxa-auth-server/test/local/payments/stripe.js @@ -5916,7 +5916,7 @@ describe('StripeHelper', () => { ); }); - it('ignores the deleted customer error when handling a payment method update event', async () => { + it('ignores the deleted stripe customer error when handling a payment method update event', async () => { const event = deepCopy(eventPaymentMethodAttached); event.type = 'payment_method.card_automatically_updated'; stripeHelper.stripe.paymentMethods.retrieve = sandbox @@ -5937,6 +5937,27 @@ describe('StripeHelper', () => { ); }); + it('ignores the firestore record not found error when handling a payment method update event', async () => { + const event = deepCopy(eventPaymentMethodAttached); + event.type = 'payment_method.card_automatically_updated'; + stripeHelper.stripe.paymentMethods.retrieve = sandbox + .stub() + .resolves(event.data.object); + stripeFirestore.insertPaymentMethodRecordWithBackfill = sandbox + .stub() + .throws( + newFirestoreStripeError( + 'Customer deleted.', + FirestoreStripeError.FIRESTORE_CUSTOMER_NOT_FOUND + ) + ); + await stripeHelper.processWebhookEventToFirestore(event); + sinon.assert.calledOnceWithExactly( + stripeFirestore.insertPaymentMethodRecordWithBackfill, + event.data.object + ); + }); + it('does not handle wibble events', async () => { const event = deepCopy(eventSubscriptionUpdated); event.type = 'wibble';