How goncy/next-mercadopago works
Project Type and Purpose
• This is a multi‑sample full‑stack Next.js repository demonstrating five different Mercado Pago payment integrations (“Checkout Pro”, “Suscripciones”, “Checkout Bricks”, “Marketplace”, “Checkout API”).
• Each integration lives under integraciones/<integration‑name> as its own Next.js App Router project: it contains both the React frontend (pages/components) and the Node.js backend (API routes and local SQLite DB).
• The configuracion folder holds developer setup docs (cloning, creating apps in Mercado Pago, test credentials, webhook tunneling).
High‑Level Components
• For each integration:
• Browser Client (React pages in src/app)
• Next.js Server (App Router handling server components and API routes under src/app/api/.../route.ts)
• Local Database (SQLite files in db/, e.g. message.db, user.db)
• External Service (Mercado Pago REST APIs: payment preference endpoints, subscription APIs, Connect/Marketplace)
• Webhook Tunnel (ngrok/localtunnel to expose a public URL for Mercado Pago → Next.js webhook route)
Component Interactions (Generic Flow)
a. Client navigates to the React page (GET /) and triggers a payment action (e.g. “Pay to add message”).
b. React frontend calls a Next.js server action or API route (POST /api/create-preference or similar)
c. Server uses Mercado Pago SDK or HTTP API to create a payment preference / subscription plan / checkout form
d. Client is redirected to Mercado Pago or renders a Bricks widget / Checkout API component
e. User completes payment/subscription on Mercado Pago
f. Mercado Pago sends a webhook notification to the publicly exposed webhook endpoint (POST /api/mercadopago/route.ts)
g. Next.js webhook handler verifies the notification, updates the SQLite DB (marks message as paid/persist subscription)
h. Client polls or navigates back to see the updated message list (GET /api/messages → SQL query → JSON response)
Important Architectural Patterns and Variations
• The “Marketplace” integration adds a second database or schema (e.g. user.db) to represent payers vs. wall owners, and uses Mercado Pago Connect to split payments.
• “Suscripciones” uses recurring Webhooks and subscription‑specific endpoints rather than single‑payment preferences.
• “Checkout Bricks” embeds Mercado Pago’s frontend SDK inside your React app; the server still exposes an endpoint to fetch a payment token.
• “Checkout API” uses Mercado Pago’s React component library for fully custom UI; server only provides tokens and receives webhooks.