import{r as t,h as e,H as i}from"./p-9add63ef.js";import{l as a,g as s}from"./p-a0974978.js";import"./p-445fd8b4.js";import{a as n}from"./p-259c3935.js";import"./p-401e165e.js";import"./p-d3366af3.js";const r=':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}';const o=r;const d=class{constructor(e){t(this,e);this.liveMode=true;this.customerId=undefined;this.successUrl=undefined;this.currency=undefined;this.loading=undefined;this.loaded=undefined;this.error=undefined;this.paymentIntent=undefined}componentWillLoad(){this.createPaymentIntent()}async handlePaymentIntentCreate(){var t,e,i;const{external_intent_id:r}=this.paymentIntent||{};const{client_id:o,account_id:d,merchant_initiated:l}=((e=(t=this.paymentIntent)===null||t===void 0?void 0:t.processor_data)===null||e===void 0?void 0:e.paypal)||{};if(!o||!d||!r)return;if(!this.paypal){try{this.paypal=await a(s({clientId:o,merchantId:d,merchantInitiated:l,reusable:true,locale:(i=window.scData)===null||i===void 0?void 0:i.locale}));this.paypal.Buttons({onInit:()=>{this.loaded=true},createBillingAgreement:()=>new Promise((t=>t(r))),onApprove:async()=>{var t;try{this.loading=true;const e=await n({method:"PATCH",path:`surecart/v1/payment_intents/${(t=this.paymentIntent)===null||t===void 0?void 0:t.id}/capture`});if(["succeeded","pending","requires_approval"].includes(e===null||e===void 0?void 0:e.status)){window.location.assign(this.successUrl)}else{throw{message:wp.i18n.__("The payment did not process. Please try again.","surecart")}}}catch(t){console.error(t);this.error=(t===null||t===void 0?void 0:t.message)||wp.i18n.__("The payment did not process. Please try again.","surecart");this.loading=false}},onError:t=>{console.error(t);alert((t===null||t===void 0?void 0:t.message)||wp.i18n.__("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=wp.i18n.__("Failed to load the PayPal JS SDK script","surecart")}}}async createPaymentIntent(){var t,e;try{this.loading=true;this.error="";this.paymentIntent=await n({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"paypal",reusable:true,live_mode:this.liveMode,customer_id:this.customerId,currency:this.currency,refresh_status:true}})}catch(i){this.error=((e=(t=i===null||i===void 0?void 0:i.additional_errors)===null||t===void 0?void 0:t[0])===null||e===void 0?void 0:e.message)||(i===null||i===void 0?void 0:i.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.loading=false}}render(){return e(i,{key:"db8effccee3b35e7143c3b686d980903fd3caf04"},this.error&&e("sc-alert",{key:"a2294356757ab5da26099163c5f4288975b7e08b",open:!!this.error,type:"danger"},e("span",{key:"76e95576ab77bfb9f273b7ec23631b48e2247dbd",slot:"title"},wp.i18n.__("Error","surecart")),this.error),e("div",{key:"d9ad5e019e93b560f6161d404b53fa7027dc9e8b",class:"sc-paypal-button-container",hidden:!this.loaded,ref:t=>this.container=t}))}static get watchers(){return{paymentIntent:["handlePaymentIntentCreate"]}}};d.style=o;export{d as sc_paypal_add_method};
//# sourceMappingURL=p-63cd8751.entry.js.map