All files / src/hooks/merchant-api/cart useGetCardInfos.tsx

44.11% Statements 15/34
15.78% Branches 3/19
57.14% Functions 4/7
45.45% Lines 15/33

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                      13x                                 13x             2x                   13x         494x 494x 494x                                                                     494x     13x                   13x                 113x     2x           2x   2x         113x    
import { DEFAULT_QUERY_CONFIG } from "@components/VirtualList/hooks/queries";
import { QKEY_GET_CUSTOMER_FEES } from "@constants/queryKeys";
import { customInstance } from "@services/api";
import { getCardType } from "@utils/index";
import { CARD_LENGTHS } from "componentsV2/Payment/components/CardNumberField";
import { useState } from "react";
import { useQuery } from "react-query";
import { parseError } from "@utils/errorHandling";
import { showMessage } from "@common/Toast";
import { useOpenProcessingIssueModal } from "@sections/PayBuilder/Checkout/hooks/useOpenProcessingIssueModal";
 
export const getCardNumber = ({
  cardNumber,
  passFees,
}: {
  cardNumber: string;
  passFees: boolean;
}) => {
  return customInstance({
    method: "POST",
    url: "/cart/payment-method",
    data: {
      number: cardNumber.replace(/\s/g, ""),
      passFees,
    },
  });
};
 
export const getFeeInfo = ({
  passFees,
  cardNumber,
}: {
  passFees: boolean;
  cardNumber: string;
}) => {
  return customInstance({
    method: "POST",
    url: "/cart/pass-fees",
    data: {
      passFees,
      cardNumber,
    },
  });
};
 
export const useGetCardInfos = ({
  configPassFees = true,
}: {
  configPassFees?: boolean;
} = {}) => {
  const [cardObject, setCardObject] = useState<Record<any, any>>();
  const { handleOpenProcessingIssue } = useOpenProcessingIssueModal();
  const handleCardNumberCheck = async (
    cardNumber: string,
    handleInvalid?: (message: string) => void,
    passFees = configPassFees,
  ) => {
    try {
      const cardObj = await getCardNumber({
        cardNumber,
        passFees,
      });
 
      setCardObject(cardObj);
      return cardObj;
    } catch (err: unknown) {
      // FIXED: Use centralized error parsing
      const parsed = parseError(err);
 
      // Special case: order not found
      if (parsed.message === "The requested order was not found.") {
        handleOpenProcessingIssue(true);
        return;
      }
 
      const errorMessage =
        parsed.userMessage || "Error occurred while validating card number";
 
      // Call custom invalid handler if provided
      if (handleInvalid) {
        handleInvalid(errorMessage);
      } else {
        // FIXED: Show error to user if no custom handler provided
        showMessage("Error", errorMessage);
      }
    }
  };
  return { handleCardNumberCheck, cardObject };
};
 
const isValidCard = (cardNumber: string) => {
  if (!cardNumber) {
    return false;
  }
  const cardType = getCardType(cardNumber.replace(" ", ""));
  const validLength =
    cardType === "AMEX" ? CARD_LENGTHS.AMEX : CARD_LENGTHS.OTHERS;
  return cardNumber.length === validLength;
};
 
export const useGetCustomerFees = ({
  passFees,
  cardNumber,
  enabled = true,
}: {
  passFees: boolean;
  cardNumber: string;
  enabled?: boolean;
}) => {
  const { data: customerFees = 0 } = useQuery(
    [QKEY_GET_CUSTOMER_FEES],
    async () => {
      const feeObj = await getFeeInfo({
        passFees: false,
        cardNumber,
      });
 
      //Set pass fees back to correct value if true
      Iif (passFees) await getFeeInfo({ passFees, cardNumber });
 
      return feeObj?.fees ?? 0;
    },
    { ...DEFAULT_QUERY_CONFIG, enabled },
  );
 
  return { customerFees };
};