Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ const PayPalButton: React.FC<PaymentSessionOptions> = (
const paypalSession = useRef<SessionOutput>(null);

useEffect(() => {
if (sdkInstance) {
if (sdkInstance && !paypalSession.current) {
paypalSession.current = sdkInstance.createPayPalOneTimePaymentSession(
paymentSessionOptions,
);
}
}, [sdkInstance, paymentSessionOptions]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would like to re-initialize the session if paymentSessionOptions changes right?


return () => {
if (paypalSession.current && typeof paypalSession.current.destroy === 'function') {
paypalSession.current.destroy();
paypalSession.current = null;
}
};
}, [sdkInstance]);

const payPalOnClickHandler = async () => {
if (!paypalSession.current) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ const VenmoButton: React.FC<PaymentSessionOptions> = (
const venmoSession = useRef<SessionOutput>(null);

useEffect(() => {
if (sdkInstance) {
if (sdkInstance && !venmoSession.current) {
venmoSession.current = sdkInstance.createVenmoOneTimePaymentSession(
paymentSessionOptions,
);
}
}, [sdkInstance, paymentSessionOptions]);

return () => {
if (
venmoSession.current &&
typeof venmoSession.current.destroy === "function"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here

) {
venmoSession.current.destroy();
venmoSession.current = null;
}
};
}, [sdkInstance]);

const venmoOnClickHandler = async () => {
if (!venmoSession.current) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const PayPalSDKProvider: React.FC<PayPalSDKProviderProps> = ({
};

loadPayPalSDK();
});
}, [clientToken, components, pageType, sdkInstance, showBoundary]);

return (
<PayPalSDKContext.Provider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState, useCallback } from "react";
import React, { useContext, useState, useCallback, useMemo } from "react";
import { PayPalSDKContext } from "../context/sdkContext";
import PayPalButton from "../components/PayPalButton";
import VenmoButton from "../components/VenmoButton";
Expand Down Expand Up @@ -33,7 +33,7 @@ const SoccerBall: React.FC = () => {
const [modalState, setModalState] = useState<ModalType>(null);

// Payment handlers
const handlePaymentCallbacks: PaymentSessionOptions = {
const handlePaymentCallbacks: PaymentSessionOptions = useMemo(() => ({
onApprove: async (data: OnApproveData) => {
console.log("Payment approved:", data);
const captureResult = await captureOrder({ orderId: data.orderId });
Expand All @@ -50,7 +50,7 @@ const SoccerBall: React.FC = () => {
console.error("Payment error:", error);
setModalState("error");
},
};
}), [])

const getModalContent = useCallback(
(state: ModalType): ModalContent | null => {
Expand Down
18 changes: 8 additions & 10 deletions client/prebuiltPages/react/oneTimePayment/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export const getBrowserSafeClientToken = async () => {
{
const response = await fetch("/paypal-api/auth/browser-safe-client-token", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const { accessToken } = await response.json();
const response = await fetch("/paypal-api/auth/browser-safe-client-token", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const { accessToken } = await response.json();

return accessToken;
}
return accessToken;
};

export const createOrder = async () => {
Expand Down
Loading