All files / src/sections/PayBuilder/Checkout helpers.tsx

97.87% Statements 46/47
91.89% Branches 34/37
100% Functions 10/10
100% Lines 42/42

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                149x 4x   149x 111x   111x 111x     149x 23x 23x 23x   21x     21x           21x   21x     149x 15x 14x 14x   14x 7x 7x         149x 42x         38x 4x 4x         4x 4x     149x         392x           149x                                           149x         4x 3x 3x   1x 1x       149x                     16x                       33x 234x       33x    
import { showMessage } from "@common/Toast";
import { MAX_ALLOWED_AMOUNT } from "@constants/constants";
import { parseNumber } from "@sections/PayBuilder/helpers";
import { getCardType } from "@utils/index";
import { CARD_LENGTHS } from "componentsV2/Payment/components/CardNumberField";
import { CHECKOUT_ERROR_MESSAGES } from "../constants";
import { getCountryCode } from "@utils/country_dial_codes";
 
export const isAmex = (cardNumber: string) =>
  cardNumber.startsWith("37") || cardNumber.startsWith("34");
 
export const isValidCardNumber = (cardNumber: string) => {
  const cardType = getCardType(cardNumber.replace(" ", ""));
  const validLength =
    cardType === "AMEX" ? CARD_LENGTHS.AMEX : CARD_LENGTHS.OTHERS;
  return cardNumber.length === validLength;
};
 
export const isNonValidExpirationDate = (expiration: string) => {
  const month = expiration.substring(0, 2);
  const year = expiration.substring(5, 7);
  if (month.length < 2 || year.length < 2) return true;
 
  const today = new Date();
 
  //Credit cards are usually valid until the last day of the specified month
  const expiryDate = new Date(
    parseInt(today.getFullYear().toString().substring(0, 2) + year),
    +month,
    0, // Day 0 of the next month gives the last day of the current month
  );
 
  Iif (expiryDate < today) return true;
 
  return false;
};
 
export const normalizeInput = (value: string) => {
  if (!value) return value;
  const currentValue = value.replace(/[^\d]/g, "");
  const cvLength = currentValue.length;
 
  if (cvLength < 3) return currentValue;
  Eif (cvLength < 5)
    return `${
      parseInt(currentValue.slice(0, 2)) > 12 ? 12 : currentValue.slice(0, 2)
    } / ${currentValue.slice(2, 5)}`;
};
 
export const getRecurringText = (cartItems: any[]): string => {
  if (
    !cartItems ||
    cartItems.length === 0 ||
    cartItems[0].recurringIntervalName === "once"
  )
    return "";
  const intervalName = cartItems[0]?.recurringIntervalName?.toLowerCase();
  const mapIntervalToDisplay: { [key: string]: string } = {
    monthly: "monthly",
    yearly: "yearly",
    quarterly: "quarterly",
  };
  const displayInterval = mapIntervalToDisplay?.[intervalName];
  return displayInterval ? displayInterval : "";
};
 
export const isExceededAmount = (
  subTotal: string,
  amount: string | null,
  quantity?: number,
) => {
  return (
    parseNumber(subTotal) + parseNumber(amount || "0") * (quantity || 1) >
    MAX_ALLOWED_AMOUNT
  );
};
 
export const checkoutScrollbarStyles = {
  scrollbarWidth: "auto",
  "&::-webkit-scrollbar": {
    width: "10px",
    backgroundColor: "transparent",
  },
  "&::-webkit-scrollbar-track": {
    backgroundColor: "transparent",
    borderRadius: "20px !important",
  },
  "&::-webkit-scrollbar-thumb": {
    backgroundColor: "#80807e",
    borderRadius: "10px",
    border: "2px solid transparent",
    backgroundClip: "content-box",
    "&:hover": {
      backgroundColor: "#80807e",
    },
  },
  scrollbarColor: "auto",
} as const;
 
export const invokeSafely = (
  callback?: () => void,
  description?: string,
  isDesktopView?: boolean,
) => {
  if (!callback) return;
  try {
    callback();
  } catch (error) {
    console.error(`Error while executing ${description || "callback"}:`, error);
    showMessage("Error", CHECKOUT_ERROR_MESSAGES.GENERIC_ERROR, isDesktopView);
  }
};
 
export const formatAddress = ({
  firstName,
  lastName,
  address,
  apartment,
  city,
  province,
  country,
  zipCode,
}: {
  [key: string]: string;
}) => ({
  name: `${firstName} ${lastName}`,
  line1: address,
  line2: apartment,
  city,
  state: province,
  country: getCountryCode(country),
  zip: zipCode,
});
 
export function validateBillingAddress(address: any) {
  // Count the number of non-empty fields
  const nonEmptyFields = Object.values(address).filter(
    (field: any) => field && field.trim() !== "",
  );
 
  // Return the address if at least 2 fields are non-empty, otherwise null
  return nonEmptyFields.length >= 2 ? address : null;
}