All files / src/pages/MerchantCheckout/hooks usePayNow.ts

90.47% Statements 19/21
80% Branches 8/10
75% Functions 3/4
90.47% Lines 19/21

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86            1x       109x 109x   109x 1x             109x 3x     3x 3x 3x 3x 3x                                                                             1x 1x     2x 2x 1x   1x         109x        
import { TypeFormInputs } from "@pages/MerchantCheckout/types";
import useGetCheckoutInfo from "@pages/MerchantCheckout/hooks/useGetCheckoutInfo";
import { gatewayPaymentMutation } from "@services/api/checkout/gateway";
import NiceModal from "@ebay/nice-modal-react";
import { PROCESSING_ISSUE_POPUP } from "modals/modal_names";
 
const usePayNow = (
  onSuccess: (response?: any) => void,
  onFailed: (error?: any) => void,
) => {
  const checkoutInfo = useGetCheckoutInfo();
  const { mutateAsync, isLoading } = gatewayPaymentMutation();
 
  const showTryAgainModal = () => {
    NiceModal.show(PROCESSING_ISSUE_POPUP, {
      handleTryAgain: () => {
        window.location.reload();
      },
    });
  };
 
  const handlePayNow = async (value: TypeFormInputs) => {
    Iif (!checkoutInfo.environment || !checkoutInfo?.netId)
      return showTryAgainModal();
 
    const parts = value.payment.expirationDate.split(" / ");
    const month = parts[0];
    const year = parts[1];
    try {
      const response = await mutateAsync({
        environment: checkoutInfo.environment,
        payment: {
          amount: Number(checkoutInfo.amount) * 100, //Send amount in cents
          paymethod: {
            card: {
              name: value.payment.nameOnCard,
              number: value.payment.cardNumber,
              cvv: value.payment.cvv,
              exp_year: Number(`20${year}`),
              exp_month: Number(month),
            },
            billing_address: {
              line1: checkoutInfo.line1,
              line2: checkoutInfo.line2,
              city: checkoutInfo.city,
              state: checkoutInfo.state,
              zip: checkoutInfo.zip,
              country: checkoutInfo.country,
            },
          },
          customer: {
            email: checkoutInfo.customerEmail,
            phone: checkoutInfo.phone.replace("+", ""),
            first_name: checkoutInfo.firstName,
            last_name: checkoutInfo.lastName,
            company_name: checkoutInfo.companyName,
          },
          external_reference: `${checkoutInfo.orderId}`,
          fee_payer: "merchant",
          send_receipt: false,
        },
        headers: {
          Authorization: `Bearer ${checkoutInfo.key}`,
          "GP-Requestor-UA": checkoutInfo.requestorUa || navigator.userAgent,
          "GP-Requestor-IP": checkoutInfo.netId,
        },
      });
 
      Eif (response.status === 201) {
        onSuccess(response);
      }
    } catch (e: any) {
      const errorData = e?.response?.data;
      if (errorData?.code === "payment_issue") {
        onFailed(errorData);
      } else {
        showTryAgainModal();
      }
    }
  };
 
  return { handlePayNow, isProcessing: isLoading };
};
 
export default usePayNow;