@godaddy/react
Version:
The `createCheckoutSession` function creates a new checkout session with GoDaddy's commerce API.
115 lines (113 loc) • 4.88 kB
JavaScript
import { C as assertNoUnresolvedPayment, D as getStripeNextAction, Et as track, S as PAYMENT_STILL_CONFIRMING, T as isUnresolvedPaymentError, Tt as TrackingEventType, _ as CheckoutConfirmationBlockedError, a as redirectToSuccessUrl, lt as eventIds, o as useCheckoutContext, v as PaymentProvider, w as getUnresolvedPayment, x as useConfirmCheckoutRecovery, y as isCheckoutConfirmationBlockedError } from "./checkout-CYyQY2Wk.js";
import { o as confirmCheckout } from "./godaddy-diAn6flX.js";
import { d as useGoDaddyContext } from "./utils-7H42wSPT.js";
import { useRef } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
//#region src/components/checkout/payment/utils/use-confirm-express-checkout.ts
function useConfirmExpressCheckout() {
const { session, jwt, isConfirmingCheckout, setIsConfirmingCheckout, setCheckoutErrors } = useCheckoutContext();
const { apiHost } = useGoDaddyContext();
const queryClient = useQueryClient();
const isPendingRef = useRef(false);
const confirmWithRecovery = useConfirmCheckoutRecovery();
return useMutation({
mutationFn: async (input) => {
if (!session) throw new Error("Express checkout session is unavailable");
if (!input?.paymentType) throw new Error("Express checkout payment type is unavailable");
const unresolved = getUnresolvedPayment(queryClient, session.id);
const isContinuation = input.paymentProvider === PaymentProvider.STRIPE && unresolved?.flow === "express" && unresolved.paymentReference === input.paymentToken;
if (isConfirmingCheckout && !isContinuation) throw new CheckoutConfirmationBlockedError("Checkout confirmation is already in progress");
if ((queryClient.isMutating() > 1 || queryClient.isFetching() > 0) && !isContinuation) throw new CheckoutConfirmationBlockedError("Checkout is currently busy");
if (isPendingRef.current) throw new CheckoutConfirmationBlockedError("Express checkout confirmation is already in progress");
assertNoUnresolvedPayment(queryClient, session.id, {
flow: "express",
paymentToken: input.paymentToken
});
isPendingRef.current = true;
try {
const { isExpress: _isExpress,...confirmCheckoutInput } = input;
setCheckoutErrors(void 0);
setIsConfirmingCheckout(true);
track({
eventId: eventIds.paymentStart,
type: TrackingEventType.EVENT,
properties: {
paymentType: input.paymentType,
provider: input.paymentProvider,
draftOrderId: session?.draftOrder?.id || "unknown"
}
});
return await confirmWithRecovery(async () => {
const result = jwt ? await confirmCheckout(confirmCheckoutInput, {
accessToken: jwt,
sessionId: session?.id || ""
}, apiHost) : await confirmCheckout(confirmCheckoutInput, session, apiHost);
if (!result) throw new Error("Express checkout confirmation failed");
return result;
});
} finally {
isPendingRef.current = false;
}
},
onSuccess: (data, input) => {
if (!data) return;
let completedEventId = null;
switch (input.paymentType) {
case "apple_pay":
completedEventId = eventIds.expressApplePayCompleted;
break;
case "google_pay":
completedEventId = eventIds.expressGooglePayCompleted;
break;
case "paze":
completedEventId = eventIds.pazePayCompleted;
break;
default: completedEventId = null;
}
if (completedEventId) track({
eventId: completedEventId,
type: TrackingEventType.EVENT,
properties: {
draftOrderId: session?.draftOrder?.id || "unknown",
paymentType: input.paymentType,
provider: "poynt"
}
});
track({
eventId: eventIds.checkoutComplete,
type: TrackingEventType.EVENT,
properties: {
draftOrderId: session?.draftOrder?.id || "unknown",
total: session?.draftOrder?.totals?.total?.value || 0,
currencyCode: session?.draftOrder?.totals?.total?.currencyCode || "unknown",
paymentType: input?.paymentType,
provider: input?.paymentProvider || "unknown"
}
});
redirectToSuccessUrl(session?.successUrl);
},
onError: (error, data) => {
if (isCheckoutConfirmationBlockedError(error)) return;
if (isUnresolvedPaymentError(error)) {
setCheckoutErrors([PAYMENT_STILL_CONFIRMING]);
setIsConfirmingCheckout(false);
return;
}
if (data?.paymentProvider === PaymentProvider.STRIPE && getStripeNextAction(error)) return;
track({
eventId: eventIds.checkoutError,
type: TrackingEventType.EVENT,
properties: {
errorCodes: error instanceof Error ? error.name : "unknown",
errorType: error instanceof Error ? error.message : void 0,
paymentType: data?.paymentType,
provider: data?.paymentProvider || "unknown",
draftOrderId: session?.draftOrder?.id || "unknown"
}
});
setIsConfirmingCheckout(false);
}
});
}
//#endregion
export { useConfirmExpressCheckout as t };