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 });
}
}
+11 -13
View File
@@ -1,7 +1,6 @@
"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { OrderStepper } from "@/components/order-stepper";
import { useT } from "@/components/locale-provider";
@@ -58,8 +57,8 @@ export default function DepositPage() {
});
const [orderId, setOrderId] = useState<string | null>(null);
const [paying, setPaying] = useState(false);
const [paymentError, setPaymentError] = useState(false);
const reveal = useReveal<HTMLDivElement>();
const router = useRouter();
useEffect(() => {
try {
@@ -103,6 +102,7 @@ export default function DepositPage() {
async function pay() {
if (paying) return;
setPaying(true);
setPaymentError(false);
if (orderId) {
try {
const r = await fetch(`/api/payments/${orderId}/deposit`, { method: "POST" });
@@ -111,18 +111,11 @@ export default function DepositPage() {
if (body.url) { window.location.href = body.url; return; }
}
} catch {}
// Stripe not configured — advance status directly (demo mode)
try {
await fetch(`/api/orders/${orderId}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ status: "in_production" }),
});
} catch {}
router.push(`/order/${orderId}`);
return;
}
router.push("/order/SX-4471");
// No Checkout URL means no payment. Keep the draft and let the customer
// retry; never simulate a successful payment or redirect to a demo order.
setPaymentError(true);
setPaying(false);
}
return (
@@ -283,6 +276,11 @@ export default function DepositPage() {
>
{paying ? t({ en: "Starting…", sv: "Startar…" }) : t({ en: "Pay. Start. →", sv: "Betala. Starta. →" })}
</button>
{paymentError && (
<p role="alert" className="mt-3 text-sm text-[var(--accent-red)]">
{t({ en: "Payment could not be started. Your order is still a draft; please try again.", sv: "Betalningen kunde inte startas. Din order är fortfarande ett utkast; försök igen." })}
</p>
)}
<Link
href="/bestall"
className="mt-4 block text-center font-mono text-[11px] uppercase tracking-[0.1em] text-muted-foreground underline-offset-4 transition-colors hover:text-foreground hover:underline"