JezK
Edit File: sc-razorpay-add-method.js
import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import"./fetch.js";import{l as loadRazorpay}from"./razorpay.js";import{d as defineCustomElement$5}from"./sc-alert2.js";import{d as defineCustomElement$4}from"./sc-button2.js";import{d as defineCustomElement$3}from"./sc-icon2.js";import{d as defineCustomElement$2}from"./sc-spinner2.js";import{a as apiFetch}from"./index4.js";const scRazorpayAddMethodCss=".sc-razorpay-button-container{display:block}",ScRazorpayAddMethodStyle0=scRazorpayAddMethodCss,ScRazorpayAddMethod$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.razorpayInstance=null,this.confirming=!1,this.liveMode=!0,this.customerId=void 0,this.successUrl=void 0,this.currency=void 0,this.loading=void 0,this.loaded=void 0,this.error=void 0,this.paymentIntent=void 0}async handlePaymentIntentCreate(){if(this.confirming)return;const{external_intent_id:e,processor_data:t}=this.paymentIntent||{},{public_key:r,customer_id:n}=(null==t?void 0:t.razorpay)||{};if(r&&e){this.confirming=!0;try{this.razorpayInstance||(this.razorpayInstance=await loadRazorpay());const t={key:r,order_id:e,customer_id:n,recurring:!0,handler:async e=>{(null==e?void 0:e.razorpay_payment_id)?window.location.assign(this.successUrl):(this.error=wp.i18n.__("Payment verification failed. Please contact support.","surecart"),this.loading=!1)},modal:{ondismiss:()=>{this.loading=!1}}},a=new this.razorpayInstance(t);a.on("payment.failed",(e=>{var t;this.error=(null===(t=null==e?void 0:e.error)||void 0===t?void 0:t.description)||wp.i18n.__("Payment failed. Please try again.","surecart"),this.loading=!1,console.error("payment.failed",e)})),a.open()}catch(e){this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart"),this.loading=!1,console.error(e)}finally{this.confirming=!1}}}async createPaymentIntent(){var e,t;try{this.loading=!0,this.error="",this.paymentIntent=await apiFetch({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"razorpay",reusable:!0,live_mode:this.liveMode,customer_id:this.customerId,currency:this.currency,refresh_status:!0}})}catch(r){console.error(r),this.error=(null===(t=null===(e=null==r?void 0:r.additional_errors)||void 0===e?void 0:e[0])||void 0===t?void 0:t.message)||(null==r?void 0:r.message)||wp.i18n.__("Something went wrong","surecart"),this.loading=!1}}render(){return h(Host,{key:"c9606ab6db81d0059ab6383e004d9e6c9cb50c47"},this.error&&h("sc-alert",{key:"f056e5066ce66d0c1de6277ffb6ae671241be0e1",open:!!this.error,type:"danger"},h("span",{key:"d8bcb110dd88dda93a1b04b64495f453259e92c7",slot:"title"},wp.i18n.__("Error","surecart")),this.error),h("div",{key:"14c88762bdadec6781bddc7d8f7b8e24a0d4e13e",class:"sc-razorpay-button-container"},h("sc-alert",{key:"32ef29c4c162b2599dbe03ce61594d94f7c926c8",open:!0,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"),h("div",{key:"f05bfba9552d0e29a2a3890d78cbda7433c7c753"},h("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"]}}static get style(){return ScRazorpayAddMethodStyle0}},[0,"sc-razorpay-add-method",{liveMode:[4,"live-mode"],customerId:[1,"customer-id"],successUrl:[1,"success-url"],currency:[1],loading:[32],loaded:[32],error:[32],paymentIntent:[32]},void 0,{paymentIntent:["handlePaymentIntentCreate"]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-razorpay-add-method","sc-alert","sc-button","sc-icon","sc-spinner"].forEach((e=>{switch(e){case"sc-razorpay-add-method":customElements.get(e)||customElements.define(e,ScRazorpayAddMethod$1);break;case"sc-alert":customElements.get(e)||defineCustomElement$5();break;case"sc-button":customElements.get(e)||defineCustomElement$4();break;case"sc-icon":customElements.get(e)||defineCustomElement$3();break;case"sc-spinner":customElements.get(e)||defineCustomElement$2()}}))}const ScRazorpayAddMethod=ScRazorpayAddMethod$1,defineCustomElement=defineCustomElement$1;export{ScRazorpayAddMethod,defineCustomElement};