@paykit-sdk/mercadopago 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +178 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4932 -0
- package/dist/index.mjs +4928 -0
- package/dist/mercadopago-provider.d.mts +54 -0
- package/dist/mercadopago-provider.d.ts +54 -0
- package/dist/mercadopago-provider.js +4914 -0
- package/dist/mercadopago-provider.mjs +4912 -0
- package/dist/schema.d.mts +624 -0
- package/dist/schema.d.ts +624 -0
- package/dist/schema.js +2 -0
- package/dist/schema.mjs +1 -0
- package/dist/utils/mapper.d.mts +44 -0
- package/dist/utils/mapper.d.ts +44 -0
- package/dist/utils/mapper.js +169 -0
- package/dist/utils/mapper.mjs +162 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# @paykit-sdk/mercadopago
|
|
2
|
+
|
|
3
|
+
Mercado Pago provider for PayKit.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @paykit-sdk/mercadopago
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @paykit-sdk/mercadopago
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { createEndpointHandlers, PayKit } from '@paykit-sdk/core';
|
|
17
|
+
import { mercadoPago } from '@paykit-sdk/mercadopago';
|
|
18
|
+
|
|
19
|
+
export const paykit = new PayKit(mercadoPago());
|
|
20
|
+
export const endpoints = createEndpointHandlers(paykit);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or with direct config:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { PayKit } from '@paykit-sdk/core';
|
|
27
|
+
import { createMercadoPago } from '@paykit-sdk/mercadopago';
|
|
28
|
+
|
|
29
|
+
export const paykit = new PayKit(
|
|
30
|
+
createMercadoPago({
|
|
31
|
+
accessToken: 'TEST-...',
|
|
32
|
+
isSandbox: true,
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Environment Variables
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
MERCADOPAGO_ACCESS_TOKEN=TEST-...
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`isSandbox` is inferred from `NODE_ENV` — set `NODE_ENV=production` for live mode. Whether requests actually hit test or live data is determined by whether `MERCADOPAGO_ACCESS_TOKEN` is a `TEST-` or `APP_USR-` token, not by `isSandbox` itself.
|
|
44
|
+
|
|
45
|
+
## Next.js API Route
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// app/api/paykit/[...endpoint]/route.ts
|
|
49
|
+
import { endpoints } from '@/lib/paykit';
|
|
50
|
+
import { EndpointPath } from '@paykit-sdk/core';
|
|
51
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
52
|
+
|
|
53
|
+
export async function POST(
|
|
54
|
+
request: NextRequest,
|
|
55
|
+
{ params }: { params: Promise<{ endpoint: string[] }> },
|
|
56
|
+
) {
|
|
57
|
+
const { endpoint: endpointArray } = await params;
|
|
58
|
+
const endpoint = ('/' + endpointArray.join('/')) as EndpointPath;
|
|
59
|
+
const handler = endpoints[endpoint];
|
|
60
|
+
|
|
61
|
+
if (!handler) {
|
|
62
|
+
return NextResponse.json(
|
|
63
|
+
{ message: 'Not found' },
|
|
64
|
+
{ status: 404 },
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const { args } = await request.json();
|
|
69
|
+
const result = await handler(...args);
|
|
70
|
+
return NextResponse.json({ result });
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Webhooks
|
|
75
|
+
|
|
76
|
+
Mercado Pago signs webhooks with a **secret signature** you configure separately in your integration's settings — it's not the same as your access token. Notifications only carry the resource type and id; the SDK fetches the full resource before mapping standard events.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// app/api/paykit/webhooks/route.ts
|
|
80
|
+
import { paykit } from '@/lib/paykit';
|
|
81
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
82
|
+
|
|
83
|
+
export async function POST(request: NextRequest) {
|
|
84
|
+
const body = await request.text();
|
|
85
|
+
|
|
86
|
+
const webhook = paykit.webhooks
|
|
87
|
+
.setup({ webhookSecret: process.env.MERCADOPAGO_WEBHOOK_SECRET! })
|
|
88
|
+
.on('payment.succeeded', async event => {
|
|
89
|
+
/* payment status "approved" */
|
|
90
|
+
})
|
|
91
|
+
.on('payment.failed', async event => {
|
|
92
|
+
/* payment status "rejected" or "cancelled" */
|
|
93
|
+
})
|
|
94
|
+
.on('refund.created', async event => {
|
|
95
|
+
/* payment status "refunded" */
|
|
96
|
+
})
|
|
97
|
+
.on('subscription.updated', async event => {
|
|
98
|
+
/* subscription_preapproval notifications */
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
await webhook.handle({
|
|
102
|
+
body,
|
|
103
|
+
headersAsObject: Object.fromEntries(request.headers),
|
|
104
|
+
fullUrl: request.url,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return NextResponse.json({ received: true });
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Mercado Pago sends the signature in the `x-signature` header (`ts=...,v1=...`), and the resource id in the `data.id` query parameter — both are required to recompute the HMAC.
|
|
112
|
+
|
|
113
|
+
## Checkout
|
|
114
|
+
|
|
115
|
+
Mercado Pago checkouts are built on Checkout Pro preferences and require an email customer and amount/currency in `provider_metadata`:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
const checkout = await paykit.checkouts.create({
|
|
119
|
+
customer: { email: 'user@example.com' },
|
|
120
|
+
item_id: 'plan_pro',
|
|
121
|
+
session_type: 'one_time',
|
|
122
|
+
quantity: 1,
|
|
123
|
+
success_url: 'https://example.com/success',
|
|
124
|
+
cancel_url: 'https://example.com/cancel',
|
|
125
|
+
provider_metadata: {
|
|
126
|
+
amount: '400',
|
|
127
|
+
currency: 'ARS',
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Redirect user to checkout.payment_url
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Subscriptions
|
|
135
|
+
|
|
136
|
+
Mercado Pago subscriptions require a pre-existing Plan (`preapproval_plan`) created via the dashboard or API — `item_id` maps to that plan's id:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const subscription = await paykit.subscriptions.create({
|
|
140
|
+
customer: { email: 'user@example.com' },
|
|
141
|
+
item_id: 'plan_id_from_mercadopago',
|
|
142
|
+
quantity: 1,
|
|
143
|
+
billing_interval: 'month',
|
|
144
|
+
amount: 4990,
|
|
145
|
+
currency: 'ARS',
|
|
146
|
+
metadata: null,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Redirect user to subscription.payment_url to authorize the mandate
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Refunds
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const refund = await paykit.refunds.create({
|
|
156
|
+
payment_id: '123456789',
|
|
157
|
+
amount: 400,
|
|
158
|
+
reason: 'requested_by_customer',
|
|
159
|
+
metadata: null,
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Unsupported
|
|
164
|
+
|
|
165
|
+
Mercado Pago has no delete-preference API and no general payment-update API — `deleteCheckout` and `updatePayment` throw `ProviderNotSupportedError`. Use `capturePayment`/`cancelPayment` for payment state transitions instead.
|
|
166
|
+
|
|
167
|
+
## Documentation
|
|
168
|
+
|
|
169
|
+
Full docs at [docs.usepaykit.com/providers/mercadopago](https://docs.usepaykit.com/providers/mercadopago).
|
|
170
|
+
|
|
171
|
+
## Support
|
|
172
|
+
|
|
173
|
+
- [Mercado Pago Documentation](https://www.mercadopago.com/developers/en/reference)
|
|
174
|
+
- [PayKit Issues](https://github.com/usepaykit/paykit-sdk/issues)
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
ISC
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MercadoPagoOptions, MercadoPagoProvider } from './mercadopago-provider.mjs';
|
|
2
|
+
import '@paykit-sdk/core';
|
|
3
|
+
import './schema.mjs';
|
|
4
|
+
|
|
5
|
+
declare const createMercadoPago: (config: MercadoPagoOptions) => MercadoPagoProvider;
|
|
6
|
+
declare const mercadoPago: () => MercadoPagoProvider;
|
|
7
|
+
|
|
8
|
+
export { MercadoPagoOptions, MercadoPagoProvider, createMercadoPago, mercadoPago };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MercadoPagoOptions, MercadoPagoProvider } from './mercadopago-provider.js';
|
|
2
|
+
import '@paykit-sdk/core';
|
|
3
|
+
import './schema.js';
|
|
4
|
+
|
|
5
|
+
declare const createMercadoPago: (config: MercadoPagoOptions) => MercadoPagoProvider;
|
|
6
|
+
declare const mercadoPago: () => MercadoPagoProvider;
|
|
7
|
+
|
|
8
|
+
export { MercadoPagoOptions, MercadoPagoProvider, createMercadoPago, mercadoPago };
|