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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 1x 1x 2x 1x 2x 1x 2x 1x 2x 2x 484x 1x 1x 1x 1x | import React, { useEffect } from "react";
import {
Box,
CircularProgress,
Collapse,
Grid,
Stack,
useMediaQuery,
} from "@mui/material";
import { CountryCode } from "libphonenumber-js";
// form
import { FormProvider } from "react-hook-form";
import { RHFInput, RHFTelInput } from "@common/Input";
import { RHFSelect } from "@common/Select";
import CardNumberInput from "./inputs/CardNumberInput";
import ExpirationInput from "./inputs/ExpirationInput";
import CvvInput from "./inputs/CvvInput";
import ZipCodeInput from "./inputs/ZipCodeInput";
import { getCountryCode, getCountryNames } from "@utils/country_dial_codes";
import ProvinceInput from "./inputs/ProvinceInput";
import { palette } from "@palette";
import { Text } from "@common/Text";
import NameOnCardInput from "./inputs/NameOnCardInput";
import { KeyboardArrowRight, KeyboardArrowUp, LockIcon } from "@assets/icons";
import { useCheckout } from "@hooks/merchant-api/cart";
import { KeyVal } from "@common/KeyVal";
import { CartDataType, useGetCart } from "@hooks/merchant-api/cart/useGetCart";
import { parseAmount } from "@utils/index";
import {
selectCardType,
selectIsPaymentRecurring,
} from "@redux/slices/checkout";
import { useAppSelector } from "@redux/hooks";
import { RHFCheckbox } from "@common/Checkbox";
import { useTheme } from "@mui/material";
import { LoginExternalLink } from "@components/Login";
import { useGetProductFeePayment } from "@hooks/merchant-api/cart/useGetProductFeePayment";
import { COMPANY_SIGNATURE, TERMS_OF_SERVICE_URL } from "@constants/constants";
import { useGetCardInfos } from "@hooks/merchant-api/cart/useGetCardInfos";
import useCheckEnvironment from "@hooks/common/useCheckEnvironment";
import { isEmpty } from "lodash";
import { useGlobalGroupIdempotencyKey } from "@services/api/useGlobalGroupIdempotencyKey";
type Props = {
setIsDisabled: React.Dispatch<React.SetStateAction<boolean>>;
destinationAccountMerchantName?: string;
closeCheckoutDrawer?(): void;
isMobileOpen?: boolean;
};
const Payment = ({
setIsDisabled,
destinationAccountMerchantName,
closeCheckoutDrawer,
isMobileOpen,
}: Props) => {
useGlobalGroupIdempotencyKey({ persist: true });
const { data: cart, refetch } = useGetCart();
const { passFees } = useGetProductFeePayment();
// Hook never returned these. Leave them unset so processingFee stays
// falsy (wire passFees: true) — do not derive from passFees in this PR.
const needsTax: boolean | undefined = undefined;
const optional: boolean | undefined = undefined;
const { handleCardNumberCheck, cardObject } = useGetCardInfos({
configPassFees: passFees,
});
const { methods, onSubmit, CVVLimit, isLoading, isDeclined } = useCheckout({
destinationAccountMerchantName,
closeCheckoutDrawer,
isDebit: cardObject?.binInfo?.cardType === "DEBIT",
});
const [isOpen, setIsOpen] = React.useState<boolean>(false);
const [cardFees, setCardFees] = React.useState<number>(0);
const [defaultCart, setDefaultCart] = React.useState<any>(cart);
const shouldAutomaticallyShownOptionalFields = React.useRef(true);
useEffect(() => {
Iif (isMobileOpen) {
refetch();
}
}, [isMobileOpen]);
const {
watch,
setValue,
setFocus,
formState: { errors, isValid },
} = methods;
const values = watch();
React.useEffect(() => {
setIsDisabled(!watch("termsConditions"));
}, [watch("termsConditions")]);
const checkoutCardType = useAppSelector(selectCardType);
const theme = useTheme();
const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
const isPaymentRecurring = useAppSelector(selectIsPaymentRecurring);
const cartItems = isEmpty(cart?.items) ? defaultCart?.items : cart?.items;
const cartFees = cart?.fees || defaultCart?.fees;
const totalAmount = (cartItems || []).reduce(
(sum: number, item: CartDataType) => sum + item.amount * item.quantity,
0,
);
React.useEffect(() => {
const isDisabled =
totalAmount + (cartFees || 0) === 0 ||
isDeclined ||
Object.keys(errors).length !== 0 ||
!values.termsConditions;
setIsDisabled(!isValid || isLoading || isDisabled);
}, [values, isLoading, cart, isDeclined]);
useEffect(() => {
Eif (cart?.id) {
setDefaultCart(cart);
}
}, [cart]);
const fees = React.useMemo(() => {
const feesValue = cardFees || cartFees;
return feesValue / 100 || 0;
}, [cartFees, cardFees]);
React.useEffect(() => {
setValue("processingFee", Boolean(needsTax && !optional));
}, [needsTax, optional]);
React.useEffect(() => {
Iif (values.expiration.length > 6) setFocus("cvv");
}, [values.expiration]);
React.useEffect(() => {
Iif (!isOpen && isValid && shouldAutomaticallyShownOptionalFields.current) {
setIsOpen(true);
shouldAutomaticallyShownOptionalFields.current = false;
}
}, [isOpen, isValid]);
const { platformBaseUrl } = useCheckEnvironment();
return (
<FormProvider {...methods}>
{isLoading && (
<Box sx={overlayStyles}>
<Stack alignItems="center">
<CircularProgress
size="57px"
sx={{
color: palette.accent[3],
}}
/>
<Text
fontSize="16px"
color={palette.neutral[100]}
sx={{ marginTop: 1 }}
>
Processing your payment...
</Text>
</Stack>
</Box>
)}
<Box
component="form"
id="credit-card-payment"
onSubmit={methods.handleSubmit(onSubmit)}
data-testid="credit-card-payment"
>
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
>
<Text fontSize="18px" color="#575353">
Card information
</Text>
<Stack direction="row" gap={1} alignItems="center">
<LockIcon />
<Text color="#8F8F8F">Secured by {COMPANY_SIGNATURE}</Text>
</Stack>
</Stack>
<Box sx={paymentCardStyle}>
<Grid container spacing={1}>
<Grid item xs={12} md={6}>
<CardNumberInput
setCardFees={setCardFees}
handleCardNumberCheck={handleCardNumberCheck}
cardObject={cardObject}
/>
</Grid>
<Grid item xs={6} md={3}>
<ExpirationInput />
</Grid>
<Grid item xs={6} md={3}>
<CvvInput CVVLimit={CVVLimit} />
</Grid>
{isDeclined && (
<Grid item xs={12}>
<Text
my={0.5}
fontWeight="book"
color={palette.tag.error.text}
lineHeight="16.8px"
>
Your transaction got declined. Try again later
</Text>
</Grid>
)}
<Grid item xs={12} md={6}>
<NameOnCardInput />
</Grid>
<Grid item xs={12} md={6}>
<RHFInput
name="email"
label="Email"
fullWidth
placeholder="yourname@email.com"
/>
</Grid>
</Grid>
</Box>
<Box
onClick={() => setIsOpen(!isOpen)}
sx={{
cursor: "pointer",
}}
>
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
mt="16px"
>
<Text fontSize="18px" color="#575353">
Additional details <span style={optionalStyle}>(optional)</span>
</Text>
{isOpen ? <KeyboardArrowUp /> : <KeyboardArrowRight />}
</Stack>
</Box>
<Collapse orientation="vertical" in={isOpen}>
<Grid container spacing={1} sx={{ marginBlock: "8px 32px" }}>
<Grid item xs={12} md={12}>
<RHFSelect
name="country"
label="Country"
options={getCountryNames().map((country) => ({
value: country,
label: country,
}))}
/>
</Grid>
<Grid item xs={12}>
<RHFInput
name="address"
label="Street address"
fullWidth
placeholder="Street address"
/>
</Grid>
<Grid item xs={12} md={4}>
<RHFInput name="city" label="City" fullWidth placeholder="City" />
</Grid>
<Grid item xs={12} md={4}>
<ProvinceInput isUS={values.country === "United States"} />
</Grid>
<Grid item xs={12} md={4}>
<ZipCodeInput
allowAnything={!(values.country === "United States")}
/>
</Grid>
<Grid item xs={12} md={6}>
<RHFInput
name="occupation"
label="Occupation"
fullWidth
placeholder="Occupation"
/>
</Grid>
<Grid item xs={12} md={6}>
<RHFInput
name="employer"
label="Employer"
fullWidth
placeholder="Employer"
/>
</Grid>
<Grid item xs={12} md={6}>
<RHFTelInput
label={`Phone Number`}
name="phone"
disableDropdown
defaultCountry={
(getCountryCode(values.country) as CountryCode | undefined) ||
"US"
}
fullWidth
/>
</Grid>
</Grid>
</Collapse>
</Box>
<Box sx={containerStyle}>
{/* SubTotal */}
<KeyVal
mobileView={true}
keyName={
<Text
variant="headline"
fontWeight="medium"
color={palette.neutral[600]}
>
Subtotal
</Text>
}
value={
<Text
variant="headline"
fontWeight="medium"
color={palette.neutral[600]}
data-testid="payment-subtotal"
>
{parseAmount(totalAmount)} USD
</Text>
}
sx={{ padding: "0 8px", alignItems: "center" }}
/>
{/* 2 */}
{needsTax && (
<Box
width="100%"
sx={{
display: "flex",
flexDirection: "column",
gap: "5px",
mb: 1,
}}
>
<KeyVal
mobileView={true}
keyName={
<Text
color={
values.processingFee
? palette.neutral.black
: palette.gray[100]
}
>
{checkoutCardType && checkoutCardType !== "Other"
? `${checkoutCardType} Card`
: "Processing"}{" "}
Fee
</Text>
}
value={
<Text
color={
values.processingFee
? palette.neutral.black
: palette.gray[100]
}
fontWeight="medium"
data-testid="payment-fees"
>
{parseAmount(fees)} USD
</Text>
}
sx={{ padding: "8px 8px" }}
/>
</Box>
)}
<Box
sx={{
width: "100%",
border: "1px solid #DDDCE5",
margin: "8px 0",
}}
/>
<KeyVal
mobileView={true}
keyName={
<Text
variant="h5"
fontWeight="medium"
color="#575353"
fontSize="18px"
sx={{ mt: "5px" }}
>
Amount to be charged
</Text>
}
value={
<Text
variant="h6"
fontWeight="medium"
color="#575353"
fontSize="18px"
textAlign="end"
data-testid="payment-amount-to-be-charged"
>
{parseAmount(
totalAmount + (cartFees && values.processingFee ? fees : 0),
)}{" "}
USD
</Text>
}
sx={{
padding: "0 8px",
alignItems: "baseline",
}}
/>
</Box>
<Stack pt={4} pb={isDesktop ? 4 : 0} gap={2}>
{optional && (
<RHFCheckbox
name="processingFee"
label="I'm covering the processing fee"
/>
)}
<Stack
direction={isDesktop ? "row" : "column"}
justifyContent={isDesktop ? "space-between" : "flex-start"}
alignItems={isDesktop ? "center" : "flex-start"}
>
<RHFCheckbox
name="termsConditions"
label="I accept the Terms & Conditions"
dataTestId="termsConditions-checkbox"
/>
<LoginExternalLink
underline={palette.neutral[600]}
href={`${platformBaseUrl}${TERMS_OF_SERVICE_URL}`}
>
<Text color={palette.neutral[600]}>
Read Give Terms & Conditions
</Text>
</LoginExternalLink>
</Stack>
{isPaymentRecurring && (
<Text color="#8F8F8F">
By confirming your payment, you allow Give to charge your card for
this payment and future payments in accordance with their terms. You
can always cancel your subscription.
</Text>
)}
</Stack>
</FormProvider>
);
};
const containerStyle = {
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
padding: "8px",
width: "100%",
background: "#FFF",
borderRadius: "8px",
marginTop: "32px",
boxShadow: "0px 2px 12px rgba(0, 0, 0, 0.05)",
};
const paymentCardStyle = {
background: "#FAFAFB",
borderRadius: "8px",
paddingTop: "8px",
};
const optionalStyle = {
color: palette.neutral[400],
fontWeight: 400,
};
const overlayStyles = {
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: "transparent",
display: "flex",
alignItems: "center",
justifyContent: "center",
zIndex: 9,
};
export default Payment;
|