UNPKG

@paypal/react-paypal-js

Version:
18 lines (17 loc) 29.6 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 e,{createContext as r,useContext as n,useRef as t,useState as a,useCallback as o,useEffect as i,useMemo as l,useLayoutEffect as s,useReducer as c}from"react";import{loadCoreSdkScript as u}from"@paypal/paypal-js/sdk-v6";var d,y,p;function f(e){const r=e;return"function"==typeof r?.client?.create&&"function"==typeof r?.paypalCheckoutV6?.create}!function(e){e.PENDING="pending",e.RESOLVED="resolved",e.REJECTED="rejected"}(d||(d={})),function(e){e.SET_LOADING_STATUS="setLoadingStatus",e.SET_INSTANCE="setInstance",e.SET_ELIGIBILITY="setEligibility",e.SET_ERROR="setError",e.RESET_STATE="resetState"}(y||(y={})),function(e){e.SET_LOADING_STATUS="setLoadingStatus",e.SET_INSTANCE="setInstance",e.SET_ERROR="setError",e.RESET_STATE="resetState"}(p||(p={}));const m={sdkInstance:null,eligiblePaymentMethods:null,eligiblePaymentMethodsPayload:null,loadingStatus:d.PENDING,error:null,isHydrated:!1};function h(e,r){switch(r.type){case y.SET_LOADING_STATUS:return{...e,loadingStatus:r.value};case y.SET_INSTANCE:return{...e,sdkInstance:r.value,loadingStatus:d.RESOLVED};case y.SET_ELIGIBILITY:return{...e,eligiblePaymentMethods:r.value.eligiblePaymentMethods,eligiblePaymentMethodsPayload:r.value.payload};case y.SET_ERROR:return{...e,error:r.value,loadingStatus:d.REJECTED};case y.RESET_STATE:return m;default:return e}}const E=r(null);function P(){const e=n(E);if(null===e)throw new Error("usePayPal must be used within a PayPalProvider");return e}function S(e){const r=t(new Proxy({},{get:(e,r,n)=>"function"==typeof e[r]?(...n)=>e[r](...n):Reflect.get(e,r,n)}));return r.current=Object.assign(r.current,e),r.current}function v(e){return e instanceof Error?e:new Error(String(e))}function g(e){const r=t();return r.current&&b(e,r.current.value)||(r.current={value:e}),r.current.value}function b(e,r,n=10,t=0){if(t>n)return!1;if(e===r)return!0;if(null==e||null==r)return!1;if(typeof e!=typeof r)return!1;if("object"!=typeof e)return!1;if(Array.isArray(e)&&Array.isArray(r)){if(e.length!==r.length)return!1;for(let a=0;a<e.length;a++)if(!b(e[a],r[a],n,t+1))return!1;return!0}if(Array.isArray(e)||Array.isArray(r))return!1;const a=e,o=r,i=Object.keys(a),l=Object.keys(o);if(i.length!==l.length)return!1;for(const e of i)if(!b(a[e],o[e],n,t+1))return!1;return!0}function C({sessionCreator:e,failedSdkRef:r,sdkInstance:n,setError:t,errorMessage:a}){if(r.current===n)return null;try{return e()}catch(e){r.current=n;return t(new Error(a,{cause:e})),null}}const I=r(null),k=r(null);function w(e=!1){const[r,n]=a(null);return[r,o(r=>{n(r),!e&&r&&console.error(r)},[e])]}const T="one-time-payment",N="save-payment",R=({children:r,amount:n,isCobrandedEligible:t,...s})=>{const{sdkInstance:c,loadingStatus:u}=P(),[y,p]=a(null),[f,m]=a(null),[h,E]=a(null),[,b]=w(),C=S(s),N=g(n),R=o(e=>{b(e),E(e)},[b]);i(()=>{if(u===d.PENDING)return;if(!c)return void R(v("no sdk instance available"));if(R(null),!f)return;let e;try{e=f===T?c.createCardFieldsOneTimePaymentSession():c.createCardFieldsSavePaymentSession(),p(e)}catch(e){R(v(e))}return()=>{e?.destroy(),p(null)}},[c,u,f,R]),i(()=>{if(y)try{Object.keys(C).forEach(e=>{const r=C[e];r&&"function"==typeof r&&y.on(e,r)})}catch(e){R(v(`Failed to register event handlers: ${e}`))}},[y,C,R]),i(()=>{if(!y)return;const e={};let r=!1;if(void 0!==N&&(e.amount=N,r=!0),void 0!==t&&(e.isCobrandedEligible=t,r=!0),r)try{y.update(e)}catch(e){R(v(`Failed to update card fields configuration: ${e}`))}},[y,N,t,R]);const D=l(()=>({cardFieldsSession:y,setCardFieldsSessionType:m,setError:R}),[y,m,R]),M=l(()=>({error:h}),[h]);return e.createElement(I.Provider,{value:D},e.createElement(k.Provider,{value:M},r))};function D(){const e=t(!1);return i(()=>(e.current=!0,()=>{e.current=!1}),[]),e}function M({presentationMode:e,fullPageOverlay:r,autoRedirect:n,createOrder:a,orderId:l,...s}){const{sdkInstance:c,loadingStatus:u}=P(),y=D(),p=t(null),f=S(s),[m,h]=w(),E=t(null),v=u===d.PENDING,g=o(()=>{p.current?.destroy(),p.current=null},[]);i(()=>{E.current!==c&&(E.current=null),c?h(null):u!==d.PENDING&&h(new Error("no sdk instance available"))},[c,h,u]),i(()=>{if(!c)return;const r=C({sessionCreator:()=>c.createPayLaterOneTimePaymentSession({orderId:l,...f}),failedSdkRef:E,sdkInstance:c,setError:h,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-payments" is not included in the SDK components array.'});if(!r)return;p.current=r;if("redirect"===e||"direct-app-switch"===e){(async()=>{try{if(!r)return;const e=r.hasReturned?.();e&&await(r.resume?.())}catch(e){h(e)}})()}return()=>{r.destroy()}},[c,l,f,e,h]);return{error:m,isPending:v,handleCancel:o(()=>{p.current?.cancel()},[]),handleClick:o(async()=>{if(!y.current)return;if(!p.current)return void h(new Error("PayLater session not available"));const t={presentationMode:e,fullPageOverlay:r,autoRedirect:n};return await p.current.start(t,a?.())},[a,e,r,n,y,h]),handleDestroy:g}}const O=({disabled:r=!1,...n})=>{const{eligiblePaymentMethods:t,isHydrated:a}=P(),{error:o,isPending:l,handleClick:s}=M(n),c=t?.getDetails("paylater"),u=c?.countryCode,d=c?.productCode;return i(()=>{o&&console.error(o)},[o]),l?null:a?e.createElement("paypal-pay-later-button",{onClick:s,countryCode:u,productCode:d,disabled:r||!!o||void 0}):e.createElement("div",null)};function A({presentationMode:e,fullPageOverlay:r,autoRedirect:n,createOrder:a,orderId:l,...s}){const{sdkInstance:c,loadingStatus:u}=P(),y=D(),p=t(null),f=S(s),[m,h]=w(),E=t(null),v=u===d.PENDING,g=o(()=>{p.current?.destroy(),p.current=null},[]),b=o(()=>{p.current?.cancel()},[]);i(()=>{E.current!==c&&(E.current=null),c?h(null):u!==d.PENDING&&h(new Error("no sdk instance available"))},[c,h,u]),i(()=>{if(!c)return;const r=C({sessionCreator:()=>c.createPayPalCreditOneTimePaymentSession({orderId:l,...f}),failedSdkRef:E,sdkInstance:c,setError:h,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-payments" is not included in the SDK components array.'});if(!r)return;p.current=r;if("redirect"===e||"direct-app-switch"===e){(async()=>{try{if(!r)return;const e=r.hasReturned?.();e&&await(r.resume?.())}catch(e){h(e)}})()}return()=>{r.destroy()}},[c,l,f,e,h]);return{error:m,isPending:v,handleClick:o(async()=>{if(!y.current)return;if(!p.current)return void h(new Error("PayPal session not available"));const t={presentationMode:e,fullPageOverlay:r,autoRedirect:n};return await p.current.start(t,a?.())},[y,e,r,n,a,h]),handleDestroy:g,handleCancel:b}}const G=({disabled:r=!1,...n})=>{const{eligiblePaymentMethods:t,isHydrated:a}=P(),{error:o,isPending:l,handleClick:s}=A(n),c=t?.getDetails("credit"),u=c?.countryCode;return i(()=>{o&&console.error(o)},[o]),l?null:a?e.createElement("paypal-credit-button",{onClick:s,countryCode:u,disabled:r||!!o||void 0}):e.createElement("div",null)};function _({presentationMode:e,fullPageOverlay:r,autoRedirect:n,createVaultToken:a,vaultSetupToken:l,...s}){const{sdkInstance:c,loadingStatus:u}=P(),y=D(),p=t(null),f=S(s),[m,h]=w(),E=t(null),v=u===d.PENDING,g=o(()=>{p.current?.destroy(),p.current=null},[]);i(()=>{E.current!==c&&(E.current=null),c?h(null):u!==d.PENDING&&h(new Error("no sdk instance available"))},[c,h,u]),i(()=>{if(!c)return;const r=C({sessionCreator:()=>c.createPayPalSavePaymentSession({vaultSetupToken:l,...f}),failedSdkRef:E,sdkInstance:c,setError:h,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-payments" is not included in the SDK components array.'});if(!r)return;p.current=r;if("redirect"===e||"direct-app-switch"===e){(async()=>{try{if(!r)return;const e=r.hasReturned?.();e&&await(r.resume?.())}catch(e){h(e)}})()}return()=>{r.destroy()}},[c,l,f,e,h]);const b=o(()=>{p.current?.cancel()},[]);return{error:m,isPending:v,handleClick:o(async()=>{if(!y.current)return;if(!p.current)return void h(new Error("Credit Save Payment session not available"));const t={presentationMode:e,fullPageOverlay:r,autoRedirect:n};a?await p.current.start(t,a()):await p.current.start(t)},[y,e,r,n,a,h]),handleDestroy:g,handleCancel:b}}const L=({disabled:r=!1,...n})=>{const{eligiblePaymentMethods:t,isHydrated:a}=P(),{error:o,isPending:l,handleClick:s}=_(n),c=t?.getDetails("credit"),u=c?.countryCode;return i(()=>{o&&console.error(o)},[o]),l?null:a?e.createElement("paypal-credit-button",{onClick:s,countryCode:u,disabled:r||!!o||void 0}):e.createElement("div",null)};function F({fullPageOverlay:e,createOrder:r,orderId:n,onShippingAddressChange:a,onShippingOptionsChange:l,...s}){const{sdkInstance:c,loadingStatus:u}=P(),y=D(),p=t(null),f=t(null),m=S(s),[h,E]=w(),v=t(null),g=u===d.PENDING,b=o(()=>{p.current?.destroy(),p.current=null},[]),I=o(()=>{p.current?.cancel()},[]);i(()=>{v.current!==c&&(v.current=null),c?E(null):u!==d.PENDING&&E(new Error("no sdk instance available"))},[c,E,u]),i(()=>{if(!c)return;const e=C({sessionCreator:()=>c.createPayPalGuestOneTimePaymentSession({orderId:n,...m,...a&&{onShippingAddressChange:a},...l&&{onShippingOptionsChange:l}}),failedSdkRef:v,sdkInstance:c,setError:E,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-guest-payments" is not included in the SDK components array.'});return e?(p.current=e,()=>{e.destroy()}):void 0},[c,n,m,a,l,y,E]);const k=o(async()=>{if(y.current)if(p.current)try{const n={presentationMode:"auto",fullPageOverlay:e,...f.current?{targetElement:f.current}:{}};await p.current.start(n,r?.())}catch(e){y.current&&E(e)}else E(new Error("PayPal Guest Checkout session not available"))},[y,e,r,E]);return{buttonRef:f,error:h,isPending:g,handleClick:k,handleCancel:I,handleDestroy:b}}const B=({disabled:r=!1,...n})=>{const{error:t,isPending:a,handleClick:o,buttonRef:l}=F(n),{isHydrated:s}=P();i(()=>{t&&console.error(t)},[t]);const c=s?e.createElement("paypal-basic-card-button",{ref:l,onClick:o,disabled:!(!r&&!a&&null===t)||void 0}):e.createElement("div",null);return e.createElement("paypal-basic-card-container",null,c)};function U({presentationMode:e,fullPageOverlay:r,autoRedirect:n,createOrder:a,orderId:l,savePayment:s,testBuyerCountry:c,...u}){const{sdkInstance:y,loadingStatus:p}=P(),f=D(),m=t(null),h=S(u),[E,v]=w(),g=t(null),b=p===d.PENDING,I=o(()=>{m.current?.destroy(),m.current=null},[]),k=o(()=>{m.current?.cancel()},[]);i(()=>{g.current!==y&&(g.current=null),y?v(null):p!==d.PENDING&&v(new Error("no sdk instance available"))},[y,v,p]),i(()=>{if(!y)return;const r=C({sessionCreator:()=>y.createPayPalOneTimePaymentSession({orderId:l,savePayment:s,testBuyerCountry:c,...h}),failedSdkRef:g,sdkInstance:y,setError:v,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-payments" is not included in the SDK components array.'});if(!r)return;m.current=r;if("redirect"===e||"direct-app-switch"===e){(async()=>{try{if(!r)return;const e=r.hasReturned?.();e&&await(r.resume?.())}catch(e){v(e)}})()}return()=>{r.destroy()}},[y,l,h,e,v,s,c]);return{error:E,isPending:b,handleClick:o(async()=>{if(!f.current)return;if(!m.current)return void v(new Error("PayPal session not available"));const t={presentationMode:e,fullPageOverlay:r,autoRedirect:n};return await m.current.start(t,a?.())},[f,e,r,n,a,v]),handleCancel:k,handleDestroy:I}}const H=({type:r="pay",disabled:n=!1,...t})=>{const{error:a,isPending:o,handleClick:l}=U(t),{isHydrated:s}=P();return i(()=>{a&&console.error(a)},[a]),s?e.createElement("paypal-button",{onClick:l,type:r,disabled:!(!n&&!o&&null===a)||void 0}):e.createElement("div",null)};function q({presentationMode:e,fullPageOverlay:r,createSubscription:n,...a}){const{sdkInstance:l,loadingStatus:s}=P(),c=D(),u=t(null),y=S(a),[p,f]=w(),m=t(null),h=s===d.PENDING,E=o(()=>{u.current?.destroy(),u.current=null},[]),v=o(()=>{u.current?.cancel()},[]);i(()=>{m.current!==l&&(m.current=null),l?f(null):s!==d.PENDING&&f(new Error("no sdk instance available"))},[l,f,s]),i(()=>{if(!l)return;const e=C({sessionCreator:()=>l.createPayPalSubscriptionPaymentSession({...y}),failedSdkRef:m,sdkInstance:l,setError:f,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-subscriptions" is not included in the SDK components array.'});return e?(u.current=e,()=>{e.destroy()}):void 0},[l,y,f]);return{error:p,isPending:h,handleClick:o(async()=>{if(!c.current)return;if(!u.current)return void f(new Error("PayPal subscription session not available"));const t={presentationMode:e,fullPageOverlay:r};return await u.current.start(t,n())},[c,e,r,n,f]),handleCancel:v,handleDestroy:E}}const V=({type:r="subscribe",disabled:n=!1,...t})=>{const{error:a,isPending:o,handleClick:l}=q(t),{isHydrated:s}=P();return i(()=>{a&&console.error(a)},[a]),s?e.createElement("paypal-button",{onClick:l,type:r,disabled:!(!n&&!o&&null===a)||void 0}):e.createElement("div",null)},K=r(null),j="undefined"!=typeof window?s:i,x=({clientMetadataId:r,clientToken:n,clientId:o,components:s=["paypal-payments"],locale:p,pageType:f,partnerAttributionId:P,shopperSessionId:S,testBuyerCountry:b,eligibleMethodsResponse:C,children:I,merchantId:k,...T})=>{const N=function(e){const r=t(e);return function(e,r){if(!e&&!r)return!0;if(!e||!r)return!1;if(e.length!==r.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==r[n])return!1;return!0}(r.current,e)||(r.current=e),r.current}(s),R=g(k),[D,M]=a(null),[O,A]=c(h,m),[G,_]=a(!1),L=t({...T,dataSdkIntegrationSource:"react-paypal-js"});j(()=>{_(!0)},[]);const[,F]=w();i(()=>{let e=!0;return(async()=>{try{const r=await u({environment:L.current.environment,debug:L.current.debug,dataNamespace:L.current.dataNamespace,dataSdkIntegrationSource:L.current.dataSdkIntegrationSource});r&&e&&M(r)}catch(r){e&&(F(r),A({type:y.SET_ERROR,value:v(r)}))}})(),()=>{e=!1}},[F]),i(()=>{if(!D)return;if(!n&&!o)return;A({type:y.SET_LOADING_STATUS,value:d.PENDING});let e=!0;return(async()=>{try{const t=n||o,a=!!n;let i;if("string"==typeof t)i=t;else if(t)try{i=await t}catch(r){const n=new Error(`Failed to resolve ${a?"clientToken":"clientId"}. Expected a Promise that resolves to a string, but it was rejected with: ${v(r).message}`);return void(e&&(F(n),A({type:y.SET_ERROR,value:n})))}if(!i||!e)return;const l={clientMetadataId:r,components:N,locale:p,pageType:f,partnerAttributionId:P,shopperSessionId:S,testBuyerCountry:b,merchantId:R},s=a?{...l,clientToken:i}:{...l,clientId:i},c=await D.createInstance(s);if(!e)return;A({type:y.SET_INSTANCE,value:c})}catch(r){e&&(F(r),A({type:y.SET_ERROR,value:v(r)}))}})(),()=>{e=!1}},[r,n,o,p,N,R,f,P,D,S,b,F]),i(()=>{const e=O.sdkInstance;if(e)try{if(C){const r=e.hydrateEligibleMethods(C);A({type:y.SET_ELIGIBILITY,value:{eligiblePaymentMethods:r,payload:null}})}}catch(e){F(e),A({type:y.SET_ERROR,value:v(e)})}},[O.sdkInstance,C,F]);const B=l(()=>({sdkInstance:O.sdkInstance,eligiblePaymentMethods:O.eligiblePaymentMethods,eligiblePaymentMethodsPayload:O.eligiblePaymentMethodsPayload,error:O.error,loadingStatus:O.loadingStatus,isHydrated:G}),[O.sdkInstance,O.eligiblePaymentMethods,O.eligiblePaymentMethodsPayload,O.error,O.loadingStatus,G]);return e.createElement(K.Provider,{value:A},e.createElement(E.Provider,{value:B},I))},$={braintreePayPalCheckoutInstance:null,loadingStatus:d.PENDING,error:null,isHydrated:!1};function z(e,r){switch(r.type){case p.SET_LOADING_STATUS:return{...e,loadingStatus:r.value};case p.SET_INSTANCE:return{...e,braintreePayPalCheckoutInstance:r.value,loadingStatus:d.RESOLVED};case p.SET_ERROR:return{...e,error:r.value,loadingStatus:d.REJECTED};case p.RESET_STATE:return $;default:return e}}const Y=r(null),J=({namespace:r,braintreeClientToken:n,children:o})=>{const[s,u]=c(z,$),[y,m]=a(!1),[,h]=w(),E=t(null);j(()=>{m(!0)},[]),i(()=>{if(!f(r)){const e=new Error("Invalid Braintree namespace. Ensure the Braintree client and paypal-checkout-v6 scripts are loaded and that namespace.client.create and namespace.paypalCheckoutV6.create are functions.");return h(e),void u({type:p.SET_ERROR,value:e})}let e=!0;u({type:p.SET_LOADING_STATUS,value:d.PENDING});return(async()=>{if(!n){const r=new Error("Braintree client token is required to initialize the PayPal Checkout instance.");return void(e&&(h(r),u({type:p.SET_ERROR,value:r})))}try{const t=await r.client.create({authorization:n});if(!e)return;const a=await r.paypalCheckoutV6.create({client:t});if(!e)return;if(await a.loadPayPalSDK(),!e)return;E.current=a,u({type:p.SET_INSTANCE,value:a})}catch(r){e&&(h(r),u({type:p.SET_ERROR,value:v(r)}))}})(),()=>{e=!1,E.current?.teardown(),E.current=null}},[r,n,h]);const P=l(()=>({braintreePayPalCheckoutInstance:s.braintreePayPalCheckoutInstance,loadingStatus:s.loadingStatus,error:s.error,isHydrated:y}),[s.braintreePayPalCheckoutInstance,s.loadingStatus,s.error,y]);return e.createElement(Y.Provider,{value:P},o)};function W({presentationMode:e,fullPageOverlay:r,autoRedirect:n,createVaultToken:a,vaultSetupToken:l,...s}){const{sdkInstance:c,loadingStatus:u}=P(),y=D(),p=t(null),f=S(s),[m,h]=w(),E=t(null),v=u===d.PENDING,g=o(()=>{p.current?.destroy(),p.current=null},[]);i(()=>{E.current!==c&&(E.current=null),c?h(null):u!==d.PENDING&&h(new Error("no sdk instance available"))},[c,h,u]),i(()=>{if(!c)return;const r=C({sessionCreator:()=>c.createPayPalSavePaymentSession({vaultSetupToken:l,...f}),failedSdkRef:E,sdkInstance:c,setError:h,errorMessage:'Failed to create payment session. This may occur if the required component "paypal-payments" is not included in the SDK components array.'});if(!r)return;p.current=r;if("redirect"===e||"direct-app-switch"===e){(async()=>{try{if(!r)return;const e=r.hasReturned?.();e&&await(r.resume?.())}catch(e){h(e)}})()}return()=>{r.destroy()}},[c,l,f,e,h]);const b=o(()=>{p.current?.cancel()},[]);return{error:m,isPending:v,handleClick:o(async()=>{if(!y.current)return;if(!p.current)return void h(new Error("Save Payment session not available"));const t={presentationMode:e,fullPageOverlay:r,autoRedirect:n};a?await p.current.start(t,a()):await p.current.start(t)},[y,e,r,n,a,h]),handleCancel:b,handleDestroy:g}}const Q=({type:r="pay",disabled:n=!1,...t})=>{const{error:a,isPending:o,handleClick:l}=W(t),{isHydrated:s}=P();return i(()=>{a&&console.error(a)},[a]),s?e.createElement("paypal-button",{type:r,disabled:!(!n&&!o&&null===a)||void 0,onClick:l}):e.createElement("div",null)};function X({presentationMode:e,fullPageOverlay:r,createOrder:n,orderId:a,...l}){const{sdkInstance:s,loadingStatus:c}=P(),u=D(),y=t(null),p=S(l),[f,m]=w(),h=t(null),E=c===d.PENDING,v=o(()=>{y.current?.destroy(),y.current=null},[]);i(()=>{h.current!==s&&(h.current=null),s?m(null):c!==d.PENDING&&m(new Error("no sdk instance available"))},[s,m,c]),i(()=>{if(!s)return;const e=C({sessionCreator:()=>s.createVenmoOneTimePaymentSession({orderId:a,...p}),failedSdkRef:h,sdkInstance:s,setError:m,errorMessage:'Failed to create payment session. This may occur if the required component "venmo-payments" is not included in the SDK components array.'});return e?(y.current=e,()=>{e.destroy()}):void 0},[s,a,p,m]);return{error:f,isPending:E,handleCancel:o(()=>{y.current?.cancel()},[]),handleClick:o(async()=>{if(!u.current)return;if(!y.current)return void m(new Error("Venmo session not available"));const t={presentationMode:e,fullPageOverlay:r};await y.current.start(t,n?.())},[u,e,r,n,m]),handleDestroy:v}}const Z=({type:r="pay",disabled:n=!1,...t})=>{const{error:a,isPending:o,handleClick:l}=X(t),{isHydrated:s}=P();return i(()=>{a&&console.error(a)},[a]),s?e.createElement("venmo-button",{onClick:l,type:r,disabled:!(!n&&!o&&null===a)||void 0}):e.createElement("div",null)};function ee({applePayConfig:e,paymentRequest:r,displayName:n,domainName:a,createOrder:l,applePaySessionVersion:s,...c}){const{sdkInstance:u,loadingStatus:y}=P(),p=D(),f=t(null),m=t(null),h=S(c),[E,g]=w(),b=t(null),I=y===d.PENDING,k=o(()=>{if(m.current){try{m.current.abort()}catch{}m.current=null}},[]),T=o(()=>{k(),f.current=null},[k]);i(()=>{b.current!==u&&(b.current=null),u?g(null):y!==d.PENDING&&g(new Error("no sdk instance available"))},[u,g,y]),i(()=>{if(!u)return;const e=C({sessionCreator:()=>u.createApplePayOneTimePaymentSession(),failedSdkRef:b,sdkInstance:u,setError:g,errorMessage:'Failed to create payment session. This may occur if the required component "applepay-payments" is not included in the SDK components array.'});return e?(f.current=e,()=>{f.current=null}):void 0},[u,g]);return{error:E,isPending:I,handleClick:o(async()=>{if(!p.current)return;if(g(null),!f.current)return void g(new Error("Apple Pay session not available"));if("undefined"==typeof window||!window.ApplePaySession?.canMakePayments())return void g(new Error("Apple Pay is not available"));if("https:"!==window.location.protocol)return void g(new Error("Apple Pay requires a secure (HTTPS) connection"));const{ApplePaySession:t}=window;try{const o=f.current,i=o.formatConfigForPaymentRequest(e),c={...r,...i},u=new t(s,c);m.current=u,u.onvalidatemerchant=async e=>{try{const r=await o.validateMerchant({validationUrl:e.validationURL,...n&&{displayName:n},...a&&{domainName:a}});u.completeMerchantValidation(r.merchantSession)}catch(e){const r=v(e);g(r),h.onError?.(r),u.abort()}},u.onpaymentmethodselected=()=>{u.completePaymentMethodSelection({newTotal:r.total})},u.onpaymentauthorized=async e=>{try{const r=await l(),n=await o.confirmOrder({orderId:r.orderId,token:e.payment.token,billingContact:e.payment.billingContact,shippingContact:e.payment.shippingContact});u.completePayment({status:t.STATUS_SUCCESS}),await h.onApprove(n)}catch(e){const r=v(e);g(r),h.onError?.(r),u.completePayment({status:t.STATUS_FAILURE})}},u.oncancel=()=>{m.current=null,h.onCancel?.()},u.begin()}catch(e){const r=v(e);g(r),h.onError?.(r)}},[p,e,r,n,a,l,s,h,g]),handleCancel:k,handleDestroy:T}}const re={WebkitAppearance:"-apple-pay-button"},ne=({buttonstyle:r="black",type:n="pay",locale:a="en",disabled:o=!1,className:l,...s})=>{const{error:c,isPending:u,handleClick:d,handleDestroy:y}=ee(s),{isHydrated:p}=P(),f=t(null),m=t(d);m.current=d;const h=o||u;return i(()=>{const e=f.current;if(!e)return;const r=()=>{m.current().catch(()=>{})};return e.addEventListener("click",r),()=>e.removeEventListener("click",r)},[]),i(()=>{c&&console.error(c)},[c]),i(()=>()=>{y()},[y]),p?e.createElement("div",{className:l},e.createElement("apple-pay-button",{ref:f,buttonstyle:r,type:n,locale:a,disabled:h||void 0,style:re})):e.createElement("div",null)};function te(){const e=n(k);if(null===e)throw new Error("usePayPalCardFields must be used within a PayPalCardFieldsProvider");return e}function ae(){const e=n(I);if(null===e)throw new Error("usePayPalCardFieldsSession must be used within a PayPalCardFieldsProvider");return e}const oe=({containerStyles:r,containerClassName:n,...a})=>{const{cardFieldsSession:o,setError:l}=ae(),s=t(null),c=t(a);return i(()=>{if(!o)return;let e;c.current=a;try{e=o.createCardFieldsComponent(c.current),s.current?.appendChild(e)}catch(e){l(v(e))}return()=>{e?.remove(),e?.destroy()}},[o]),e.createElement("div",{style:r,className:n,ref:s})},ie=({containerStyles:r,containerClassName:n,placeholder:t,label:a,style:o,ariaDescription:i,ariaLabel:l,ariaInvalidErrorMessage:s})=>e.createElement(oe,{type:"number",containerStyles:r,containerClassName:n,placeholder:t,label:a,style:o,ariaDescription:i,ariaLabel:l,ariaInvalidErrorMessage:s}),le=({containerStyles:r,containerClassName:n,placeholder:t,label:a,style:o,ariaDescription:i,ariaLabel:l,ariaInvalidErrorMessage:s})=>e.createElement(oe,{type:"expiry",containerStyles:r,containerClassName:n,placeholder:t,label:a,style:o,ariaDescription:i,ariaLabel:l,ariaInvalidErrorMessage:s}),se=({containerStyles:r,containerClassName:n,placeholder:t,label:a,style:o,ariaDescription:i,ariaLabel:l,ariaInvalidErrorMessage:s})=>e.createElement(oe,{type:"cvv",containerStyles:r,containerClassName:n,placeholder:t,label:a,style:o,ariaDescription:i,ariaLabel:l,ariaInvalidErrorMessage:s});function ce(){const e=n(Y);if(null===e)throw new Error("useBraintreePayPal must be used within a BraintreePayPalProvider");return e}function ue({onApprove:e,onCancel:r,onError:n,onShippingAddressChange:a,onShippingOptionsChange:l,amount:s,currency:c,intent:u,commit:y,offerCredit:p,userAuthenticationEmail:f,returnUrl:m,cancelUrl:h,displayName:E,presentationMode:P,lineItems:v,shippingOptions:b,amountBreakdown:I}){const{braintreePayPalCheckoutInstance:k,loadingStatus:T}=ce(),N=D(),R=t(null),[M,O]=w(),A=t(null),G=S({onApprove:e,onCancel:r,onError:n,onShippingAddressChange:a,onShippingOptionsChange:l}),_=g(v),L=g(b),F=g(I),B=T===d.PENDING;i(()=>{A.current!==k&&(A.current=null),k?O(null):T!==d.PENDING&&O(new Error("Braintree checkout instance not available"))},[k,O,T]),i(()=>{if(!k)return;const e=C({sessionCreator:()=>k.createOneTimePaymentSession({amount:s,currency:c,intent:u,commit:y,offerCredit:p,userAuthenticationEmail:f,returnUrl:m,cancelUrl:h,displayName:E,presentationMode:P,lineItems:_,shippingOptions:L,amountBreakdown:F,...G}),failedSdkRef:A,sdkInstance:k,setError:O,errorMessage:"Failed to create Braintree payment session. Ensure the BraintreePayPalProvider is properly initialized with a valid client token and namespace."});return e?(R.current=e,()=>{R.current=null}):void 0},[k,s,c,u,y,p,f,m,h,E,P,_,L,F,G,O]);return{error:M,isPending:B,handleClick:o(()=>{N.current&&(R.current?R.current.start():O(new Error("Braintree payment session not available")))},[N,O])}}function de(e={}){const{payload:r}=e,{sdkInstance:o,eligiblePaymentMethods:l,eligiblePaymentMethodsPayload:s,error:c}=P(),u=function(){const e=n(K);if(null===e)throw new Error("usePayPalDispatch must be used within a PayPalProvider");return e}(),[d,p]=w(),[f,m]=a(!1),h=t(l),E=t(s);h.current=l,E.current=s;const S=g(r),v=t(null);i(()=>{if(!o)return;if(v.current?.instance===o&&v.current?.payload===S)return;if(h.current&&null===v.current&&b(E.current,S))return void(v.current={instance:o,payload:S});v.current={instance:o,payload:S};let e=!0;return m(!0),o.findEligibleMethods(S).then(r=>{e&&u({type:y.SET_ELIGIBILITY,value:{eligiblePaymentMethods:r,payload:S}})}).catch(r=>{e&&p(r)}).finally(()=>{e&&m(!1)}),()=>{e=!1,v.current=null}},[o,S,u,p]);const C=!!l&&!b(s??void 0,S??void 0),I=f||!l&&!d||C;return c?{eligiblePaymentMethods:l,isLoading:I,error:new Error(`PayPal context error: ${c}`)}:{eligiblePaymentMethods:l,isLoading:I,error:d}}function ye({buyerCountry:e,currencyCode:r,shopperSessionId:n}){const{sdkInstance:t,loadingStatus:l}=P(),s=D(),[c,u]=a(null),[y,p]=w();i(()=>{t?p(null):l!==d.PENDING&&p(new Error("no sdk instance available"))},[t,p,l]),i(()=>{if(!t)return;const a=t.createPayPalMessages({buyerCountry:e,currencyCode:r,shopperSessionId:n});return u(a),()=>{u(null)}},[e,r,t,n]);const f=o(async e=>{if(!s.current)return;if(!c)return void p(new Error("PayPal Messages session not available"));const r=await c.fetchContent(e);if(null!==r)return r;p(new Error("Failed to fetch PayPal Messages content"))},[s,c,p]),m=o(e=>{if(s.current){if(c)return c.createLearnMore(e);p(new Error("PayPal Messages session not available"))}},[s,c,p]);return{error:y,isReady:Boolean(c),handleCreateLearnMore:m,handleFetchContent:f}}function pe(){const{cardFieldsSession:e,setCardFieldsSessionType:r}=ae(),[n,t]=a(null),[l,s]=w();i(()=>{r(T)},[r]);const c=o(async(r,n)=>{if(!e)return s(v("Submit error: CardFields session not available")),void t(null);try{const a=await r,o=await e.submit(a,n);t(o),s(null)}catch(e){s(v(e)),t(null)}},[e,s]);return{submit:c,submitResponse:n,error:l}}function fe(){const{cardFieldsSession:e,setCardFieldsSessionType:r}=ae(),[n,t]=a(null),[l,s]=w();i(()=>{r(N)},[r]);const c=o(async(r,n)=>{if(!e)return s(v("Submit error: CardFields session not available")),void t(null);try{const a=await r,o=await e.submit(a,n);t(o),s(null)}catch(e){s(v(e)),t(null)}},[e,s]);return{submit:c,submitResponse:n,error:l}}export{ne as ApplePayOneTimePaymentButton,p as BRAINTREE_DISPATCH_ACTION,J as BraintreePayPalProvider,y as INSTANCE_DISPATCH_ACTION,d as INSTANCE_LOADING_STATE,O as PayLaterOneTimePaymentButton,se as PayPalCardCvvField,le as PayPalCardExpiryField,R as PayPalCardFieldsProvider,ie as PayPalCardNumberField,G as PayPalCreditOneTimePaymentButton,L as PayPalCreditSavePaymentButton,B as PayPalGuestPaymentButton,H as PayPalOneTimePaymentButton,x as PayPalProvider,Q as PayPalSavePaymentButton,V as PayPalSubscriptionButton,Z as VenmoOneTimePaymentButton,ee as useApplePayOneTimePaymentSession,ce as useBraintreePayPal,ue as useBraintreePayPalOneTimePaymentSession,de as useEligibleMethods,M as usePayLaterOneTimePaymentSession,P as usePayPal,te as usePayPalCardFields,pe as usePayPalCardFieldsOneTimePaymentSession,fe as usePayPalCardFieldsSavePaymentSession,A as usePayPalCreditOneTimePaymentSession,_ as usePayPalCreditSavePaymentSession,F as usePayPalGuestPaymentSession,ye as usePayPalMessages,U as usePayPalOneTimePaymentSession,W as usePayPalSavePaymentSession,q as usePayPalSubscriptionPaymentSession,X as useVenmoOneTimePaymentSession,f as validateBraintreeNamespace};