import { NextRequest, NextResponse } from "next/server"; import { randomUUID } from "crypto"; import { preferenceClient } from "../lib/mercadopago"; import { bridgePost, bridgePatch } from "../lib/bridge"; const PRICE_MXN = Number(process.env.AFC_PRICE_MXN) || 15; const BASE_URL = process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000"; export async function POST(req: NextRequest) { try { const { diskId, amountAfc } = await req.json(); if (!diskId || !amountAfc || amountAfc < 1) { return NextResponse.json( { error: "diskId and amountAfc (>=1) required" }, { status: 400 } ); } const amountMxn = amountAfc * PRICE_MXN; const paymentId = randomUUID(); // Create payment record in bridge await bridgePost("/api/payments", { id: paymentId, diskId, amountAfc, amountMxn, }); // Create MercadoPago preference const preference = await preferenceClient.create({ body: { items: [ { id: paymentId, title: `${amountAfc} AfterCoin (AFC)`, quantity: 1, unit_price: amountMxn, currency_id: "MXN", }, ], external_reference: paymentId, back_urls: { success: `${BASE_URL}/afc/buy/success?payment_id=${paymentId}`, failure: `${BASE_URL}/afc/buy/failure?payment_id=${paymentId}`, pending: `${BASE_URL}/afc/buy/pending?payment_id=${paymentId}`, }, auto_return: "approved", notification_url: process.env.MERCADOPAGO_WEBHOOK_URL || `${BASE_URL}/api/afc/webhook`, }, }); // Store the MP preference ID await bridgePatch(`/api/payments/${paymentId}`, { mp_preference_id: preference.id, }); return NextResponse.json({ paymentId, initPoint: preference.init_point, sandboxInitPoint: preference.sandbox_init_point, }); } catch (e: unknown) { const message = e instanceof Error ? e.message : "Unknown error"; console.error("create-preference error:", e); return NextResponse.json({ error: message }, { status: 500 }); } }