All files / app/payment mapToOrderRequestBody.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  3x   3x 3x     3x 8x     8x 1x     7x 7x     7x           7x 6x     7x    
import { OrderPaymentRequestBody, OrderRequestBody } from '@bigcommerce/checkout-sdk';
import { isEmpty, isNil, omitBy } from 'lodash';
 
import { unformatCreditCardExpiryDate, unformatCreditCardNumber } from './creditCard';
import { parseUniquePaymentMethodId } from './paymentMethod';
import { PaymentFormValues } from './PaymentForm';
 
export default function mapToOrderRequestBody(
    { paymentProviderRadio, ...values }: PaymentFormValues,
    isPaymentDataRequired: boolean
): OrderRequestBody {
    if (!isPaymentDataRequired) {
        return {};
    }
 
    const { methodId, gatewayId } = parseUniquePaymentMethodId(paymentProviderRadio);
    const payload: OrderRequestBody = {
        payment: { gatewayId, methodId },
    };
    const paymentData = omitBy({
        ...values,
        ccExpiry: 'ccExpiry' in values && values.ccExpiry ? unformatCreditCardExpiryDate(values.ccExpiry) : null,
        ccNumber: 'ccNumber' in values && values.ccNumber ? unformatCreditCardNumber(values.ccNumber) : null,
    }, isNil) as OrderPaymentRequestBody['paymentData'];
 
    if (payload.payment && !isEmpty(paymentData)) {
        payload.payment.paymentData = paymentData;
    }
 
    return payload;
}