Env-guarded so build stays green: /api/payments/[order_id]/deposit+release + /api/webhooks/stripe (lazy Stripe, 50/50 escrow, 501 until keys); /api/auth login|me|logout (HMAC signed-session scaffold, guarded dev login); lib/engine bridges edge->FastAPI (estimates defers to ENGINE_URL when set); components/embed for NocoDB/Metabase signed URLs; .env.example full inventory. tsc 0, build green. Co-Authored-By: Claude Opus 4.8 <[email protected]>
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getStripe } from "@/lib/stripe";
|
|
import { getOrder, setStatus } from "@/lib/orders-store";
|
|
|
|
// 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 { 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 });
|
|
if (order.status !== "accepted") {
|
|
return NextResponse.json({ error: { code: "not_accepted", message: "Balans frigörs först vid godkänd leverans" } }, { status: 409 });
|
|
}
|
|
|
|
const stripe = getStripe();
|
|
if (!stripe) {
|
|
return NextResponse.json(
|
|
{ error: { code: "stripe_not_configured", message: "Stripe ej konfigurerad" } },
|
|
{ status: 501 },
|
|
);
|
|
}
|
|
|
|
const origin = req.nextUrl.origin;
|
|
const session = await stripe.checkout.sessions.create({
|
|
mode: "payment",
|
|
line_items: [{
|
|
quantity: 1,
|
|
price_data: {
|
|
currency: "sek",
|
|
unit_amount: order.balance_ore,
|
|
product_data: { name: `0pt order ${order_id} — slutbetalning 50 %` },
|
|
},
|
|
}],
|
|
success_url: `${origin}/order/${order_id}/validera`,
|
|
cancel_url: `${origin}/order/${order_id}/validera`,
|
|
metadata: { order_id, kind: "balance" },
|
|
});
|
|
await setStatus(order_id, "delivered");
|
|
return NextResponse.json({ checkout_url: session.url });
|
|
}
|