diff --git a/api/src/billing.ts b/api/src/billing.ts index c3e1c5e..3bbd788 100644 --- a/api/src/billing.ts +++ b/api/src/billing.ts @@ -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 }); diff --git a/api/src/index.ts b/api/src/index.ts index decf3a9..6296489 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -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": diff --git a/api/test/billing.test.mjs b/api/test/billing.test.mjs index f30654e..958632e 100644 --- a/api/test/billing.test.mjs +++ b/api/test/billing.test.mjs @@ -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);