Просмотр исходного кода

fix(payments-plugin): Events triggered by Mollie webhook include request

Fixes #2872
Michael Bromley 1 год назад
Родитель
Сommit
4ca45932dc

+ 5 - 0
packages/payments-plugin/e2e/mollie-payment.e2e-spec.ts

@@ -539,6 +539,11 @@ describe('Mollie payments', () => {
             expect(orderPlacedEvent?.ctx.languageCode).toBe('nl');
         });
 
+        it('Resulting events should have a ctx.req ', () => {
+            // We've set the languageCode to 'nl' in the mock response's metadata
+            expect(orderPlacedEvent?.ctx?.req).toBeDefined();
+        });
+
         it('Should have Mollie metadata on payment', async () => {
             const {
                 order: { payments },

+ 10 - 4
packages/payments-plugin/src/mollie/mollie.controller.ts

@@ -1,12 +1,16 @@
-import { Body, Controller, Param, Post } from '@nestjs/common';
+import { Body, Controller, Param, Post, Req } from '@nestjs/common';
 import { Ctx, Logger, RequestContext, Transaction, ChannelService, LanguageCode } from '@vendure/core';
+import { Request } from 'express';
 
 import { loggerCtx } from './constants';
 import { MollieService } from './mollie.service';
 
 @Controller('payments')
 export class MollieController {
-    constructor(private mollieService: MollieService, private channelService: ChannelService) {}
+    constructor(
+        private mollieService: MollieService,
+        private channelService: ChannelService,
+    ) {}
 
     @Post('mollie/:channelToken/:paymentMethodId')
     @Transaction()
@@ -14,6 +18,7 @@ export class MollieController {
         @Param('channelToken') channelToken: string,
         @Param('paymentMethodId') paymentMethodId: string,
         @Body() body: any,
+        @Req() req: Request,
     ): Promise<void> {
         if (!body.id) {
             return Logger.warn(' Ignoring incoming webhook, because it has no body.id.', loggerCtx);
@@ -21,7 +26,7 @@ export class MollieController {
         try {
             // We need to construct a RequestContext based on the channelToken,
             // because this is an incoming webhook, not a graphql request with a valid Ctx
-            const ctx = await this.createContext(channelToken);
+            const ctx = await this.createContext(channelToken, req);
             await this.mollieService.handleMollieStatusUpdate(ctx, {
                 paymentMethodId,
                 orderId: body.id,
@@ -36,13 +41,14 @@ export class MollieController {
         }
     }
 
-    private async createContext(channelToken: string): Promise<RequestContext> {
+    private async createContext(channelToken: string, req: Request): Promise<RequestContext> {
         const channel = await this.channelService.getChannelFromToken(channelToken);
         return new RequestContext({
             apiType: 'admin',
             isAuthorized: true,
             authorizedAsOwnerOnly: false,
             channel,
+            req,
             languageCode: LanguageCode.en,
         });
     }

+ 1 - 0
packages/payments-plugin/src/mollie/mollie.service.ts

@@ -282,6 +282,7 @@ export class MollieService {
                 apiType: 'admin',
                 isAuthorized: true,
                 authorizedAsOwnerOnly: false,
+                req: ctx.req,
                 channel: ctx.channel,
                 languageCode: mollieOrder.metadata.languageCode as LanguageCode,
             });