import{ref as b,inject as E,onUnmounted as N,resolveComponent as d,createElementBlock as A,openBlock as w,createVNode as u,withCtx as m,createTextVNode as S,toDisplayString as L,unref as y,createElementVNode as g,createBlock as $,createCommentVNode as I}from"/vue.js";import{m as i,r as O,u as P}from"./nc-main.DEgIQeOo.js";import"./service-worker.DGZzV4eE.js";const C=b(!1);let s;function v(c){C.value=c}async function U(){return new Promise((c,e)=>{const n=document.createElement("script");n.setAttribute("src","https://beta.hyperswitch.io/v1/HyperLoader.js"),n.setAttribute("type","text/javascript"),document.head.appendChild(n),n.onload=()=>c(),n.onerror=()=>e()})}let p,k;async function j(c,e){s=c,p||(p=Hyper("pk_snd_20ab944e9d684c3db90177706f134777"));const n={items:[{id:"xl-tshirt"}],country:"US",payment:{amount:10,currency:"USD"}};O.callServer(s,"payment/create",{parameter:n},o=>{const t=o.client_secret,r={};k=p.widgets({appearance:r,clientSecret:t});const l={layout:{type:"accordion",defaultCollapsed:!1,radios:!0,spacedAccordionItems:!0},wallets:{walletReturnUrl:P()+"/payment/complete"}};k.create("payment",l).mount("#unified-checkout"),e()})}async function B(c){c.preventDefault(),v(!0);const e={return_url:P()+"/payment/complete"},n=setTimeout(()=>{T()},10),o=await p.confirmPayment({widgets:k,confirmParams:e});clearTimeout(n),console.log("🚀 ~ processPayment ~ hyper.confirmPayment() ~ ret:",o);const t=o.error||{type:o.error_code,message:o.error_message};if(t&&t.message)t.type==="validation_error"?i.setError(s,`An payment validation error occurred, error: '${t.message}'`):i.setError(s,`An unexpected payment error type '${t.type||"unknown"}' occurred, error: '${t.message}'`);else if(o.status==="succeeded"){const{amount:r,amount_received:l,net_amount:a,connector:f,payment_id:_,created:h,currency:x}=o;i.setSuccess(s,`Payment succeeded, result: ${JSON.stringify({amount:r,amount_received:l,net_amount:a,connector:f,payment_id:_,created:h,currency:x},null,2)}`)}else i.setError(s,`An unexpected payment error occurred, result: ${JSON.stringify(o)}`);v(!1)}async function T(){const c=new URLSearchParams(window.location.search).get("payment_intent_client_secret");if(!c)return;console.log("🚀 ~ checkStatus ~ clientSecret:",c);const e=await p.retrievePayment(c);switch(console.log("🚀 ~ checkStatus ~ hyper.retrievePayment() ~ ret:",e),(e.payment||{}).status){case"succeeded":i.setSuccess(s,"Payment succeeded!");break;case"processing":i.setInfo(s,"Your payment is processing.");break;case"requires_payment_method":i.setWarning(s,"Your payment was not successful, please try again.");break;default:i.setError(s,`Something went wrong, result: '${JSON.stringify(e,null,2)}'`);break}}const V={class:"nc-payment",id:"payment-form"},R=Object.assign({name:"NcPayment"},{__name:"nc-payment",setup(c){const e=b(!1),n=E("state");async function o(){U().then(()=>{j(n,()=>{let t,r="orca-payment-element-iframeRef-unified-checkout",l=0,a=setInterval(()=>{l++,t=document.getElementById(r),t?(console.debug("id found",t),clearInterval(a),setTimeout(()=>{e.value=!0},1e3)):(l>20&&(r="orca-element-unified-checkout"),console.debug("id",t))},100)})}).catch(()=>{i.setError(n,"Loading HyperSwitch Payment script failed")})}return o(),N(()=>{e.value=!1}),(t,r)=>{const l=d("nc-header"),a=d("nc-row"),f=d("nc-col"),_=d("nc-button"),h=d("nc-span");return w(),A("form",V,[u(a,{class:"nc-center-h"},{default:m(()=>[u(l,{size:"medium"},{default:m(()=>[S(L(y(e)?"Select payment method:":"Loading payment methods..."),1)]),_:1})]),_:1}),u(a,{class:"nc-center-h nc-margin-top"},{default:m(()=>[u(f,{id:"unified-checkout"})]),_:1}),u(a,{class:"nc-center-h"},{default:m(()=>[u(_,{disabled:!y(e),onClick:y(B)},{default:m(()=>[...r[0]||(r[0]=[g("div",{class:"spinner hidden",id:"spinner"},null,-1),g("span",{id:"button-text"},"Pay now",-1)])]),_:1},8,["disabled","onClick"]),r[1]||(r[1]=g("div",{class:"hidden",id:"payment-message"},null,-1))]),_:1}),u(a,{class:"nc-center-h"},{default:m(()=>[y(C)?(w(),$(h,{key:0},{default:m(()=>[...r[2]||(r[2]=[S("Processing payment...",-1)])]),_:1})):I("",!0)]),_:1})])}}});export{R as default};
