ภาพรวม ZynexPay API
ZynexPay API ใช้ RESTful architecture รองรับการชำระเงินผ่าน QR Code, PromptPay, บัตรเครดิต/เดบิต และช่องทางอื่นๆ ข้อมูลทั้งหมดรับ-ส่งในรูปแบบ JSON
ใช้ Test Keys (pk_test_ / sk_test_) เพื่อทดสอบ Integration
โดยไม่มีการเรียกเก็บเงินจริง สลับเป็น Live Keys เมื่อพร้อม
Authentication
ทุก API request ต้องมี Secret Key ใน Header เพื่อยืนยันตัวตน
Authorization: Bearer sk_live_YOUR_SECRET_KEY
Content-Type: application/json
| Key Type | Prefix | ใช้ที่ |
|---|---|---|
Publishable Key |
pk_live_ |
Frontend (checkout form) |
Secret Key |
sk_live_ |
Backend (server-side only) |
Test Publishable |
pk_test_ |
Frontend (sandbox) |
Test Secret |
sk_test_ |
Backend (sandbox) |
Base URL
https://api.zynexpay.com/v1
https://sandbox.zynexpay.com/v1
สร้างรายการชำระเงิน
/v1/payments
สร้างรายการชำระเงินใหม่ ระบบจะส่ง URL สำหรับลูกค้าชำระเงินกลับมา
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
amount |
number | ✅ | จำนวนเงิน (หน่วยบาท เช่น 150.00) |
currency |
string | ✅ | สกุลเงิน: THB |
description |
string | ✅ | คำอธิบายรายการ เช่น "Order #1234" |
callback_url |
string | ✅ | URL รับ webhook เมื่อชำระสำเร็จ |
return_url |
string | ❌ | URL redirect หลังชำระเงินเสร็จ |
metadata |
object | ❌ | ข้อมูลเพิ่มเติม (key-value) |
customer_email |
string | ❌ | อีเมลลูกค้า (ส่งใบเสร็จ) |
expires_in |
integer | ❌ | หมดอายุ (วินาที) default: 1800 |
Request
curl -X POST https://api.zynexpay.com/v1/payments \
-H "Authorization: Bearer sk_live_YOUR_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"amount": 1500.00,
"currency": "THB",
"description": "Order #1234",
"callback_url": "https://yoursite.com/webhook",
"return_url": "https://yoursite.com/thank-you",
"customer_email": "customer@example.com",
"metadata": {
"order_id": "ORD-1234"
}
}'
Response 200 OK
{
"id": "pay_a1b2c3d4e5f6",
"object": "payment",
"amount": 1500.00,
"currency": "THB",
"status": "pending",
"description": "Order #1234",
"checkout_url": "https://checkout.zynexpay.com/pay/pay_a1b2c3d4e5f6",
"qr_code_url": "https://api.zynexpay.com/v1/qr/pay_a1b2c3d4e5f6.png",
"expires_at": "2026-03-03T12:30:00Z",
"created_at": "2026-03-03T12:00:00Z",
"metadata": {
"order_id": "ORD-1234"
}
}
ดึงรายละเอียดการชำระ
/v1/payments/{payment_id}
ดึงรายละเอียดรายการชำระเงินจาก Payment ID
curl https://api.zynexpay.com/v1/payments/pay_a1b2c3d4e5f6 \
-H "Authorization: Bearer sk_live_YOUR_SECRET_KEY"
Response 200 OK
{
"id": "pay_a1b2c3d4e5f6",
"object": "payment",
"amount": 1500.00,
"currency": "THB",
"status": "succeeded",
"paid_at": "2026-03-03T12:05:23Z",
"payment_method": "promptpay",
"fee": 3.75,
"net": 1496.25
}
แสดงรายการชำระทั้งหมด
/v1/payments
Query Parameters
| Parameter | Type | Description |
|---|---|---|
limit |
integer | จำนวนผลลัพธ์ (default: 20, max: 100) |
offset |
integer | ข้ามรายการ (pagination) |
status |
string | pending | succeeded | failed |
expired
|
from |
string | เริ่มต้น (ISO 8601) |
to |
string | สิ้นสุด (ISO 8601) |
สร้าง QR Code / PromptPay
/v1/payments/qr
สร้าง QR Code สำหรับให้ลูกค้าสแกนชำระเงินผ่าน PromptPay หรือ Mobile Banking
const zynexpay = require('zynexpay');
zynexpay.configure({ secretKey: 'sk_live_YOUR_KEY' });
const qr = await zynexpay.payments.createQR({
amount: 350.00,
currency: 'THB',
description: 'สินค้า A',
callback_url: 'https://yoursite.com/webhook'
});
console.log(qr.qr_code_url);
// => https://api.zynexpay.com/v1/qr/pay_xxx.png
ใช้ qr_code_url จาก response โดยตรง — สามารถ embed เป็น
<img> ในหน้าเว็บ หรือส่งให้ลูกค้าทาง LINE/Email
Webhook Events
ZynexPay จะส่ง POST request ไปยัง endpoint ของคุณเมื่อมี event เกิดขึ้น
| Event | Description |
|---|---|
payment.success |
ลูกค้าชำระเงินสำเร็จ |
payment.failed |
การชำระเงินล้มเหลว / ถูกปฏิเสธ |
payment.expired |
รายการหมดอายุ (ไม่ชำระทันเวลา) |
payment.refund |
คืนเงินให้ลูกค้า |
payout.completed |
ถอนเงินเข้าบัญชีธนาคารสำเร็จ |
payout.failed |
การถอนเงินล้มเหลว |
charge.dispute |
ลูกค้าเปิดข้อพิพาท (Chargeback) |
Verify Webhook Signature
ทุก webhook request จะมี header X-ZynexPay-Signature ใช้ตรวจสอบว่า request มาจาก
ZynexPay จริง
const crypto = require('crypto');
app.post('/webhook', (req, res) => {
const signature = req.headers['x-zynexpay-signature'];
const payload = JSON.stringify(req.body);
const expected = crypto
.createHmac('sha256', 'whsec_YOUR_SIGNING_SECRET')
.update(payload)
.digest('hex');
if (signature === expected) {
// ✅ Verified — process the event
const event = req.body;
switch (event.type) {
case 'payment.success':
// อัปเดตสถานะ order
break;
case 'payment.failed':
// แจ้ง admin
break;
}
res.status(200).json({ received: true });
} else {
res.status(401).json({ error: 'Invalid signature' });
}
});
Error Codes
เมื่อเกิดข้อผิดพลาด API จะส่ง HTTP status code พร้อมรายละเอียด
| Code | Status | Description |
|---|---|---|
400 |
Bad Request | ข้อมูลไม่ถูกต้อง หรือขาด parameter |
401 |
Unauthorized | API Key ไม่ถูกต้อง หรือหมดอายุ |
403 |
Forbidden | ไม่มีสิทธิ์ดำเนินการนี้ |
404 |
Not Found | ไม่พบรายการที่ร้องขอ |
422 |
Unprocessable | ข้อมูลรูปแบบถูกต้อง แต่ไม่สามารถดำเนินการ |
429 |
Rate Limited | เกินจำนวน request ที่อนุญาต |
500 |
Server Error | เกิดข้อผิดพลาดฝั่ง server |
{
"error": {
"code": "invalid_amount",
"message": "Amount must be greater than 0",
"param": "amount",
"status": 400
}
}
Rate Limits
| Tier | Limit | Window |
|---|---|---|
| Standard | 100 requests | per minute |
| Pro | 500 requests | per minute |
| Enterprise | Custom | ติดต่อ Sales |
เมื่อเกิน limit จะได้รับ response 429 Too Many Requests พร้อม header
Retry-After
SDKs & Libraries
ZynexPay มี SDK สำหรับภาษาที่นิยม
npm install zynexpay
pip install zynexpay
composer require zynexpay/sdk
Maven: com.zynexpay:sdk