create-appncy
Version:
Create projects as goncy would
63 lines (45 loc) • 1.93 kB
Markdown
Instructions on how to setup Mercado Pago on the app.
[](https://www.mercadopago.com.ar/developers/panel/app) and copy the `Access Token` to the `.env.local` file:
```bash
MP_ACCESS_TOKEN=
```
Install the Mercado Pago dependency:
```bash
pnpm add mercadopago
```
From the server, you can create a preference (a description of something you want to sell), using the `@/payment/api` helper:
```ts
import {redirect} from "next/navigation";
import api from "@/payment/api";
function createTicket(title: string, amount: number) {
const initPoint = await api.preference({
items: [
{
id: "some-ticket",
title,
quantity: 1,
unit_price: amount,
},
],
});
redirect(initPoint);
}
```
Head to your Mercado Pago integration and in the `Webhooks` section, set the URL to be `https://your-app.com/payment` and select `Payments` on events.
> You can use [`cloudflared`](https://github.com/cloudflare/cloudflared) (cloudflared --url localhost:3000) to test the webhook locally. Your webhook URL will be `https://your-cloudflared-url.com/payment`.
Then, create a `app/payment/route.ts` file with the following content:
```ts
import type {NextRequest} from "next/server";
import api from "@/payment/api";
export async function POST(request: NextRequest) {
const body = await request.json().then((data) => data as {data: {id: string}});
await api.validate(body.data.id);
return Response.json({success: true});
}
```
> This route will receive payment notifications from Mercado Pago and validate that the payment exists. In production you should include a validation with the `secret` provided when creating the webhook.