瀏覽代碼

feat(payments-plugin): wip

Martijn 1 年之前
父節點
當前提交
fb4cacef21

+ 4 - 0
packages/payments-plugin/src/mollie/mollie.plugin.ts

@@ -214,6 +214,10 @@ export interface MolliePluginOptions {
     providers: [MollieService, { provide: PLUGIN_INIT_OPTIONS, useFactory: () => MolliePlugin.options }],
     configuration: (config: RuntimeVendureConfig) => {
         config.paymentOptions.paymentMethodHandlers.push(molliePaymentHandler);
+        // config.customFields.Order.push({
+        //     name: 'activeMollieOrderId',
+        //     type: 'string',
+        // })
         return config;
     },
     shopApiExtensions: {

+ 12 - 3
packages/payments-plugin/src/mollie/mollie.service.ts

@@ -9,6 +9,7 @@ import { Inject, Injectable } from '@nestjs/common';
 import { ModuleRef } from '@nestjs/core';
 import {
     ActiveOrderService,
+    assertFound,
     EntityHydrator,
     ErrorResult,
     Injector,
@@ -24,7 +25,7 @@ import {
     ProductVariantService,
     RequestContext,
 } from '@vendure/core';
-import { OrderStateMachine } from '@vendure/core/dist/service/helpers/order-state-machine/order-state-machine';
+import { OrderStateMachine } from '@vendure/core/';
 import { totalCoveredByPayments } from '@vendure/core/dist/service/helpers/utils/order-utils';
 
 import { loggerCtx, PLUGIN_INIT_OPTIONS } from './constants';
@@ -145,9 +146,10 @@ export class MollieService {
             }
             redirectUrl = paymentMethodRedirectUrl;
         }
+        // FIXME: Do we still need to manually do all the above checks like has-customer etc?
         if (order.state !== 'ArrangingPayment' && order.state !== 'ArrangingAdditionalPayment') {
-            // TODO get order state machine and check if transitionable to ArrangingPayment
-            // const orderStateMachine = this.injector.get(OrderStateMachine);
+            // Check if order is transitionable to ArrangingPayment, because that will happen after Mollie payment
+            await this.canTransitionTo(ctx, order, 'ArrangingPayment');
         }
         const variantsWithInsufficientSaleableStock = await this.getVariantsWithInsufficientStock(ctx, order);
         if (variantsWithInsufficientSaleableStock.length) {
@@ -405,6 +407,13 @@ export class MollieService {
         return variantsWithInsufficientSaleableStock;
     }
 
+    private async canTransitionTo(ctx: RequestContext, order: Order, state: OrderState) {
+        // Fetch new order object, because `transition()` mutates the order object
+        const orderCopy = await assertFound(this.orderService.findOne(ctx, order.id));
+        const orderStateMachine = this.injector.get(OrderStateMachine);
+        await orderStateMachine.transition(ctx, orderCopy, state);
+    }
+
     private async getPaymentMethod(
         ctx: RequestContext,
         paymentMethodCode: string,