Skip to content

About

Sample Next.js app for Mayar embedded checkout. The invoice payment page opens in an iframe instead of a redirect.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Kertas Kerja Digital Store

Sample Next.js app for Mayar embedded checkout.

Screenshot 2026-08-14 at 10-17-47 Screenshot 2026-08-14 at 10-17-28 Screenshot 2026-08-14 at 10-18-03 Screenshot 2026-08-14 at 10-17-20

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.

What you get

  • 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.

How checkout works

  1. The buyer clicks Beli on a product.
  2. The buyer submits name, email, and mobile number.
  3. The Next.js server creates a Mayar invoice with POST /hl/v2/invoices/create.
  4. The modal loads data.link in an iframe.
  5. The browser asks /api/checkout/status every 4 seconds.
  6. That route reads GET /hl/v2/invoices/{id} on the server.
  7. When status is paid, the modal shows a success message.
  8. Mayar sends the product email.

The Mayar API key never goes to the browser.

Prerequisites

  • 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.

Setup

git clone https://github.com/mayarid/kertaskerja-digital-store.git
cd kertaskerja-digital-store
bun install
cp .env.example .env.local

Edit .env.local:

MAYAR_API_KEY=paste-your-api-key-here
MAYAR_ENV=sandbox
APP_URL=http://localhost:3000

Use MAYAR_ENV=production only when you want live charges.

Do not commit .env.local.

Environment variables

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

Run the app

bun run dev

Open http://localhost:3000.

Other commands:

bun run build
bun run start

Try a payment

  1. Open the store.
  2. Choose a product and click Beli.
  3. Enter a real name, email, and Indonesian mobile number, for example 081234567890.
  4. Click Lanjut ke pembayaran.
  5. Complete payment inside the iframe.
  6. Wait for the success message in the same modal.
  7. 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.

Project map

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

Notes

  • 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 paid as success. closed shows 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.

Docs

About

Sample Next.js app for Mayar embedded checkout. The invoice payment page opens in an iframe instead of a redirect.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages