fix(recovery): restore source removed by production documentation merge
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getOwnedOrder } from "@/lib/orders-store";
|
||||
import { requireOrderOwner } from "@/src/auth/require-order-owner";
|
||||
import { isSiaxError, siaxErrorResponseForLegacy } from "../../../siax-error-mapping";
|
||||
|
||||
// POST /api/payments/[order_id]/deposit
|
||||
// Returns { url } for Stripe Checkout. Returns 501 when STRIPE_SECRET_KEY is absent.
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ order_id: string }> },
|
||||
) {
|
||||
const auth = await requireOrderOwner(request);
|
||||
if (auth.response) return auth.response;
|
||||
const { order_id } = await params;
|
||||
|
||||
const secretKey = process.env.STRIPE_SECRET_KEY;
|
||||
if (!secretKey) {
|
||||
return NextResponse.json({ error: "payment_not_configured" }, { status: 501 });
|
||||
}
|
||||
|
||||
// Never derive from the client-supplied Origin header (SSRF + open-redirect vector) —
|
||||
// request.nextUrl.origin reflects the actual request URL, not attacker-controlled input.
|
||||
const origin = request.nextUrl.origin;
|
||||
|
||||
let order;
|
||||
try {
|
||||
order = await getOwnedOrder(order_id, auth.owner.tenantId);
|
||||
} catch (err: unknown) {
|
||||
// 0pt#53: durable-store failure -> documented DEPENDENCY_UNAVAILABLE/503.
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
}
|
||||
if (!order) {
|
||||
return NextResponse.json({ error: { code: "not_found", message: "Order saknas" } }, { status: 404 });
|
||||
}
|
||||
const depositOre = order.deposit_ore ?? Math.round((order.total_ore ?? 89500) / 2);
|
||||
|
||||
const body = new URLSearchParams({
|
||||
"payment_method_types[0]": "card",
|
||||
"line_items[0][price_data][currency]": "sek",
|
||||
"line_items[0][price_data][product_data][name]": "0pt data deposit",
|
||||
"line_items[0][price_data][unit_amount]": String(depositOre),
|
||||
"line_items[0][quantity]": "1",
|
||||
mode: "payment",
|
||||
"metadata[order_id]": order_id,
|
||||
"metadata[kind]": "deposit",
|
||||
success_url: `${origin}/order/${order_id}?deposit=success`,
|
||||
cancel_url: `${origin}/bestall/forskott?order=${order_id}`,
|
||||
});
|
||||
|
||||
const stripeRes = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${secretKey}`,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body,
|
||||
});
|
||||
|
||||
if (!stripeRes.ok) {
|
||||
const err = (await stripeRes.json()) as { error?: { message?: string } };
|
||||
return NextResponse.json({ error: err.error?.message ?? "stripe_error" }, { status: 502 });
|
||||
}
|
||||
|
||||
const session = (await stripeRes.json()) as { url: string };
|
||||
return NextResponse.json({ url: session.url });
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getStripe } from "@/lib/stripe";
|
||||
import { getOwnedOrder } from "@/lib/orders-store";
|
||||
import { requireOrderOwner } from "@/src/auth/require-order-owner";
|
||||
import { isSiaxError, siaxErrorResponseForLegacy } from "../../../siax-error-mapping";
|
||||
|
||||
// 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 requireOrderOwner(req);
|
||||
if (auth.response) return auth.response;
|
||||
const { order_id } = await ctx.params;
|
||||
let order;
|
||||
try {
|
||||
order = await getOwnedOrder(order_id, auth.owner.tenantId);
|
||||
} catch (err: unknown) {
|
||||
// 0pt#53: durable-store failure -> documented DEPENDENCY_UNAVAILABLE/503.
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
}
|
||||
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 });
|
||||
}
|
||||
Reference in New Issue
Block a user