diff --git a/packages/core/src/app/customer/CheckoutButtonList.tsx b/packages/core/src/app/customer/CheckoutButtonList.tsx index 014991a51d..74c1cd98d0 100644 --- a/packages/core/src/app/customer/CheckoutButtonList.tsx +++ b/packages/core/src/app/customer/CheckoutButtonList.tsx @@ -4,8 +4,9 @@ import React, { FunctionComponent, memo } from 'react'; import { TranslatedString } from '@bigcommerce/checkout/locale'; import CheckoutButton from './CheckoutButton'; -import { AmazonPayV2Button, ApplePayButton, PayPalCommerceButton } from './customWalletButton'; +import { AmazonPayV2Button, ApplePayButton } from './customWalletButton'; import { getSupportedMethodIds } from './getSupportedMethods'; +import { PayPalCommerceButton } from '@bigcommerce/checkout/paypal-utils'; export interface CheckoutButtonListProps { methodIds?: string[]; diff --git a/packages/core/src/app/customer/WalletButtonV1Resolver.tsx b/packages/core/src/app/customer/WalletButtonV1Resolver.tsx index 713a58756e..6c4a5fbc61 100644 --- a/packages/core/src/app/customer/WalletButtonV1Resolver.tsx +++ b/packages/core/src/app/customer/WalletButtonV1Resolver.tsx @@ -2,7 +2,8 @@ import { CustomerInitializeOptions, CustomerRequestOptions } from "@bigcommerce/ import React, { FunctionComponent } from "react"; import CheckoutButton from "./CheckoutButton"; -import { ApplePayButton, PayPalCommerceButton } from "./customWalletButton"; +import { ApplePayButton } from "./customWalletButton"; +import { PayPalCommerceButton } from "@bigcommerce/checkout/paypal-utils"; interface CheckoutButtonV1ResolverProps { methodId: string; diff --git a/packages/core/src/app/customer/customWalletButton/PayPalCommerceButton.tsx b/packages/core/src/app/customer/customWalletButton/PayPalCommerceButton.tsx deleted file mode 100644 index 2e1cfc908b..0000000000 --- a/packages/core/src/app/customer/customWalletButton/PayPalCommerceButton.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import { CustomerInitializeOptions } from '@bigcommerce/checkout-sdk'; -import { noop } from 'lodash'; -import React, { FunctionComponent, useCallback, useContext } from 'react'; - -import { LocaleContext } from '@bigcommerce/checkout/locale'; - -import { navigateToOrderConfirmation } from '../../checkout'; -import CheckoutButton, { CheckoutButtonProps } from '../CheckoutButton'; - -const PayPalCommerceButton: FunctionComponent = ({ - methodId, - initialize, - onError, - onClick = noop, - ...rest -}) => { - const localeContext = useContext(LocaleContext); - const initializeOptions = useCallback( - (options: CustomerInitializeOptions) => - initialize({ - ...options, - [methodId]: { - container: rest.containerId, - onError, - onClick: () => onClick(methodId), - onComplete: navigateToOrderConfirmation, - }, - }), - [initialize, localeContext, onError, rest.containerId], - ); - - return ; -}; - -export default PayPalCommerceButton; diff --git a/packages/core/src/app/customer/customWalletButton/index.ts b/packages/core/src/app/customer/customWalletButton/index.ts index 2222a7eb9c..38350fea48 100644 --- a/packages/core/src/app/customer/customWalletButton/index.ts +++ b/packages/core/src/app/customer/customWalletButton/index.ts @@ -1,3 +1,2 @@ export { default as ApplePayButton } from './ApplePayButton'; export { default as AmazonPayV2Button } from './AmazonPayV2Button'; -export { default as PayPalCommerceButton } from './PayPalCommerceButton'; diff --git a/packages/paypal-utils/src/PayPalCommerceButton.tsx b/packages/paypal-utils/src/PayPalCommerceButton.tsx new file mode 100644 index 0000000000..2772fdf8c9 --- /dev/null +++ b/packages/paypal-utils/src/PayPalCommerceButton.tsx @@ -0,0 +1,68 @@ +import { noop } from 'lodash'; +import React, {FunctionComponent, useEffect} from 'react'; +import { CustomerRequestOptions, CustomerInitializeOptions } from '@bigcommerce/checkout-sdk'; +import { useCheckout } from '@bigcommerce/checkout/payment-integration-api'; + +interface CheckoutButtonProps { // TODO: MOVE TO PROPER PLACE + containerId: string; + methodId: string; + isShowingWalletButtonsOnTop?: boolean; + deinitialize(options: CustomerRequestOptions): void; + initialize(options: CustomerInitializeOptions): void; + onError?(error: Error): void; + onClick?(methodId: string): void; +}; + +const PayPalCommerceButton: FunctionComponent = ({ + methodId, + onError, + onClick = noop, + containerId, +}) => { + const { checkoutService } = useCheckout(); + useEffect(() => { + const initializeCustomer = async () => { + try { + await checkoutService.initializeCustomer({ + methodId, + [methodId]: { + container: containerId, + onError, + onClick: () => onClick(methodId), + onComplete: () => {}, //TODO: Add navigateToOrderConfirmation + }, + }); + } catch (error) { + if (error instanceof Error) { + if (onError) { + onError(error); + } + } + } + }; + + void initializeCustomer(); + + return () => { + const deinitializeCustomer = async () => { + try { + await checkoutService.deinitializeCustomer({ + methodId, + }); + } catch (error) { + if (error instanceof Error) { + if (onError) { + onError(error); + } + } + } + }; + + void deinitializeCustomer(); + }; + }, [checkoutService, onError]); + + return
+}; + +export default PayPalCommerceButton; diff --git a/packages/paypal-utils/src/index.ts b/packages/paypal-utils/src/index.ts index d6424a2d1d..5cb0c5b925 100644 --- a/packages/paypal-utils/src/index.ts +++ b/packages/paypal-utils/src/index.ts @@ -1,2 +1,3 @@ export { default as PaypalCommerceCreditBanner } from './PaypalCommerceCreditBanner'; export { default as BraintreePaypalCreditBanner } from './BraintreePaypalCreditBanner'; +export { default as PayPalCommerceButton } from './PayPalCommerceButton';