import{r as t,h as e,H as a}from"./p-9add63ef.js";import"./p-445fd8b4.js";import{l as i}from"./p-a1377337.js";import{a as s}from"./p-259c3935.js";import"./p-401e165e.js";import"./p-d3366af3.js";const n=".sc-razorpay-button-container{display:block}";const r=n;const d=class{constructor(e){t(this,e);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:t,processor_data:e}=this.paymentIntent||{};const{public_key:a,customer_id:s}=(e===null||e===void 0?void 0:e.razorpay)||{};if(!a||!t)return;this.confirming=true;try{if(!this.razorpayInstance){this.razorpayInstance=await i()}const e={key:a,order_id:t,customer_id:s,recurring:true,handler:async t=>{if(t===null||t===void 0?void 0:t.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(e);n.on("payment.failed",(t=>{var e;this.error=((e=t===null||t===void 0?void 0:t.error)===null||e===void 0?void 0:e.description)||wp.i18n.__("Payment failed. Please try again.","surecart");this.loading=false;console.error("payment.failed",t)}));n.open()}catch(t){this.error=(t===null||t===void 0?void 0:t.message)||wp.i18n.__("Something went wrong","surecart");this.loading=false;console.error(t)}finally{this.confirming=false}}async createPaymentIntent(){var t,e;try{this.loading=true;this.error="";this.paymentIntent=await s({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=((e=(t=a===null||a===void 0?void 0:a.additional_errors)===null||t===void 0?void 0:t[0])===null||e===void 0?void 0:e.message)||(a===null||a===void 0?void 0:a.message)||wp.i18n.__("Something went wrong","surecart");this.loading=false}}render(){return e(a,{key:"c797b31b9a7625ab18a85a84043cd3d08d2502c5"},this.error&&e("sc-alert",{key:"188d3fc4f2d09898b51c09a1fe6937a375208c5a",open:!!this.error,type:"danger"},e("span",{key:"f5c32c2b5dd77b97a10a382be95dc2cfba6d6e7d",slot:"title"},wp.i18n.__("Error","surecart")),this.error),e("div",{key:"d03cd5a52a87e3619356f720c671a32c90a4c177",class:"sc-razorpay-button-container"},e("sc-alert",{key:"7e381208deab7bb0fb2d53d77bd3ba66526095a8",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"),e("div",{key:"d5bdd1c1e62773c4111a0834a65b8f9988a96e62"},e("sc-button",{key:"d1fd503f1297bc40db29896c3051471a67a613f6",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-aa1eeef5.entry.js.map