Skip to content

Commit

Permalink
fix(payments-plugin): Events triggered by Mollie webhook include request
Browse files Browse the repository at this point in the history
Fixes #2872
  • Loading branch information
michaelbromley committed May 31, 2024
1 parent b325a83 commit 4ca4593
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/payments-plugin/e2e/mollie-payment.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
14 changes: 10 additions & 4 deletions packages/payments-plugin/src/mollie/mollie.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
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()
async webhook(
@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);
}
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,
Expand All @@ -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,
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/payments-plugin/src/mollie/mollie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 4ca4593

Please sign in to comment.