|
|
@@ -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,
|
|
|
});
|
|
|
}
|