Skip to content

Commit

Permalink
feat(payments-plugin): Mollie: support extra parameters for listing m…
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored and alexisvigoureux committed Dec 4, 2023
1 parent 11f45e2 commit cba1b2d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/payments-plugin/src/mollie/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './mollie.plugin';
export { getLocale, toAmount } from './mollie.helpers';
export * from './';
54 changes: 53 additions & 1 deletion packages/payments-plugin/src/mollie/mollie.plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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.
Expand All @@ -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>;
}

/**
Expand Down
18 changes: 17 additions & 1 deletion packages/payments-plugin/src/mollie/mollie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -59,6 +63,7 @@ export class MollieService {
private orderService: OrderService,
private entityHydrator: EntityHydrator,
private variantService: ProductVariantService,
private moduleRef: ModuleRef,
) {}

/**
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit cba1b2d

Please sign in to comment.