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
45 lines
1.8 KiB
TypeScript
45 lines
1.8 KiB
TypeScript
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 });
|
|
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" },
|
|
});
|
|
// Creating a Checkout Session is not payment. Only a verified paid webhook
|
|
// may advance the order to delivered.
|
|
return NextResponse.json({ checkout_url: session.url });
|
|
}
|