import{r as e,h as t,H as a}from"./p-e97fde0a.js";import{a as i}from"./p-dd3464cd.js";import{l as s}from"./p-a1377337.js";import"./p-401e165e.js";import"./p-d3366af3.js";const n=".sc-razorpay-button-container{display:block}";const r=n;const d=class{constructor(t){e(this,t);this.razorpayInstance=null;this.confirming=false;this.liveMode=true;this.customerId=undefined;this.successUrl=undefined;this.currency=undefined;this.loading=undefined;this.loaded=undefined;this.error=undefined;this.paymentIntent=undefined}async handlePaymentIntentCreate(){if(this.confirming)return;const{external_intent_id:e,processor_data:t}=this.paymentIntent||{};const{public_key:a,customer_id:i}=(t===null||t===void 0?void 0:t.razorpay)||{};if(!a||!e)return;this.confirming=true;try{if(!this.razorpayInstance){this.razorpayInstance=await s()}const t={key:a,order_id:e,customer_id:i,recurring:true,handler:async e=>{if(e===null||e===void 0?void 0:e.razorpay_payment_id){window.location.assign(this.successUrl)}else{this.error=wp.i18n.__("Payment verification failed. Please contact support.","surecart");this.loading=false}},modal:{ondismiss:()=>{this.loading=false}}};const n=new this.razorpayInstance(t);n.on("payment.failed",(e=>{var t;this.error=((t=e===null||e===void 0?void 0:e.error)===null||t===void 0?void 0:t.description)||wp.i18n.__("Payment failed. Please try again.","surecart");this.loading=false;console.error("payment.failed",e)}));n.open()}catch(e){this.error=(e===null||e===void 0?void 0:e.message)||wp.i18n.__("Something went wrong","surecart");this.loading=false;console.error(e)}finally{this.confirming=false}}async createPaymentIntent(){var e,t;try{this.loading=true;this.error="";this.paymentIntent=await i({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"razorpay",reusable:true,live_mode:this.liveMode,customer_id:this.customerId,currency:this.currency,refresh_status:true}})}catch(a){console.error(a);this.error=((t=(e=a===null||a===void 0?void 0:a.additional_errors)===null||e===void 0?void 0:e[0])===null||t===void 0?void 0:t.message)||(a===null||a===void 0?void 0:a.message)||wp.i18n.__("Something went wrong","surecart");this.loading=false}}render(){return t(a,{key:"8a0f713c4fe959aa504fbe65506872c9736641d3"},this.error&&t("sc-alert",{key:"8fbda4390b9510a1cd051698c54c0d9ed5578669",open:!!this.error,type:"danger"},t("span",{key:"af6fab9c1b997ed63af403274ccb8e8d80868e2f",slot:"title"},wp.i18n.__("Error","surecart")),this.error),t("div",{key:"a542db5d779b0ec4df1dd67c187a6b53fa8f5c64",class:"sc-razorpay-button-container"},t("sc-alert",{key:"ba03bef8745326b78b251fd7255b560d6cfc9738",open:true,type:"warning"},wp.i18n.__("In order to add a new card, we will need to make a small transaction to authenticate it. This is for authentication purposes and will be immediately refunded.","surecart"),t("div",{key:"2d3e0e6274f9897f00ba2fde00f18d167e2f4b9f"},t("sc-button",{key:"323f54355cf998d51536aaa278a33ea1697b3b7a",loading:this.loading,type:"primary",onClick:()=>this.createPaymentIntent(),style:{marginTop:"var(--sc-spacing-medium)"}},wp.i18n.__("Add New Card","surecart"))))))}static get watchers(){return{paymentIntent:["handlePaymentIntentCreate"]}}};d.style=r;export{d as sc_razorpay_add_method};
//# sourceMappingURL=p-bf961523.entry.js.map