@godaddy/react
Version:
The `createCheckoutSession` function creates a new checkout session with GoDaddy's commerce API.
208 lines (202 loc) • 7.15 kB
JavaScript
import { $ as PaymentMethodType, B as getApplicationId, H as useCanOfferShippingAddressAsBilling, R as useLoadPoyntCollect, St as CheckoutSection, U as BillingCollectionLocations, V as useBillingPolicy, W as BillingCollectionModes, b as useConfirmCheckout, nt as CheckoutSectionHeader, o as useCheckoutContext, p as usePoyntACHCollect, rt as AddressForm, v as PaymentProvider, z as PaymentAddressToggle } from "./checkout-CYyQY2Wk.js";
import { D as GraphQLErrorWithCodes } from "./godaddy-diAn6flX.js";
import { d as useGoDaddyContext } from "./utils-7H42wSPT.js";
import "./utils-CL8rvaTY.js";
import { useId, useLayoutEffect, useRef, useState } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useFormContext } from "react-hook-form";
//#region src/components/checkout/payment/payment-methods/ach/godaddy.tsx
function GoDaddyACHForm() {
const { t } = useGoDaddyContext();
const { session } = useCheckoutContext();
const { setCollect, setIsLoadingNonce } = usePoyntACHCollect();
const { isPoyntLoaded } = useLoadPoyntCollect();
const { godaddyPaymentsConfig, setCheckoutErrors } = useCheckoutContext();
const [error, setError] = useState("");
const paymentMethod = useFormContext().watch("paymentMethod");
const billingPolicy = useBillingPolicy();
const canOfferShippingAddressAsBilling = useCanOfferShippingAddressAsBilling();
const shouldShowBilling = billingPolicy.location === BillingCollectionLocations.INLINE_PAYMENT_FORM && paymentMethod === PaymentMethodType.ACH && billingPolicy.mode !== BillingCollectionModes.NONE;
const billingCopy = billingPolicy.mode === BillingCollectionModes.NAMES && t.payment.billingInformation ? t.payment.billingInformation : t.payment.billingAddress;
const description = t.payment.descriptions?.ach;
const confirmCheckout = useConfirmCheckout();
const elementId = `gdpay-ach-element-${useId()}`;
const applicationId = getApplicationId(session, godaddyPaymentsConfig?.appId);
const businessId = godaddyPaymentsConfig?.businessId || session?.businessId;
const fontFamily = "\"GD Sherpa\", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\"";
const baseInputStyle = `
display: flex;
height: 48px;
width: 100%;
border-radius: 0.4375rem;
border: 1px solid oklch(0.9 0.025 245);
background: oklch(1 0 0);
padding-left: 12px;
padding-right: 12px;
padding-top: 8px;
padding-bottom: 8px;
font-size: 14px;
line-height: 1.5;
color: oklch(0.13 0 0);
font-family: ${fontFamily};
transition: color 0.2s, background 0.2s, border-color 0.2s;
&::placeholder {
color: oklch(0.556 0 0);
}
&:focus-visible {
outline: none;
border-color: oklch(0.57 0.22 255);
box-shadow: 0px 0px 0px 2px oklch(0.57 0.22 255) inset;
}
&:disabled {
cursor: not-allowed;
opacity: 0.5;
}
`;
const options = {
iFrame: {
width: "100%",
height: "360px",
border: "0px"
},
paymentMethods: ["ach"],
displayComponents: { labels: true },
customCss: {
container: `
--collect-radio-size: 16px;
--collect-radio-dot-size: 7px;
--collect-radio-checked-bg-color: oklch(0.8 0.17 185);
--collect-radio-border-color: oklch(0.9 0.025 245);
--collect-radio-dot-color: oklch(0.13 0 0);
--collect-radio-bg-color: oklch(1 0 0);
--collect-border-radius-round: 50%;
--collect-spacing-lg: 10px;
--collect-spacing-md: 8px;
font-family: ${fontFamily};
`,
rowAccountHolderName: `
flex: 1 1 100%;
padding: 0;
margin: 0;
margin-bottom: 16px;
`,
rowAccountHolderType: `
flex: 1 1 100%;
padding: 0;
margin: 0;
margin-bottom: 16px;
`,
rowRoutingNumber: `
flex: 1 1 100%;
padding: 0;
margin: 0;
`,
rowBankAccountNumber: `
flex: 1 1 100%;
padding: 0;
margin: 0;
padding-top: 16px;
`,
radio: { accountHolderType: {
label: `
font-size: 14px;
font-weight: 500;
line-height: 1.5;
color: oklch(0.13 0 0);
cursor: pointer;`,
container: `
display: inline-flex;
margin-right: 24px;
margin-bottom: 0px;
.poynt-collect-payment-radio-input:checked + .poynt-collect-payment-radio-label::after {
left: 5.5px;
top: 9px;
}
`
} },
input: {
ownerName: baseInputStyle,
routingNumber: baseInputStyle,
accountNumber: baseInputStyle
}
}
};
const collect = useRef(null);
const optionsRef = useRef(options);
useLayoutEffect(() => {
if (!isPoyntLoaded || collect.current || !businessId) return;
collect.current = new window.TokenizeJs({
businessId,
storeId: session?.storeId,
channelId: session?.channelId,
applicationId
});
collect?.current?.on("ready", () => {
setCollect(collect.current);
});
collect?.current?.mount(elementId, document, optionsRef.current);
collect?.current?.on("nonce", async (event) => {
const nonce = event?.data?.nonce;
if (nonce) try {
await confirmCheckout.mutateAsync({
paymentToken: nonce,
paymentType: PaymentMethodType.ACH,
paymentProvider: PaymentProvider.POYNT
});
setIsLoadingNonce(false);
setError("");
} catch (err) {
if (err instanceof GraphQLErrorWithCodes) setCheckoutErrors(err.codes);
setIsLoadingNonce(false);
}
else {
setCheckoutErrors(["TRANSACTION_PROCESSING_FAILED"]);
setIsLoadingNonce(false);
}
});
collect?.current?.on("error", (event) => {
setError(event?.data?.error?.message || t.errors.errorProcessingPayment);
setIsLoadingNonce(false);
});
collect?.current?.on("validated", (event) => {
if (event?.data?.validated) setError("");
else setIsLoadingNonce(false);
});
}, [
isPoyntLoaded,
businessId,
confirmCheckout.mutateAsync,
setCollect,
setCheckoutErrors,
t,
setIsLoadingNonce,
session?.storeId,
session?.channelId,
applicationId,
elementId
]);
return /* @__PURE__ */ jsxs(Fragment, { children: [
description ? /* @__PURE__ */ jsx("div", {
className: "pb-4",
children: description
}) : null,
/* @__PURE__ */ jsx("div", { id: elementId }),
error ? /* @__PURE__ */ jsx("p", {
className: "text-[0.8rem] font-medium text-destructive",
children: error
}) : null,
canOfferShippingAddressAsBilling && paymentMethod === PaymentMethodType.ACH ? /* @__PURE__ */ jsx(PaymentAddressToggle, { className: "pt-4" }) : null,
shouldShowBilling ? /* @__PURE__ */ jsxs(CheckoutSection, {
className: "pt-5",
children: [/* @__PURE__ */ jsx(CheckoutSectionHeader, {
title: billingCopy.title,
description: billingCopy.description
}), /* @__PURE__ */ jsx(AddressForm, {
sectionKey: "billing",
onlyNames: billingPolicy.mode === BillingCollectionModes.NAMES
})]
}) : null
] });
}
//#endregion
export { GoDaddyACHForm };