Sample Next.js app for Mayar embedded checkout.
This store sells digital products. After the buyer enters name, email, and phone, the Mayar invoice page opens inside an iframe. The site does not redirect the buyer to a new tab or a new page.
Use this repo to learn the iframe checkout pattern for Mayar Invoice API v2.
- A small digital-goods storefront
- Server-side invoice create (
POST /api/checkout) - Embedded Mayar payment page in a modal
- Server-side invoice status polling (
GET /api/checkout/status) - A success state in the same modal when Mayar reports
paid
Fulfillment stays in Mayar. This app does not send files and does not store access records.
- The buyer clicks Beli on a product.
- The buyer submits name, email, and mobile number.
- The Next.js server creates a Mayar invoice with
POST /hl/v2/invoices/create. - The modal loads
data.linkin an iframe. - The browser asks
/api/checkout/statusevery 4 seconds. - That route reads
GET /hl/v2/invoices/{id}on the server. - When
statusispaid, the modal shows a success message. - Mayar sends the product email.
The Mayar API key never goes to the browser.
- Bun 1.1 or later
- A Mayar account
- A Read & Write API key
Create a production key at https://web.mayar.id/api-keys.
Create a sandbox key at https://web.mayar.io/api-keys.
git clone https://github.com/mayarid/kertaskerja-digital-store.git
cd kertaskerja-digital-store
bun install
cp .env.example .env.localEdit .env.local:
MAYAR_API_KEY=paste-your-api-key-here
MAYAR_ENV=sandbox
APP_URL=http://localhost:3000Use MAYAR_ENV=production only when you want live charges.
Do not commit .env.local.
| Name | Required | Description |
|---|---|---|
MAYAR_API_KEY |
Yes | Server-only Mayar API key. Never expose it to the client. |
MAYAR_ENV |
Yes | sandbox or production. Defaults to sandbox when unset. |
APP_URL |
Recommended | Public URL of this app, for example http://localhost:3000. |
Sandbox API: https://api.mayar.io/hl/v2
Production API: https://api.mayar.id/hl/v2
bun run devOpen http://localhost:3000.
Other commands:
bun run build
bun run start- Open the store.
- Choose a product and click Beli.
- Enter a real name, email, and Indonesian mobile number, for example
081234567890. - Click Lanjut ke pembayaran.
- Complete payment inside the iframe.
- Wait for the success message in the same modal.
- Check the buyer email for the Mayar delivery message.
Mayar Invoice requires mobile. The form rejects an invalid phone number before it calls the API.
app/page.tsx Store UI and checkout modal
app/api/checkout/route.ts Create invoice
app/api/checkout/status/route.ts Read invoice status
lib/mayar.ts Mayar v2 HTTP helper
lib/mayar-config.ts Server-only API key and environment
lib/mayar-invoice.ts Invoice payload and status helpers
- Mayar docs do not define a special iframe API. This sample embeds
data.link. If the payment page is blank, the Mayar host may block framing. - Status polling treats only
paidas success.closedshows an expired-invoice message. - This sample does not use webhooks.
- The Mayar rate limit is 50 requests per minute. The 4-second poll stays under that limit.
- Duplicate create requests can return
429. Wait one minute, then try again.