PaymentMethodHandler

PaymentMethodHandler

A PaymentMethodHandler contains the code which is used to generate a Payment when a call to the addPaymentToOrder mutation is made. If contains any necessary steps of interfacing with a third-party payment gateway before the Payment is created and can also define actions to fire when the state of the payment is changed.

PaymentMethodHandlers are instantiated using a PaymentMethodConfigOptions object, which configures the business logic used to create, settle and refund payments.

Example

import { PaymentMethodHandler, CreatePaymentResult, SettlePaymentResult, LanguageCode } from '@vendure/core';
// A mock 3rd-party payment SDK
import gripeSDK from 'gripe';

export const examplePaymentHandler = new PaymentMethodHandler({
  code: 'example-payment-provider',
  description: [{
    languageCode: LanguageCode.en,
    value: 'Example Payment Provider',
  }],
  args: {
    apiKey: { type: 'string' },
  },
  createPayment: async (ctx, order, amount, args, metadata): Promise<CreatePaymentResult> => {
    try {
      const result = await gripeSDK.charges.create({
        amount,
        apiKey: args.apiKey,
        source: metadata.authToken,
      });
      return {
        amount: order.total,
        state: 'Settled' as const,
        transactionId: result.id.toString(),
        metadata: result.outcome,
      };
    } catch (err) {
      return {
        amount: order.total,
        state: 'Declined' as const,
        metadata: {
          errorMessage: err.message,
        },
      };
    }
  },
  settlePayment: async (ctx, order, payment, args): Promise<SettlePaymentResult> => {
    return { success: true };
  }
});

Signature

class PaymentMethodHandler<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<T> {
  constructor(config: PaymentMethodConfigOptions<T>)
}

Extends

Members

constructor

method
type:
(config: PaymentMethodConfigOptions<T>) => PaymentMethodHandler