PlernPay Payment API — รับชำระเงินผ่าน PromptPay & TrueMoney Wallet อัตโนมัติ
เปิดตัว API ใหม่ POST /v1/topup/truemoney ให้ระบบสามารถดึงเงินจากซองของขวัญ TrueMoney เข้ากระเป๋าอัตโนมัติ พร้อมระบบป้องกันการใช้ซองซ้ำและข้าม Cloudflare WAF สมบูรณ์แบบ
POST /v1/topup/truemoney ส่งแค่ voucher_url ก็ขึ้นเงินได้ทันทีphone หรือตั้งค่าเบอร์ TrueMoney เริ่มต้นของ Application ในระบบ PlernPay Adminhttps://api.plernpay.comใช้ 2 key — Client ID + Client Secret
เปิดเผยได้ — ใช้ฝั่ง frontend (SSE stream)
ห้ามเปิดเผย — ใช้ฝั่ง server เท่านั้น
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"}'30 requests / นาที ต่อ Application (ค่าเริ่มต้น) — ใช้เพดานที่กำหนดให้บัญชี ติดต่อแอดมินเพื่อปรับ limit
หากเกินเพดานจะได้รับ HTTP 429 / code 1030 ให้รอตาม Retry-After ก่อนลองใหม่ โดยแอปยังใช้งานอยู่
รายการ API สำหรับสร้าง QR Code รับชำระเงิน และตรวจสอบการโอนเงินเข้า PromptPay
สร้าง QR Code สำหรับรับชำระเงินผ่านระบบ PromptPay — HTTP 503 / code 1022 หมายถึง Bot ยังไม่พร้อม ส่วน code 1050 หมายถึงระบบปิดปรับปรุงชั่วคราว ให้รอ Retry-After (วินาที) ก่อนลองใหม่ ห้าม retry POST อัตโนมัติเมื่อเครือข่ายขาด เพราะอาจสร้างรายการสำเร็จแล้ว
X-Client-ID: pi_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx X-Client-Secret: ps_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Content-Type: application/json
| Field | Type | Required | Description |
|---|---|---|---|
amount | number | required | จำนวนเงิน (1 - 999,999) |
memo | string | optional | หมายเหตุ เช่น username, order_id (สูงสุด 255 ตัวอักษร) |
{
"amount": 100,
"memo": "user:john_doe"
}| Field | Type | Description |
|---|---|---|
code | number | 200 |
ref | string | Reference ID สำหรับเช็คสถานะ |
amount | number | จำนวนเงินที่ขอ |
unique_amount | number | ยอดจริงที่ต้องโอน (เพิ่มสตางค์สุ่ม) |
qr_code | string | PromptPay QR payload สำหรับสร้าง QR |
promptpay_id | string | หมายเลข PromptPay ปลายทาง |
memo | string | null | หมายเหตุที่ส่งมา |
status | string | "pending" |
expires_at | string | เวลาหมดอายุ 15 นาทีหลังสร้างรายการ (ISO 8601) |
expires_in | number | วินาทีที่เหลือก่อนหมดอายุ (เริ่มต้น 900 วินาที) |
{
"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
}เมื่อสร้าง 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
}ทุก error response ส่งรูปแบบ code + error
{
"code": 1012,
"error": "Insufficient balance",
"balance": 0.00
}| Code | HTTP | Message & Explanation |
|---|---|---|
1000 | 401 | Missing credentials (ไม่ได้ส่ง X-Client-ID หรือ X-Client-Secret) |
1001 | 401 | Invalid credentials (Client ID หรือ Secret ไม่ถูกต้อง) |
1002 | 403 | Application inactive (แอปพลิเคชันถูกระงับการใช้งาน) |
1003 | 403 | Account inactive (บัญชีผู้ใช้ถูกระงับ) |
1010 | 400 | Invalid amount (จำนวนเงินไม่ถูกต้อง ต้องอยู่ระหว่าง 1 - 999,999) |
1011 | 400 | PromptPay ID not configured (ยังไม่ได้ตั้งค่าเบอร์ PromptPay ในระบบ) |
1012 | 402 | Insufficient balance (ยอดเงินเครดิตในระบบไม่พอหักค่าบริการ) |
1013 | 400 | No unique amount available (ยอดเศษสตางค์ไม่ว่างชั่วคราว — กรุณาลองใหม่อีกครั้ง) |
1020 | 404 | Topup not found (ไม่พบรายการชำระเงิน) |
1021 | 400 | Topup not pending (สถานะรายการไม่ใช่ pending เช่น หมดอายุแล้ว หรือ ได้รับเงินแล้ว) |
1022 | 503 | Bot not connected (บอทตรวจจับยอดเงินไม่ได้เชื่อมต่อ — กรุณาเริ่มบอทก่อนสร้าง QR) |
1030 | 429 | Rate limit exceeded (เกินเพดานคำขอ — รอตาม Retry-After โดยแอปยังใช้งานอยู่) |
1050 | 503 | Temporarily unavailable for maintenance (ระบบปิดปรับปรุงชั่วคราว — รอตาม Retry-After ก่อนลองใหม่) |
2001 | 500 | Internal error (ข้อผิดพลาดภายในระบบ) |
ตัวอย่างการเชื่อมต่อ 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); }
);