Midtrans Payment API

Next.js Integration with Firestore

This is a Next.js application integrated with Midtrans payment gateway and Firebase Firestore for managing transactions.

Use the REST API below to create transactions, check payment status, and receive payment notifications.

REST API Documentation

POST/api/transaction

Create a payment transaction and get a Snap token from Midtrans.

Request Body:

{
  "parameter": {
    "transaction_details": {
      "order_id": "ORDER-123",
      "gross_amount": 100000
    },
    "item_details": [
      {
        "id": "ITEM-1",
        "price": 100000,
        "quantity": 1,
        "name": "Product Name"
      }
    ]
  }
}

Response:

{
  "token": "abc123def456..."
}
GET/api/transaction?order_id=ORDER-123

Check the status of a payment transaction.

Query Parameters:

  • order_id (required) - The Midtrans order ID

Response:

{
  "transaction_id": "abc123",
  "order_id": "ORDER-123",
  "gross_amount": 100000,
  "payment_type": "bank_transfer",
  "transaction_status": "settlement",
  "transaction_time": "2026-02-07 10:00:00"
}
POST/api/notification

Webhook endpoint for Midtrans payment notifications. Automatically updates order status in Firestore.

Status Code Mapping:

  • 200 - settlement (paid)
  • 201 - pending
  • 202 - denied (failed)
  • 203, 204 - cancelled
  • 407, 408, 409 - failed

Firestore Collection:

Orders are stored in orders collection with fields: orderId, status, transactionStatus, paymentType, fraudStatus, grossAmount, createdAt, updatedAt

Required Environment Variables

MIDTRANS_SERVER_KEY=<your-midtrans-server-key>
NEXT_PUBLIC_CLIENT=<your-midtrans-client-key>
FIREBASE_SERVICE_ACCOUNT=<base64-encoded-firebase-service-account>