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.

Example

// A mock 3rd-party payment SDK
import gripeSDK from 'gripe';
 * export const examplePaymentHandler = new PaymentMethodHandler({
    code: 'example-payment-provider',
    description: 'Example Payment Provider',
    args: {
        apiKey: 'string',
    },
    createPayment: async (order, args, metadata): Promise<PaymentConfig> => {
        try {
            const result = await gripeSDK.charges.create({
                apiKey: args.apiKey,
                amount: order.total,
                source: metadata.authToken,
            });
            return {
                amount: order.total,
                state: 'Settled' as 'Settled',
                transactionId: result.id.toString(),
                metadata: result.outcome,
            };
        } catch (err) {
            return {
                amount: order.total,
                state: 'Declined' as 'Declined',
                metadata: {
                    errorMessage: err.message,
                },
            };
        }
    },
});

Signature

class PaymentMethodHandler<T extends PaymentMethodArgs = PaymentMethodArgs> implements ConfigurableOperationDef {
  readonly code: string;
  readonly description: string;
  readonly args: T;
  constructor(config: PaymentMethodConfigOptions<T>)
  createPayment(order: Order, args: ConfigArg[], metadata: PaymentMetadata) => ;
  onStateTransitionStart(fromState: PaymentState, toState: PaymentState, args: ConfigArg[], data: PaymentTransitionData) => OnTransitionStartReturnType;
}

Members

code

property
type:
string

description

property
type:
string

args

property
type:
T

constructor

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

createPayment

method
type:
(order: Order, args: ConfigArg[], metadata: PaymentMetadata) =>

Called internally to create a new Payment

onStateTransitionStart

method
type:
(fromState: PaymentState, toState: PaymentState, args: ConfigArg[], data: PaymentTransitionData) => OnTransitionStartReturnType

This function is called before the state of a Payment is transitioned. If the PaymentMethodHandler was instantiated with a onStateTransitionStart function, that function will be invoked and its return value used to determine whether the transition can occur.

alpha