railyard/apps/console/components/BillingActions.tsx
caelum0x f919636b62 fix(console): one trial per org, no second subscription, refunds and stale subscription events
Wave E verifier (Y-2/Y-3):
- A card check started a fresh 14-day trial every time: cancel or refund,
  pay $1 again (credited back) and get another trial, for $0 net. The
  trial now starts only when trial_ends_at is unset; refunds keep it set
  and a paid plan marks it used. access() sends a card-on-file org that
  used its trial to the plan, and the Billing screen says so.
- startCheckout opened a second plan checkout for an already subscribed
  (or cancelling) org, which bills twice; and a second card check for an
  org with a card on file. Both are refused (BillingError, ?error=already).
- A refund of an older card-check payment kept its dollar credit, and a
  second refund event for the same payment could take it back twice. The
  debit now always applies once per payment, tracked in billing_events.
- subscription.* events of a subscription that is not the org's current
  one (an old one expiring) no longer end or pause the current plan.
- Deploy records normalise the app name the CLI sends (folder 'My Shop'
  -> my-shop) so the second deploy is not a 409; rollback_of must be a
  uuid (was a database error).
Tests: 7 new (fail before, pass after); console 37/37, tsc clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 16:50:14 +03:00

78 lines
3.1 KiB
TypeScript

"use client";
import { useState } from "react";
type State = "no_card" | "trial" | "trial_ended" | "active" | "paused" | "cancelling" | "cancelled";
export function BillingActions(props: {
state: State;
trialDaysLeft: number | null;
interval: "month" | "year";
paused: boolean;
hasCard?: boolean;
trialUsed?: boolean;
credit: string;
monthPrice: string;
yearPrice: string;
}) {
const [interval, setInterval] = useState<"month" | "year">(props.interval);
const subscribeForm = (
<form method="post" action="/api/billing/checkout" className="row">
<input type="hidden" name="kind" value="plan" />
<input type="hidden" name="interval" value={interval} />
<div className="switch" role="group" aria-label="Billing period">
<button type="button" aria-pressed={interval === "month"} onClick={() => setInterval("month")}>Monthly</button>
<button type="button" aria-pressed={interval === "year"} onClick={() => setInterval("year")}>Yearly</button>
</div>
<button className="primary" type="submit">
Subscribe, {interval === "year" ? `${props.yearPrice} a year` : `${props.monthPrice} a month`}
</button>
</form>
);
if (props.state === "cancelled" && props.hasCard) {
return (
<>
<h2>Cancelled</h2>
<p className="muted">Your card is on file. Subscribe to turn Railyard back on.</p>
<p className="muted">Credit on your account: {props.credit}</p>
{subscribeForm}
</>
);
}
if (props.state === "no_card" || props.state === "cancelled") {
return (
<>
<h2>Start with a $1 card check</h2>
<p className="muted">
{props.trialUsed
? "The dollar is credited back to your account, then you can subscribe. The free trial was already used."
: "The dollar is credited back to your account, then you get the full plan for 14 days."}
</p>
<form method="post" action="/api/billing/checkout">
<input type="hidden" name="kind" value="card_check" />
<button className="primary" type="submit">Add a card</button>
</form>
</>
);
}
return (
<>
<h2>
{props.state === "trial" ? `Trial: ${props.trialDaysLeft} days left` : props.state === "trial_ended" ? "Trial ended" : props.state === "paused" ? "Paused" : props.state === "cancelling" ? "Ends at the next billing date" : "Railyard plan"}
</h2>
<p className="muted">Credit on your account: {props.credit}</p>
{props.state === "trial" || props.state === "trial_ended" ? subscribeForm : null}
<div className="row" style={{ marginTop: 18 }}>
<form method="post" action="/api/billing/pause">
<input type="hidden" name="paused" value={props.paused ? "0" : "1"} />
<button className="ghost" type="submit" aria-pressed={props.paused}>{props.paused ? "Resume" : "Pause"}</button>
</form>
{props.state !== "cancelling" && props.state !== "trial_ended" ? (
<form method="post" action="/api/billing/cancel">
<button className="ghost" type="submit">Cancel</button>
</form>
) : null}
</div>
</>
);
}