Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ in production, shared with every page as the `apiOrigin` prop and read through
| Endpoint | Sends | Returns |
| --- | --- | --- |
| `GET /v2/pricing` | nothing | `{country, plans: [{plan, cycle, currency, amount, per_seat, min_seats, max_seats, features, discount_percent, discounted_amount, discounted_total}], education: {open}}` |
| `POST /v2/checkouts` | `{plan, cycle, locale, seats?, discount_code?, attribution?}` | 201 `{url}`, opened in Polar's overlay |
| `POST /v2/checkouts` | `{plan, cycle, locale, seats?, discount_code?, attribution?}` | 201 `{url}`, opened in Polar's overlay; for a Team guest, 403 `sign_in_required` with `sign_in_url`, which the page goes to |
| `POST /v2/discounts/preview` | `{code}` | `{valid, amount_type?, amount?, currency?}` |
| `POST /v2/newsletter/subscriptions` | `{email, locale, turnstile_token}` | 202, whether or not the address was new |
| `GET /v2/newsletter/stats` | nothing | `{subscribers}` |
Expand Down
10 changes: 9 additions & 1 deletion resources/js/components/pricing/checkout-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export interface CheckoutStrings {
seats: string;
}

export type CheckoutOutcome = { kind: 'open'; url: string } | { kind: 'error'; message: string };
export type CheckoutOutcome =
| { kind: 'open'; url: string }
// A guest's Team checkout: the portal's sign-in, which comes back to the purchase.
| { kind: 'signIn'; url: string }
| { kind: 'error'; message: string };

export function checkoutOutcome(status: number, data: unknown, strings: CheckoutStrings): CheckoutOutcome {
if (status >= 200 && status < 300) {
Expand All @@ -24,6 +28,10 @@ export function checkoutOutcome(status: number, data: unknown, strings: Checkout

const problem = problemOf(data);

if (status === 403 && problem.code === 'sign_in_required' && problem.signInUrl?.startsWith('https://')) {
return { kind: 'signIn', url: problem.signInUrl };
}

if (status === 422 && problem.code === 'plan_unavailable') {
return { kind: 'error', message: strings.planUnavailable };
}
Expand Down
6 changes: 6 additions & 0 deletions resources/js/components/pricing/use-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export function useCheckout() {
return;
}

if (outcome.kind === 'signIn') {
window.location.assign(outcome.url);

return;
}

try {
await withTimeout(sdk, SDK_WAIT_MS);
await openCheckoutOverlay(outcome.url, overlayTheme());
Expand Down
5 changes: 4 additions & 1 deletion resources/js/lib/problem.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// tablepro-cloud answers errors as RFC 9457 problems: a stable `code`, and for `validation_failed` the fields in `errors[].location`.
// tablepro-cloud answers errors as RFC 9457 problems: a stable `code`, for `validation_failed` the fields in `errors[].location`,
// and for `sign_in_required` the `sign_in_url` to go to.
// Their `title` and `detail` are English, so the pages show their own catalog's words instead.

export interface Problem {
code: string | null;
locations: string[];
signInUrl: string | null;
}

export function problemOf(data: unknown): Problem {
Expand All @@ -17,5 +19,6 @@ export function problemOf(data: unknown): Problem {

return typeof location === 'string' ? [location] : [];
}),
signInUrl: typeof body.sign_in_url === 'string' ? body.sign_in_url : null,
};
}
18 changes: 18 additions & 0 deletions tests/js/checkout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ test('each problem code api.md lists for POST /v2/checkouts has its own message,
assert.deepEqual(checkoutOutcome(422, null, strings), { kind: 'error', message: strings.failed });
});

test('a guest who picks Team goes to sign in when the answer names an https sign-in URL', () => {
const signIn = 'https://account.tablepro.app/teams/new?cycle=yearly&locale=vi&seats=7';
const failed = { kind: 'error', message: strings.failed };

assert.deepEqual(checkoutOutcome(403, problem(403, 'sign_in_required', { sign_in_url: signIn }), strings), { kind: 'signIn', url: signIn });

// Anything short of a sign_in_required 403 with an https URL is a checkout that did not start.
assert.deepEqual(checkoutOutcome(403, problem(403, 'sign_in_required'), strings), failed);
assert.deepEqual(checkoutOutcome(403, problem(403, 'sign_in_required', { sign_in_url: 'http://account.tablepro.app/teams/new' }), strings), failed);
assert.deepEqual(checkoutOutcome(403, problem(403, 'sign_in_required', { sign_in_url: 'javascript:alert(1)' }), strings), failed);
assert.deepEqual(checkoutOutcome(403, problem(403, 'sign_in_required', { sign_in_url: 42 }), strings), failed);
assert.deepEqual(checkoutOutcome(403, problem(403, 'forbidden', { sign_in_url: signIn }), strings), failed);
assert.deepEqual(checkoutOutcome(422, problem(422, 'sign_in_required', { sign_in_url: signIn }), strings), failed);

// An API that still sells Team to guests answers with Polar's checkout, which opens as before.
assert.deepEqual(checkoutOutcome(201, { url: 'https://polar.sh/checkout/team' }, strings), { kind: 'open', url: 'https://polar.sh/checkout/team' });
});

test('a 422 on seats names the range only once /v2/pricing gave a maximum, and otherwise asks for fewer', () => {
const range = (max: number) => `Choose between 5 and ${max} seats.`;

Expand Down