# srossiprieto/next-mercadopago architecture

> Architecture diagram of the GitHub repository srossiprieto/next-mercadopago, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/srossiprieto/next-mercadopago
- Repository: https://github.com/srossiprieto/next-mercadopago

## Overview

This project is a full-stack integration tutorial focused on demonstrating multiple ways to integrate Mercado Pago into Next.js applications. Its purpose is to show different payment flows—involving Checkout Pro, Suscripciones, Checkout Bricks, and Marketplace—using the Next.js App Router and illustrating key configuration steps for creating applications, managing credentials, exposing ports, and handling webhooks.

##### Main Components Identification
- Integration Applications: There are four distinct Next.js applications under the “integraciones” directory. Each one represents a different payment flow:
- Checkout Pro: Where users pay to post a message, involving payment redirection via Mercado Pago, webhook notifications, and payment verification.
- Suscripciones: Integrates subscriptions (without pre-defined payment plans) along with webhook handling.
- Checkout Bricks: Embedded payment data collection directly within the application.
- Marketplace: Acts as an intermediary allowing a user to post messages on someone else’s “wall” and earning a commission on each message, again using Checkout Pro flow.
- Configuration Toolkit: Located in the “configuracion” folder, these modules provide guidance on:
- Creating a Mercado Pago application.
- Setting up test accounts and credentials.
- Exposing local ports publicly.
- Configuring webhooks for receiving notifications.
- External Systems:
- Mercado Pago’s APIs and webhooks, as the primary external payment service.
- Databases and Utilities:
- Each integration app uses local database files (e.g., message.db and user.db) for persistence.
- Frontend and Backend Elements:
- The Next.js structure (with pages, app folder, API routes) covers both client (user interface for message forms and pages) and server-side aspects (API endpoints for payment processing and webhooks).

##### Relationships and Data Flow
- Users interact with the frontend of a given integration application (e.g., submitting a message).
- The application backend (Next.js API routes) communicates with Mercado Pago’s API to create payment preferences or subscribe users.
- Depending on the integration type, the user may be redirected externally (e.g., Checkout Pro marketplace) or process payment within the app (Checkout Bricks).
- Webhooks are set up in the project to asynchronously receive notifications about payments or subscriptions from Mercado Pago. These notifications flow into specific API endpoints within the apps for verification and update.
- Local databases are used to store payment statuses, messages, and user data, recording the outcome of transactions and notifications.

##### Architectural Patterns and Design Choices
- Modular Structure: The project modularizes each integration type into its own directory, facilitating focused development and testing.
- Separation of Concerns: The “configuracion” directory holds all preparatory and helper instructions, separating configuration from core integration logic.
- API-Driven Communication: Each integration app employs API routes for processing payment requests and handling webhooks, reflecting modern RESTful (or sometimes event-driven) design.
- Shared Technologies: All integration apps use Next.js (with App Router), Tailwind CSS for styling, and configuration files (like ESLint, postcss, and TypeScript configurations) for consistency.

##### Elements
- Component Blocks:
- Databases as separate labeled blocks.
- Asynchronous callback flow via webhooks from Mercado Pago back to the apps.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    User["User"]:::external

    %% Integration Apps Subgraph
    subgraph "Integration Apps"
        %% Checkout Pro App
        subgraph "Checkout Pro App"
            cp_ui["Checkout Pro UI (Frontend)"]:::frontend
            cp_back["Checkout Pro Backend (API&Webhooks)"]:::backend
            cp_db(("Checkout Pro DB (message.db,user.db)")):::database
            cp_ui --> cp_back
            cp_back --> cp_db
        end

        %% Suscripciones App
        subgraph "Suscripciones App"
            s_ui["Suscripciones UI (Frontend)"]:::frontend
            s_back["Suscripciones Backend (API&Webhooks)"]:::backend
            s_db(("Suscripciones DB (message.db,user.db)")):::database
            s_ui --> s_back
            s_back --> s_db
        end

        %% Checkout Bricks App
        subgraph "Checkout Bricks App"
            cb_ui["Checkout Bricks UI (Frontend)"]:::frontend
            cb_back["Checkout Bricks Backend (API&Webhooks)"]:::backend
            cb_db(("Checkout Bricks DB (message.db,user.db)")):::database
            cb_ui --> cb_back
            cb_back --> cb_db
        end

        %% Marketplace App
        subgraph "Marketplace App"
            mp_ui["Marketplace UI (Frontend)"]:::frontend
            mp_back["Marketplace Backend (API&Webhooks)"]:::backend
            mp_db(("Marketplace DB (message.db,user.db)")):::database
            mp_ui --> mp_back
            mp_back --> mp_db
        end
    end

    %% User interactions with Integration Apps Frontends
    User --> cp_ui
    User --> s_ui
    User --> cb_ui
    User --> mp_ui

    %% External Service - Mercado Pago
    mp_service["Mercado Pago Service"]:::external

    %% Communication: Integration App Backends to Mercado Pago
    cp_back -->|"payment"| mp_service
    s_back -->|"payment"| mp_service
    cb_back -->|"payment"| mp_service
    mp_back -->|"payment"| mp_service

    mp_service -->|"webhook"| cp_back
    mp_service -->|"webhook"| s_back
    mp_service -->|"webhook"| cb_back
    mp_service -->|"webhook"| mp_back

    %% Configuration Toolkit Subgraph
    subgraph "Configuration Toolkit"
        createApp["Crear Aplicación"]:::config
        cred["Credenciales"]:::config
        testAcc["Cuentas_de_Prueba"]:::config
        exposePort["Exponer_Puerto"]:::config
        webhookConf["Webhook_Configuration"]:::config
    end

    %% Configuration influences each Integration App Backend
    createApp --> cp_back
    createApp --> s_back
    createApp --> cb_back
    createApp --> mp_back

    cred --> cp_back
    cred --> s_back
    cred --> cb_back
    cred --> mp_back

    testAcc --> cp_back
    testAcc --> s_back
    testAcc --> cb_back
    testAcc --> mp_back

    exposePort --> cp_back
    exposePort --> s_back
    exposePort --> cb_back
    exposePort --> mp_back

    webhookConf --> cp_back
    webhookConf --> s_back
    webhookConf --> cb_back
    webhookConf --> mp_back

    %% Click Events for Integration Apps
    click cp_back "https://github.com/srossiprieto/next-mercadopago/tree/main/integraciones/checkout-pro"
    click s_back "https://github.com/srossiprieto/next-mercadopago/tree/main/integraciones/suscripciones"
    click cb_back "https://github.com/srossiprieto/next-mercadopago/tree/main/integraciones/checkout-bricks"
    click mp_back "https://github.com/srossiprieto/next-mercadopago/tree/main/integraciones/marketplace"

    %% Click Events for Configuration Toolkit
    click createApp "https://github.com/srossiprieto/next-mercadopago/tree/main/configuracion/crear-aplicacion"
    click cred "https://github.com/srossiprieto/next-mercadopago/tree/main/configuracion/credenciales"
    click testAcc "https://github.com/srossiprieto/next-mercadopago/tree/main/configuracion/cuentas-de-prueba"
    click exposePort "https://github.com/srossiprieto/next-mercadopago/tree/main/configuracion/exponer-puerto"
    click webhookConf "https://github.com/srossiprieto/next-mercadopago/tree/main/configuracion/webhook"

    %% Styles
    classDef frontend fill:#aaf,stroke:#333,stroke-width:2px;
    classDef backend fill:#faa,stroke:#333,stroke-width:2px;
    classDef database fill:#afa,stroke:#333,stroke-width:2px;
    classDef config fill:#ffc,stroke:#333,stroke-width:2px;
    classDef external fill:#fcc,stroke:#333,stroke-width:2px;
```
