Просмотр исходного кода

feat(payments-plugin): Mollie: support extra parameters for listing methods (#2516)

Closes #2510
Casper Iversen 2 лет назад
Родитель
Сommit
cb9846bcc0

+ 1 - 0
packages/payments-plugin/src/mollie/index.ts

@@ -1,2 +1,3 @@
 export * from './mollie.plugin';
+export { getLocale, toAmount } from './mollie.helpers';
 export * from './';

+ 53 - 1
packages/payments-plugin/src/mollie/mollie.plugin.ts

@@ -1,4 +1,12 @@
-import { PluginCommonModule, RuntimeVendureConfig, VendurePlugin } from '@vendure/core';
+import type { ListParameters } from '@mollie/api-client/dist/types/src/binders/methods/parameters';
+import {
+    Injector,
+    Order,
+    PluginCommonModule,
+    RequestContext,
+    RuntimeVendureConfig,
+    VendurePlugin,
+} from '@vendure/core';
 
 import { PLUGIN_INIT_OPTIONS } from './constants';
 import { shopSchema } from './mollie-shop-schema';
@@ -7,6 +15,8 @@ import { molliePaymentHandler } from './mollie.handler';
 import { MollieResolver } from './mollie.resolver';
 import { MollieService } from './mollie.service';
 
+export type AdditionalEnabledPaymentMethodsParams = Partial<Omit<ListParameters, 'resource'>>;
+
 /**
  * @description
  * Configuration options for the Mollie payments plugin.
@@ -33,6 +43,48 @@ export interface MolliePluginOptions {
      * @since 2.0.0
      */
     useDynamicRedirectUrl?: boolean;
+
+    /**
+     * @description
+     * Provide additional parameters to the Mollie enabled payment methods API call. By default,
+     * the plugin will already pass the `resource` parameter.
+     *
+     * For example, if you want to provide a `locale` and `billingCountry` for the API call, you can do so like this:
+     *
+     * **Note:** The `order` argument is possibly `null`, this could happen when you fetch the available payment methods
+     * before the order is created.
+     *
+     * @example
+     * ```ts
+     * import { VendureConfig } from '\@vendure/core';
+     * import { MolliePlugin, getLocale } from '\@vendure/payments-plugin/package/mollie';
+     *
+     * export const config: VendureConfig = {
+     *   // ...
+     *   plugins: [
+     *     MolliePlugin.init({
+     *       enabledPaymentMethodsParams: (injector, ctx, order) => {
+     *         const locale = order?.billingAddress?.countryCode
+     *             ? getLocale(order.billingAddress.countryCode, ctx.languageCode)
+     *             : undefined;
+     *
+     *         return {
+     *           locale,
+     *           billingCountry: order?.billingAddress?.countryCode,
+     *         },
+     *       }
+     *     }),
+     *   ],
+     * };
+     * ```
+     *
+     * @since 2.2.0
+     */
+    enabledPaymentMethodsParams?: (
+        injector: Injector,
+        ctx: RequestContext,
+        order: Order | null,
+    ) => AdditionalEnabledPaymentMethodsParams | Promise<AdditionalEnabledPaymentMethodsParams>;
 }
 
 /**

+ 17 - 1
packages/payments-plugin/src/mollie/mollie.service.ts

@@ -2,13 +2,16 @@ import createMollieClient, {
     Order as MollieOrder,
     OrderStatus,
     PaymentMethod as MollieClientMethod,
+    Locale,
 } from '@mollie/api-client';
 import { CreateParameters } from '@mollie/api-client/dist/types/src/binders/orders/parameters';
 import { Inject, Injectable } from '@nestjs/common';
+import { ModuleRef } from '@nestjs/core';
 import {
     ActiveOrderService,
     EntityHydrator,
     ErrorResult,
+    Injector,
     Logger,
     Order,
     OrderService,
@@ -28,6 +31,7 @@ import {
     MolliePaymentIntentInput,
     MolliePaymentIntentResult,
     MolliePaymentMethod,
+    MolliePaymentMethodsInput,
 } from './graphql/generated-shop-types';
 import { amountToCents, getLocale, toAmount, toMollieAddress, toMollieOrderLines } from './mollie.helpers';
 import { MolliePluginOptions } from './mollie.plugin';
@@ -59,6 +63,7 @@ export class MollieService {
         private orderService: OrderService,
         private entityHydrator: EntityHydrator,
         private variantService: ProductVariantService,
+        private moduleRef: ModuleRef,
     ) {}
 
     /**
@@ -334,9 +339,20 @@ export class MollieService {
         if (!apiKey) {
             throw Error(`No apiKey configured for payment method ${paymentMethodCode}`);
         }
+
         const client = createMollieClient({ apiKey });
+        const activeOrder = await this.activeOrderService.getActiveOrder(ctx, undefined);
+        const additionalParams = await this.options.enabledPaymentMethodsParams?.(
+            new Injector(this.moduleRef),
+            ctx,
+            activeOrder ?? null,
+        );
+
         // We use the orders API, so list available methods for that API usage
-        const methods = await client.methods.list({ resource: 'orders' });
+        const methods = await client.methods.list({
+            ...additionalParams,
+            resource: 'orders',
+        });
         return methods.map(m => ({
             ...m,
             code: m.id,