All files / src/sections/PayBuilder/Checkout/hooks useCheckoutProcess.tsx

84.21% Statements 16/19
40% Branches 2/5
60% Functions 3/5
84.21% Lines 16/19

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                                                    12x             384x 384x 384x   384x   384x 53x 53x                   384x         384x               384x 384x   384x       384x                                           384x                                 384x              
import { useEffect } from "react";
import { useAddToCart } from "@hooks/merchant-api/cart";
import { useGetCardInfos } from "@hooks/merchant-api/cart/useGetCardInfos";
import { PaymentSuccessInfo } from "@pages/Checkout/components/PaymentSuccess";
import { useCart } from "@sections/PayBuilder/provider/CartContext";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { customInstance } from "@services/api";
import { AxiosError } from "axios";
import { useMutation } from "react-query";
import { CheckoutResponse, ICheckoutInputs } from "../types";
import { FieldPath } from "react-hook-form";
import { useCartLoading } from "./useCartLoading";
import { useHandleCheckoutError } from "./useHandleCheckoutError";
import { useCheckoutSuccessHandler } from "./useCheckoutSuccessHandler";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
 
import { useCheckoutHandler } from "./useCheckoutHandler";
 
type UseCheckoutProcessParam = {
  onPaymentSuccess?: (data: PaymentSuccessInfo) => void;
  onPaymentDeclined?: (mspResponseCode?: string) => void;
  isCustomerPayingFees?: boolean;
  setFieldError?: (key: FieldPath<ICheckoutInputs>, message?: string) => void;
  onPaymentFailed?: () => void;
};
 
const useCheckoutProcess = ({
  onPaymentSuccess,
  onPaymentDeclined,
  isCustomerPayingFees,
  setFieldError,
  onPaymentFailed,
}: UseCheckoutProcessParam = {}) => {
  const { cartItems } = useCart();
  const { methods, data: payBuilderData } = usePayBuilderForm();
  const isDeliveryEnabled = methods.watch().Checkout.delivery.render;
 
  const { isLoading, setLoading } = useCartLoading();
 
  useEffect(() => {
    return () => {
      setLoading(false);
    };
  }, [setLoading]);
 
  const {
    addToCartHandler,
    product,
    orderID,
    isCustomerChoicePayer,
    configurationPassFees,
  } = useAddToCart({
    newCartItems: cartItems,
    showModal: false,
  });
 
  const { handleSuccess } = useCheckoutSuccessHandler({
    onPaymentSuccess,
    onPaymentDeclined,
    onPaymentFailed,
    isDeliveryEnabled,
    addToCartHandler,
    setLoading,
  });
  const { handleError } = useHandleCheckoutError();
  const { isFeatureFlagLoading } = useGetFeatureFlagValues();
 
  const { handleCardNumberCheck } = useGetCardInfos({
    configPassFees: !isCustomerPayingFees,
  });
 
  const cartCheckoutMutation = useMutation<
    CheckoutResponse,
    AxiosError,
    Record<string, unknown>
  >(
    async (query: Record<string, unknown>) => {
      const response = await customInstance({
        url: `/cart/checkout `,
        method: "POST",
        data: query,
      });
 
      return (response?.data ?? response) as CheckoutResponse;
    },
    {
      mutationKey: "checkout-mutation",
      onError: (error) => {
        handleError({ error, setFieldError, setLoading });
      },
    },
  );
 
  const { handleCheckout } = useCheckoutHandler({
    product,
    orderID,
    isCustomerChoicePayer,
    configurationPassFees,
    handleCardNumberCheck,
    addToCartHandler,
    cartCheckoutMutation,
    handleSuccess,
    cartItems,
    payBuilderData,
    isDeliveryEnabled,
    setLoading,
    setFieldError,
    onPaymentFailed,
  });
 
  return {
    handleCheckout,
    isLoading: isLoading || isFeatureFlagLoading,
  };
};
 
export default useCheckoutProcess;