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
6 changes: 4 additions & 2 deletions api/src/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,11 @@ export async function changePlan(request: Request, env: Env, now: number) {
return json(publicPlan(row));
}

export async function manageBilling(request: Request, env: Env, now: number) {
export async function manageBilling(request: Request, env: Env, now: number, origin: string) {
const { id } = await requireMember(request, env, now);
const answer = await paygate(env, "POST", "/billing-link", { external_id: id });
// The hosted page's "Back to Floodwall" link: the account page on the site the reader came from
// (one of ALLOWED_ORIGINS, which Paygate also checks against the app's allowed origins).
const answer = await paygate(env, "POST", "/billing-link", { external_id: id, return_url: `${origin}/account` });
if (answer.status === 404) throw new HttpError(404, MESSAGES.noBilling);
if (answer.status !== 200 && answer.status !== 201) paygateFailed("billing link", answer);
return json({ url: answer.body.url });
Expand Down
2 changes: 1 addition & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function route(request: Request, env: Env, origin: string | null) {
case "POST /billing/change":
return changePlan(request, env, now);
case "POST /billing/manage":
return manageBilling(request, env, now);
return manageBilling(request, env, now, origin!);
case "GET /billing/promo":
return lookupCode(request, env, now);
case "GET /billing/credits":
Expand Down
2 changes: 1 addition & 1 deletion api/test/billing.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("billing through Paygate", () => {
const r = await w.call("/billing/manage", { body: {}, cookie });
assert.equal(r.status, 200);
assert.equal(r.body.url, "https://pay.erpai.studio/b/floodwall?t=abc");
assert.deepEqual(w.paygateCalls[0].body, { external_id: await userId(ADMIN) });
assert.deepEqual(w.paygateCalls[0].body, { external_id: await userId(ADMIN), return_url: `${SITE}/account` });

answer = () => Response.json({ error: { code: "not_found" } }, { status: 404 });
assert.equal((await w.call("/billing/manage", { body: {}, cookie })).status, 404);
Expand Down
Loading