Files
0pt/app/api/payments/[order_id]/deposit/route.ts
T
admin c7fd9d45a2
CI (SIAX Cloud) / integration (push) Has been skipped
masterplan-lock / check (pull_request) Successful in 3s
masterplan-lock / check (push) Successful in 3s
CI (SIAX Cloud) / security (push) Successful in 15s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 18s
CI (SIAX Cloud) / quality (pull_request) Successful in 31s
CI (SIAX Cloud) / quality (push) Successful in 40s
CI (SIAX Cloud) / security (pull_request) Successful in 42s
CI (SIAX Cloud) / integration (pull_request) Successful in 45s
fix(prod): map SiaxError in the 7 legacy money routes — DEPENDENCY_UNAVAILABLE/503 instead of opaque 500 (0pt#53)
getSqlForDurableStore throws SiaxError(DEPENDENCY_UNAVAILABLE, httpStatus 503,
retryable) when a money-bearing store would fall back to an in-memory Map in a
production runtime (the durable-store guard). The /api/0pt/* surface already
maps it via src/http/siax-error-response.ts; the seven legacy routes outside
the middleware matcher did not — the documented 503 leaked as Next's opaque
500 with no body.

- app/api/siax-error-mapping.ts: shared isSiaxError + siaxErrorResponseForLegacy
- orders (POST/GET), orders/[id] (GET/PATCH), subscriptions (GET/POST),
  payments/deposit, payments/release, admin/metrics: try/catch -> mapper,
  each route's own typed error contract preserved (idempotency conflict 409 etc.)
- webhooks/stripe: store outage -> 503 received:false reason:store_unavailable
  so Stripe's delivery retry path stays open (never a silent received:true)
- tests/api/legacy-routes-siax-error-mapping.test.ts: pins the 503 + body
2026-09-22 04:33:57 +02:00

68 lines
2.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getOwnedOrder } from "@/lib/orders-store";
import { requireOrderOwner } from "@/src/auth/require-order-owner";
import { isSiaxError, siaxErrorResponseForLegacy } from "../../../siax-error-mapping";
// POST /api/payments/[order_id]/deposit
// Returns { url } for Stripe Checkout. Returns 501 when STRIPE_SECRET_KEY is absent.
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ order_id: string }> },
) {
const auth = await requireOrderOwner(request);
if (auth.response) return auth.response;
const { order_id } = await params;
const secretKey = process.env.STRIPE_SECRET_KEY;
if (!secretKey) {
return NextResponse.json({ error: "payment_not_configured" }, { status: 501 });
}
// Never derive from the client-supplied Origin header (SSRF + open-redirect vector) —
// request.nextUrl.origin reflects the actual request URL, not attacker-controlled input.
const origin = request.nextUrl.origin;
let order;
try {
order = await getOwnedOrder(order_id, auth.owner.tenantId);
} catch (err: unknown) {
// 0pt#53: durable-store failure -> documented DEPENDENCY_UNAVAILABLE/503.
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
throw err;
}
if (!order) {
return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
}
const depositOre = order.deposit_ore ?? Math.round((order.total_ore ?? 89500) / 2);
const body = new URLSearchParams({
"payment_method_types[0]": "card",
"line_items[0][price_data][currency]": "sek",
"line_items[0][price_data][product_data][name]": "0pt data deposit",
"line_items[0][price_data][unit_amount]": String(depositOre),
"line_items[0][quantity]": "1",
mode: "payment",
"metadata[order_id]": order_id,
"metadata[kind]": "deposit",
success_url: `${origin}/order/${order_id}?deposit=success`,
cancel_url: `${origin}/bestall/forskott?order=${order_id}`,
});
const stripeRes = await fetch("https://api.stripe.com/v1/checkout/sessions", {
method: "POST",
headers: {
Authorization: `Bearer ${secretKey}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body,
});
if (!stripeRes.ok) {
const err = (await stripeRes.json()) as { error?: { message?: string } };
return NextResponse.json({ error: err.error?.message ?? "stripe_error" }, { status: 502 });
}
const session = (await stripeRes.json()) as { url: string };
return NextResponse.json({ url: session.url });
}