import{r as e,h as t,H as i}from"./p-e97fde0a.js";import{a}from"./p-dd3464cd.js";import{l as s}from"./p-d4e54db7.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:i,customer_id:a}=(t===null||t===void 0?void 0:t.razorpay)||{};if(!i||!e)return;this.confirming=true;try{if(!this.razorpayInstance){this.razorpayInstance=await s()}const t={key:i,order_id:e,customer_id:a,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 a({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(i){console.error(i);this.error=((t=(e=i===null||i===void 0?void 0:i.additional_errors)===null||e===void 0?void 0:e[0])===null||t===void 0?void 0:t.message)||(i===null||i===void 0?void 0:i.message)||wp.i18n.__("Something went wrong","surecart");this.loading=false}}render(){return t(i,{key:"ebafd5a3210bb2a7b2f7fc0a3411cb12f02b5479"},this.error&&t("sc-alert",{key:"0fa2906acf2b6ff355b888817eb0b6bd1e020b35",open:!!this.error,type:"danger"},t("span",{key:"b7c9714e15f20b86b1c4e9219e6360c84d309664",slot:"title"},wp.i18n.__("Error","surecart")),this.error),t("div",{key:"bf5cf63be0fbcf1e3f3f428f969e7f112f3b129b",class:"sc-razorpay-button-container"},t("sc-alert",{key:"243b0c714036125f5fb31329316931808fbdb1ad",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:"a67806699bda00e780b396ea3e7061c5f44d33f6"},t("sc-button",{key:"017eb9b8777cfcd04203ee4c8e29d8cabc272dd0",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-2bf867cf.entry.js.map