d4lanis

payload mercadopago

by d4lanis
Payments
v3
^3.94.0
MIT

Overview

Summary generated from the README

Adds Mercado Pago Checkout Pro to a Payload app. It registers payments and webhook event collections plus a settings global, and exposes endpoints for webhooks, refunds, sync and reconciliation. Credentials are read from environment variables, and it requires Payload 3.94 or newer. Only one-time payments are supported.

Features

  • Creates Checkout Pro preferences with items, payer, back URLs, installments and metadata
  • Validates webhook x-signature with HMAC SHA-256 and rejects unsigned calls by default
  • Stores webhook events idempotently and fetches payment status from the Mercado Pago API
  • Full and partial refunds plus cancellation of pending payments
  • Reconciliation by payment id or external reference with a cron-ready endpoint
  • Lifecycle hooks for created, updated, approved, pending, rejected and refunded payments

Use cases

  • Accept one-time Mercado Pago payments for orders in a Payload-backed shop
  • Update order records automatically when a payment is approved or refunded
  • Reconcile missed or out-of-order payment notifications on a schedule

From the author

Mercado Pago Checkout Pro plugin for Payload CMS: preferences, signed webhooks, refunds and payment reconciliation.

Installation

npm i payload-mercadopago

0

Stars

0

Forks

0

Open Issues

4 days ago

Last Updated

0

Weekly Downloads

v

npm Version

Dependencies

GitHub stars

00 since Nov 29

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20260
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
npm weekly downloads

Trends appear after 7 days of data. Daily snapshots have not started yet.

Each point is npm's rolling 7-day download count.

Health Score

35/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1479751591-root.svg)](https://payloaddirectory.dev/plugins/1479751591-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1479751591-root"><img src="https://payloaddirectory.dev/badge/1479751591-root.svg" alt="Payload Directory"></a>
Updated Dec 1, 2026Created Dec 1, 2026

README

payload-mercadopago

Mercado Pago Checkout Pro plugin for Payload CMS 3.x.

It adds preferences, signed webhooks, payment records, refunds and reconciliation helpers to any Payload app.

Features

  • Checkout Pro preferences with items, payer, back URLs, expiration, installments, statement descriptor and metadata.
  • Signed webhooks with HMAC x-signature validation and timing-safe comparison.
  • Idempotent webhook log so duplicated or out-of-order events do not corrupt payment state.
  • Payments collection with status, method, amounts, payer, refunds and raw Mercado Pago data.
  • Refunds (full and partial) and cancellation of pending payments.
  • Reconciliation by payment_id or external_reference, including a cron-ready endpoint.
  • Hooks to update your own orders or business entities.
  • Admin UI for payments, events and refunds.
  • Fail-closed security by default when the webhook secret is missing.

Only one-time Checkout Pro payments are supported. Subscriptions, Point/QR and marketplace split payments are intentionally out of scope.

Requirements

  • Node.js 18+
  • Payload CMS 3.94+
  • A Mercado Pago application with test or production credentials.

Installation

Install directly from GitHub:

pnpm add github:d4lanis/payload-mercadopago#v0.1.1

For local development inside another repository:

pnpm add link:../payload-mercadopago

Environment variables

MP_ACCESS_TOKEN=TEST-...
MP_PUBLIC_KEY=TEST-...
MP_WEBHOOK_SECRET=...
MP_ENVIRONMENT=sandbox
MP_STATEMENT_DESCRIPTOR=MESA
MP_CRON_SECRET=change-me

MP_PUBLIC_KEY is optional for Checkout Pro but useful for status displays. Secrets are never exposed to the browser.

Payload configuration

import { mercadoPagoPlugin } from 'payload-mercadopago'
import type { MercadoPagoPluginConfig } from 'payload-mercadopago/types'

const mercadoPagoOptions: MercadoPagoPluginConfig = {
  access: {
    admin: ({ req }) => Boolean(req.user),
  },
  admin: {
    group: 'Pagos',
  },
  checkout: {
    autoReturn: 'approved',
    maxInstallments: 12,
    statementDescriptor: 'MESA',
  },
  hooks: {
    onPaymentUpdated: async ({ payment, previousStatus, payload }) => {
      // Update your order/document here.
    },
  },
}

export const config = buildConfig({
  // ...
  plugins: [mercadoPagoPlugin(mercadoPagoOptions)],
})

The plugin registers:

ItemDefault slug
Payments collectionmercadopago-payments
Webhook events collectionmercadopago-events
Settings globalmercadopago-settings

You can rename them with collections and globals. The settings global stores non-secret options; credentials stay in environment variables.

Create a Checkout Pro preference

import { createPreference, resolveConfig } from 'payload-mercadopago'

const config = await resolveConfig(payload, mercadoPagoOptions)

const { initPoint, preference, payment } = await createPreference({
  config,
  payload,
  input: {
    externalReference: String(order.id),
    expectedAmount: 290,
    items: [
      { quantity: 1, title: 'Comida', unitPrice: 241 },
      { quantity: 1, title: 'Envío', unitPrice: 49 },
    ],
    payer: {
      email: customer.email,
      name: 'Ada',
      surname: 'Lovelace',
    },
    backUrls: {
      success: 'https://shop.example.com/pedido/123?new=1',
      failure: 'https://shop.example.com/checkout?canceled=1',
      pending: 'https://shop.example.com/pedido/123?pending=1',
    },
    metadata: { orderId: String(order.id) },
  },
})

// Redirect the browser to initPoint.

expectedAmount is validated against the sum of the items. Amounts are decimals in the currency unit (for example 35.50, not cents).

Webhook

Point the Mercado Pago dashboard to:

https://your-domain.com/api/mercadopago/webhook

Select payment and merchant_order notifications. Copy the webhook secret from the Mercado Pago dashboard into MP_WEBHOOK_SECRET.

The endpoint:

  1. Validates x-signature with HMAC SHA-256.
  2. Stores the notification in mercadopago-events.
  3. Fetches the payment from the Mercado Pago API (never trusts the body status).
  4. Updates the payment record with monotonic status transitions.
  5. Runs onPaymentCreated, onPaymentUpdated and the status-specific hook.
  6. Returns 200 only when processing succeeds, so Mercado Pago can retry failures.

If MP_WEBHOOK_SECRET is missing, webhooks are rejected unless allowUnsignedWebhooks: true is explicitly set for local development.

Hooks

type MercadoPagoHooks = {
  onPaymentCreated?: (args) => Promise<void> | void
  onPaymentUpdated?: (args) => Promise<void> | void
  onPaymentApproved?: (args) => Promise<void> | void
  onPaymentPending?: (args) => Promise<void> | void
  onPaymentRejected?: (args) => Promise<void> | void
  onPaymentRefunded?: (args) => Promise<void> | void
}

Each hook receives { payment, mpPayment, previousStatus, payload, req }.

Hooks should be idempotent: Mercado Pago can retry a webhook. If a hook throws, the event is marked as failed and Mercado Pago will retry.

Server API

import {
  cancelPayment,
  getPaymentFromApi,
  refundPayment,
  searchPayments,
  syncPayment,
  syncPaymentByExternalReference,
  verifyWebhookSignature,
} from 'payload-mercadopago'
  • syncPayment({ config, paymentId, payload })
  • syncPaymentByExternalReference({ config, externalReference, payload })
  • refundPayment({ config, input: { paymentId, amount? }, payload })
  • cancelPayment({ config, paymentId, payload })

Endpoints

MethodPathAccess
POST/api/mercadopago/webhookPublic, signature-verified
POST/api/mercadopago/refundAdmin
POST/api/mercadopago/refund/cancelAdmin
POST/api/mercadopago/syncAdmin
POST/api/mercadopago/reconcileAdmin or x-cron-secret
GET/api/mercadopago/statusAdmin

Refund body:

{ "paymentId": "123456", "amount": 50 }

Omit amount for a full refund.

Reconcile body:

{ "externalReference": "order-123" }

For cron, send x-cron-secret: $MP_CRON_SECRET.

Metadata and amounts

  • Store your own order id in externalReference.
  • Use metadata for non-sensitive references.
  • Amounts are decimals in the account currency (MXN by default).
  • The plugin flags amountMismatch when the Mercado Pago payment amount does not match the expected preference amount. Do not fulfill an order when this flag is set.

Testing

Run the plugin checks:

pnpm lint
pnpm typecheck
pnpm test
pnpm build

For end-to-end testing:

  1. Create test credentials in the Mercado Pago developer dashboard.
  2. Create test users and use the official test cards.
  3. Expose your local app over HTTPS (Cloudflare Tunnel, ngrok, etc.).
  4. Register https://<tunnel>/api/mercadopago/webhook and copy the secret.
  5. Pay with Checkout Pro, verify the payment record, webhook event and your order update.
  6. Test a refund from the payment record in the admin.

The Mercado Pago sandbox can be intermittent. Use the dashboard's webhook simulator and retry if a test notification does not arrive.

Security

  • Access tokens and webhook secrets stay in environment variables.
  • The webhook validates signatures with timingSafeEqual.
  • Payment status is always fetched from the Mercado Pago API.
  • Admin endpoints use your access callback.
  • Webhooks are rejected when the secret is missing unless explicitly allowed.
  • Do not log full request headers or access tokens.

License

MIT

Similar plugins