import{r as e,h as t,H as a}from"./p-9add63ef.js";import"./p-8dccb656.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(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:s}=(t===null||t===void 0?void 0:t.razorpay)||{};if(!a||!e)return;this.confirming=true;try{if(!this.razorpayInstance){this.razorpayInstance=await i()}const t={key:a,order_id:e,customer_id:s,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 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=((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:"c9606ab6db81d0059ab6383e004d9e6c9cb50c47"},this.error&&t("sc-alert",{key:"f056e5066ce66d0c1de6277ffb6ae671241be0e1",open:!!this.error,type:"danger"},t("span",{key:"d8bcb110dd88dda93a1b04b64495f453259e92c7",slot:"title"},wp.i18n.__("Error","surecart")),this.error),t("div",{key:"14c88762bdadec6781bddc7d8f7b8e24a0d4e13e",class:"sc-razorpay-button-container"},t("sc-alert",{key:"32ef29c4c162b2599dbe03ce61594d94f7c926c8",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:"f05bfba9552d0e29a2a3890d78cbda7433c7c753"},t("sc-button",{key:"c7d5a629434c7000d31cc1c54add2676d01a73a4",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-c33bf660.entry.js.map