1 line
7.5 KiB
JavaScript
1 line
7.5 KiB
JavaScript
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[580],{9781:function(e,t,n){Promise.resolve().then(n.bind(n,248))},248:function(e,t,n){"use strict";n.r(t),n.d(t,{default:function(){return r}});var c=n(7437),a=n(2265),s=n(9376),o=n(605);function i(){let[e,t]=(0,a.useState)("Shifted Offroad"),[n,i]=(0,a.useState)(""),[r,l]=(0,a.useState)(""),[d,u]=(0,a.useState)("storeshifted"),[h,f]=(0,a.useState)(null),[m,p]=(0,a.useState)(!1),[j,g]=(0,a.useState)(""),[x,b]=(0,a.useState)(""),[S,y]=(0,a.useState)(""),[N,v]=(0,a.useState)("4999"),[w,C]=(0,a.useState)("usd"),k=(0,s.useSearchParams)(),{data:P,status:O}=(0,o.useSession)(),I="authenticated"===O;(0,a.useEffect)(()=>{let e=window.localStorage.getItem("connectAccountId");e&&l(e);let t=window.localStorage.getItem("connectStoreSlug");t&&u(t)},[]),(0,a.useEffect)(()=>{let e=k.get("accountId");e&&l(e)},[k]),(0,a.useEffect)(()=>{(async function(){if(I)try{let e=await fetch("/api/store/me"),t=await e.json();e.ok&&t.slug&&(u(t.slug),t.stripeAccountId&&l(t.stripeAccountId))}catch(e){}})()},[P]),(0,a.useEffect)(()=>{r&&window.localStorage.setItem("connectAccountId",r)},[r]),(0,a.useEffect)(()=>{d&&window.localStorage.setItem("connectStoreSlug",d)},[d]);let E=(0,a.useMemo)(()=>d?"/storefront/".concat(d):"",[d]);async function T(){if(!I){g("Please sign in first.");return}g(""),p(!0);try{let t=await fetch("/api/connect/account/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({displayName:e,contactEmail:n,slug:d})}),c=await t.json();if(!t.ok)throw Error(c.error||"Failed to create account");l(c.accountId),g("Connected account created.")}catch(e){g(e.message||"Failed to create account.")}finally{p(!1)}}async function F(){if(r){g(""),p(!0);try{let e=await fetch("/api/connect/account/status?accountId=".concat(r)),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to fetch status");f(t)}catch(e){g(e.message||"Failed to fetch status.")}finally{p(!1)}}}async function _(){if(r){g(""),p(!0);try{let e=await fetch("/api/connect/account/link",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:r})}),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to create account link");window.location.href=t.url}catch(e){g(e.message||"Failed to start onboarding.")}finally{p(!1)}}}async function A(){if(r){g(""),p(!0);try{let e=await fetch("/api/connect/products/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:r,name:x,description:S,priceInCents:Number(N),currency:w})}),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to create product");g("Product created: ".concat(t.productId))}catch(e){g(e.message||"Failed to create product.")}finally{p(!1)}}}async function J(){if(r){g(""),p(!0);try{let e=await fetch("/api/connect/subscription/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:r})}),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to create subscription");window.location.href=t.url}catch(e){g(e.message||"Failed to create subscription.")}finally{p(!1)}}}async function R(){if(r){g(""),p(!0);try{let e=await fetch("/api/connect/subscription/portal",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:r})}),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to create portal session");window.location.href=t.url}catch(e){g(e.message||"Failed to open billing portal.")}finally{p(!1)}}}return(0,c.jsx)("main",{className:"connect-page",children:(0,c.jsx)("section",{className:"section",children:(0,c.jsxs)("div",{className:"container connect-page__inner",children:[(0,c.jsx)("h1",{className:"page-title",children:"Stripe Connect Demo"}),(0,c.jsx)("p",{className:"connect-page__subtitle",children:"This demo onboards connected accounts, creates products on their account, and provides a storefront for customers to purchase."}),(0,c.jsxs)("div",{className:"connect-card",children:[(0,c.jsx)("h2",{children:"Account Access"}),I?(0,c.jsxs)("div",{className:"connect-actions",children:[(0,c.jsxs)("div",{className:"connect-muted",children:["Signed in as ",P.user.email]}),(0,c.jsx)("button",{className:"btn btn--ghost",onClick:()=>(0,o.signOut)(),children:"Sign out"})]}):(0,c.jsxs)("div",{className:"connect-actions",children:[(0,c.jsx)("div",{className:"connect-muted",children:"Sign in to create an account."}),(0,c.jsx)("button",{className:"btn",onClick:()=>(0,o.signIn)(),children:"Sign in"})]})]}),(0,c.jsxs)("div",{className:"connect-card",children:[(0,c.jsx)("h2",{children:"Create Connected Account"}),(0,c.jsxs)("div",{className:"connect-form",children:[(0,c.jsx)("input",{value:e,onChange:e=>t(e.target.value),placeholder:"Display name"}),(0,c.jsx)("input",{value:n,onChange:e=>i(e.target.value),placeholder:"Contact email"}),(0,c.jsx)("input",{value:d,onChange:e=>u(e.target.value),placeholder:"Store slug (e.g. storeshifted)"}),(0,c.jsx)("button",{className:"btn",onClick:T,disabled:m,children:"Create Account"})]}),r?(0,c.jsxs)("div",{className:"connect-muted",children:["Connected account: ",r]}):null]}),(0,c.jsxs)("div",{className:"connect-card",children:[(0,c.jsx)("h2",{children:"Onboard to Collect Payments"}),(0,c.jsxs)("div",{className:"connect-actions",children:[(0,c.jsx)("button",{className:"btn",onClick:_,disabled:m,children:"Onboard to collect payments"}),(0,c.jsx)("button",{className:"btn",onClick:F,disabled:m,children:"Refresh status"})]}),h?(0,c.jsxs)("div",{className:"connect-status",children:[(0,c.jsxs)("div",{children:["Ready to process payments: ",String(h.readyToProcessPayments)]}),(0,c.jsxs)("div",{children:["Onboarding complete: ",String(h.onboardingComplete)]}),(0,c.jsxs)("div",{children:["Requirements status: ",h.requirementsStatus||"unknown"]})]}):null]}),(0,c.jsxs)("div",{className:"connect-card",children:[(0,c.jsx)("h2",{children:"Create Product on Connected Account"}),(0,c.jsxs)("div",{className:"connect-form",children:[(0,c.jsx)("input",{value:x,onChange:e=>b(e.target.value),placeholder:"Product name"}),(0,c.jsx)("input",{value:S,onChange:e=>y(e.target.value),placeholder:"Description"}),(0,c.jsx)("input",{value:N,onChange:e=>v(e.target.value),placeholder:"Price in cents"}),(0,c.jsx)("input",{value:w,onChange:e=>C(e.target.value),placeholder:"Currency (usd)"}),(0,c.jsx)("button",{className:"btn",onClick:A,disabled:m,children:"Create Product"})]})]}),(0,c.jsxs)("div",{className:"connect-card",children:[(0,c.jsx)("h2",{children:"Storefront"}),(0,c.jsx)("p",{className:"connect-muted",children:"This demo uses the connected account ID in the URL. Replace this with your own identifier in production."}),(0,c.jsx)("div",{className:"connect-actions",children:(0,c.jsx)("a",{className:"btn",href:E||"#",children:"Open Storefront"})})]}),(0,c.jsxs)("div",{className:"connect-card",children:[(0,c.jsx)("h2",{children:"Subscription (Platform Billing)"}),(0,c.jsx)("p",{className:"connect-muted",children:"Creates a hosted subscription checkout session and a billing portal for the connected account."}),(0,c.jsxs)("div",{className:"connect-actions",children:[(0,c.jsx)("button",{className:"btn",onClick:J,disabled:m,children:"Subscribe"}),(0,c.jsx)("button",{className:"btn",onClick:R,disabled:m,children:"Open Billing Portal"})]})]}),j?(0,c.jsx)("div",{className:"connect-message",children:j}):null]})})})}function r(){return(0,c.jsx)(a.Suspense,{fallback:null,children:(0,c.jsx)(i,{})})}},9376:function(e,t,n){"use strict";var c=n(5475);n.o(c,"useSearchParams")&&n.d(t,{useSearchParams:function(){return c.useSearchParams}})}},function(e){e.O(0,[605,971,117,744],function(){return e(e.s=9781)}),_N_E=e.O()}]); |