import{r as t,h as e,H as a}from"./p-dfbe004d.js";import{l as i,g as s}from"./p-d83e58e6.js";import{_ as n}from"./p-8415f806.js";import"./p-fd56940f.js";import{a as r}from"./p-7393a06c.js";import"./p-9a7a47e8.js";import"./p-1c2e2695.js";const o=class{constructor(e){t(this,e),this.liveMode=!0}componentWillLoad(){this.createPaymentIntent()}async handlePaymentIntentCreate(){var t,e;const{external_intent_id:a}=this.paymentIntent||{},{client_id:o,account_id:l,merchant_initiated:c}=(null===(e=null===(t=this.paymentIntent)||void 0===t?void 0:t.processor_data)||void 0===e?void 0:e.paypal)||{};if(o&&l&&a&&!this.paypal)try{this.paypal=await i(s({clientId:o,merchantId:l,merchantInitiated:c,reusable:!0})),this.paypal.Buttons({onInit:()=>{this.loaded=!0},createBillingAgreement:()=>new Promise((t=>t(a))),onApprove:async()=>{var t;try{this.loading=!0;const e=await r({method:"PATCH",path:`surecart/v1/payment_intents/${null===(t=this.paymentIntent)||void 0===t?void 0:t.id}/capture`});if(!["succeeded","pending","requires_approval"].includes(null==e?void 0:e.status))throw{message:n("The payment did not process. Please try again.","surecart")};window.location.assign(this.successUrl)}catch(t){console.error(t),this.error=(null==t?void 0:t.message)||n("The payment did not process. Please try again.","surecart"),this.loading=!1}},onError:t=>{console.error(t),alert((null==t?void 0:t.message)||n("The payment did not process. Please try again.","surecart"))}}).render(this.container)}catch(t){console.error("Failed to load the PayPal JS SDK script",t),this.error=n("Failed to load the PayPal JS SDK script","surecart")}}async createPaymentIntent(){var t,e;try{this.loading=!0,this.error="",this.paymentIntent=await r({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"paypal",reusable:!0,live_mode:this.liveMode,customer_id:this.customerId,currency:this.currency}})}catch(a){this.error=(null===(e=null===(t=null==a?void 0:a.additional_errors)||void 0===t?void 0:t[0])||void 0===e?void 0:e.message)||(null==a?void 0:a.message)||n("Something went wrong","surecart")}finally{this.loading=!1}}render(){return e(a,null,this.error&&e("sc-alert",{open:!!this.error,type:"danger"},e("span",{slot:"title"},n("Error","surecart")),this.error),e("div",{class:"sc-paypal-button-container",hidden:!this.loaded,ref:t=>this.container=t}))}static get watchers(){return{paymentIntent:["handlePaymentIntentCreate"]}}};o.style=':host{display:block}.paypal-buttons{position:relative;line-height:0;text-align:center}.paypal-buttons:after{content:" ";border-bottom:1px solid var(--sc-input-border-color);width:100%;height:0;top:50%;left:0;right:0;position:absolute}';export{o as sc_paypal_add_method}