fix(stripe): use new data structure for webhooks event.request

This commit is contained in:
Matteo Pagliazzi
2020-12-26 16:45:36 +01:00
parent bc74e40280
commit 17d918a172
2 changed files with 9 additions and 8 deletions

View File

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

View File

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