API Documentation
เวอร์ชันปรับปรุงของเอกสาร API สำหรับผู้พัฒนา – รวมตัวอย่างและแนวทางการใช้งานอย่างปลอดภัย
Secure
REST JSON
Low Latency
Quick Start
Quick Start
สร้าง API key, ตั้งค่า env, เรียก login, สร้าง payment intent และฟัง webhook.
Security Guidance
ใช้ HTTPS, หมุนเวียน secret, ตรวจสอบ signature ของทุก webhook, เก็บ token อย่างปลอดภัย.
Authentication#
ใช้ Bearer <token> ใน Header Authorization หลังจากล็อกอินสำเร็จ
POST /api/v1/auth/login
Content-Type: application/json
{ "email": "demo@elixopay.com", "password": "Password123" }
# Response (200)
{
"success": true,
"data": {
"token": "<JWT>",
"refreshToken": "<JWT_REFRESH>",
"user": { "id": 1, "email": "demo@elixopay.com" }
}
}
ใช้ Authorization: Bearer <token> กับทุก endpoint ที่มีการระบุว่า "Bearer".
ควรเก็บ token ฝั่งเซิร์ฟเวอร์หรือใช้ HttpOnly cookie ใน production.
Payments#
| Endpoint | Method | Description | Auth |
|---|---|---|---|
| /api/v1/payments | POST | Create Payment (Stripe Intent) | Bearer |
| /api/v1/payments/:id/confirm | POST | Confirm Payment Intent | Bearer |
| /api/v1/payments/:id/cancel | POST | Cancel Pending Payment | Bearer |
| /api/v1/payments/:id/refund | POST | Refund a Succeeded Payment | Bearer |
| /api/v1/payments/stats | GET | Aggregate Statistics | Bearer |
| /api/v1/payments | GET | List Payments (query params) | Bearer |
curl -X POST "$BASE_URL/api/v1/payments" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"amount":299900,"currency":"thb","description":"Test order #1"}'
หมายเหตุ: จำนวนเงิน (amount) ใช้หน่วยเป็นสตางค์ (เช่น 2,999.00 บาท = 299900)
Webhooks#
Stripe จะส่งอีเวนต์มายัง /api/v1/webhooks/stripe เพื่ออัปเดตสถานะการชำระเงิน (เช่น payment_intent.succeeded, payment_intent.payment_failed, charge.refunded).
stripe listen --forward-to http://localhost:3000/api/v1/webhooks/stripe
- ควรกำหนด secret ใน
STRIPE_WEBHOOK_SECRET. - ตรวจสอบ signature ทุกครั้งก่อนปรับสถานะ payment.
Error Handling#
ทุก Response ที่ผิดพลาดจะมีรูปแบบ:
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Amount must be positive"
}
}
4xx – Client input ผิด เช่น validation, auth
5xx – Internal error ติดต่อ support พร้อม request id