API Documentation

PlernPay Payment API — รับชำระเงินผ่าน PromptPay & TrueMoney Wallet อัตโนมัติ

NEW FEATURE2026-09-28

รองรับ TrueMoney Voucher (ซองของขวัญ / อั่งเปา)

เปิดตัว API ใหม่ POST /v1/topup/truemoney ให้ระบบสามารถดึงเงินจากซองของขวัญ TrueMoney เข้ากระเป๋าอัตโนมัติ พร้อมระบบป้องกันการใช้ซองซ้ำและข้าม Cloudflare WAF สมบูรณ์แบบ

  • Endpoint: POST /v1/topup/truemoney ส่งแค่ voucher_url ก็ขึ้นเงินได้ทันที
  • ตั้งค่าเบอร์ปลายทาง: กำหนดได้ผ่านฟิลด์ phone หรือตั้งค่าเบอร์ TrueMoney เริ่มต้นของ Application ในระบบ PlernPay Admin
  • Error Code ครอบคลุม: รองรับ Error 1040–1049 สำหรับแจ้งสถานะซองหมดอายุ, ซองถูกใช้แล้ว, ซองไม่ถูกต้อง ฯลฯ
Base URL
https://api.plernpay.com

Authentication

ใช้ 2 key — Client ID + Client Secret

Client ID (pi_xxx)

เปิดเผยได้ — ใช้ฝั่ง frontend (SSE stream)

Client Secret (ps_xxx)

ห้ามเปิดเผย — ใช้ฝั่ง server เท่านั้น

ตัวอย่างการเรียก API PromptPay:
curl -X POST https://api.plernpay.com/v1/topup/create \
  -H "X-Client-ID: pi_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -H "X-Client-Secret: ps_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{"amount": 100, "memo": "order:12345"}'

สถานะ

pendingรอชำระเงิน
confirmedชำระเงินสำเร็จ
expiredหมดอายุ
cancelledถูกยกเลิก

Rate Limit

30 requests / นาที ต่อ Application (ค่าเริ่มต้น) — ใช้เพดานที่กำหนดให้บัญชี ติดต่อแอดมินเพื่อปรับ limit

หากเกินเพดานจะได้รับ HTTP 429 / code 1030 ให้รอตาม Retry-After ก่อนลองใหม่ โดยแอปยังใช้งานอยู่

Endpoints

PromptPay API

รายการ API สำหรับสร้าง QR Code รับชำระเงิน และตรวจสอบการโอนเงินเข้า PromptPay

สร้าง QR Code สำหรับรับชำระเงินผ่านระบบ PromptPay — HTTP 503 / code 1022 หมายถึง Bot ยังไม่พร้อม ส่วน code 1050 หมายถึงระบบปิดปรับปรุงชั่วคราว ให้รอ Retry-After (วินาที) ก่อนลองใหม่ ห้าม retry POST อัตโนมัติเมื่อเครือข่ายขาด เพราะอาจสร้างรายการสำเร็จแล้ว

Headers
X-Client-ID: pi_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Client-Secret: ps_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Content-Type: application/json
Parameters
FieldTypeRequiredDescription
amountnumberrequiredจำนวนเงิน (1 - 999,999)
memostringoptionalหมายเหตุ เช่น username, order_id (สูงสุด 255 ตัวอักษร)
Request Body Example
{
  "amount": 100,
  "memo": "user:john_doe"
}
Response Fields
FieldTypeDescription
codenumber200
refstringReference ID สำหรับเช็คสถานะ
amountnumberจำนวนเงินที่ขอ
unique_amountnumberยอดจริงที่ต้องโอน (เพิ่มสตางค์สุ่ม)
qr_codestringPromptPay QR payload สำหรับสร้าง QR
promptpay_idstringหมายเลข PromptPay ปลายทาง
memostring | nullหมายเหตุที่ส่งมา
statusstring"pending"
expires_atstringเวลาหมดอายุ 15 นาทีหลังสร้างรายการ (ISO 8601)
expires_innumberวินาทีที่เหลือก่อนหมดอายุ (เริ่มต้น 900 วินาที)
Response Example
{
  "code": 200,
  "ref": "PLERNPAY-20260403-A001-7F2B",
  "amount": 100,
  "unique_amount": 100.47,
  "qr_code": "00020101021129370016...",
  "promptpay_id": "0891234567",
  "memo": "user:john_doe",
  "status": "pending",
  "expires_at": "2026-04-03T10:15:00.000Z",
  "expires_in": 900
}

การเรียก API ช่วงปิดปรับปรุงระบบ

เมื่อสร้าง QR ใหม่แล้วได้รับ HTTP 503 พร้อม code 1050 ให้แจ้งผู้ใช้ว่าระบบปิดปรับปรุงชั่วคราว และรออย่างน้อยตาม Retry-After ก่อนลองใหม่ สถานะนี้ไม่ทำให้บัญชีออกจากระบบ

HTTP/1.1 503 Service Unavailable
Retry-After: 30
Cache-Control: no-store

{
  "code": 1050,
  "error": "Temporarily unavailable for maintenance",
  "retry_after": 30
}
  • คำขอสร้าง QR ใหม่จะพักชั่วคราว ส่วน status, SSE และการยกเลิกรายการเดิมยังเรียกใช้งานได้ตามสิทธิ์เดิม
  • Public TrueMoney API ยังเรียกใช้งานได้ การเติมเครดิตผ่านหน้าบัญชีอาจพักชั่วคราวช่วงสลับระบบ
  • code 1022 หมายถึงบอตยังไม่พร้อม และ code 1030 หมายถึงเกินเพดานคำขอ ให้ตรวจ code พร้อม HTTP status
  • หากคำขอทางการเงิน timeout หรือไม่ทราบผล ให้ตรวจสถานะและกระทบยอดก่อนส่งซ้ำ ห้าม retry POST อัตโนมัติ

Error Codes

PromptPay Errors (14)

ทุก error response ส่งรูปแบบ code + error

ตัวอย่าง Error Response:
{
  "code": 1012,
  "error": "Insufficient balance",
  "balance": 0.00
}
CodeHTTPMessage & Explanation
1000401Missing credentials (ไม่ได้ส่ง X-Client-ID หรือ X-Client-Secret)
1001401Invalid credentials (Client ID หรือ Secret ไม่ถูกต้อง)
1002403Application inactive (แอปพลิเคชันถูกระงับการใช้งาน)
1003403Account inactive (บัญชีผู้ใช้ถูกระงับ)
1010400Invalid amount (จำนวนเงินไม่ถูกต้อง ต้องอยู่ระหว่าง 1 - 999,999)
1011400PromptPay ID not configured (ยังไม่ได้ตั้งค่าเบอร์ PromptPay ในระบบ)
1012402Insufficient balance (ยอดเงินเครดิตในระบบไม่พอหักค่าบริการ)
1013400No unique amount available (ยอดเศษสตางค์ไม่ว่างชั่วคราว — กรุณาลองใหม่อีกครั้ง)
1020404Topup not found (ไม่พบรายการชำระเงิน)
1021400Topup not pending (สถานะรายการไม่ใช่ pending เช่น หมดอายุแล้ว หรือ ได้รับเงินแล้ว)
1022503Bot not connected (บอทตรวจจับยอดเงินไม่ได้เชื่อมต่อ — กรุณาเริ่มบอทก่อนสร้าง QR)
1030429Rate limit exceeded (เกินเพดานคำขอ — รอตาม Retry-After โดยแอปยังใช้งานอยู่)
1050503Temporarily unavailable for maintenance (ระบบปิดปรับปรุงชั่วคราว — รอตาม Retry-After ก่อนลองใหม่)
2001500Internal error (ข้อผิดพลาดภายในระบบ)

Quick Start Guide

ตัวอย่างการเชื่อมต่อ PromptPay QR & Real-time Stream

// ========================================================
// ตัวอย่าง: รับชำระผ่าน PromptPay QR + Real-time Stream
// ========================================================

// 1. ฝั่ง Server (Node.js): สร้าง QR Code
const res = await fetch("https://api.plernpay.com/v1/topup/create", {
  method: "POST",
  headers: {
    "X-Client-ID": "pi_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "X-Client-Secret": "ps_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ amount: 100, memo: "user:john_doe" }),
});
const result = await res.json();
if (!res.ok) {
  // HTTP 503/code 1022 (บอตยังไม่พร้อม) หรือ 1050 (ปิดปรับปรุง):
  // รออย่างน้อย Retry-After ก่อนให้ผู้ใช้ลองใหม่
  // ห้าม retry POST อัตโนมัติเมื่อ network timeout หรือไม่ทราบผล
  throw new Error(result.error);
}
const { ref, qr_code, unique_amount } = result;

// 2. ฝั่ง Frontend (Browser): ดักฟังผลการชำระเงิน Real-time
let stopPaymentStream = () => {};
function watchPayment(url, onUpdate) {
  stopPaymentStream(); // Close the previous reference before opening another.
  let events, timer, stopped = false, delay = 15000;
  function stop() {
    stopped = true;
    events?.close();
    clearTimeout(timer);
    window.removeEventListener("pagehide", stop);
  }
  function connect() {
    if (stopped) return;
    events = new EventSource(url);
    events.onopen = () => { delay = 15000; };
    events.onmessage = (e) => {
      const data = JSON.parse(e.data);
      if (["confirmed", "expired", "cancelled", "failed"].includes(data.status)) stop();
      onUpdate(data);
    };
    events.onerror = () => {
      events.close(); // Disable native reconnect before scheduling our own.
      if (stopped) return;
      clearTimeout(timer);
      timer = setTimeout(connect, delay + Math.random() * 1000);
      delay = Math.min(delay * 2, 60000);
    };
  }
  stopPaymentStream = stop;
  window.addEventListener("pagehide", stop, { once: true });
  connect();
  return stop; // Call on component unmount, cancellation or reference change.
}
watchPayment(
  `https://api.plernpay.com/v1/topup/${ref}/stream?client_id=pi_xxx`,
  (data) => { if (data.status === "confirmed") console.log("ชำระสำเร็จ", data.amount, data.bank); }
);