Skip to content

Commit

Permalink
fix(payments-plugin): Revert to use raw-body-parser, and update Str…
Browse files Browse the repository at this point in the history
…ipeController's return logic
  • Loading branch information
Balázs Gallay committed Oct 17, 2023
1 parent cd16726 commit dcf6eac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/payments-plugin/src/stripe/raw-body.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { json } from 'body-parser';
import { raw } from 'body-parser';
import * as http from 'http';

import { RequestWithRawBody } from './types';
Expand All @@ -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)) {
Expand Down
10 changes: 8 additions & 2 deletions packages/payments-plugin/src/stripe/stripe.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<RequestContext> {
Expand Down

0 comments on commit dcf6eac

Please sign in to comment.