Loading...
Loading...
Integrate MercadoPago Checkout Pro (redirect-based) into Next.js applications with any PostgreSQL database (Supabase, AWS RDS, Neon, PlanetScale, self-hosted, Prisma, Drizzle, or raw pg). Use when the user needs to: (1) Add MercadoPago payment processing to a Next.js app, (2) Create a checkout flow with MercadoPago, (3) Set up payment webhooks for MercadoPago, (4) Build payment success/failure pages, (5) Create a shopping cart with payment integration, (6) Troubleshoot MercadoPago integration issues (auto_return errors, webhook failures, hydration mismatches, double submissions). Triggers on requests mentioning MercadoPago, Mercado Pago, payment integration with MP, Argentine/Latin American payment processing, or checkout with MercadoPago. Supports all MercadoPago currencies (ARS, BRL, MXN, CLP, COP, PEN, UYU).
npx skill4agent add martinacostadev/mercadopago-integration mercadopago-integrationPay button --> POST /api/checkout (create preference) --> Redirect to MercadoPago
|
MercadoPago --webhook--> /api/webhooks/mercadopago --> Update DB
|
+--redirect--> /payment-success?purchase=ID --> Verify via APIreferences/database-supabase.mdreferences/database-prisma.mdreferences/database-postgresql.md| Detail | Why | Example |
|---|---|---|
| Currency | Preference creation | |
| Success/failure routes | | |
| Brand name | Card statement descriptor | |
| Product/item table | FK in | |
| Cart store location | Hook reads items from it | |
| DB client path | API routes import it | |
npm install mercadopago zodNEXT_PUBLIC_MERCADOPAGO_ACCESS_TOKEN=TEST-xxxx # from https://www.mercadopago.com/developers/panel/app
NEXT_PUBLIC_APP_URL=http://localhost:3000 # HTTPS in productionassets/migration.sqlsrc/lib/db/purchases.tsreferences/database-supabase.mdreferences/database-prisma.mdreferences/database-postgresql.mdinterface PurchaseInsert {
user_email: string;
status: 'pending';
total_amount: number;
}
interface PurchaseUpdate {
status?: 'pending' | 'approved' | 'rejected';
mercadopago_payment_id?: string;
mercadopago_preference_id?: string;
user_email?: string;
updated_at?: string;
}
// Required exports:
export async function createPurchase(data: PurchaseInsert): Promise<{ id: string }>;
export async function updatePurchase(id: string, data: PurchaseUpdate): Promise<void>;
export async function getPurchaseStatus(id: string): Promise<{ id: string; status: string } | null>;
export async function createPurchaseItems(purchaseId: string, items: { item_id: string; price: number }[]): Promise<void>;src/lib/mercadopago/client.tsimport { MercadoPagoConfig, Preference, Payment } from 'mercadopago';
const client = new MercadoPagoConfig({
accessToken: process.env.MERCADOPAGO_ACCESS_TOKEN!,
});
const preference = new Preference(client);
const payment = new Payment(client);
interface CreatePreferenceParams {
items: { id: string; title: string; quantity: number; unit_price: number }[];
purchaseId: string;
buyerEmail?: string;
}
export async function createPreference({
items, purchaseId, buyerEmail,
}: CreatePreferenceParams) {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000';
return preference.create({
body: {
items: items.map((item) => ({
id: item.id,
title: item.title,
quantity: item.quantity,
unit_price: item.unit_price,
currency_id: 'ARS', // Change per references/countries.md
})),
...(buyerEmail ? { payer: { email: buyerEmail } } : {}),
back_urls: {
success: `${baseUrl}/payment-success?purchase=${purchaseId}`,
failure: `${baseUrl}/payment-failure?purchase=${purchaseId}`,
pending: `${baseUrl}/payment-success?purchase=${purchaseId}&status=pending`,
},
// CRITICAL: auto_return requires HTTPS. Omit on localhost or MP returns 400.
...(baseUrl.startsWith('https') ? { auto_return: 'approved' as const } : {}),
external_reference: purchaseId,
notification_url: `${baseUrl}/api/webhooks/mercadopago`,
statement_descriptor: 'YOUR_BRAND', // Replace with user's brand
expires: true,
expiration_date_from: new Date().toISOString(),
expiration_date_to: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(),
},
});
}
export async function getPayment(paymentId: string) {
return payment.get({ id: paymentId });
}src/app/api/checkout/route.tsimport { NextResponse } from 'next/server';
import { createPurchase, updatePurchase } from '@/lib/db/purchases';
import { createPreference } from '@/lib/mercadopago/client';
import { z } from 'zod';
const checkoutSchema = z.object({
items: z.array(z.object({
id: z.string(),
title: z.string(),
quantity: z.number().positive(),
unit_price: z.number().positive(),
})).min(1),
email: z.string().email().optional(),
});
export async function POST(request: Request) {
try {
const body = await request.json();
const validation = checkoutSchema.safeParse(body);
if (!validation.success) {
return NextResponse.json({ error: 'Invalid request data' }, { status: 400 });
}
const { items, email } = validation.data;
const totalAmount = items.reduce((sum, i) => sum + i.unit_price * i.quantity, 0);
const purchase = await createPurchase({
user_email: email || 'pending@checkout',
status: 'pending',
total_amount: totalAmount,
});
const mpPreference = await createPreference({
items, purchaseId: purchase.id, buyerEmail: email,
});
await updatePurchase(purchase.id, {
mercadopago_preference_id: mpPreference.id,
});
return NextResponse.json({
preferenceId: mpPreference.id,
initPoint: mpPreference.init_point,
purchaseId: purchase.id,
});
} catch (error) {
console.error('Checkout error:', error);
return NextResponse.json({ error: 'Checkout failed' }, { status: 500 });
}
}src/app/api/webhooks/mercadopago/route.tsimport { NextResponse } from 'next/server';
import { getPurchaseStatus, updatePurchase } from '@/lib/db/purchases';
import { getPayment } from '@/lib/mercadopago/client';
export async function POST(request: Request) {
try {
const body = await request.json();
if (body.type !== 'payment' && body.action !== 'payment.created' && body.action !== 'payment.updated') {
return NextResponse.json({ received: true });
}
const paymentId = body.data?.id;
if (!paymentId) return NextResponse.json({ received: true });
const payment = await getPayment(paymentId.toString());
if (!payment?.external_reference) return NextResponse.json({ received: true });
let status: 'pending' | 'approved' | 'rejected' = 'pending';
if (payment.status === 'approved') status = 'approved';
else if (['rejected', 'cancelled', 'refunded'].includes(payment.status || '')) status = 'rejected';
// Idempotency: skip if already in terminal state
const existing = await getPurchaseStatus(payment.external_reference);
if (existing?.status === 'approved' || existing?.status === 'rejected') {
return NextResponse.json({ received: true });
}
const payerEmail = payment.payer?.email;
await updatePurchase(payment.external_reference, {
status,
mercadopago_payment_id: paymentId.toString(),
...(payerEmail ? { user_email: payerEmail } : {}),
updated_at: new Date().toISOString(),
});
return NextResponse.json({ received: true });
} catch (error) {
console.error('Webhook error:', error);
return NextResponse.json({ error: 'Webhook processing failed' }, { status: 500 });
}
}
// GET endpoint for MercadoPago verification pings
export async function GET() {
return NextResponse.json({ status: 'ok' });
}src/app/api/purchases/[id]/route.tsimport { NextResponse } from 'next/server';
import { getPurchaseStatus } from '@/lib/db/purchases';
export async function GET(
_request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const data = await getPurchaseStatus(id);
if (!data) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json({ id: data.id, status: data.status });
}src/hooks/useCheckout.tsuseRefuseState'use client';
import { useCallback, useRef, useState } from 'react';
export function useCheckout() {
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const guard = useRef(false);
const submitCheckout = useCallback(async (items: unknown[]) => {
if (guard.current) return;
setError(null);
guard.current = true;
setIsSubmitting(true);
try {
const res = await fetch('/api/checkout', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || 'Checkout failed');
if (data.initPoint) window.location.href = data.initPoint;
else throw new Error('No payment link returned');
} catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error');
setIsSubmitting(false);
guard.current = false;
}
}, []);
return { submitCheckout, isSubmitting, error };
}src/app/payment-success/page.tsxuseSearchParams<Suspense>'use client';
import { useSearchParams } from 'next/navigation';
import { Suspense, useCallback, useEffect, useState } from 'react';
type Status = 'loading' | 'approved' | 'pending' | 'rejected' | 'error';
function PaymentResult() {
const purchaseId = useSearchParams().get('purchase');
const [status, setStatus] = useState<Status>(purchaseId ? 'loading' : 'approved');
const verify = useCallback(async (id: string) => {
try {
const res = await fetch(`/api/purchases/${id}`);
if (!res.ok) { setStatus('error'); return; }
const { status } = await res.json();
setStatus(status === 'approved' ? 'approved'
: status === 'pending' ? 'pending' : 'rejected');
} catch { setStatus('error'); }
}, []);
useEffect(() => { if (purchaseId) verify(purchaseId); }, [purchaseId, verify]);
// Render UI based on status: loading/approved/pending/rejected/error
return <div>{/* Customize UI per status */}</div>;
}
export default function PaymentSuccessPage() {
return <Suspense fallback={<div>Loading...</div>}><PaymentResult /></Suspense>;
}references/troubleshooting.md| Gotcha | Fix |
|---|---|
| Only set when URL starts with |
| Use |
| Hydration mismatch (localStorage cart) | Add |
| Double purchase on double-click | Use |
| Success page trusts redirect URL | Always verify via |
| Webhook duplicate updates | Check if purchase is already terminal before updating |
| Webhooks can't reach localhost | Use ngrok: |
| Wrap component in |
mercadopagozodMERCADOPAGO_ACCESS_TOKEN.envNEXT_PUBLIC_NEXT_PUBLIC_APP_URL.envpurchasespurchase_itemssrc/lib/db/purchases.ts/api/checkout/api/webhooks/mercadopago/api/purchases/[id]auto_returnuseSearchParams<Suspense>references/database-supabase.mdreferences/database-prisma.mdreferences/database-postgresql.mdreferences/troubleshooting.mdreferences/countries.mdreferences/usage-examples.mdassets/migration.sql