import { checkoutApi } from '@akinon/next/data/client/checkout'; import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks'; import { getCookie } from '@akinon/next/utils'; import { getUrlPathWithLocale } from '@akinon/next/utils/localization'; import { RootState } from '@theme/redux/store'; import { useState, useMemo } from 'react'; export default function useGooglePay() { const dispatch = useAppDispatch(); const { preOrder } = useAppSelector((state: RootState) => state.checkout); const [errors, setErrors] = useState(null); const paymentErrors = useMemo(() => { if (typeof errors === 'string') return errors; if (Array.isArray(errors)) return errors.join(', '); if (typeof errors === 'object') return Object.values(errors ?? {}).join(', '); return null; }, [errors]); const redirectToThankYouPage = (contextList: any[]) => { const thankYouPageContext = contextList.find( (c: any) => c.page_name === 'ThankYouPage' ); if (thankYouPageContext) { const redirectUrl = thankYouPageContext.page_context.context_data.redirect_url; const redirectUrlWithLocale = `${window.location.origin}${getUrlPathWithLocale( redirectUrl, getCookie('pz-locale') )}`; window.location.href = redirectUrlWithLocale; } }; const handleGooglePaySuccess = async (paymentData: any) => { setErrors(null); const rawTokenString = paymentData.paymentMethodData.tokenizationData.token; try { const walletSelectionPageResponse = await dispatch( checkoutApi.endpoints.setWalletSelectionPage.initiate({ payment_option: preOrder.payment_option?.pk }) ).unwrap(); const walletPaymentPageContext = walletSelectionPageResponse.context_list.find( (c: any) => c.page_name === 'WalletPaymentPage' ); if (!walletPaymentPageContext) { setErrors('Error: Could not proceed to payment step (Context error).'); return; } const walletPaymentPageResponse = await dispatch( checkoutApi.endpoints.setWalletPaymentPage.initiate({ payment_token: JSON.stringify(rawTokenString) }) ).unwrap(); if (walletPaymentPageResponse.errors) { setErrors(walletPaymentPageResponse.errors); return; } const redirectPageContext = walletPaymentPageResponse.context_list.find( (c: any) => c.page_name === 'WalletRedirectCompletePage' ); if (redirectPageContext) { window.location.href = redirectPageContext.page_context.redirect_url; return; } const completePageContext = walletPaymentPageResponse.context_list.find( (c: any) => c.page_name === 'WalletCompletePage' ); if (completePageContext) { const paymentCompleteResponse = await dispatch( checkoutApi.endpoints.setWalletCompletePage.initiate({ success: true }) ).unwrap(); if (paymentCompleteResponse.errors) { setErrors(paymentCompleteResponse.errors); return; } redirectToThankYouPage(paymentCompleteResponse.context_list); } else { setErrors('Payment could not be completed.'); } } catch (error) { console.error('Google Pay Flow Error:', error); setErrors('An unexpected error occurred during the process.'); } }; return { paymentErrors, setPaymentErrors: setErrors, handleGooglePaySuccess }; }