UNPKG

@paypal/react-paypal-js

Version:
72 lines (70 loc) 2.7 kB
/*! * react-paypal-js v9.2.0 (2026-04-27T17:34:47.479Z) * Copyright 2020-present, PayPal, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import 'server-only'; /** * Server-side function to fetch eligible payment methods from the PayPal API. * * Use this in server environments (Next.js server components, Remix loaders, etc.) * to pre-fetch eligibility data before hydrating the client. Pass the response * to the `PayPalProvider` via the `eligibleMethodsResponse` prop. * * @param options - Configuration for the eligibility request * @param options.headers - HTTP headers for the request, including the `Authorization` bearer token * @param options.environment - Target environment ("sandbox" or "production") * @param options.payload - Optional request payload with customer/purchase details * @param options.signal - Optional AbortSignal for request cancellation * @returns Promise resolving to the eligibility API response * * @example * // Next.js server component * const response = await useFetchEligibleMethods({ * headers: { * "Content-Type": "application/json", * Authorization: `Bearer ${clientToken}`, * }, * environment: "sandbox", * payload: { purchase_units: [{ amount: { currency_code: "USD" } }] }, * }); * * <PayPalProvider eligibleMethodsResponse={response} ... /> */ async function useFetchEligibleMethods(options) { const { payload, signal, environment, headers } = options; const defaultPayload = payload ?? {}; const baseUrl = environment === "production" ? "https://api-m.paypal.com" : "https://api-m.sandbox.paypal.com"; try { const response = await fetch(`${baseUrl}/v2/payments/find-eligible-methods`, { method: "POST", headers, body: JSON.stringify(defaultPayload), signal }); if (!response.ok) { throw new Error(`Eligibility API error: ${response.status}`); } const data = await response.json(); return data; } catch (error) { throw new Error(`Failed to fetch eligible methods: ${error instanceof Error ? error.message : String(error)}`); } } export { useFetchEligibleMethods };