ทดสอบชำระเงิน เริ่มใช้งาน
Stripe Payment Gateway

คู่มือการใช้งาน API

Base URL https://api-payment-stripe.numedapp.com Currency USD Version v1.0
API Endpoints
20+
Payment · Webhook · Audit
Payment Methods
3
Link · Intent · Checkout
Database
PostgreSQL
History · Logs · Callbacks
📋

ภาพรวม

API สำหรับเชื่อมต่อ Stripe Payment Gateway รองรับ Payment Link, Payment Intent, Checkout Session, Webhook และ Audit Log

1ระบบของคุณเรียก POST /api/payments/generate-url
2API สร้าง Stripe Payment Link → คืน paymentUrl
3ส่ง link ให้ลูกค้าเปิดชำระเงินผ่าน Stripe
4Stripe ส่ง Webhook → POST /api/webhooks/stripe
5API บันทึกประวัติ + ส่ง callback ไประบบของคุณ
{ }

รูปแบบ Response

ทุก API ตอบกลับเป็น JSON ในรูปแบบเดียวกัน

JSON
{
  "status": true,
  "code": 200,
  "message": "OK",
  "data": { }
}

// Error Response
{
  "status": false,
  "code": 400,
  "message": "error message",
  "data": null
}
💚

Health Check

ตรวจสอบสถานะระบบ, database และ Stripe config

GET /api/health Health check แบบละเอียด

Response

JSON
{
  "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 }
    }
  }
}
GET /api/health/live Liveness probe

Response

JSON
{ "status": true, "code": 200, "message": "OK", "data": { "status": "alive" } }
GET /api/health/ready Readiness probe

Response

JSON
{ "status": true, "code": 200, "message": "OK", "data": { "status": "ready" } }

คืน HTTP 503 ถ้า database หรือ Stripe ไม่พร้อม

💳

Payment Intent

ฝังฟอร์มชำระเงินใน frontend ด้วย Stripe Elements

POST /api/payments/payment-intent สร้าง Payment Intent

Request Body

FieldTypeDescription
amountrequirednumberจำนวนเงิน (USD)
descriptionstringรายละเอียด
customerEmailstringอีเมลลูกค้า
metadataobjectข้อมูลเพิ่มเติม

Response

JSON
{
  "status": true,
  "code": 201,
  "message": "Payment intent created",
  "data": {
    "clientSecret": "pi_xxx_secret_xxx",
    "paymentIntentId": "pi_xxx",
    "amount": 50000,
    "currency": "usd",
    "status": "requires_payment_method"
  }
}
GET /api/payments/payment-intent/:id ดูสถานะการชำระ
GET /api/payments/payment-intent/:id/session ดึง session สำหรับหน้าชำระเงิน
GET /api/payments/config ดึง Stripe Publishable Key
🛒

Checkout Session

สร้าง Stripe Checkout Session แบบ redirect

POST /api/payments/checkout-session สร้าง Checkout Session

Request Body

FieldTypeDescription
amountrequirednumberจำนวนเงิน (USD)
productNamestringชื่อสินค้า
successUrlrequiredstringURL หลังชำระสำเร็จ
cancelUrlrequiredstringURL เมื่อยกเลิก
customerEmailstringอีเมลลูกค้า

Response

JSON
{ "status": true, "code": 201, "message": "Checkout session created", "data": { "sessionId": "cs_xxx", "url": "https://checkout.stripe.com/..." } }
↩️

Refund

คืนเงินให้ลูกค้าผ่าน Payment Intent ID

POST /api/payments/refund คืนเงิน

Request Body

FieldTypeDescription
paymentIntentIdrequiredstringPayment Intent ID
amountnumberจำนวนเงินคืน (ไม่ใส่ = คืนเต็มจำนวน)
reasonstringduplicate / fraudulent / requested_by_customer

ตัวอย่าง cURL

cURL
curl -X POST https://api-payment-stripe.numedapp.com/api/payments/refund \
  -H "Content-Type: application/json" \
  -d '{ "paymentIntentId": "pi_xxx" }'
📊

รายการชำระเงิน

ดูรายการชำระเงินที่บันทึกใน PostgreSQL

GET /api/payments ดูรายการชำระเงินทั้งหมด

Query Parameters

ParamDescription
statuspending · succeeded · failed · refunded
limitจำนวนรายการ (default: 50, max: 100)
offsetoffset สำหรับ pagination
🔔

Stripe Webhook

รับ callback จาก Stripe เมื่อมี event เกิดขึ้น

POST /api/webhooks/stripe รับ Webhook จาก Stripe

การตั้งค่า Stripe Dashboard

Steps
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

Shell
stripe listen --forward-to https://api-payment-stripe.numedapp.com/api/webhooks/stripe
stripe trigger checkout.session.completed
GET /api/webhooks/config ดู webhook config
📡

Events & Callback

ดู webhook events และ callback logs ที่บันทึกใน database

GET /api/webhooks/events ดู webhook events ล่าสุด

Query Parameters

ParamDescription
limitจำนวนรายการ (default: 50)
offsetpagination offset
GET /api/webhooks/callbacks ดู callback logs

Query: success (true/false), webhookEventId

Callback Payload ที่ส่งไประบบของคุณ

JSON
{
  "eventId": "evt_xxx",
  "eventType": "checkout.session.completed",
  "action": "checkout_completed",
  "message": "Checkout session completed",
  "data": {
    "sessionId": "cs_xxx",
    "paymentIntentId": "pi_xxx",
    "amountTotal": 500,
    "currency": "usd",
    "paymentStatus": "paid",
    "metadata": { "orderId": "1234" }
  }
}
🕐

ประวัติการชำระเงิน

ดู timeline ทุก event ของการชำระเงิน

GET /api/payments/history ดูประวัติทุก event

Query Parameters

ParamDescription
paymentIdกรองตาม payment ID
paymentIntentIdกรองตาม Payment Intent
eventTypepayment_created · payment_succeeded · refund_created
limit / offsetpagination
📝

System Logs

ดู system logs ที่บันทึกจาก API requests, webhooks และ errors

GET /api/logs/logs ดู system logs

Query Parameters

ParamDescription
levelinfo · warn · error
sourcehttp · webhook · payment · error
limit / offsetpagination