JezK
Edit File: sc-checkout-razorpay-payment-provider2.js
import{proxyCustomElement,HTMLElement}from"@stencil/core/internal/client";import{s as state$2}from"./mutations2.js";import{l as listenTo}from"./functions.js";import{l as lockCheckout,b as unLockCheckout}from"./mutations4.js";import"./watchers4.js";import{s as state}from"./getters5.js";import{s as state$1}from"./watchers5.js";import{o as onChange}from"./store4.js";import{c as currentFormState}from"./getters3.js";import{u as updateFormState}from"./mutations5.js";import{c as createErrorNotice}from"./mutations3.js";import{l as loadRazorpay}from"./razorpay.js";import"./fetch.js";import{a as addQueryArgs}from"./add-query-args.js";import{a as apiFetch}from"./index4.js";const ScCheckoutRazorpayPaymentProvider=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.razorpayInstance=null,this.confirming=!1,this.processorId=void 0}componentWillLoad(){loadRazorpay().then((t=>this.razorpayInstance=t)).catch((t=>createErrorNotice({message:t.message}))),this.unlistenToFormState=onChange("formState",(()=>{"paying"===currentFormState()&&this.confirm()})),this.fetchMethods(),this.unlistenToCheckout=listenTo("checkout",["currency","reusable_payment_method_required"],(()=>this.fetchMethods()))}disconnectedCallback(){var t,e;null===(t=this.unlistenToFormState)||void 0===t||t.call(this),null===(e=this.unlistenToCheckout)||void 0===e||e.call(this),state.methods=[]}async fetchMethods(){const t=state$2.checkout;if(this.processorId&&(null==t?void 0:t.currency))if(null==t?void 0:t.reusable_payment_method_required)try{lockCheckout("methods");const e=await apiFetch({path:addQueryArgs(`surecart/v1/processors/${this.processorId}/payment_method_types`,{currency:t.currency,reusable:!0,per_page:100})});state.methods=(null==e?void 0:e.data)||[]}catch(t){createErrorNotice(t),console.error(t)}finally{unLockCheckout("methods")}else state.methods.length&&(state.methods=[])}async confirm(){var t,e,o,r,a,s,i,n;if("razorpay"===(null==state$1?void 0:state$1.id)&&(null===(o=null===(e=null===(t=null==state$2?void 0:state$2.checkout)||void 0===t?void 0:t.payment_intent)||void 0===e?void 0:e.processor_data)||void 0===o?void 0:o.razorpay)&&"paid"!==(null===(r=null==state$2?void 0:state$2.checkout)||void 0===r?void 0:r.status)&&!this.confirming){this.confirming=!0;try{const{external_intent_id:t,processor_data:e,reusable:o}=(null===(a=null==state$2?void 0:state$2.checkout)||void 0===a?void 0:a.payment_intent)||{},{public_key:r,customer_id:c}=(null==e?void 0:e.razorpay)||{};if(!t||!r)return void createErrorNotice({message:wp.i18n.sprintf(wp.i18n.__("Payment gateway configuration incomplete. Please ensure Razorpay is properly configured for transactions.","surecart"))});this.razorpayInstance||(this.razorpayInstance=await loadRazorpay());const l=null===(s=null==state$2?void 0:state$2.checkout)||void 0===s?void 0:s.customer;let d={key:r,order_id:t,prefill:l?{...l.name&&{name:l.name},...l.email&&{email:l.email},...l.phone&&{contact:l.phone}}:void 0,customer_id:c,recurring:o,handler:t=>{if(null==t?void 0:t.razorpay_payment_id)return updateFormState("PAID");createErrorNotice({message:wp.i18n.__("Payment verification failed. Please contact support.","surecart")}),updateFormState("REJECT")},modal:{ondismiss:()=>{updateFormState("REJECT")}}};(null===(n=null===(i=null===window||void 0===window?void 0:window.wp)||void 0===i?void 0:i.hooks)||void 0===n?void 0:n.applyFilters)&&(d=window.wp.hooks.applyFilters("surecart_razorpay_checkout_options",d));const m=new this.razorpayInstance(d);m.on("payment.failed",(t=>{var e;createErrorNotice({message:(null===(e=null==t?void 0:t.error)||void 0===e?void 0:e.description)||wp.i18n.__("Payment failed. Please try again.","surecart")}),updateFormState("REJECT"),console.error("payment.failed",t)})),m.open()}catch(t){createErrorNotice(t),console.error(t),updateFormState("REJECT")}finally{this.confirming=!1}}}},[1,"sc-checkout-razorpay-payment-provider",{processorId:[1,"processor-id"]}]);function defineCustomElement(){"undefined"!=typeof customElements&&["sc-checkout-razorpay-payment-provider"].forEach((t=>{"sc-checkout-razorpay-payment-provider"===t&&(customElements.get(t)||customElements.define(t,ScCheckoutRazorpayPaymentProvider))}))}export{ScCheckoutRazorpayPaymentProvider as S,defineCustomElement as d};