import{r as t,c as n,h as r,g as e}from"./p-b891c2f1.js";import{_ as i}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{a as o}from"./p-6fd3642a.js";import{h as s}from"./p-eaac2001.js";import"./p-112455b1.js";import"./p-1c2e2695.js";
/*!
 * paypal-js v5.0.4 (2022-04-06T22:09:33.042Z)
 * 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
 *
 * http://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.
 */function a(t,n){void 0===n&&(n={});var r=document.createElement("script");return r.src=t,Object.keys(n).forEach((function(t){r.setAttribute(t,n[t]),"data-csp-nonce"===t&&r.setAttribute("nonce",n["data-csp-nonce"])})),r}function c(t,n){if(void 0===n&&(n=u()),d(t,n),"undefined"==typeof window)return n.resolve(null);var r=function(t){var n="https://www.paypal.com/sdk/js";t.sdkBaseURL&&(n=t.sdkBaseURL,delete t.sdkBaseURL),function(t){var n=t["merchant-id"],r=t["data-merchant-id"],e="",i="";Array.isArray(n)?n.length>1?(e="*",i=n.toString()):e=n.toString():"string"==typeof n&&n.length>0?e=n:"string"==typeof r&&r.length>0&&(e="*",i=r),t["merchant-id"]=e,t["data-merchant-id"]=i}(t);var r,e,i=Object.keys(t).filter((function(n){return null!=t[n]&&""!==t[n]})).reduce((function(n,r){var e=t[r].toString();return"data-"===r.substring(0,5)?n.dataAttributes[r]=e:n.queryParams[r]=e,n}),{queryParams:{},dataAttributes:{}}),o=i.queryParams,s=i.dataAttributes;return{url:"".concat(n,"?").concat((r=o,e="",Object.keys(r).forEach((function(t){0!==e.length&&(e+="&"),e+=t+"="+r[t]})),e)),dataAttributes:s}}(t),e=r.url,i=r.dataAttributes,o=i["data-namespace"]||"paypal",s=l(o);return function(t,n){var r=document.querySelector('script[src="'.concat(t,'"]'));if(null===r)return null;var e=a(t,n),i=r.cloneNode();if(delete i.dataset.uidAuto,Object.keys(i.dataset).length!==Object.keys(e.dataset).length)return null;var o=!0;return Object.keys(i.dataset).forEach((function(t){i.dataset[t]!==e.dataset[t]&&(o=!1)})),o?r:null}(e,i)&&s?n.resolve(s):function(t,n){void 0===n&&(n=u()),d(t,n);var r=t.url,e=t.attributes;if("string"!=typeof r||0===r.length)throw new Error("Invalid url.");if(void 0!==e&&"object"!=typeof e)throw new Error("Expected attributes to be an object.");return new n((function(t,n){if("undefined"==typeof window)return t();!function(t){var n=t.onSuccess,r=t.onError,e=a(t.url,t.attributes);e.onerror=r,e.onload=n,document.head.insertBefore(e,document.head.firstElementChild)}({url:r,attributes:e,onSuccess:function(){return t()},onError:function(){var t=new Error('The script "'.concat(r,'" failed to load.'));return window.fetch?fetch(r).then((function(r){return 200===r.status&&n(t),r.text()})).then((function(t){var r=function(t){var n=t.split("/* Original Error:")[1];return n?n.replace(/\n/g,"").replace("*/","").trim():t}(t);n(new Error(r))})).catch((function(t){n(t)})):n(t)}})}))}({url:e,attributes:i},n).then((function(){var t=l(o);if(t)return t;throw new Error("The window.".concat(o," global variable is not available."))}))}function u(){if("undefined"==typeof Promise)throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill.");return Promise}function l(t){return window[t]}function d(t,n){if("object"!=typeof t||null===t)throw new Error("Expected an options object.");if(void 0!==n&&"function"!=typeof n)throw new Error("Expected PromisePonyfill to be a function.")}const h=class{constructor(r){t(this,r),this.scError=n(this,"scError",7),this.scSetState=n(this,"scSetState",7),this.scPaid=n(this,"scPaid",7),this.busy=!1,this.buttons=["paypal","card"],this.label="paypal",this.color="gold"}handleOrderChange(){this.cardContainer.innerHTML="",this.paypalContainer.innerHTML="",this.loadScript()}async loadScript(){var t;if(!this.clientId||!this.merchantId)return;const n=s(this.order);try{const r=await c({"client-id":this.clientId.replace(/ /g,""),...n?{}:{"merchant-id":this.merchantId.replace(/ /g,"")},commit:!1,intent:n?"tokenize":"capture",vault:n,currency:(null===(t=this.order)||void 0===t?void 0:t.currency.toUpperCase())||"USD"});this.renderButtons(r)}catch(t){console.error("failed to load the PayPal JS SDK script",t)}}componentDidLoad(){this.loadScript()}renderButtons(t){const n={onClick:async(t,n)=>{const r=this.el.closest("sc-checkout");return await r.validate()?n.resolve():n.reject()},onInit:()=>{this.loaded=!0},onCancel:()=>{this.scSetState.emit("REJECT")},onApprove:async()=>{var t,n;try{const r=await o({method:"PATCH",path:`surecart/v1/payment_intents/${null===(n=null===(t=this.order)||void 0===t?void 0:t.payment_intent)||void 0===n?void 0:n.id}/capture`});["succeeded","pending","requires_approval"].includes(null==r?void 0:r.status)?this.scPaid.emit():(this.scError.emit({code:"could_not_capture",message:i("The payment did not process. Please try again.","surecart")}),this.scSetState.emit("REJECT"))}catch(t){console.error(t),this.scError.emit({code:"could_not_capture",message:i("The payment did not process. Please try again.","surecart")}),this.scSetState.emit("REJECT")}},onError:t=>{this.scError.emit({code:null==t?void 0:t.code,message:null==t?void 0:t.message}),this.scSetState.emit("REJECT")}};if(n[s(this.order)?"createBillingAgreement":"createOrder"]=async()=>new Promise((async(t,n)=>{var r,e;const o=this.el.closest("sc-checkout"),s=await o.submit();return s instanceof Error?n(s):"finalized"!==(null==s?void 0:s.status)?n(new Error("Something went wrong. Please try again.")):(null===(r=null==s?void 0:s.payment_intent)||void 0===r?void 0:r.external_intent_id)?t(null===(e=null==s?void 0:s.payment_intent)||void 0===e?void 0:e.external_intent_id):(this.scError.emit({code:"missing_payment_intent",message:i("Something went wrong. Please contact us for payment.","surecart")}),n())})),t.FUNDING.PAYPAL){const r=t.Buttons({fundingSource:t.FUNDING.PAYPAL,style:{label:this.label,color:this.color},...n});r.isEligible()&&r.render(this.paypalContainer)}if(t.FUNDING.CARD){const r=t.Buttons({fundingSource:t.FUNDING.CARD,style:{color:"black"},...n});r.isEligible()&&r.render(this.cardContainer)}}render(){return r("div",{class:{"paypal-buttons":!0,"paypal-buttons--busy":this.busy}},(!this.loaded||this.busy)&&r("sc-skeleton",{style:{height:"55px","--border-radius":"4px",cursor:"wait"}}),r("div",{class:"sc-paypal-button-container",hidden:!this.loaded||this.busy},r("div",{hidden:!this.buttons.includes("card"),class:"sc-paypal-card-button",ref:t=>this.cardContainer=t}),r("div",{hidden:!this.buttons.includes("paypal"),class:"sc-paypal-button",ref:t=>this.paypalContainer=t})))}get el(){return e(this)}static get watchers(){return{order:["handleOrderChange"]}}};h.style=":host{display:block}.paypal-buttons{position:relative}";export{h as sc_paypal_buttons}