PaymentMethodHandler

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: [{
        languageCode: LanguageCode.en,
        value: 'Example Payment Provider',
    }],
    args: {
        apiKey: { type: '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,
                },
            };
        }
    },
    settlePayment: async (order, payment, args): Promise<SettlePaymentResult> {
        return { success: true };
    }
});

Signature

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

Members

constructor

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