PayGate API Reference
Cổng thanh toán (mock) cho English app — thu tiền gói Premium (1 lần hoặc thuê bao định kỳ). Trình bày kiểu Stripe để IT-BA/QC đọc hiểu & test.
Base URL: http://localhost:4242 · Đặc tả máy đọc: openapi.yaml
Giới thiệu
REST API trả JSON. Tài nguyên: customers, payment_methods, charges, refunds, subscriptions, events. Mọi request /v1/* cần khóa Bearer.
Xác thực
Gửi khóa bí mật trong header Authorization (Bearer). Khóa test bắt đầu bằng sk_test_.
Authorization: Bearer sk_test_demo_4242
Lỗi
Body lỗi: { "error": { "code", "message" } }.
| HTTP | code | Ý nghĩa nghiệp vụ |
|---|---|---|
| 400 | missing_field / invalid_json / invalid_plan | Dữ liệu gửi lên thiếu/sai |
| 401 | unauthorized | Thiếu/sai khóa |
| 402 | card_declined, insufficient_funds, expired_card, incorrect_cvc | Thẻ lỗi (mỗi loại 1 thông báo cho user) |
| 404 | resource_missing | Không tìm thấy tài nguyên |
| 429 | rate_limited | Gọi quá nhiều |
| 500 | processing_error | Lỗi tạm phía cổng, nên retry |
Token thẻ thử (field source)
tok_chargeabletok_declinedtok_insufficienttok_expiredtok_cvc_failtok_error
Rate limit
Mặc định 30 request / 10 giây / mỗi IP. Vượt ngưỡng trả 429. App nên retry/backoff.
Phân trang
Các endpoint GET danh sách trả { object:"list", has_more, data:[...] }. Tham số ?limit= và ?starting_after=<id> (cursor).
POST/v1/customers
Tạo khách hàng (gắn email). Khách dùng để lưu thẻ + đăng ký gói.
| Tham số | Mô tả |
|---|---|
email bắt buộc | Email khách |
name | Tên (tùy chọn) |
curl -s $BASE/v1/customers \ -H "Authorization: Bearer sk_test_demo_4242" \ -d '{"email":"hv@example.com","name":"Nguyen A"}'
{ "id":"cus_...", "object":"customer", "email":"hv@example.com" }POST/v1/payment_methods
Lưu thẻ cho khách để thanh toán lần sau (không nhập lại thẻ).
| Tham số | Mô tả |
|---|---|
customer bắt buộc | id khách |
card bắt buộc | { number, brand, exp } |
{ "id":"pm_...", "brand":"visa", "last4":"4242" }POST/v1/charges
Tạo thanh toán 1 lần. Dùng source (token thử) hoặc payment_method (thẻ đã lưu). Header tùy chọn Idempotency-Key chống thu trùng.
| Tham số | Mô tả |
|---|---|
amount bắt buộc | Số tiền (đồng VND) |
currency bắt buộc | vnd |
source / payment_method | Token thẻ hoặc thẻ đã lưu |
curl -s $BASE/v1/charges \ -H "Authorization: Bearer sk_test_demo_4242" \ -d '{"amount":99000,"currency":"vnd","source":"tok_chargeable"}'
{ "id":"ch_...", "status":"succeeded", "amount":99000 }
{ "error":{ "code":"insufficient_funds" } }GET/v1/charges/{id}
Tra trạng thái 1 thanh toán. Trạng thái: pendingsucceededfailedrefunded
curl -s $BASE/v1/charges/ch_abc \ -H "Authorization: Bearer sk_test_demo_4242"
GET/v1/charges
Liệt kê thanh toán có phân trang (cho màn hình Lịch sử giao dịch).
curl -s $BASE/v1/charges?limit=5 \ -H "Authorization: Bearer sk_test_demo_4242"
POST/v1/refunds
Hoàn tiền 1 charge đã succeeded. Sau hoàn, charge chuyển refunded.
| Tham số | Mô tả |
|---|---|
charge bắt buộc | id charge cần hoàn |
amount | Mặc định hoàn toàn bộ |
curl -s $BASE/v1/refunds \ -H "Authorization: Bearer sk_test_demo_4242" \ -d '{"charge":"ch_abc"}'
POST/v1/subscriptions
Đăng ký gói Premium định kỳ. Plan: premium_monthly (99.000đ/tháng) · premium_yearly (990.000đ/năm).
| Tham số | Mô tả |
|---|---|
customer bắt buộc | id khách |
plan bắt buộc | premium_monthly | premium_yearly |
{ "id":"sub_...", "status":"active",
"plan":"premium_monthly", "current_period_end":... }POST/v1/subscriptions/{id}/cancel
Hủy gói. Trạng thái chuyển canceled. Sinh event subscription.canceled.
curl -s -X POST $BASE/v1/subscriptions/sub_abc/cancel \ -H "Authorization: Bearer sk_test_demo_4242"
GET/v1/events · POST/v1/events/simulate
Hệ thống sinh sự kiện cho mỗi hành động (charge.succeeded, charge.failed, charge.refunded, subscription.created, subscription.canceled). App polling endpoint này thay cho nhận webhook callback. Dùng ?type= để lọc. simulate để tự tạo event test (vd subscription.renewed).
curl -s $BASE/v1/events?type=charge.failed \ -H "Authorization: Bearer sk_test_demo_4242"
curl -s $BASE/v1/events/simulate \ -H "Authorization: Bearer sk_test_demo_4242" \ -d '{"type":"subscription.renewed"}'
PayGate mock · tài liệu phục vụ Buổi 6 — Integrate.