-
Notifications
You must be signed in to change notification settings - Fork 123
BCDC hook implementation #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: f82d94b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| buyerCountry?: string; | ||
| targetElement?: string | HTMLElement; | ||
| presentationMode?: "auto"; | ||
| fullPageOverlay?: boolean; | ||
| autoRedirect?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these are the guest payment presentation mode .start options I think it is fair to do a paypal-js change to consolidate this as a type.
Ideally any change that is in paypal-js should be a separate PR.
kand
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good start! A few comments now, but I'll continue the review on Monday!
| try { | ||
| await callback(...args); | ||
| } finally { | ||
| resetState(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what conditions does this "reset button state"? Reading this, I think this will only reset state if the callback throws an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using this "reset button state" to manually reset button after payment flow is completed. We dont need this since SDK is handling the button re-enabling here. https://github.paypal.com/PayPal-R/core-web-sdk/blob/8cc18742de053cf3a8014f13a086d2299526fa96/packages/web-sdk-payments/src/CheckoutSession/PayPalGuestCheckoutSession.ts#L239
| const isSessionActiveRef = useRef(false); | ||
|
|
||
| // Track whether shipping callbacks are present to trigger session recreation | ||
| const hasShippingCallbacks = Boolean( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useProxyProps prevents changes to callbacks triggering a component re-render. If I'm understanding hasShippingCallbacks, it appears to be reversing the useProxyProps functionality. Would the same effect be achieved by unpacking the shipping callbacks from the argument on line 39, then having those be dependencies to the session creation useEffect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point!! That seems to be a much cleaner way to destructure the shipping callbacks from props and passing then to session creation conditionally. Later use them as dependencies to trigger session creation.
| const buttonRef = useRef<HTMLElement>(null); | ||
| const proxyCallbacks = useProxyProps(callbacks); | ||
| const [error, setError] = useError(); | ||
| const [isProcessing, setIsProcessing] = useState(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the isProcessing value used anywhere. What would this be used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialy I used isProcessing manually disable the button for testing purposes by adding it as a attribute value. Since SDK takes care of disabling the button when the form is open, we no longer need this. Thanks for pointing out. I'll remove it.
| targetElement?: string | HTMLElement; | ||
| presentationMode?: "auto"; | ||
| fullPageOverlay?: boolean; | ||
| autoRedirect?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BCDC has inline, popup, and modal presentation modes, so any options those can use need to be available. I think fullPageOverlay is valid because it's used by popup. I'm not sure about autoRedirect though, I'm only seeing that in relation to redirect presentation modes. Could you verify if this is an option BCDC needs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed theautoRedirect option.
| ) & { | ||
| buyerCountry?: string; | ||
| targetElement?: string | HTMLElement; | ||
| presentationMode?: "auto"; | ||
| fullPageOverlay?: boolean; | ||
| onShippingAddressChange?: ( | ||
| data: OnShippingAddressChangeData, | ||
| ) => Promise<void>; | ||
| onShippingOptionsChange?: ( | ||
| data: OnShippingOptionsChangeData, | ||
| ) => Promise<void>; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that I'm also seeing a presentationMode should include inline, popup, and modal.PayPalGuestPresentationModeOptions defined in paypal-js that looks like:
export type PayPalGuestPresentationModeOptions =
PresentationModeOptionsForAuto & {
targetElement?: string | EventTarget;
};
I'm wondering if we should update that type to include some of the properties that are not currently in PayPalGuestPresentationModeOptions but are used in the hook, such as onShippingOptionsChange/onShippingAddressChange and targetElement or buyerCountry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huddled on this with @kand and I'll open a PR to update PayPalGuestPresentationModeOptions with some of the other relevant properties 👍
We also discussed hardcoding the presentationMode and removing it as an argument for the hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened a PR to update the presentation mode options type here 👋
| export type UsePayPalGuestPaymentSessionProps = ( | ||
| | (Omit<PayPalGuestOneTimePaymentSessionOptions, "orderId"> & { | ||
| createOrder: () => PayPalGuestOneTimePaymentSessionPromise; | ||
| orderId?: never; | ||
| }) | ||
| | (PayPalGuestOneTimePaymentSessionOptions & { | ||
| createOrder?: never; | ||
| orderId: string; | ||
| }) | ||
| ) & { | ||
| buyerCountry?: string; | ||
| targetElement?: string | HTMLElement; | ||
| presentationMode?: "auto"; | ||
| fullPageOverlay?: boolean; | ||
| onShippingAddressChange?: ( | ||
| data: OnShippingAddressChangeData, | ||
| ) => Promise<void>; | ||
| onShippingOptionsChange?: ( | ||
| data: OnShippingOptionsChangeData, | ||
| ) => Promise<void>; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| export type UsePayPalGuestPaymentSessionProps = ( | |
| | (Omit<PayPalGuestOneTimePaymentSessionOptions, "orderId"> & { | |
| createOrder: () => PayPalGuestOneTimePaymentSessionPromise; | |
| orderId?: never; | |
| }) | |
| | (PayPalGuestOneTimePaymentSessionOptions & { | |
| createOrder?: never; | |
| orderId: string; | |
| }) | |
| ) & { | |
| buyerCountry?: string; | |
| targetElement?: string | HTMLElement; | |
| presentationMode?: "auto"; | |
| fullPageOverlay?: boolean; | |
| onShippingAddressChange?: ( | |
| data: OnShippingAddressChangeData, | |
| ) => Promise<void>; | |
| onShippingOptionsChange?: ( | |
| data: OnShippingOptionsChangeData, | |
| ) => Promise<void>; | |
| }; | |
| type PayPalGuestPresentationModeHookOptions = Omit<PayPalGuestPresentationModeOptions, "presentationMode">; | |
| export type UsePayPalGuestPaymentSessionProps = ( | |
| | (Omit<PayPalGuestOneTimePaymentSessionOptions, "orderId"> & { | |
| createOrder: () => PayPalGuestOneTimePaymentSessionPromise; | |
| orderId?: never; | |
| }) | |
| | (PayPalGuestOneTimePaymentSessionOptions & { | |
| createOrder?: never; | |
| orderId: string; | |
| }) | |
| ) & PayPalGuestPresentationModeHookOptions; |
| try { | ||
| const target = targetElement || buttonRef.current; | ||
| const startOptions: PayPalGuestPresentationModeOptions = { | ||
| presentationMode, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard code the presentationMode in order to prevent passing values other than auto
| presentationMode, | |
| presentationMode: "auto", |
| useEffect(() => { | ||
| if (!sdkInstance) { | ||
| setError(new Error("no sdk instance available")); | ||
| } | ||
| }, [sdkInstance, setError]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kand Is making an update to other session hooks to be more mindful about when to set an error.
| useEffect(() => { | |
| if (!sdkInstance) { | |
| setError(new Error("no sdk instance available")); | |
| } | |
| }, [sdkInstance, setError]); | |
| useEffect(() => { | |
| if (sdkInstance) { | |
| setError(null); | |
| } else if (loadingStatus !== INSTANCE_LOADING_STATE.PENDING) { | |
| setError(new Error("no sdk instance available")); | |
| } | |
| }, [sdkInstance, setError, loadingStatus]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good catch @EvanReinstein, thanks!
| test("should error if there is no sdkInstance when called", () => { | ||
| mockUsePayPal.mockReturnValue({ | ||
| sdkInstance: null, | ||
| loadingStatus: INSTANCE_LOADING_STATE.PENDING, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| loadingStatus: INSTANCE_LOADING_STATE.PENDING, | |
| loadingStatus: INSTANCE_LOADING_STATE.REJECTED, |
I made this change in my updates PR since it tests only the case where sdkInstance is null while the loadingStatus check is true.
There are a couple other updates to this test as well you could copy over from my PR, including the name of the test and checking that the create session call is not made!
| result.current.handleCancel(); | ||
| }); | ||
|
|
||
| expect(mockPayPalSession.cancel).not.toHaveBeenCalled(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason the session's cancel isn't called?
|
|
||
| expect(() => { | ||
| act(() => { | ||
| result.current.handleCancel(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There currently isn't any logic using the session in the handleCancel callback, or that would throw an error. So, should this test be refactored? Or is there logic missing from handleCancel?
| const target = targetElement || buttonRef.current; | ||
| const startOptions: PayPalGuestPresentationModeOptions = { | ||
| presentationMode: "auto", | ||
| ...(fullPageOverlay !== undefined && { fullPageOverlay }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does undefined need to be checked specifically, or could this just be a truthy check? Also, could this be rewritten as a ternary like the line following it?
| ...(fullPageOverlay !== undefined && { fullPageOverlay }), | |
| ...(fullPageOverlay ? { fullPageOverlay } : {}), |
| } catch (err) { | ||
| if (isMountedRef.current) { | ||
| isSessionActiveRef.current = false; | ||
| setError(err instanceof Error ? err : new Error(String(err))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this function should take unknown, see it's type here. Were you seeing a typescript error when this wasn't cast?
| setError(err instanceof Error ? err : new Error(String(err))); | |
| setError(err); |
| const checkoutOptionsPromise = createOrder | ||
| ? createOrder() | ||
| : undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using optional chaining, I believe this could be shortened to:
| const checkoutOptionsPromise = createOrder | |
| ? createOrder() | |
| : undefined; | |
| const checkoutOptionsPromise = createOrder?.(); |
This could be moved down to line 123 as well, same as the PayLater session is doing:
| } from "@paypal/paypal-js/sdk-v6"; | ||
| import type { BasePaymentSessionReturn } from "../types"; | ||
|
|
||
| export interface PayPalGuestPaymentSessionReturn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this interface need to be exported? I don't see it used anywhere outside this file.
| "presentationMode" | ||
| >; | ||
|
|
||
| export type UsePayPalGuestPaymentSessionProps = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this type need to be exported? If it's only exported for the test file, I think all the const prop constructions could just be inlined directly into their respective usePayPalGuestPaymentSession calls.
React hook for guest payment session (BCDC).