fix: close demo payment bypass and guard order transitions
masterplan-lock / check (pull_request) Successful in 4s
CI (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (pull_request) Successful in 15s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / integration (pull_request) Successful in 26s
masterplan-lock / check (push) Successful in 35s
CI (SIAX Cloud) / quality (pull_request) Successful in 37s
Security Scan (SIAX Cloud) / security (push) Successful in 1m4s
CI (SIAX Cloud) / integration (push) Successful in 1m14s
CI (SIAX Cloud) / quality (push) Successful in 2m12s

This commit is contained in:
2026-09-15 18:52:23 +02:00
parent ae190fa60d
commit 7ce92827c0
10 changed files with 149 additions and 29 deletions
+23 -4
View File
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import { getOrder, setStatus } from "@/lib/orders-store";
import { getOrder, setStatusIfCurrent } from "@/lib/orders-store";
import { requireRouteAuth } from "@/src/auth/require-route-auth";
// GET /api/orders/{id}
export async function GET(_req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
@@ -12,6 +13,8 @@ export async function GET(_req: NextRequest, ctx: { params: Promise<{ id: string
// PATCH /api/orders/{id} — status transition (deposit → in_production → review → accepted ...).
// Escrow rule (docs/01): balance never charged before 'accepted'. Stripe wiring follows.
export async function PATCH(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
const auth = await requireRouteAuth(req);
if (auth.response) return auth.response;
const { id } = await ctx.params;
let body: { status?: string };
try {
@@ -22,11 +25,27 @@ export async function PATCH(req: NextRequest, ctx: { params: Promise<{ id: strin
if (!body || typeof body !== "object") {
return NextResponse.json({ error: { code: "bad_json", message: "Invalid JSON body" } }, { status: 400 });
}
const allowed = ["draft", "quoted", "deposit_pending", "in_production", "review", "revision", "accepted", "delivered"];
// Paid states are owned exclusively by the signed, matching Stripe webhook.
if (body.status === "in_production" || body.status === "delivered") {
return NextResponse.json({ error: { code: "payment_event_required" } }, { status: 403 });
}
const allowed = ["quoted", "deposit_pending", "review", "revision", "accepted"];
if (!body.status || !allowed.includes(body.status)) {
return NextResponse.json({ error: { code: "bad_status", message: "Ogiltig status" } }, { status: 400 });
}
const order = await setStatus(id, body.status);
const order = await getOrder(id);
if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
return NextResponse.json(order);
const previous: Record<string, string[]> = {
quoted: ["draft"],
deposit_pending: ["draft", "quoted"],
review: ["in_production"],
revision: ["review"],
accepted: ["review", "revision"],
};
if (!previous[body.status]?.includes(order.status)) {
return NextResponse.json({ error: { code: "invalid_transition" } }, { status: 409 });
}
const updated = await setStatusIfCurrent(id, order.status, body.status);
if (!updated) return NextResponse.json({ error: { code: "stale_transition" } }, { status: 409 });
return NextResponse.json(updated);
}
@@ -1,10 +1,13 @@
import { NextRequest, NextResponse } from "next/server";
import { getStripe } from "@/lib/stripe";
import { getOrder } from "@/lib/orders-store";
import { requireRouteAuth } from "@/src/auth/require-route-auth";
// POST /api/payments/{order_id}/release (docs/04) — charge the held 50% balance.
// Called on ACCEPT only; escrow balance is never charged before approval.
export async function POST(req: NextRequest, ctx: { params: Promise<{ order_id: string }> }) {
const auth = await requireRouteAuth(req);
if (auth.response) return auth.response;
const { order_id } = await ctx.params;
const order = await getOrder(order_id);
if (!order) return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
+3 -3
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import type Stripe from "stripe";
import { getStripe } from "@/lib/stripe";
import { getOrder, setStatus } from "@/lib/orders-store";
import { getOrder, setStatusIfCurrent } from "@/lib/orders-store";
// POST /api/webhooks/stripe (docs/04) — verify signature, advance order status.
export async function POST(req: NextRequest) {
@@ -43,7 +43,7 @@ export async function POST(req: NextRequest) {
if (order.status !== "accepted" && order.status !== "delivered") {
return NextResponse.json({ error: "invalid_order_state" }, { status: 409 });
}
if (order.status === "accepted" && !(await setStatus(oid, "delivered"))) {
if (order.status === "accepted" && !(await setStatusIfCurrent(oid, "accepted", "delivered"))) {
return NextResponse.json({ error: "order_update_failed" }, { status: 503 });
}
} else {
@@ -51,7 +51,7 @@ export async function POST(req: NextRequest) {
if (order.status !== "draft" && order.status !== "deposit_pending" && !alreadyAdvanced) {
return NextResponse.json({ error: "invalid_order_state" }, { status: 409 });
}
if (!alreadyAdvanced && !(await setStatus(oid, "in_production"))) {
if (!alreadyAdvanced && !(await setStatusIfCurrent(oid, order.status, "in_production"))) {
return NextResponse.json({ error: "order_update_failed" }, { status: 503 });
}
}