Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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,11 +12,21 @@ const PayPalButton: React.FC<PaymentSessionOptions> = (
const paypalSession = useRef<SessionOutput>(null);

useEffect(() => {
if (sdkInstance) {
if (sdkInstance && !paypalSession.current) {
paypalSession.current = sdkInstance.createPayPalOneTimePaymentSession(
paymentSessionOptions,
);
}

return () => {
if (
paypalSession.current &&
typeof paypalSession.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.

Do we have to do this check? I would assume destroy always exists if the paypalSession exists?

) {
paypalSession.current.destroy();
paypalSession.current = null;
}
};
}, [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?


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

useEffect(() => {
if (sdkInstance) {
if (sdkInstance && !venmoSession.current) {
venmoSession.current = sdkInstance.createVenmoOneTimePaymentSession(
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, paymentSessionOptions]);

const venmoOnClickHandler = async () => {
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,24 +33,27 @@ const SoccerBall: React.FC = () => {
const [modalState, setModalState] = useState<ModalType>(null);

// Payment handlers
const handlePaymentCallbacks: PaymentSessionOptions = {
onApprove: async (data: OnApproveData) => {
console.log("Payment approved:", data);
const captureResult = await captureOrder({ orderId: data.orderId });
console.log("Payment capture result:", captureResult);
setModalState("success");
},

onCancel: () => {
console.log("Payment cancelled");
setModalState("cancel");
},

onError: (error: Error) => {
console.error("Payment error:", error);
setModalState("error");
},
};
const handlePaymentCallbacks: PaymentSessionOptions = useMemo(
() => ({
onApprove: async (data: OnApproveData) => {
console.log("Payment approved:", data);
const captureResult = await captureOrder({ orderId: data.orderId });
console.log("Payment capture result:", captureResult);
setModalState("success");
},

onCancel: () => {
console.log("Payment cancelled");
setModalState("cancel");
},

onError: (error: Error) => {
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