From 17d918a172f1a6d4cf5ab8ccd39e220ffdbb5047 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 26 Dec 2020 16:45:36 +0100 Subject: [PATCH] fix(stripe): use new data structure for webhooks event.request --- .../api/unit/libs/payments/stripe/webhooks.test.js | 14 +++++++------- website/server/libs/payments/stripe/webhooks.js | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/test/api/unit/libs/payments/stripe/webhooks.test.js b/test/api/unit/libs/payments/stripe/webhooks.test.js index de8d515273..b6d04151f3 100644 --- a/test/api/unit/libs/payments/stripe/webhooks.test.js +++ b/test/api/unit/libs/payments/stripe/webhooks.test.js @@ -90,11 +90,11 @@ describe('Stripe - Webhooks', () => { sandbox.stub(payments, 'cancelSubscription').resolves({}); }); - it('does not do anything if event.request is null (subscription cancelled manually)', async () => { + it('does not do anything if event.request is not null (subscription cancelled manually)', async () => { constructEventStub.returns({ id: 123, type: eventType, - request: 123, + request: { id: 123 }, }); await stripePayments.handleWebhooks({ body, headers }, stripe); @@ -118,7 +118,7 @@ describe('Stripe - Webhooks', () => { customer: customerId, }, }, - request: null, + request: { id: null }, }); await expect(stripePayments.handleWebhooks({ body, headers }, stripe)) @@ -151,7 +151,7 @@ describe('Stripe - Webhooks', () => { customer: customerId, }, }, - request: null, + request: { id: null }, }); await stripePayments.handleWebhooks({ body, headers }, stripe); @@ -182,7 +182,7 @@ describe('Stripe - Webhooks', () => { customer: customerId, }, }, - request: null, + request: { id: null }, }); await expect(stripePayments.handleWebhooks({ body, headers }, stripe)) @@ -220,7 +220,7 @@ describe('Stripe - Webhooks', () => { customer: customerId, }, }, - request: null, + request: { id: null }, }); await expect(stripePayments.handleWebhooks({ body, headers }, stripe)) @@ -261,7 +261,7 @@ describe('Stripe - Webhooks', () => { customer: customerId, }, }, - request: null, + request: { id: null }, }); await stripePayments.handleWebhooks({ body, headers }, stripe); diff --git a/website/server/libs/payments/stripe/webhooks.js b/website/server/libs/payments/stripe/webhooks.js index f3a48221b1..0a541b968d 100644 --- a/website/server/libs/payments/stripe/webhooks.js +++ b/website/server/libs/payments/stripe/webhooks.js @@ -32,6 +32,7 @@ export async function handleWebhooks (options, stripeInc) { try { // Verify the event by fetching it from Stripe event = stripeApi.webhooks.constructEvent(body, headers['stripe-signature'], endpointSecret); + console.log(event); } catch (err) { logger.error(new Error('Error verifying Stripe webhook'), { err }); throw new BadRequest(`Webhook Error: ${err.message}`); @@ -80,7 +81,7 @@ export async function handleWebhooks (options, stripeInc) { case 'customer.subscription.deleted': { // event.request !== null means that the user itself cancelled the subscrioption, // the cancellation on our side has been already handled - if (event.request !== null) break; + if (event.request && event.request.id !== null) break; const subscription = event.data.object; const customerId = subscription.customer;