คู่มือการใช้งาน API
เชื่อมต่อ Stripe เพื่อรับชำระเงิน สร้าง Payment Link รับ Webhook และเก็บประวัติลง PostgreSQL
ภาพรวม
API สำหรับเชื่อมต่อ Stripe Payment Gateway รองรับ Payment Link, Payment Intent, Checkout Session, Webhook และ Audit Log
POST /api/payments/generate-urlpaymentUrlPOST /api/webhooks/stripeรูปแบบ Response
ทุก API ตอบกลับเป็น JSON ในรูปแบบเดียวกัน
{
"status": true,
"code": 200,
"message": "OK",
"data": { }
}
// Error Response
{
"status": false,
"code": 400,
"message": "error message",
"data": null
}
Health Check
ตรวจสอบสถานะระบบ, database และ Stripe config
Response
{
"status": true,
"code": 200,
"message": "OK",
"data": {
"service": "api-payment-stripe",
"version": "1.0.0",
"uptime": 120,
"checks": {
"database": { "status": "up", "latencyMs": 68 },
"stripe": { "status": "up", "secretKey": true },
"callback": { "status": "optional", "configured": false }
}
}
}
Response
{ "status": true, "code": 200, "message": "OK", "data": { "status": "alive" } }Response
{ "status": true, "code": 200, "message": "OK", "data": { "status": "ready" } }คืน HTTP 503 ถ้า database หรือ Stripe ไม่พร้อม
Payment Link
สร้าง Stripe Payment Link ให้ลูกค้าเปิดชำระเงินผ่านหน้า hosted ของ Stripe
Request Body
| Field | Type | Description |
|---|---|---|
amountrequired | number | จำนวนเงิน (บาท) |
productName | string | ชื่อสินค้า/บริการ |
description | string | รายละเอียด |
successUrl | string | URL redirect หลังชำระสำเร็จ |
metadata | object | ข้อมูลเพิ่มเติม เช่น orderId |
ตัวอย่าง cURL
curl -X POST https://api-payment-stripe.numedapp.com/api/payments/generate-url \
-H "Content-Type: application/json" \
-d '{
"amount": 500,
"productName": "Premium Plan",
"description": "Order #1234",
"successUrl": "https://api-payment-stripe.numedapp.com/success",
"metadata": { "orderId": "1234" }
}'Response
{
"status": true,
"code": 201,
"message": "Payment URL generated",
"data": {
"paymentUrl": "https://buy.stripe.com/xxx",
"paymentLinkId": "plink_xxx",
"amount": 500,
"currency": "thb",
"active": true
}
}Request body เหมือน generate-url แต่ response คืน url แทน paymentUrl
Payment Intent
ฝังฟอร์มชำระเงินใน frontend ด้วย Stripe Elements
Request Body
| Field | Type | Description |
|---|---|---|
amountrequired | number | จำนวนเงิน (บาท) |
description | string | รายละเอียด |
customerEmail | string | อีเมลลูกค้า |
metadata | object | ข้อมูลเพิ่มเติม |
Response
{
"status": true,
"code": 201,
"message": "Payment intent created",
"data": {
"clientSecret": "pi_xxx_secret_xxx",
"paymentIntentId": "pi_xxx",
"amount": 50000,
"currency": "thb",
"status": "requires_payment_method"
}
}Checkout Session
สร้าง Stripe Checkout Session แบบ redirect
Request Body
| Field | Type | Description |
|---|---|---|
amountrequired | number | จำนวนเงิน (บาท) |
productName | string | ชื่อสินค้า |
successUrlrequired | string | URL หลังชำระสำเร็จ |
cancelUrlrequired | string | URL เมื่อยกเลิก |
customerEmail | string | อีเมลลูกค้า |
Response
{ "status": true, "code": 201, "message": "Checkout session created", "data": { "sessionId": "cs_xxx", "url": "https://checkout.stripe.com/..." } }Refund
คืนเงินให้ลูกค้าผ่าน Payment Intent ID
Request Body
| Field | Type | Description |
|---|---|---|
paymentIntentIdrequired | string | Payment Intent ID |
amount | number | จำนวนเงินคืน (ไม่ใส่ = คืนเต็มจำนวน) |
reason | string | duplicate / fraudulent / requested_by_customer |
ตัวอย่าง cURL
curl -X POST https://api-payment-stripe.numedapp.com/api/payments/refund \
-H "Content-Type: application/json" \
-d '{ "paymentIntentId": "pi_xxx" }'รายการชำระเงิน
ดูรายการชำระเงินที่บันทึกใน PostgreSQL
Query Parameters
| Param | Description |
|---|---|
status | pending · succeeded · failed · refunded |
limit | จำนวนรายการ (default: 50, max: 100) |
offset | offset สำหรับ pagination |
Stripe Webhook
รับ callback จาก Stripe เมื่อมี event เกิดขึ้น
การตั้งค่า Stripe Dashboard
1. Developers → Webhooks → Add endpoint 2. URL: https://api-payment-stripe.numedapp.com/api/webhooks/stripe 3. เลือก events: - payment_intent.succeeded - payment_intent.payment_failed - checkout.session.completed - charge.refunded 4. คัดลอก Signing secret → STRIPE_WEBHOOK_SECRET
ทดสอบ local ด้วย Stripe CLI
stripe listen --forward-to https://api-payment-stripe.numedapp.com/api/webhooks/stripe stripe trigger checkout.session.completed
Events & Callback
ดู webhook events และ callback logs ที่บันทึกใน database
Query Parameters
| Param | Description |
|---|---|
limit | จำนวนรายการ (default: 50) |
offset | pagination offset |
Query: success (true/false), webhookEventId
Callback Payload ที่ส่งไประบบของคุณ
{
"eventId": "evt_xxx",
"eventType": "checkout.session.completed",
"action": "checkout_completed",
"message": "Checkout session completed",
"data": {
"sessionId": "cs_xxx",
"paymentIntentId": "pi_xxx",
"amountTotal": 500,
"currency": "thb",
"paymentStatus": "paid",
"metadata": { "orderId": "1234" }
}
}ประวัติการชำระเงิน
ดู timeline ทุก event ของการชำระเงิน
Query Parameters
| Param | Description |
|---|---|
paymentId | กรองตาม payment ID |
paymentIntentId | กรองตาม Payment Intent |
eventType | payment_created · payment_succeeded · refund_created |
limit / offset | pagination |
System Logs
ดู system logs ที่บันทึกจาก API requests, webhooks และ errors
Query Parameters
| Param | Description |
|---|---|
level | info · warn · error |
source | http · webhook · payment · error |
limit / offset | pagination |