import { useMemo } from 'react';
import { useAuth } from '@broadleaf/auth-react';
import { AmazonPaymentServicesClient } from '@broadleaf/amazon-payment-services-api';
import { useCreateTokenService } from '@broadleaf/amazon-payment-services-react';
import { CustomerClient } from '@broadleaf/commerce-customer';
import type { AuthState } from '@broadleaf/payment-js';
import type { ClientOptions } from '@broadleaf/commerce-core';
const { isAuthenticated, isLoading, getAccessToken, user } = useAuth();
const { csrMode, csrAnonymous } = useCsrContext();
const authState = useMemo<AuthState>(() => {
const customerEmail = user?.email_address;
const customerId = user?.customer_id;
return {
isAuthenticated,
isAuthenticating: isLoading,
getAccessToken,
customerEmail,
customerId,
isCsr: csrMode,
isAnonymousCsr: csrAnonymous,
};
}, [csrAnonymous, csrMode, getAccessToken, isAuthenticated, isLoading, user]);
const options: ClientOptions = {
applicationToken,
baseHost,
locale: currentLocale,
preview,
};
const amazonPaymentServicesClient = new AmazonPaymentServicesClient(options);
const customerClient = new CustomerClient(options);
const { handleCreateTokenService, error: createTokenServiceError } =
useCreateTokenService({
amazonPaymentServicesClient,
authState,
accessCode: 'accessCode',
checkoutUrl: 'https://sbcheckout.PayFort.com/FortAPI/paymentPage',
customerClient,
defaultLanguage: 'en',
merchantIdentifier: '123abc',
});