Browse Source

fix(payments-plugin): improve order state transition handling for channel-specific contexts and default channel as a fallback (#3420)

Leftovers.today 5 months ago
parent
commit
4f29f0af93
1 changed files with 20 additions and 9 deletions
  1. 20 9
      packages/payments-plugin/src/stripe/stripe.controller.ts

+ 20 - 9
packages/payments-plugin/src/stripe/stripe.controller.ts

@@ -36,7 +36,7 @@ export class StripeController {
         private requestContextService: RequestContextService,
         private connection: TransactionalConnection,
         private channelService: ChannelService,
-    ) {}
+    ) { }
 
     @Post('stripe')
     async webhook(
@@ -111,18 +111,29 @@ export class StripeController {
                 // Orders can switch channels (e.g., global to UK store), causing lookups by the original
                 // channel to fail. Using a default channel avoids "entity-with-id-not-found" errors.
                 // See https://github.com/vendure-ecommerce/vendure/issues/3072
-                const defaultChannel = await this.channelService.getDefaultChannel(ctx);
-                const ctxWithDefaultChannel = await this.createContext(
-                    defaultChannel.token,
-                    languageCode,
-                    request,
-                );
-                const transitionToStateResult = await this.orderService.transitionToState(
-                    ctxWithDefaultChannel,
+
+                // First use the channel specific context to transition the order state, which is the default behavior
+                // prior to issue: https://github.com/vendure-ecommerce/vendure/issues/3072
+                let transitionToStateResult = await this.orderService.transitionToState(
+                    ctx,
                     orderId,
                     'ArrangingPayment',
                 );
 
+                // If the channel specific context fails, try to use the default channel context
+                // to transition the order state. Issue: https://github.com/vendure-ecommerce/vendure/issues/3072
+                if (transitionToStateResult instanceof OrderStateTransitionError) {
+                    const defaultChannel = await this.channelService.getDefaultChannel(ctx);
+                    const ctxWithDefaultChannel = await this.createContext(defaultChannel.token, languageCode, request);
+                    
+                    transitionToStateResult = await this.orderService.transitionToState(
+                        ctxWithDefaultChannel,
+                        orderId,
+                        'ArrangingPayment',
+                    );
+                }
+
+                // If the order is still not in the ArrangingPayment state, log an error
                 if (transitionToStateResult instanceof OrderStateTransitionError) {
                     Logger.error(
                         `Error transitioning order ${orderCode} to ArrangingPayment state: ${transitionToStateResult.message}`,