Spaces:
Running

quantilimited commited on
Commit
1511c70
·
verified ·
1 Parent(s): 0291469

Add bank transfer orders and the in-site checkout page

Browse files
Files changed (7) hide show
  1. SETUP.html +8 -8
  2. js/admin.js +90 -2
  3. js/app.js +3 -2
  4. js/cloud.js +74 -2
  5. js/components.js +1 -1
  6. js/pricing.js +246 -47
  7. styles.css +118 -0
SETUP.html CHANGED
@@ -310,7 +310,7 @@
310
  <tr><td>Scholar</td><td>£9.00</td><td>Monthly, recurring</td></tr>
311
  <tr><td>Examiner</td><td>£19.00</td><td>Monthly, recurring</td></tr>
312
  </table>
313
- <div class="tip">The buyer only sees the account details after signing in and confirming their email, and only when they press a plan. The panel shows the amount, the sort code, the account number and a reference like <code class="inline">EX-4F2A9C31</code> — the reference is what lets you match a payment to a person.</div>
314
  <div class="do">
315
  <b>Stripe is still there if you ever want cards.</b> Developer → Stripe takes payment links or the <code class="inline">stripe-checkout</code> function, and <code class="inline">supabase/SETUP.md</code> has the keys. Leave it empty and the shop uses bank transfer.
316
  </div>
@@ -321,7 +321,7 @@
321
  <div class="step-head">
322
  <div class="num">8</div>
323
  <div>
324
- <h2>Try it, then give yourself the plan</h2>
325
  <p class="why">A bank transfer cannot tell the site anything, so you approve each buyer by hand — one click.</p>
326
  </div>
327
  </div>
@@ -330,15 +330,15 @@
330
  <div class="chrome"><i></i><i></i><i></i><span class="url">your site → Developer → People</span></div>
331
  <div class="pane">
332
  <div class="canvas">
333
- <h4>Give someone a plan</h4>
334
- <div class="field"><span><span class="lab">Buyer's email</span><code>lucas.owen13@icloud.com</code></span></div>
335
- <div class="field"><span><span class="lab">Plan</span><code>Student · £4.00</code></span></div>
336
- <div class="row"><button class="btn green">Grant access</button><span class="pin">8</span></div>
337
  </div>
338
  </div>
339
  </div>
340
- <div class="do"><b>Do this:</b> sign in, open <b>Pricing</b> and press a plan. The panel shows the amount, the reference and the account — exactly what a buyer sees. Then open <b>Developer → People</b>, type the buyer's email, pick the plan and press <b>Grant access</b> <span class="pin">8</span>. The plan shows on their Account page immediately.</div>
341
- <div class="warn">Bank transfer is manual by design: the money arrives with the reference, and you match it to the person. Check the <code class="inline">EX-…</code> reference in your banking app, then grant. Automatic unlocking needs a card processor — that is the Stripe route in <code class="inline">supabase/SETUP.md</code>.</div>
342
  </div>
343
  </section>
344
 
 
310
  <tr><td>Scholar</td><td>£9.00</td><td>Monthly, recurring</td></tr>
311
  <tr><td>Examiner</td><td>£19.00</td><td>Monthly, recurring</td></tr>
312
  </table>
313
+ <div class="tip">The buyer only sees the account details after signing in and confirming their email, and only when they open the checkout page inside the site. The page shows the amount, the sort code, the account number and a reference like <code class="inline">EX-4F2A9C31</code>. Opening it creates an order, so <b>Developer → People</b> shows their reference, email and plan even before they say they have paid.</div>
314
  <div class="do">
315
  <b>Stripe is still there if you ever want cards.</b> Developer → Stripe takes payment links or the <code class="inline">stripe-checkout</code> function, and <code class="inline">supabase/SETUP.md</code> has the keys. Leave it empty and the shop uses bank transfer.
316
  </div>
 
321
  <div class="step-head">
322
  <div class="num">8</div>
323
  <div>
324
+ <h2>Try it, then approve the payment</h2>
325
  <p class="why">A bank transfer cannot tell the site anything, so you approve each buyer by hand — one click.</p>
326
  </div>
327
  </div>
 
330
  <div class="chrome"><i></i><i></i><i></i><span class="url">your site → Developer → People</span></div>
331
  <div class="pane">
332
  <div class="canvas">
333
+ <h4>Bank transfers</h4>
334
+ <div class="field"><span><span class="lab">Reference</span><code>EX-4F2A9C31</code></span><span class="pin">8</span></div>
335
+ <div class="field"><span><span class="lab">Buyer</span><code>student@example.com · Student · £4.00</code></span></div>
336
+ <div class="row"><button class="btn green">Mark paid</button></div>
337
  </div>
338
  </div>
339
  </div>
340
+ <div class="do"><b>Do this:</b> sign in, open <b>Plans</b> and press one. The checkout page opens with the amount, the reference and the account — exactly what a buyer sees — and an order appears in <b>Developer → People</b> straight away. Press <b>I have sent the payment</b> on the page, then check your bank and press <b>Mark paid</b> <span class="pin">8</span> on the order. That records the payment and puts the plan on their Account page in one click.</div>
341
+ <div class="warn">Bank transfer is manual by design: the money arrives with the reference, and you match it to the person. Orders show "Opened" until the buyer confirms, then "Sent — check the bank". Automatic unlocking needs a card processor — that is the Stripe route in <code class="inline">supabase/SETUP.md</code>.</div>
342
  </div>
343
  </section>
344
 
js/admin.js CHANGED
@@ -489,6 +489,10 @@ function PeopleTab() {
489
  const [planId, setPlanId] = useState(cloud.plans[0] ? cloud.plans[0].id : "");
490
  const [error, setError] = useState("");
491
  const [busy, setBusy] = useState(false);
 
 
 
 
492
 
493
  const grant = async () => {
494
  setError("");
@@ -505,18 +509,102 @@ function PeopleTab() {
505
  setBusy(false);
506
  };
507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
  return html`<div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509
  <div class="card">
510
  <div class="section-title" style=${{ marginBottom: "8px" }}>Give someone a plan</div>
511
  <div class="tiny faint" style=${{ marginBottom: "10px", lineHeight: 1.55 }}>
512
- For payments taken outside Stripe, or to fix a webhook that missed. The plan stays until you revoke it.
513
  </div>
514
  <div class="grid two">
515
  <${Text} label="Buyer's email" value=${email} onChange=${setEmail} placeholder="student@example.com" />
516
  <${Select} label="Plan" value=${planId} onChange=${setPlanId}
517
  options=${cloud.plans.map((p) => ({ value: p.id, label: p.name + " · " + money(p.price_cents, p.currency) }))} />
518
  </div>
519
- ${error && html`<div style=${{ marginTop: "8px" }}><${Notice} kind="error">${error}<//></div>`}
520
  <button class="btn primary small" style=${{ marginTop: "10px" }} onClick=${grant} disabled=${busy}>${busy ? html`<span class="spin"></span>` : "Grant access"}</button>
521
  </div>
522
 
 
489
  const [planId, setPlanId] = useState(cloud.plans[0] ? cloud.plans[0].id : "");
490
  const [error, setError] = useState("");
491
  const [busy, setBusy] = useState(false);
492
+ const [busyOrder, setBusyOrder] = useState("");
493
+
494
+ const pending = cloud.orders.filter((o) => o.status === "opened" || o.status === "sent");
495
+ const recent = cloud.orders.filter((o) => o.status === "paid" || o.status === "cancelled").slice(0, 8);
496
 
497
  const grant = async () => {
498
  setError("");
 
509
  setBusy(false);
510
  };
511
 
512
+ const markPaid = async (order) => {
513
+ setError("");
514
+ setBusyOrder(order.id);
515
+ try {
516
+ await cloud.markOrderPaid(order);
517
+ cloud.flash("Marked paid — " + (order.plan_name || order.plan_id) + " is on for " + order.email + ".");
518
+ } catch (err) {
519
+ setError(err && err.message ? err.message : "Could not record that payment.");
520
+ }
521
+ setBusyOrder("");
522
+ };
523
+
524
+ const dismiss = async (order) => {
525
+ setError("");
526
+ try {
527
+ await cloud.dismissOrder(order);
528
+ cloud.flash("Order dismissed.");
529
+ } catch (err) {
530
+ setError(err && err.message ? err.message : "Could not dismiss that order.");
531
+ }
532
+ };
533
+
534
  return html`<div>
535
+ ${error && html`<div style=${{ marginBottom: "12px" }}><${Notice} kind="error">${error}<//></div>`}
536
+
537
+ ${pending.length > 0 &&
538
+ html`<div class="card" style=${{ marginBottom: "12px" }}>
539
+ <div class="row between wrap" style=${{ marginBottom: "8px" }}>
540
+ <div class="section-title">Bank transfers</div>
541
+ <span class="pill warn">${pending.length} to check</span>
542
+ </div>
543
+ <div class="tiny faint" style=${{ marginBottom: "8px", lineHeight: 1.55 }}>
544
+ An order appears the moment someone opens the payment page. "Sent" means they have told us the money is on its way — check your bank, then press Mark paid.
545
+ </div>
546
+ <div class="list">
547
+ ${pending.map(
548
+ (o) => html`<div class="list-row" key=${o.id}>
549
+ <div class="main">
550
+ <div class="t">
551
+ <span class="mono">${o.reference}</span>
552
+ <span class=${"pill " + (o.status === "sent" ? "warn" : "neutral")} style=${{ marginLeft: "6px" }}>
553
+ ${o.status === "sent" ? "Sent — check the bank" : "Opened, not sent yet"}
554
+ </span>
555
+ </div>
556
+ <div class="s">${o.email} · ${o.plan_name || o.plan_id} · ${money(o.amount_cents, o.currency)}</div>
557
+ <div class="s">
558
+ Opened ${new Date(o.created_at || Date.now()).toLocaleDateString("en-GB")}
559
+ ${o.sent_at ? " · marked sent " + new Date(o.sent_at).toLocaleString("en-GB") : ""}
560
+ ${o.payer_note ? " · “" + o.payer_note + "”" : ""}
561
+ </div>
562
+ </div>
563
+ <button class="btn small primary" disabled=${busyOrder === o.id} onClick=${() => markPaid(o)}>
564
+ ${busyOrder === o.id ? html`<span class="spin"></span>` : "Mark paid"}
565
+ </button>
566
+ <button class="btn small ghost" onClick=${() => dismiss(o)}>Dismiss</button>
567
+ </div>`
568
+ )}
569
+ </div>
570
+ </div>`}
571
+
572
+ ${recent.length > 0 &&
573
+ html`<div class="card" style=${{ marginBottom: "12px" }}>
574
+ <div class="row between" style=${{ marginBottom: "8px" }}>
575
+ <div class="section-title">Recent orders</div>
576
+ <span class="tiny faint">last ${recent.length}</span>
577
+ </div>
578
+ <div class="list">
579
+ ${recent.map(
580
+ (o) => html`<div class="list-row" key=${o.id}>
581
+ <div class="main">
582
+ <div class="t">
583
+ <span class="mono">${o.reference}</span>
584
+ <span class=${"pill " + (o.status === "paid" ? "green" : "neutral")} style=${{ marginLeft: "6px" }}>
585
+ ${o.status === "paid" ? "Paid" : "Cancelled"}
586
+ </span>
587
+ </div>
588
+ <div class="s">
589
+ ${o.email} · ${o.plan_name || o.plan_id} · ${money(o.amount_cents, o.currency)}
590
+ ${o.confirmed_at ? " · confirmed " + new Date(o.confirmed_at).toLocaleDateString("en-GB") : ""}
591
+ </div>
592
+ </div>
593
+ </div>`
594
+ )}
595
+ </div>
596
+ </div>`}
597
+
598
  <div class="card">
599
  <div class="section-title" style=${{ marginBottom: "8px" }}>Give someone a plan</div>
600
  <div class="tiny faint" style=${{ marginBottom: "10px", lineHeight: 1.55 }}>
601
+ For payments taken outside the site, or to fix a transfer that never appeared above. The plan stays until you revoke it.
602
  </div>
603
  <div class="grid two">
604
  <${Text} label="Buyer's email" value=${email} onChange=${setEmail} placeholder="student@example.com" />
605
  <${Select} label="Plan" value=${planId} onChange=${setPlanId}
606
  options=${cloud.plans.map((p) => ({ value: p.id, label: p.name + " · " + money(p.price_cents, p.currency) }))} />
607
  </div>
 
608
  <button class="btn primary small" style=${{ marginTop: "10px" }} onClick=${grant} disabled=${busy}>${busy ? html`<span class="spin"></span>` : "Grant access"}</button>
609
  </div>
610
 
js/app.js CHANGED
@@ -6,7 +6,7 @@ import { EditModeProvider, useEditMode } from "./copy.js";
6
  import { Sidebar, Drawer, TopBar, SECTIONS, Icon } from "./components.js";
7
  import { Dashboard, Chat, Coach, Essay, Poetry, Improve } from "./screens.js";
8
  import { ModelAnswers, MockExam, ProcessLog, Settings } from "./screens-extra.js";
9
- import { Pricing } from "./pricing.js";
10
  import { Account, Connect, Welcome, NewPassword, LinkProblem } from "./auth.js";
11
  import { Developer } from "./admin.js";
12
 
@@ -14,7 +14,7 @@ import { Developer } from "./admin.js";
14
  const HIDDEN_ROUTES = ["connect", "welcome", "new-password", "link-problem"];
15
 
16
  function isRoute(id) {
17
- return SECTIONS.some((s) => s.id === id) || HIDDEN_ROUTES.includes(id);
18
  }
19
 
20
  function currentSection() {
@@ -149,6 +149,7 @@ function Shell() {
149
  case "developer":
150
  return html`<${Developer} />`;
151
  default:
 
152
  return html`<${Dashboard} onNavigate=${navigate} />`;
153
  }
154
  })();
 
6
  import { Sidebar, Drawer, TopBar, SECTIONS, Icon } from "./components.js";
7
  import { Dashboard, Chat, Coach, Essay, Poetry, Improve } from "./screens.js";
8
  import { ModelAnswers, MockExam, ProcessLog, Settings } from "./screens-extra.js";
9
+ import { Pricing, Checkout } from "./pricing.js";
10
  import { Account, Connect, Welcome, NewPassword, LinkProblem } from "./auth.js";
11
  import { Developer } from "./admin.js";
12
 
 
14
  const HIDDEN_ROUTES = ["connect", "welcome", "new-password", "link-problem"];
15
 
16
  function isRoute(id) {
17
+ return SECTIONS.some((s) => s.id === id) || HIDDEN_ROUTES.includes(id) || id.startsWith("pay/");
18
  }
19
 
20
  function currentSection() {
 
149
  case "developer":
150
  return html`<${Developer} />`;
151
  default:
152
+ if (section.startsWith("pay/")) return html`<${Checkout} planId=${section.slice(4)} />`;
153
  return html`<${Dashboard} onNavigate=${navigate} />`;
154
  }
155
  })();
js/cloud.js CHANGED
@@ -22,6 +22,7 @@ const T = {
22
  secrets: "explication_model_secrets",
23
  content: "explication_content",
24
  entitlements: "explication_entitlements",
 
25
  usage: "explication_usage"
26
  };
27
 
@@ -358,6 +359,7 @@ export function CloudProvider({ children }) {
358
  models: [],
359
  content: {},
360
  entitlements: [],
 
361
  usage: []
362
  });
363
  const [busy, setBusy] = useState(false);
@@ -393,14 +395,17 @@ export function CloudProvider({ children }) {
393
  map[row.key] = row.value;
394
  });
395
  let entitlements = [];
 
396
  let usage = [];
397
  if (who) {
398
- const [entRes, useRes] = await Promise.all([
399
  sb.from(T.entitlements).select("*").order("granted_at", { ascending: false }),
400
- sb.from(T.usage).select("*").order("created_at", { ascending: false }).limit(2000)
 
401
  ]);
402
  entitlements = entRes.data || [];
403
  usage = useRes.data || [];
 
404
  }
405
  if (!mounted.current) return;
406
  setCloud({
@@ -409,6 +414,7 @@ export function CloudProvider({ children }) {
409
  models: modelsRes.data || [],
410
  content: map,
411
  entitlements,
 
412
  usage
413
  });
414
  setError("");
@@ -466,6 +472,7 @@ export function CloudProvider({ children }) {
466
  }, []);
467
 
468
  const entitlements = config.ready ? cloud.entitlements : preview.entitlements;
 
469
  const usage = config.ready ? cloud.usage : preview.usage;
470
 
471
  const activeEntitlements = useMemo(() => {
@@ -787,6 +794,63 @@ export function CloudProvider({ children }) {
787
  [user, client, config.ready]
788
  );
789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
790
  const hostedChat = useCallback(
791
  async (model, messages, onToken) => {
792
  if (!config.ready) throw new Error("Hosted models need Supabase. Connect it in Developer settings.");
@@ -866,6 +930,7 @@ export function CloudProvider({ children }) {
866
  models,
867
  content: config.ready ? cloud.content : preview.content,
868
  entitlements,
 
869
  usage,
870
  plan,
871
  entitlement: activeEntitlement,
@@ -891,6 +956,9 @@ export function CloudProvider({ children }) {
891
  revokeGrant,
892
  startCheckout,
893
  startBankTransfer,
 
 
 
894
  hostedChat,
895
  refresh: () => refresh(client),
896
  canDownload: (tier) => Number(tier || 0) <= 1 || (!!plan && Number(plan.download_tier || 0) >= Number(tier || 0)),
@@ -910,6 +978,7 @@ export function CloudProvider({ children }) {
910
  plans,
911
  models,
912
  entitlements,
 
913
  usage,
914
  plan,
915
  activeEntitlement,
@@ -936,6 +1005,9 @@ export function CloudProvider({ children }) {
936
  revokeGrant,
937
  startCheckout,
938
  startBankTransfer,
 
 
 
939
  hostedChat,
940
  refresh
941
  ]
 
22
  secrets: "explication_model_secrets",
23
  content: "explication_content",
24
  entitlements: "explication_entitlements",
25
+ orders: "explication_orders",
26
  usage: "explication_usage"
27
  };
28
 
 
359
  models: [],
360
  content: {},
361
  entitlements: [],
362
+ orders: [],
363
  usage: []
364
  });
365
  const [busy, setBusy] = useState(false);
 
395
  map[row.key] = row.value;
396
  });
397
  let entitlements = [];
398
+ let orders = [];
399
  let usage = [];
400
  if (who) {
401
+ const [entRes, useRes, orderRes] = await Promise.all([
402
  sb.from(T.entitlements).select("*").order("granted_at", { ascending: false }),
403
+ sb.from(T.usage).select("*").order("created_at", { ascending: false }).limit(2000),
404
+ sb.from(T.orders).select("*").order("created_at", { ascending: false })
405
  ]);
406
  entitlements = entRes.data || [];
407
  usage = useRes.data || [];
408
+ orders = orderRes.data || [];
409
  }
410
  if (!mounted.current) return;
411
  setCloud({
 
414
  models: modelsRes.data || [],
415
  content: map,
416
  entitlements,
417
+ orders,
418
  usage
419
  });
420
  setError("");
 
472
  }, []);
473
 
474
  const entitlements = config.ready ? cloud.entitlements : preview.entitlements;
475
+ const orders = config.ready ? cloud.orders : [];
476
  const usage = config.ready ? cloud.usage : preview.usage;
477
 
478
  const activeEntitlements = useMemo(() => {
 
794
  [user, client, config.ready]
795
  );
796
 
797
+ // The buyer confirms on the payment page. The function checks the order belongs
798
+ // to them and moves it to 'sent' — the site itself cannot mark anything paid.
799
+ const confirmBankTransfer = useCallback(
800
+ async (orderId, note) => {
801
+ if (!orderId) throw new Error("There is no order to confirm.");
802
+ if (!config.ready) throw new Error("Payments need the published site, not a preview.");
803
+ const { data, error: err } = await client.functions.invoke("bank-transfer", {
804
+ body: { action: "sent", order_id: orderId, note: String(note || "") }
805
+ });
806
+ if (err) throw err;
807
+ if (!data || data.ok === false) throw new Error((data && data.message) || "That could not be recorded.");
808
+ await refresh(client);
809
+ return data;
810
+ },
811
+ [client, config.ready, refresh]
812
+ );
813
+
814
+ // Developer only: the money arrived, so record it and grant the plan in one go.
815
+ const markOrderPaid = useCallback(
816
+ async (order) => {
817
+ if (!order) throw new Error("No order picked.");
818
+ if (!config.ready) throw new Error("Connect Supabase first.");
819
+ // Grant first: if the second write fails, the plan is never lost — the
820
+ // order stays in the list and can be pressed again.
821
+ const { error: grantErr } = await client.from(T.entitlements).insert({
822
+ user_id: order.user_id || null,
823
+ email: String(order.email || "").toLowerCase(),
824
+ plan_id: order.plan_id,
825
+ source: "bank",
826
+ expires_at: null
827
+ });
828
+ if (grantErr) throw grantErr;
829
+ const { error: err } = await client
830
+ .from(T.orders)
831
+ .update({
832
+ status: "paid",
833
+ confirmed_at: new Date().toISOString(),
834
+ confirmed_by: (user && user.email) || ""
835
+ })
836
+ .eq("id", order.id);
837
+ if (err) throw err;
838
+ await refresh(client);
839
+ },
840
+ [client, config.ready, refresh, user]
841
+ );
842
+
843
+ const dismissOrder = useCallback(
844
+ async (order) => {
845
+ if (!order) return;
846
+ if (!config.ready) return;
847
+ const { error: err } = await client.from(T.orders).update({ status: "cancelled" }).eq("id", order.id);
848
+ if (err) throw err;
849
+ await refresh(client);
850
+ },
851
+ [client, config.ready, refresh]
852
+ );
853
+
854
  const hostedChat = useCallback(
855
  async (model, messages, onToken) => {
856
  if (!config.ready) throw new Error("Hosted models need Supabase. Connect it in Developer settings.");
 
930
  models,
931
  content: config.ready ? cloud.content : preview.content,
932
  entitlements,
933
+ orders,
934
  usage,
935
  plan,
936
  entitlement: activeEntitlement,
 
956
  revokeGrant,
957
  startCheckout,
958
  startBankTransfer,
959
+ confirmBankTransfer,
960
+ markOrderPaid,
961
+ dismissOrder,
962
  hostedChat,
963
  refresh: () => refresh(client),
964
  canDownload: (tier) => Number(tier || 0) <= 1 || (!!plan && Number(plan.download_tier || 0) >= Number(tier || 0)),
 
978
  plans,
979
  models,
980
  entitlements,
981
+ orders,
982
  usage,
983
  plan,
984
  activeEntitlement,
 
1005
  revokeGrant,
1006
  startCheckout,
1007
  startBankTransfer,
1008
+ confirmBankTransfer,
1009
+ markOrderPaid,
1010
+ dismissOrder,
1011
  hostedChat,
1012
  refresh
1013
  ]
js/components.js CHANGED
@@ -168,7 +168,7 @@ export function TopBar({ section, onMenu, onNavigate }) {
168
  const app = useApp();
169
  const engine = useEngine();
170
  const cloud = useCloud();
171
- const title = (SECTIONS.find((s) => s.id === section) || SECTIONS[0]).title;
172
  return html`<header class="topbar">
173
  <button class="icon-btn menu-btn" onClick=${onMenu} aria-label="Open menu"><${Icon} name="menu" size=${18} /></button>
174
  <h1>${title}</h1>
 
168
  const app = useApp();
169
  const engine = useEngine();
170
  const cloud = useCloud();
171
+ const title = section.startsWith("pay/") ? "Checkout" : (SECTIONS.find((s) => s.id === section) || SECTIONS[0]).title;
172
  return html`<header class="topbar">
173
  <button class="icon-btn menu-btn" onClick=${onMenu} aria-label="Open menu"><${Icon} name="menu" size=${18} /></button>
174
  <h1>${title}</h1>
js/pricing.js CHANGED
@@ -1,4 +1,4 @@
1
- import { html, useState } from "./runtime.js";
2
  import { useCloud, money, planPeriod } from "./cloud.js";
3
  import { Copy } from "./copy.js";
4
  import { Icon, Notice, Pill } from "./components.js";
@@ -82,75 +82,275 @@ function Group({ title, note, plans, cloud, buy, busy }) {
82
  </div>`;
83
  }
84
 
85
- function TransferPanel({ transfer, onClose }) {
86
- const [copied, setCopied] = useState(false);
87
- const d = transfer.details;
88
- const copy = () => {
89
- const text = [
90
- "Explication — " + d.planName,
91
- "Amount: " + d.amount,
92
- "Account name: " + d.accountName,
93
- "Sort code: " + d.sortCode,
94
- "Account number: " + d.accountNumber,
95
- "Reference: " + d.reference
96
- ].join("\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  navigator.clipboard
98
  .writeText(text)
99
  .then(() => {
100
- setCopied(true);
101
- setTimeout(() => setCopied(false), 2000);
102
  })
103
  .catch(() => {});
104
  };
105
- const line = (label, value) => html`<div class="row between" style=${{ borderTop: "1px solid var(--hairline)", padding: "9px 0" }}>
106
- <span class="tiny muted">${label}</span>
107
- <span class="tiny mono" style=${{ fontWeight: 600 }}>${value}</span>
108
- </div>`;
109
- return html`<div class="card" style=${{ marginTop: "16px", borderColor: "var(--accent)" }}>
110
- <div class="row between">
111
- <div class="section-title">Pay ${d.amount} by bank transfer</div>
112
- <button class="btn small ghost" onClick=${onClose}>Close</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  </div>
114
- <p class="tiny muted" style=${{ marginTop: "6px", maxWidth: "580px", lineHeight: 1.6 }}>
115
- Send exactly ${d.amount} from your bank app, with the reference in the payment note. ${d.note} Your plan is switched on as soon as the payment lands.
116
- </p>
117
- ${line("Account name", d.accountName)}
118
- ${line("Sort code", d.sortCode)}
119
- ${line("Account number", d.accountNumber)}
120
- ${line("Reference", d.reference)}
121
- <div class="row" style=${{ marginTop: "10px" }}>
122
- <button class="btn small primary" onClick=${copy}>${copied ? "Copied" : "Copy the details"}</button>
123
- <span class="tiny faint">${d.planName}</span>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  </div>
125
- </div>`;
126
  }
127
 
128
  export function Pricing() {
129
  const cloud = useCloud();
130
  const [busy, setBusy] = useState("");
131
  const [error, setError] = useState("");
132
- const [transfer, setTransfer] = useState(null);
133
 
134
  const plans = cloud.plans.filter((p) => p.active !== false).sort((a, b) => (a.sort || 0) - (b.sort || 0));
135
  const oneOff = plans.filter((p) => p.kind === "one_time");
136
  const subs = plans.filter((p) => p.kind === "subscription");
137
 
138
- const buy = async (plan) => {
139
  setError("");
 
140
  if (!cloud.user) {
141
  window.location.hash = "#/account";
142
  return;
143
  }
144
- setBusy(plan.id);
145
- setTransfer(null);
146
- try {
147
- const details = await cloud.startBankTransfer(plan);
148
- setTransfer({ plan, details });
149
- } catch (err) {
150
- setError(err && err.message ? err.message : "The payment details could not be loaded.");
151
- } finally {
152
- setBusy("");
153
- }
154
  };
155
 
156
  return html`<main class="page">
@@ -173,7 +373,6 @@ export function Pricing() {
173
  <${Notice} kind="warn">This is the preview shop front — connect Supabase and Stripe in the developer console to take real payments.<//>
174
  </div>`}
175
  ${error && html`<div style=${{ marginTop: "14px" }}><${Notice} kind="error">${error}<//></div>`}
176
- ${transfer && html`<${TransferPanel} transfer=${transfer} onClose=${() => setTransfer(null)} />`}
177
 
178
  <div class="free-strip">
179
  <div>
 
1
+ import { html, useEffect, useState } from "./runtime.js";
2
  import { useCloud, money, planPeriod } from "./cloud.js";
3
  import { Copy } from "./copy.js";
4
  import { Icon, Notice, Pill } from "./components.js";
 
82
  </div>`;
83
  }
84
 
85
+ function PayField({ label, value }) {
86
+ return html`<div class="pay-field">
87
+ <span class="lab">${label}</span>
88
+ <span class="val">${value}</span>
89
+ </div>`;
90
+ }
91
+
92
+ function OrderSummary({ order, plan, email }) {
93
+ const name = (plan && plan.name) || order.planName || "Your plan";
94
+ const features = (plan && plan.features) || [];
95
+ const period = order.kind === "subscription" ? "per " + (order.interval || "month") : "once";
96
+ return html`<aside class="card checkout-summary">
97
+ <div class="eyebrow">Your order</div>
98
+ <div class="checkout-plan">${name}</div>
99
+ <div class="checkout-price">${order.amount} <span>${period}</span></div>
100
+ ${features.length > 0 &&
101
+ html`<ul class="plan-features">
102
+ ${features.map((f, i) => html`<li key=${i}><${Icon} name="check" size=${12} /> <span>${f}</span></li>`)}
103
+ </ul>`}
104
+ <div class="divider" style=${{ margin: "14px 0 10px" }}></div>
105
+ <div class="row between"><span class="tiny muted">Account</span><span class="tiny mono">${email}</span></div>
106
+ <div class="row between" style=${{ marginTop: "6px" }}><span class="tiny muted">Reference</span><span class="tiny mono">${order.reference}</span></div>
107
+ <div class="tiny faint" style=${{ marginTop: "12px", lineHeight: 1.6 }}>
108
+ ${order.kind === "subscription"
109
+ ? "Monthly. Nothing is taken automatically — you send each month you want to keep it."
110
+ : "One payment. Nothing renews."}
111
+ </div>
112
+ </aside>`;
113
+ }
114
+
115
+ function Awaiting({ order }) {
116
+ const when = order.sentAt
117
+ ? "You told us on " + new Date(order.sentAt).toLocaleString("en-GB", { dateStyle: "long", timeStyle: "short" }) + ". "
118
+ : "";
119
+ return html`<div class="pay-step">
120
+ <div class="pay-step-head">
121
+ <span class="pay-step-num"><${Icon} name="check" size=${12} /></span>
122
+ <div>
123
+ <div class="pay-step-title">${order.status === "paid" ? "Payment confirmed" : "Marked as sent"}</div>
124
+ <div class="tiny muted" style=${{ marginTop: "2px", lineHeight: 1.6 }}>
125
+ ${order.status === "paid"
126
+ ? "The payment has been checked and your plan is active on this account."
127
+ : when + "We check each transfer by hand, then switch your plan on — usually within one working day."}
128
+ </div>
129
+ </div>
130
+ </div>
131
+ <div class="callout green" style=${{ marginTop: "12px" }}>
132
+ <strong>${order.status === "paid" ? "You're all set." : "Nothing else to do."}</strong>
133
+ Your reference is ${order.reference}. Keep it if you need to ask us about this payment.
134
+ </div>
135
+ ${order.status === "paid" && html`<div class="row" style=${{ marginTop: "12px" }}><a class="btn small primary" href="#/account">See your plan</a></div>`}
136
+ </div>`;
137
+ }
138
+
139
+ export function Checkout({ planId }) {
140
+ const cloud = useCloud();
141
+ const [order, setOrder] = useState(null);
142
+ const [loading, setLoading] = useState(true);
143
+ const [error, setError] = useState("");
144
+ const [note, setNote] = useState("");
145
+ const [copied, setCopied] = useState("");
146
+ const [busy, setBusy] = useState(false);
147
+
148
+ const plan = cloud.plans.find((p) => p.id === planId) || null;
149
+ const signedIn = !!cloud.user;
150
+ const userId = cloud.user ? cloud.user.id : "";
151
+
152
+ // Opening this page is what creates the order, so the developer can see the
153
+ // reference, email and plan before the money arrives.
154
+ useEffect(() => {
155
+ let live = true;
156
+ setOrder(null);
157
+ setError("");
158
+ if (!userId) {
159
+ setLoading(false);
160
+ return () => {
161
+ live = false;
162
+ };
163
+ }
164
+ setLoading(true);
165
+ cloud
166
+ .startBankTransfer({ id: planId })
167
+ .then((data) => {
168
+ if (!live) return;
169
+ setOrder(data);
170
+ setNote(data.payerNote || "");
171
+ })
172
+ .catch((err) => {
173
+ if (live) setError(err && err.message ? err.message : "The payment details could not be loaded.");
174
+ })
175
+ .finally(() => {
176
+ if (live) setLoading(false);
177
+ });
178
+ return () => {
179
+ live = false;
180
+ };
181
+ }, [planId, userId]);
182
+
183
+ const copy = (what, text) => {
184
  navigator.clipboard
185
  .writeText(text)
186
  .then(() => {
187
+ setCopied(what);
188
+ setTimeout(() => setCopied(""), 2000);
189
  })
190
  .catch(() => {});
191
  };
192
+
193
+ const confirm = async () => {
194
+ setBusy(true);
195
+ setError("");
196
+ try {
197
+ const data = await cloud.confirmBankTransfer(order.orderId, note);
198
+ setOrder(data);
199
+ } catch (err) {
200
+ setError(err && err.message ? err.message : "That could not be recorded.");
201
+ }
202
+ setBusy(false);
203
+ };
204
+
205
+ if (!signedIn) {
206
+ return html`<main class="page narrow">
207
+ <div class="eyebrow">Checkout</div>
208
+ <h1 class="page-title">Pay by bank transfer</h1>
209
+ <p class="subtitle">Sign in first, so the payment is attached to your account and your plan.</p>
210
+ <div class="card" style=${{ marginTop: "16px" }}>
211
+ <div class="row between wrap">
212
+ <div>
213
+ <div style=${{ fontWeight: 600 }}>${plan ? plan.name + " · " + money(plan.price_cents, plan.currency) : "Your plan"}</div>
214
+ <div class="tiny faint" style=${{ marginTop: "4px" }}>The payment page opens as soon as you are signed in.</div>
215
+ </div>
216
+ <div class="row">
217
+ <a class="btn small" href="#/pricing">Back to plans</a>
218
+ <a class="btn small primary" href="#/account">Sign in</a>
219
+ </div>
220
+ </div>
221
+ </div>
222
+ </main>`;
223
+ }
224
+
225
+ if (loading) {
226
+ return html`<main class="page narrow">
227
+ <div class="card" style=${{ marginTop: "16px" }}>
228
+ <div class="row"><span class="spin"></span><span class="tiny muted">Preparing your payment page…</span></div>
229
+ </div>
230
+ </main>`;
231
+ }
232
+
233
+ if (!order) {
234
+ return html`<main class="page narrow">
235
+ <div class="eyebrow">Checkout</div>
236
+ <h1 class="page-title">Payment page</h1>
237
+ <div style=${{ marginTop: "14px" }}><${Notice} kind="error">${error || "This plan is not on sale."}<//></div>
238
+ <div class="row" style=${{ marginTop: "12px" }}><a class="btn small" href="#/pricing">Back to plans</a></div>
239
+ </main>`;
240
+ }
241
+
242
+ const detailsText = [
243
+ "Explication — " + order.planName,
244
+ "Amount: " + order.amount,
245
+ "Account name: " + order.accountName,
246
+ "Sort code: " + order.sortCode,
247
+ "Account number: " + order.accountNumber,
248
+ "Reference: " + order.reference
249
+ ].join("\n");
250
+
251
+ return html`<main class="page">
252
+ <div class="row between wrap" style=${{ alignItems: "flex-end" }}>
253
+ <div>
254
+ <div class="eyebrow">Checkout</div>
255
+ <h1 class="page-title">Pay ${order.amount}</h1>
256
+ <p class="subtitle">${order.planName} · bank transfer. No card, no fees.</p>
257
+ </div>
258
+ <a class="btn small ghost" href="#/pricing">Back to plans</a>
259
  </div>
260
+
261
+ ${error && html`<div style=${{ marginTop: "12px" }}><${Notice} kind="error">${error}<//></div>`}
262
+
263
+ <div class="checkout">
264
+ <${OrderSummary} order=${order} plan=${plan} email=${cloud.user.email} />
265
+
266
+ <section>
267
+ <div class="pay-step">
268
+ <div class="pay-step-head">
269
+ <span class="pay-step-num">1</span>
270
+ <div>
271
+ <div class="pay-step-title">Send ${order.amount} from your bank app</div>
272
+ <div class="tiny muted" style=${{ marginTop: "2px" }}>Use the reference below, so the payment is matched to this order.</div>
273
+ </div>
274
+ </div>
275
+ <div class="pay-grid">
276
+ <${PayField} label="Account name" value=${order.accountName} />
277
+ <${PayField} label="Sort code" value=${order.sortCode} />
278
+ <${PayField} label="Account number" value=${order.accountNumber} />
279
+ </div>
280
+ <div class="pay-ref">
281
+ <div>
282
+ <span class="lab">Payment reference</span>
283
+ <span class="val">${order.reference}</span>
284
+ </div>
285
+ <button class="btn small" onClick=${() => copy("ref", order.reference)}>${copied === "ref" ? "Copied" : "Copy"}</button>
286
+ </div>
287
+ <div class="row between wrap" style=${{ marginTop: "10px" }}>
288
+ <span class="tiny faint" style=${{ maxWidth: "430px", lineHeight: 1.55 }}>
289
+ These details are fetched from our server each time this page opens. They are not stored in the site's files.
290
+ </span>
291
+ <button class="btn small" onClick=${() => copy("all", detailsText)}>${copied === "all" ? "Copied" : "Copy all details"}</button>
292
+ </div>
293
+ </div>
294
+
295
+ ${order.status === "opened" &&
296
+ html`<div class="pay-step">
297
+ <div class="pay-step-head">
298
+ <span class="pay-step-num">2</span>
299
+ <div>
300
+ <div class="pay-step-title">Confirm you have sent it</div>
301
+ <div class="tiny muted" style=${{ marginTop: "2px", lineHeight: 1.6 }}>
302
+ We check every transfer by hand and switch your plan on once it lands — usually within one working day.
303
+ </div>
304
+ </div>
305
+ </div>
306
+ <div style=${{ marginTop: "12px", maxWidth: "420px" }}>
307
+ <div class="field">
308
+ <label>Name on the account you paid from <span class="hint">optional, helps us match it</span></label>
309
+ <input class="input" value=${note} onInput=${(e) => setNote(e.target.value)} placeholder="e.g. L Owen" spellCheck=${false} />
310
+ </div>
311
+ </div>
312
+ <div class="row wrap" style=${{ marginTop: "12px" }}>
313
+ <button class="btn primary small" onClick=${confirm} disabled=${busy}>
314
+ ${busy ? html`<span class="spin"></span>` : "I have sent the payment"}
315
+ </button>
316
+ <span class="tiny faint">This records the order — it does not take any money.</span>
317
+ </div>
318
+ </div>`}
319
+
320
+ ${(order.status === "sent" || order.status === "paid") && html`<${Awaiting} order=${order} />`}
321
+
322
+ ${order.status === "cancelled" &&
323
+ html`<div class="pay-step">
324
+ <div class="pay-step-title">This order was cancelled</div>
325
+ <div class="tiny muted" style=${{ marginTop: "4px" }}>Nothing was charged. You can start again from the plans page.</div>
326
+ <div class="row" style=${{ marginTop: "12px" }}><a class="btn small primary" href="#/pricing">Choose a plan</a></div>
327
+ </div>`}
328
+
329
+ <p class="tiny faint" style=${{ marginTop: "4px", lineHeight: 1.6 }}>
330
+ ${order.note || "One payment. Nothing renews."}
331
+ </p>
332
+ </section>
333
  </div>
334
+ </main>`;
335
  }
336
 
337
  export function Pricing() {
338
  const cloud = useCloud();
339
  const [busy, setBusy] = useState("");
340
  const [error, setError] = useState("");
 
341
 
342
  const plans = cloud.plans.filter((p) => p.active !== false).sort((a, b) => (a.sort || 0) - (b.sort || 0));
343
  const oneOff = plans.filter((p) => p.kind === "one_time");
344
  const subs = plans.filter((p) => p.kind === "subscription");
345
 
346
+ const buy = (plan) => {
347
  setError("");
348
+ setBusy("");
349
  if (!cloud.user) {
350
  window.location.hash = "#/account";
351
  return;
352
  }
353
+ window.location.hash = "#/pay/" + plan.id;
 
 
 
 
 
 
 
 
 
354
  };
355
 
356
  return html`<main class="page">
 
373
  <${Notice} kind="warn">This is the preview shop front — connect Supabase and Stripe in the developer console to take real payments.<//>
374
  </div>`}
375
  ${error && html`<div style=${{ marginTop: "14px" }}><${Notice} kind="error">${error}<//></div>`}
 
376
 
377
  <div class="free-strip">
378
  <div>
styles.css CHANGED
@@ -430,3 +430,121 @@ a { color: var(--accent); text-decoration: none; }
430
  .price-title { font-size: 33px; }
431
  .price-lede { font-size: 13px; }
432
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
  .price-title { font-size: 33px; }
431
  .price-lede { font-size: 13px; }
432
  }
433
+
434
+ /* ---------- Checkout ---------- */
435
+ .checkout {
436
+ display: grid;
437
+ grid-template-columns: 310px minmax(0, 1fr);
438
+ gap: 16px;
439
+ align-items: start;
440
+ margin-top: 16px;
441
+ }
442
+ .checkout-summary { position: sticky; top: 70px; }
443
+ .checkout-plan {
444
+ font-family: var(--serif);
445
+ font-size: 22px;
446
+ font-weight: 600;
447
+ letter-spacing: -.02em;
448
+ margin-top: 8px;
449
+ }
450
+ .checkout-price {
451
+ font-size: 29px;
452
+ font-weight: 600;
453
+ letter-spacing: -.02em;
454
+ margin-top: 2px;
455
+ }
456
+ .checkout-price span {
457
+ font-size: 12px;
458
+ font-weight: 500;
459
+ color: var(--faint);
460
+ letter-spacing: 0;
461
+ }
462
+ .pay-step {
463
+ border: 1px solid var(--hairline);
464
+ border-radius: 14px;
465
+ background: #fff;
466
+ padding: 16px;
467
+ margin-bottom: 12px;
468
+ }
469
+ .pay-step-head { display: flex; gap: 10px; align-items: flex-start; }
470
+ .pay-step-num {
471
+ width: 22px;
472
+ height: 22px;
473
+ border-radius: 50%;
474
+ background: var(--accent-soft);
475
+ color: var(--accent);
476
+ display: grid;
477
+ place-items: center;
478
+ font-size: 11px;
479
+ font-weight: 700;
480
+ flex: none;
481
+ }
482
+ .pay-step-title {
483
+ font-family: var(--serif);
484
+ font-size: 16px;
485
+ font-weight: 600;
486
+ letter-spacing: -.01em;
487
+ }
488
+ .pay-grid {
489
+ display: grid;
490
+ grid-template-columns: repeat(3, minmax(0, 1fr));
491
+ gap: 8px;
492
+ margin-top: 14px;
493
+ }
494
+ .pay-field {
495
+ border: 1px solid var(--hairline);
496
+ border-radius: 10px;
497
+ padding: 9px 11px;
498
+ background: var(--deep);
499
+ min-width: 0;
500
+ }
501
+ .pay-field .lab {
502
+ display: block;
503
+ font-size: 9px;
504
+ letter-spacing: .07em;
505
+ text-transform: uppercase;
506
+ color: var(--faint);
507
+ font-weight: 700;
508
+ }
509
+ .pay-field .val {
510
+ display: block;
511
+ font-family: var(--mono);
512
+ font-size: 13px;
513
+ font-weight: 600;
514
+ margin-top: 3px;
515
+ overflow: hidden;
516
+ text-overflow: ellipsis;
517
+ }
518
+ .pay-ref {
519
+ display: flex;
520
+ align-items: center;
521
+ justify-content: space-between;
522
+ gap: 10px;
523
+ margin-top: 10px;
524
+ padding: 11px 12px;
525
+ border: 1px dashed #cfdcd4;
526
+ background: #fbfdfb;
527
+ border-radius: 10px;
528
+ }
529
+ .pay-ref .lab {
530
+ display: block;
531
+ font-size: 9px;
532
+ letter-spacing: .07em;
533
+ text-transform: uppercase;
534
+ color: var(--faint);
535
+ font-weight: 700;
536
+ }
537
+ .pay-ref .val {
538
+ font-family: var(--mono);
539
+ font-size: 15px;
540
+ font-weight: 700;
541
+ letter-spacing: .04em;
542
+ }
543
+ @media (max-width: 900px) {
544
+ .checkout { grid-template-columns: 1fr; }
545
+ .checkout-summary { position: static; }
546
+ .pay-grid { grid-template-columns: 1fr 1fr; }
547
+ }
548
+ @media (max-width: 520px) {
549
+ .pay-grid { grid-template-columns: 1fr; }
550
+ }