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-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: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:"d434b1ece99a3d7ed5b0b005472aef82a3cabd80"},this.error&&t("sc-alert",{key:"fed161ca2343f5b9db5df598d3b9f0a739eeeefc",open:!!this.error,type:"danger"},t("span",{key:"9a9086dad9d14ecb5eba654eb5c2e9b4cbcceea4",slot:"title"},wp.i18n.__("Error","surecart")),this.error),t("div",{key:"7cca93f810ddd4706807907146f34dd7502e9d4f",class:"sc-razorpay-button-container"},t("sc-alert",{key:"00e86de696c7a24e265f6fbc3aff51af6a25d358",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:"882ef2bb8bb966765f4453f2ddf021c8cb8fe3ef"},t("sc-button",{key:"95769fd51f0955a1022aec4c0bd516aa2b105399",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-0f11b177.entry.js.map