Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/clever-moose-spend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@paypal/react-paypal-js": minor
---

Adds Braintree PayPal One Time Payment Session hook.
41 changes: 35 additions & 6 deletions package-lock.json
Copy link
Copy Markdown
Contributor Author

@EvanReinstein EvanReinstein Apr 17, 2026

Choose a reason for hiding this comment

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

I pulled changes from main and ran npm install this updated a handful of packages to include a peer boolean.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { act, render, waitFor } from "@testing-library/react";

import { expectCurrentErrorValue } from "../../hooks/useErrorTestUtil";
import { BraintreePayPalProvider } from "./BraintreePayPalProvider";
import { useBraintreePayPal } from "../../hooks/useBraintreePayPal";
import { useBraintreePayPal } from "../../hooks/Braintree/useBraintreePayPal";
import { INSTANCE_LOADING_STATE } from "../../types/ProviderEnums";

import type { BraintreeV6Namespace } from "../../types";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Creates a Braintree payment session with error handling and retry prevention.
*
* @param sessionCreator - Function that creates the payment session
* @param failedInstanceRef - Ref tracking which checkout instance failed
* @param checkoutInstance - Current Braintree checkout instance
* @param setError - Error state setter
* @returns The payment session or null if creation fails
*/
export function createBraintreePaymentSession<T>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this function is very similar to [createPaymentSession](https://github.com/paypal/paypal-js/blob/ffee35fcf23a510d691931ec237624edbb4762b2/packages/react-paypal-js/src/v6/utils.ts#L278) with same signature but a different error message. Can we consider reusing createPaymentSession with a configurable error message and we can remove this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yea I went back and forth on this. Mostly b/c I thought it meant updating every usage of the new util with an error message. Let me investigate further.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We decided to take this up in a follow up, in order to keep the scope smaller.

sessionCreator: () => T,
failedInstanceRef: { current: unknown },
checkoutInstance: unknown,
setError: (error: Error | null) => void,
): T | null {
// Skip retry if this checkout instance already failed
if (failedInstanceRef.current === checkoutInstance) {
return null;
}

try {
return sessionCreator();
} catch (err) {
failedInstanceRef.current = checkoutInstance;

const detailedError = new Error(
"Failed to create Braintree payment session. Ensure the BraintreePayPalProvider is properly initialized with a valid client token and namespace.",
{ cause: err },
);

setError(detailedError);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useContext } from "react";

import { BraintreePayPalContext } from "../context/BraintreePayPalContext";
import { BraintreePayPalContext } from "../../context/BraintreePayPalContext";

import type { BraintreePayPalState } from "../context/BraintreePayPalContext";
import type { BraintreePayPalProvider } from "../components/Braintree/BraintreePayPalProvider";
import type { BraintreePayPalState } from "../../context/BraintreePayPalContext";
import type { BraintreePayPalProvider } from "../../components/Braintree/BraintreePayPalProvider";

/**
* Returns {@link BraintreePayPalState} provided by a parent {@link BraintreePayPalProvider}.
Expand Down
Loading
Loading