diff --git a/packages/payments-plugin/src/stripe/raw-body.middleware.ts b/packages/payments-plugin/src/stripe/raw-body.middleware.ts index 0f02e7fe41..71c21b55ac 100644 --- a/packages/payments-plugin/src/stripe/raw-body.middleware.ts +++ b/packages/payments-plugin/src/stripe/raw-body.middleware.ts @@ -1,4 +1,4 @@ -import { json } from 'body-parser'; +import { raw } from 'body-parser'; import * as http from 'http'; import { RequestWithRawBody } from './types'; @@ -7,7 +7,7 @@ import { RequestWithRawBody } from './types'; * Middleware which adds the raw request body to the incoming message object. This is needed by * Stripe to properly verify webhook events. */ -export const rawBodyMiddleware = json({ +export const rawBodyMiddleware = raw({ type: '*/*', verify(req: RequestWithRawBody, res: http.ServerResponse, buf: Buffer, encoding: string) { if (Buffer.isBuffer(buf)) { diff --git a/packages/payments-plugin/src/stripe/stripe.controller.ts b/packages/payments-plugin/src/stripe/stripe.controller.ts index 6f56a92ee7..0c80a5d512 100644 --- a/packages/payments-plugin/src/stripe/stripe.controller.ts +++ b/packages/payments-plugin/src/stripe/stripe.controller.ts @@ -45,7 +45,7 @@ export class StripeController { return; } - const event = request.body as Stripe.Event; + const event = JSON.parse(request.body.toString()) as Stripe.Event; const paymentIntent = event.data.object as Stripe.PaymentIntent; if (!paymentIntent) { @@ -120,14 +120,20 @@ export class StripeController { `Error adding payment to order ${orderCode}: ${addPaymentToOrderResult.message}`, loggerCtx, ); + return; } + // The payment intent ID is added to the order only if we can reach this point. Logger.info( `Stripe payment intent id ${paymentIntent.id} added to order ${orderCode}`, loggerCtx, ); - response.status(HttpStatus.OK).send('Ok'); }); + + // Send the response status only if we didn't sent anything yet. + if (!response.headersSent) { + response.status(HttpStatus.OK).send('Ok'); + } } private async createContext(channelToken: string, req: RequestWithRawBody): Promise {