All files / src/pages/MerchantCheckout MerchantCheckout.tsx

95% Statements 19/20
90% Branches 9/10
100% Functions 6/6
100% Lines 16/16

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                      1x 7x 6x 6x     6x 6x   1x 1x   1x   1x                 6x                                                 6x             6x                           1x 13x 6x            
import { Box, Stack } from "@mui/material";
import { styled } from "@theme/v2/Provider";
import MerchantCheckoutTitle from "@pages/MerchantCheckout/components/MerchantCheckoutTitle";
import CheckoutPending from "@pages/MerchantCheckout/CheckoutPending/CheckoutPending";
import CheckoutComplete from "@pages/MerchantCheckout/CheckoutComplete/CheckoutComplete";
import useMerchantCheckout from "@pages/MerchantCheckout/hooks/useMerchantCheckout";
import PrivacyPolicyFooter from "@pages/MerchantCheckout/components/PrivacyPolicyFooter";
import { PaymentErrorBoundary } from "@components/ErrorBoundary";
import { useEffect, useRef } from "react";
import useGetCheckoutInfo from "@pages/MerchantCheckout/hooks/useGetCheckoutInfo";
 
const MerchantCheckout = () => {
  const { handlePaymentCompleted, isPaymentComplete } = useMerchantCheckout();
  const checkoutInfo = useGetCheckoutInfo();
  const postedRef = useRef(false);
  // When the payment becomes complete, notify the parent (WooCommerce) so it can close the modal
  // and navigate to the order confirmation page using the provided redirect URL.
  useEffect(() => {
    if (!isPaymentComplete || postedRef.current) return;
 
    const redirectUrl = checkoutInfo.redirectUrl;
    Iif (!redirectUrl) return;
 
    postedRef.current = true;
 
    window.parent?.postMessage(
      {
        type: "givepayments:payment_complete",
        redirect_url: redirectUrl,
      },
      "*",
    );
  }, [isPaymentComplete, checkoutInfo?.redirectUrl]);
 
  return (
    <PaymentErrorBoundary
      isCritical
      errorContext={{
        isMerchantCheckout: true,
        isPaymentComplete,
      }}
    >
      <StyledRoot>
        <StyledBody>
          <MerchantCheckoutTitle />
          <CheckoutMidSectionContainer isFullScreen={isPaymentComplete}>
            {isPaymentComplete ? (
              <CheckoutComplete />
            ) : (
              <CheckoutPending onPaymentSuccess={handlePaymentCompleted} />
            )}
          </CheckoutMidSectionContainer>
          <PrivacyPolicyFooter />
        </StyledBody>
      </StyledRoot>
    </PaymentErrorBoundary>
  );
};
 
const StyledRoot = styled(Box)(({ theme }) => ({
  width: "100dvw",
  height: "100dvh",
  overflow: "auto",
  backgroundColor: theme.palette.surface?.primary,
}));
 
const StyledBody = styled(Stack)(({ theme }) => ({
  height: "100%",
  width: "100%",
  maxWidth: "600px",
  margin: "auto",
  alignItems: "center",
  gap: theme.spacing(2),
  paddingTop: theme.spacing(7),
  [theme.breakpoints.down("v2_sm")]: {
    paddingTop: `${theme.spacing(5.5)}`,
    paddingInline: theme.spacing(3),
  },
}));
 
const CheckoutMidSectionContainer = styled(Stack, {
  shouldForwardProp: (prop) => prop !== "isFullScreen",
})<{ isFullScreen: boolean }>(({ isFullScreen }) => ({
  height: isFullScreen ? "100%" : "fit-content",
  width: "100%",
}));
 
export default MerchantCheckout;