Browse Source

fix(payments-plugin): Attach incoming req to `ctx` in Stripe webhook

Fixes #1643
Michael Bromley 3 years ago
parent
commit
cb13e9943e

+ 2 - 2
packages/payments-plugin/src/stripe/raw-body.middleware.ts

@@ -1,14 +1,14 @@
 import { json } from 'body-parser';
 import { ServerResponse } from 'http';
 
-import { IncomingMessageWithRawBody } from './types';
+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({
-    verify(req: IncomingMessageWithRawBody, _: ServerResponse, buf: Buffer) {
+    verify(req: RequestWithRawBody, _: ServerResponse, buf: Buffer) {
         if (Buffer.isBuffer(buf)) {
             req.rawBody = Buffer.from(buf);
         }

+ 11 - 14
packages/payments-plugin/src/stripe/stripe.controller.ts

@@ -1,6 +1,5 @@
 import { Controller, Headers, HttpStatus, Post, Req, Res } from '@nestjs/common';
 import {
-    ChannelService,
     InternalServerError,
     LanguageCode,
     Logger,
@@ -8,16 +7,17 @@ import {
     OrderService,
     PaymentMethod,
     RequestContext,
-    TransactionalConnection
+    RequestContextService,
+    TransactionalConnection,
 } from '@vendure/core';
 import { OrderStateTransitionError } from '@vendure/core/dist/common/error/generated-graphql-shop-errors';
 import { Response } from 'express';
 import Stripe from 'stripe';
+
 import { loggerCtx } from './constants';
 import { stripePaymentMethodHandler } from './stripe.handler';
 import { StripeService } from './stripe.service';
-import { IncomingMessageWithRawBody } from './types';
-
+import { RequestWithRawBody } from './types';
 
 const missingHeaderErrorMessage = 'Missing stripe-signature header';
 const signatureErrorMessage = 'Error verifying Stripe webhook signature';
@@ -27,15 +27,15 @@ const noPaymentIntentErrorMessage = 'No payment intent in the event payload';
 export class StripeController {
     constructor(
         private connection: TransactionalConnection,
-        private channelService: ChannelService,
         private orderService: OrderService,
         private stripeService: StripeService,
+        private requestContextService: RequestContextService,
     ) {}
 
     @Post('stripe')
     async webhook(
         @Headers('stripe-signature') signature: string | undefined,
-        @Req() request: IncomingMessageWithRawBody,
+        @Req() request: RequestWithRawBody,
         @Res() response: Response,
     ): Promise<void> {
         if (!signature) {
@@ -75,7 +75,7 @@ export class StripeController {
             return;
         }
 
-        const ctx = await this.createContext(channelToken);
+        const ctx = await this.createContext(channelToken, request);
 
         const order = await this.orderService.findOneByCode(ctx, orderCode);
         if (!order) {
@@ -119,14 +119,11 @@ export class StripeController {
         response.status(HttpStatus.OK).send('Ok');
     }
 
-    private async createContext(channelToken: string): Promise<RequestContext> {
-        const channel = await this.channelService.getChannelFromToken(channelToken);
-
-        return new RequestContext({
+    private async createContext(channelToken: string, req: RequestWithRawBody): Promise<RequestContext> {
+        return this.requestContextService.create({
             apiType: 'admin',
-            isAuthorized: true,
-            authorizedAsOwnerOnly: false,
-            channel,
+            channelOrToken: channelToken,
+            req,
             languageCode: LanguageCode.en,
         });
     }

+ 2 - 1
packages/payments-plugin/src/stripe/types.ts

@@ -1,4 +1,5 @@
 import '@vendure/core/dist/entity/custom-entity-fields';
+import { Request } from 'express';
 import { IncomingMessage } from 'http';
 
 // Note: deep import is necessary here because CustomCustomerFields is also extended in the Braintree
@@ -39,6 +40,6 @@ export interface StripePluginOptions {
     storeCustomersInStripe?: boolean;
 }
 
-export interface IncomingMessageWithRawBody extends IncomingMessage {
+export interface RequestWithRawBody extends Request {
     rawBody: Buffer;
 }