How dedekpo/mercadopago-nextjs-by-andredev works
Main Components
• Client / UI Layer:
– Represented by Next.js pages such as app/page.tsx and the layout file (app/layout.tsx).
– The UI leverages the custom hook (app/hooks/useMercadoPago.ts) to abstract interactions with Mercado Pago.
• API Endpoints (Serverless Functions):
– Group all Mercado Pago API related endpoints under app/api/mercado-pago.
– Create-checkout (app/api/mercado-pago/create-checkout/route.ts): Initiates payments and creates a checkout session.
– Pending (app/api/mercado-pago/pending/route.ts): Manages or queries pending payment status.
– Webhook (app/api/mercado-pago/webhook/route.ts): Receives and processes responses from Mercado Pago’s webhook notifications.
• Backend / Server Logic:
– The app/server/mercado-pago/handle-payment.ts module contains logic for handling payment confirmations and likely validating signatures.
• Utility / Library Layer:
– The module app/lib/mercado-pago.ts provides common functions or integrations with the Mercado Pago SDK and handles configuration or API calls.
• External Service - Mercado Pago:
– Although not part of your code, it’s important to include Mercado Pago itself as an external service.
– The data flow should indicate that the checkout creation, payment confirmations and webhook events are managed between your app and Mercado Pago.
Relationships and Data Flow
• The UI (client/browser) initiates a payment by calling the create-checkout endpoint via a form or a button, which then interacts with the Mercado Pago API.
• Once the checkout is initiated, the external Mercado Pago service processes the payment and sends asynchronous notifications to your webhook endpoint.
• Additionally, if a payment is pending, the pending API endpoint and server functions (located under app/server/mercado-pago) are used to handle verification and updates.
• The shared utility (app/lib/mercado-pago.ts) is used by both API routes and server logic to ensure consistency in handling the integration logic.
Architectural Patterns and Technologies
• Following Next.js paradigms and the App Router pattern—separating API routes from front-end pages.
• Serverless function design for the API endpoints, which effectively isolates concerns for different requests (checkout, pending, webhook).
• Modularization through libraries (app/lib/) and hooks (app/hooks/) ensures clean separation between UI logic and business/payment processing logic.
• Integration with an external payment provider (Mercado Pago) following a typical third-party payment integration flow.