payments/src/lib/status_matching.ts

import { PaymentStatus } from './types';

const EXTERNAL_STATUS_MAPPING = {
  modern_treasury: {
    processing: PaymentStatus.PROCESSING,
    sent: PaymentStatus.PROCESSING,
    completed: PaymentStatus.COMPLETED,
    failed: PaymentStatus.FAILED,
    denied: PaymentStatus.DENIED,
    cancelled: PaymentStatus.CANCELLED,
    returned: PaymentStatus.REJECTED,
    reversed: PaymentStatus.REJECTED,
  },
  gocardless: {
    submitted: PaymentStatus.SUBMITTED,
    confirmed: PaymentStatus.PROCESSING,
    paid_out: PaymentStatus.COMPLETED,
    failed: PaymentStatus.FAILED,
    cancelled: PaymentStatus.CANCELLED,
    charged_back: PaymentStatus.CHARGED_BACK,
    customer_approval_denied: PaymentStatus.REJECTED,
  },
};

/**
 * Given an external service and its status, match a PS payment status.
 *
 * @param {PaymentStatus} previous
 * @param {string} service
 * @param {string} status
 * @return {PaymentStatus}
 *
 * @function
 * @memberOf module:payments
 * @access public
 */
const matchExternalStatus = (
  previous: PaymentStatus,
  service: string,
  status: string
): PaymentStatus => EXTERNAL_STATUS_MAPPING[service]?.[status] || previous;

export { matchExternalStatus, EXTERNAL_STATUS_MAPPING };