1import{W as K,b as W,r as o,s as P,j as e,h as Y,B as E,C as T,X as z,Y as M,Z as O}from"./index-3UNfnSbJ.js";import{C as y,d as g}from"./card-BFY06baf.js";import{S as X}from"./shield-check-DWnR01Yh.js";import{C as j}from"./circle-alert-D1w2gWZo.js";const m=l=>l.toLocaleString("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2}),Q=()=>{const{ref:l}=K(),[$]=W(),x=$.get("rail")==="stripe",[t,A]=o.useState(null),[L,p]=o.useState(!0),[w,h]=o.useState(null),[q,b]=o.useState(null),[R,N]=o.useState(!1),[B,U]=o.useState(!1),[_,D]=o.useState(!1),i=(t==null?void 0:t.shipping_address)??null,s=i&&(i.line1||i.address_line1)?{name:i.name??null,line1:i.line1??i.address_line1??null,line2:i.line2??i.address_line2??null,city:i.city??null,state:i.state??null,zip:i.postal_code??i.zip??null}:null,f=!s||_,[d,u]=o.useState(null),[k,F]=o.useState(0),v=o.useRef(null);o.useEffect(()=>{let n=!1;const r=`${l??""}:${k}`;if(v.current!==r)return v.current=r,(async()=>{var S;if(!l){h("This payment link is missing its reference."),u("missing_ref"),p(!1);return}p(!0),h(null),u(null);let c=!1;try{const{data:a,error:C}=await P.functions.invoke("open-payment-link",{body:x?{ref:l,create_stripe_session:!0}:{ref:l}});if(n)return;if(a!=null&&a.error)throw u(a.code??"unknown"),new Error(a.error);if(C)throw u("server_error"),C;if(x){if(!(a!=null&&a.url))throw u("server_error"),new Error("Could not start the secure card page.");c=!0,window.location.replace(a.url);return}if(!((S=a==null?void 0:a.line_items)!=null&&S.length))throw u("no_items"),new Error("This payment link has no items.");A(a)}catch(a){n||h((a==null?void 0:a.message)||"Could not open this payment link.")}finally{!n&&!c&&p(!1)}})(),()=>{n=!0}},[l,k,x]);const I=o.useCallback(async n=>{if(l){N(!0),b(null);try{const{data:r,error:c}=await P.functions.invoke("nmi-charge",{body:{checkout_type:"payment_link",amount:(t==null?void 0:t.total)??0,card:{payment_token:n.payment_token,first_name:n.first_name,last_name:n.last_name},billingAddress:n.billing,payment_link:{ref:l,shippingAddress:n.shipping}}});if(c)throw c;if(r!=null&&r.error)throw new Error(r.error);if(!(r!=null&&r.success))throw new Error("Payment could not be completed.");U(!0)}catch(r){b((r==null?void 0:r.message)||"Payment could not be completed. Please try again.")}finally{N(!1)}}},[l,t==null?void 0:t.total]);if(L)return e.jsx("main",{className:"min-h-screen flex items-center justify-center bg-background px-6",children:e.jsxs("div",{className:"max-w-sm w-full text-center space-y-4",children:[e.jsx(Y,{className:"h-8 w-8 text-primary animate-spin mx-auto"}),e.jsx("h1",{className:"text-lg font-semibold text-foreground",children:"Preparing your secure checkoutâ¦"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"One moment while we confirm your pricing."})]})});if(w||!t){const n=d==="already_paid"?{title:"This order is already paid",body:"This payment link was completed, so there is nothing left to pay. A receipt was emailed at the time of payment.",hint:"If you were expecting a different order, ask your advisor for your own link.",icon:e.jsx(T,{className:"h-8 w-8 text-primary mx-auto"})}:d==="expired"?{title:"This payment link expired",body:"Payment links stay active for a limited time for your security.",hint:"Ask your advisor to send a fresh link â it takes a moment.",icon:e.jsx(j,{className:"h-8 w-8 text-destructive mx-auto"})}:d==="not_found"||d==="missing_ref"||d==="not_reopenable"?{title:"This link isn't valid",body:"We couldn't find a checkout for this address. The link may have been copied incompletely.",hint:"Ask your advisor to resend the full link.",icon:e.jsx(j,{className:"h-8 w-8 text-destructive mx-auto"})}:{title:"Payment link unavailable",body:w||"Something went wrong opening this checkout.",hint:"This is usually temporary â try again, or reach out to your advisor.",icon:e.jsx(j,{className:"h-8 w-8 text-destructive mx-auto"})},r=!["already_paid","expired","not_found","missing_ref","not_reopenable"].includes(d??"");return e.jsx("main",{className:"min-h-screen flex items-center justify-center bg-background px-6",children:e.jsxs("div",{className:"max-w-sm w-full text-center space-y-4",children:[n.icon,e.jsx("h1",{className:"text-lg font-semibold text-foreground",children:n.title}),e.jsx("p",{className:"text-sm text-muted-foreground",children:n.body}),e.jsx("p",{className:"text-xs text-muted-foreground",children:n.hint}),r&&e.jsx(E,{type:"button",variant:"outline",size:"sm",onClick:()=>F(c=>c+1),children:"Try again"})]})})}return B?e.jsx("main",{className:"min-h-screen flex items-center justify-center bg-background px-6",children:e.jsxs("div",{className:"max-w-sm w-full text-center space-y-4",children:[e.jsx(T,{className:"h-10 w-10 text-primary mx-auto"}),e.jsx("h1",{className:"text-xl font-semibold text-foreground",children:"Payment c
1omplete"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Thank you",t.first_name?`, ${t.first_name}`:"",". Your order is confirmed and a receipt is on its way to your email."]})]})}):e.jsx("main",{className:"min-h-screen bg-background px-4 py-8",children:e.jsxs("div",{className:"mx-auto w-full max-w-md space-y-4",children:[e.jsxs("header",{className:"space-y-1 text-center",children:[e.jsx("h1",{className:"text-xl font-semibold text-foreground",children:t.first_name?`${t.first_name}, complete your order`:"Complete your order"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Secure checkout â your card details never touch our servers."})]}),e.jsx(y,{children:e.jsxs(g,{className:"p-4 space-y-3",children:[t.line_items.map((n,r)=>e.jsxs("div",{className:"flex items-start justify-between gap-3 text-sm",children:[e.jsxs("span",{className:"text-foreground",children:[n.name,n.quantity>1&&e.jsxs("span",{translate:"no","data-no-translate":"true",className:"text-muted-foreground",children:[" x",n.quantity]})]}),e.jsx("span",{className:"text-foreground whitespace-nowrap",children:m(n.unit_price*n.quantity)})]},`${n.id}-${r}`)),e.jsx(z,{}),e.jsxs("div",{className:"flex items-center justify-between text-sm text-muted-foreground",children:[e.jsx("span",{children:"Subtotal"}),e.jsx("span",{translate:"no","data-no-translate":"true",children:m(t.subtotal)})]}),t.discount&&e.jsxs("div",{className:"flex items-center justify-between text-sm text-primary",children:[e.jsx("span",{translate:"no","data-no-translate":"true",children:t.discount.label||`Discount (${t.discount.percent}%)`}),e.jsxs("span",{translate:"no","data-no-translate":"true",children:["-",m(t.discount.amount)]})]}),e.jsx(z,{}),e.jsxs("div",{className:"flex items-center justify-between text-base font-semibold text-foreground",children:[e.jsx("span",{children:"Total"}),e.jsx("span",{translate:"no","data-no-translate":"true",children:m(t.total)})]})]})}),s&&!_&&e.jsx(y,{children:e.jsxs(g,{className:"p-4 space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm font-medium text-foreground",children:[e.jsx(M,{className:"h-4 w-4 text-primary"}),"Shipping to"]}),e.jsxs("p",{className:"text-sm text-muted-foreground leading-snug",children:[[s.name,s.line1,s.line2].filter(Boolean).join(", "),e.jsx("br",{}),[s.city,s.state,s.zip].filter(Boolean).join(", ")]}),e.jsx(E,{type:"button",variant:"outline",size:"sm",onClick:()=>D(!0),children:"Change shipping address"})]})}),e.jsx(y,{children:e.jsx(g,{className:"p-4",children:e.jsx(O,{amount:t.total,isProcessing:R,buttonLabel:`Pay ${m(t.total)}`,onPay:I,errorMessage:q,collectShipping:f,shippingAddress:!f&&s?{name:s.name||void 0,address_line1:s.line1||"",address_line2:s.line2||void 0,city:s.city||"",state:s.state||"",zip:s.zip||"",country:"US"}:null,initialShipping:f&&s?{name:s.name||void 0,address_line1:s.line1||"",address_line2:s.line2||void 0,city:s.city||"",state:s.state||"",zip:s.zip||"",country:"US"}:null})})}),e.jsxs("p",{className:"flex items-center justify-center gap-1.5 text-xs text-muted-foreground",children:[e.jsx(X,{className:"h-3.5 w-3.5"}),"Encrypted and processed securely."]})]})})};export{Q as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.