fix: advance orders only after matching paid Stripe events
masterplan-lock / check (pull_request) Successful in 5s
masterplan-lock / check (push) Successful in 8s
CI (SIAX Cloud) / security (push) Successful in 18s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
Security Scan (SIAX Cloud) / security (push) Successful in 18s
CI (SIAX Cloud) / security (pull_request) Successful in 18s
CI (SIAX Cloud) / integration (push) Successful in 28s
CI (SIAX Cloud) / quality (push) Successful in 36s
CI (SIAX Cloud) / integration (pull_request) Successful in 48s
CI (SIAX Cloud) / quality (pull_request) Successful in 56s

This commit is contained in:
2026-09-15 18:15:42 +02:00
parent 8911709931
commit ae190fa60d
7 changed files with 178 additions and 6 deletions
+31 -3
View File
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import type Stripe from "stripe";
import { getStripe } from "@/lib/stripe";
import { setStatus } from "@/lib/orders-store";
import { getOrder, setStatus } from "@/lib/orders-store";
// POST /api/webhooks/stripe (docs/04) — verify signature, advance order status.
export async function POST(req: NextRequest) {
@@ -23,10 +24,37 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "bad_signature" }, { status: 400 });
}
if (event.type === "checkout.session.completed") {
const s = event.data.object as { metadata?: { order_id?: string; kind?: string } };
const s = event.data.object as Stripe.Checkout.Session;
const oid = s.metadata?.order_id;
const kind = s.metadata?.kind;
if (oid) await setStatus(oid, kind === "balance" ? "delivered" : "in_production");
if (!oid || (kind !== "deposit" && kind !== "balance")) {
return NextResponse.json({ error: "unmatched_session" }, { status: 422 });
}
if (s.payment_status !== "paid") {
return NextResponse.json({ error: "payment_not_confirmed" }, { status: 503 });
}
const order = await getOrder(oid);
if (!order) return NextResponse.json({ error: "order_not_found" }, { status: 503 });
const expectedAmount = kind === "balance" ? order.balance_ore : order.deposit_ore;
if (s.amount_total !== expectedAmount || s.currency?.toLowerCase() !== "sek") {
return NextResponse.json({ error: "payment_mismatch" }, { status: 422 });
}
if (kind === "balance") {
if (order.status !== "accepted" && order.status !== "delivered") {
return NextResponse.json({ error: "invalid_order_state" }, { status: 409 });
}
if (order.status === "accepted" && !(await setStatus(oid, "delivered"))) {
return NextResponse.json({ error: "order_update_failed" }, { status: 503 });
}
} else {
const alreadyAdvanced = ["in_production", "review", "revision", "accepted", "delivered"].includes(order.status);
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"))) {
return NextResponse.json({ error: "order_update_failed" }, { status: 503 });
}
}
}
return NextResponse.json({ received: true });
}