import{__,sprintf}from"@wordpress/i18n";import{addQueryArgs}from"@wordpress/url";import{state as checkoutState}from"../../../../store/checkout/index";import{listenTo}from"../../../../store/checkout/functions";import{lockCheckout,unLockCheckout}from"../../../../store/checkout/mutations";import{state as processorsState}from"../../../../store/processors/index";import{state as selectedProcessor}from"../../../../store/selected-processor/index";import{onChange as onChangeFormState}from"../../../../store/form/index";import{currentFormState}from"../../../../store/form/getters";import{updateFormState}from"../../../../store/form/mutations";import{createErrorNotice}from"../../../../store/notices/mutations";import{loadRazorpay}from"../../../../functions/razorpay";import apiFetch from"../../../../functions/fetch";export class ScCheckoutRazorpayPaymentProvider{constructor(){this.razorpayInstance=null,this.confirming=!1,this.processorId=void 0}componentWillLoad(){loadRazorpay().then((e=>this.razorpayInstance=e)).catch((e=>createErrorNotice({message:e.message}))),this.unlistenToFormState=onChangeFormState("formState",(()=>{"paying"===currentFormState()&&this.confirm()})),this.fetchMethods(),this.unlistenToCheckout=listenTo("checkout",["currency","reusable_payment_method_required"],(()=>this.fetchMethods()))}disconnectedCallback(){var e,t;null===(e=this.unlistenToFormState)||void 0===e||e.call(this),null===(t=this.unlistenToCheckout)||void 0===t||t.call(this),processorsState.methods=[]}async fetchMethods(){const e=checkoutState.checkout;if(this.processorId&&(null==e?void 0:e.currency))if(null==e?void 0:e.reusable_payment_method_required)try{lockCheckout("methods");const t=await apiFetch({path:addQueryArgs(`surecart/v1/processors/${this.processorId}/payment_method_types`,{currency:e.currency,reusable:!0,per_page:100})});processorsState.methods=(null==t?void 0:t.data)||[]}catch(e){createErrorNotice(e),console.error(e)}finally{unLockCheckout("methods")}else processorsState.methods.length&&(processorsState.methods=[])}async confirm(){var e,t,o,r,a,s,i,c;if("razorpay"===(null==selectedProcessor?void 0:selectedProcessor.id)&&(null===(o=null===(t=null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.payment_intent)||void 0===t?void 0:t.processor_data)||void 0===o?void 0:o.razorpay)&&"paid"!==(null===(r=null==checkoutState?void 0:checkoutState.checkout)||void 0===r?void 0:r.status)&&!this.confirming){this.confirming=!0;try{const{external_intent_id:e,processor_data:t,reusable:o}=(null===(a=null==checkoutState?void 0:checkoutState.checkout)||void 0===a?void 0:a.payment_intent)||{},{public_key:r,customer_id:n}=(null==t?void 0:t.razorpay)||{};if(!e||!r)return void createErrorNotice({message:sprintf(__("Payment gateway configuration incomplete. Please ensure Razorpay is properly configured for transactions.","surecart"))});this.razorpayInstance||(this.razorpayInstance=await loadRazorpay());const l=null===(s=null==checkoutState?void 0:checkoutState.checkout)||void 0===s?void 0:s.customer;let d={key:r,order_id:e,prefill:l?{...l.name&&{name:l.name},...l.email&&{email:l.email},...l.phone&&{contact:l.phone}}:void 0,customer_id:n,recurring:o,handler:e=>{if(null==e?void 0:e.razorpay_payment_id)return updateFormState("PAID");createErrorNotice({message:__("Payment verification failed. Please contact support.","surecart")}),updateFormState("REJECT")},modal:{ondismiss:()=>{updateFormState("REJECT")}}};(null===(c=null===(i=null===window||void 0===window?void 0:window.wp)||void 0===i?void 0:i.hooks)||void 0===c?void 0:c.applyFilters)&&(d=window.wp.hooks.applyFilters("surecart_razorpay_checkout_options",d));const u=new this.razorpayInstance(d);u.on("payment.failed",(e=>{var t;createErrorNotice({message:(null===(t=null==e?void 0:e.error)||void 0===t?void 0:t.description)||__("Payment failed. Please try again.","surecart")}),updateFormState("REJECT"),console.error("payment.failed",e)})),u.open()}catch(e){createErrorNotice(e),console.error(e),updateFormState("REJECT")}finally{this.confirming=!1}}}static get is(){return"sc-checkout-razorpay-payment-provider"}static get encapsulation(){return"shadow"}static get properties(){return{processorId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"Razorpay processor id. Required for the recurring `payment_method_types` fetch."},attribute:"processor-id",reflect:!1}}}}