import{o as e}from"./chunk-QXcFz7f5.js";import{c as t,l as n,n as r,t as i}from"./app-G6wC4CCm.js";import{t as a}from"./loader-circle-DtuHVunH.js";import{r as o}from"./theme-Cl--dRB8.js";var s=e(t(),1),c=i(),l=`https://checkout.razorpay.com/v1/checkout.js`;function u({subscription_id:e,key_id:t,plan:i,prefill:u,cancel_url:d}){o();let[f,p]=(0,s.useState)(null),m=(0,s.useRef)(!1);return(0,s.useEffect)(()=>{if(m.current)return;m.current=!0;let n=()=>{if(window.Razorpay)return Promise.resolve();let e=document.querySelector(`script[src="${l}"]`);return e?new Promise((t,n)=>{e.addEventListener(`load`,()=>t(),{once:!0}),e.addEventListener(`error`,()=>n(Error(`checkout.js failed to load`)),{once:!0})}):new Promise((e,t)=>{let n=document.createElement(`script`);n.src=l,n.async=!0,n.onload=()=>e(),n.onerror=()=>t(Error(`checkout.js failed to load`)),document.body.appendChild(n)})},r=document.querySelector(`meta[name="csrf-token"]`)?.content??``,a=async e=>{try{let t=await fetch(`/checkout/razorpay/verify`,{method:`POST`,credentials:`same-origin`,headers:{Accept:`application/json`,"Content-Type":`application/json`,"X-CSRF-TOKEN":r},body:JSON.stringify(e)});if(!t.ok){let e=await t.json().catch(()=>null);p(e&&typeof e.error==`string`&&e.error||`Verification failed (${t.status}).`);return}let n=await t.json();window.location.href=n.redirect}catch{p(`Network error verifying payment. Please contact support.`)}};n().then(()=>{if(!window.Razorpay){p(`Razorpay checkout failed to load.`);return}let n=new window.Razorpay({key:t,subscription_id:e,name:`AppShot AI`,description:`${i.name??`Plan`} · ${i.period??``}`,prefill:u,theme:{color:`#0052FF`},handler:e=>{a(e)},modal:{ondismiss:()=>{window.location.href=d}}});n.on(`payment.failed`,(...e)=>{let t=e[0];p(t?.error?.description??`Payment failed. Please try again.`)}),n.open()}).catch(e=>p(e.message))},[d,t,i.name,i.period,u,e]),(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(r,{title:`Checkout`}),(0,c.jsx)(`main`,{className:`flex min-h-screen items-center justify-center bg-background px-4`,children:(0,c.jsx)(`div`,{className:`flex w-full max-w-sm flex-col items-center gap-4 rounded-xl border border-border bg-card p-8 text-center shadow-sm`,children:f?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(`p`,{className:`text-sm font-medium text-destructive`,children:f}),(0,c.jsx)(`button`,{type:`button`,onClick:()=>n.visit(d),className:`text-[12px] text-muted-foreground underline underline-offset-2 hover:text-foreground`,children:`Back to pricing`})]}):(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(a,{className:`h-6 w-6 animate-spin text-accent`,strokeWidth:2}),(0,c.jsx)(`p`,{className:`text-[13px] text-muted-foreground`,children:`Opening Razorpay checkout…`}),(0,c.jsx)(`p`,{className:`text-[11px] text-muted-foreground/70`,children:`If the popup doesn't appear, check your popup blocker or refresh the page.`})]})})})]})}export{u as default};