Browse Source

fix(payments-plugin): Fix state transitioning error case in Stripe webhook (#1485)

Sebastian Geschke 3 years ago
parent
commit
280d2e3e8a
1 changed files with 19 additions and 12 deletions
  1. 19 12
      packages/payments-plugin/src/stripe/stripe.controller.ts

+ 19 - 12
packages/payments-plugin/src/stripe/stripe.controller.ts

@@ -8,17 +8,17 @@ import {
     OrderService,
     PaymentMethod,
     RequestContext,
-    TransactionalConnection,
+    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';
 
+
 const missingHeaderErrorMessage = 'Missing stripe-signature header';
 const signatureErrorMessage = 'Error verifying Stripe webhook signature';
 const noPaymentIntentErrorMessage = 'No payment intent in the event payload';
@@ -77,18 +77,25 @@ export class StripeController {
 
         const ctx = await this.createContext(channelToken);
 
-        const transitionToStateResult = await this.orderService.transitionToState(
-            ctx,
-            orderId,
-            'ArrangingPayment',
-        );
+        const order = await this.orderService.findOneByCode(ctx, orderCode);
+        if (!order) {
+            throw Error(`Unable to find order ${orderCode}, unable to settle payment ${paymentIntent.id}!`);
+        }
 
-        if (transitionToStateResult instanceof OrderStateTransitionError) {
-            Logger.error(
-                `Error transitioning order ${orderCode} to ArrangingPayment state: ${transitionToStateResult.message}`,
-                loggerCtx,
+        if (order.state !== 'ArrangingPayment') {
+            const transitionToStateResult = await this.orderService.transitionToState(
+                ctx,
+                orderId,
+                'ArrangingPayment',
             );
-            return;
+
+            if (transitionToStateResult instanceof OrderStateTransitionError) {
+                Logger.error(
+                    `Error transitioning order ${orderCode} to ArrangingPayment state: ${transitionToStateResult.message}`,
+                    loggerCtx,
+                );
+                return;
+            }
         }
 
         const paymentMethod = await this.getPaymentMethod(ctx);