เอกสาร API
คู่มือสำหรับนักพัฒนาในการเชื่อมต่อระบบ Trush Payment
เริ่มต้นใช้งาน
1. สมัครใช้งานและรับ API Key
- สมัครสมาชิกที่ Trush Payment Dashboard
- ไปที่ Settings > API Keys
- สร้าง API Key ใหม่ (แยกระหว่าง Test และ Production)
# Test API Key
test_pk_xxxxxxxxxxxxxxxxxxxxx
test_sk_xxxxxxxxxxxxxxxxxxxxx
# Production API Key
live_pk_xxxxxxxxxxxxxxxxxxxxx
live_sk_xxxxxxxxxxxxxxxxxxxxx
2. ติดตั้ง SDK
เราให้บริการ SDK สำหรับภาษายอดนิยม:
Node.js / JavaScript
npm install trush-payment
PHP
composer require trush-payment/sdk
Python
pip install trush-payment
Go
go get github.com/trush-payment/go-sdk
การใช้งานพื้นฐาน
สร้าง Payment Intent
Node.js
const TrushPayment = require('trush-payment');
const trush = new TrushPayment('your_secret_key');
const payment = await trush.payments.create({
amount: 10000, // จำนวนเงินในหน่วยสตางค์ (100 บาท)
currency: 'THB',
description: 'ค่าสินค้า Order #1234',
payment_methods: ['credit_card', 'promptpay', 'mobile_banking'],
return_url: 'https://yourdomain.com/payment/success',
webhook_url: 'https://yourdomain.com/webhook'
});
console.log(payment.payment_url);
// https://payment.trushpayment.com/pay/xxxxx
PHP
<?php
require_once('vendor/autoload.php');
$trush = new \TrushPayment\Client('your_secret_key');
$payment = $trush->payments->create([
'amount' => 10000,
'currency' => 'THB',
'description' => 'ค่าสินค้า Order #1234',
'payment_methods' => ['credit_card', 'promptpay', 'mobile_banking'],
'return_url' => 'https://yourdomain.com/payment/success',
'webhook_url' => 'https://yourdomain.com/webhook'
]);
echo $payment->payment_url;
Python
import trush_payment
trush = trush_payment.Client('your_secret_key')
payment = trush.payments.create(
amount=10000,
currency='THB',
description='ค่าสินค้า Order #1234',
payment_methods=['credit_card', 'promptpay', 'mobile_banking'],
return_url='https://yourdomain.com/payment/success',
webhook_url='https://yourdomain.com/webhook'
)
print(payment.payment_url)
REST API
Authentication
ใช้ Secret Key ใน Header:
curl https://api.trushpayment.com/v1/payments \
-H "Authorization: Bearer your_secret_key" \
-H "Content-Type: application/json"
Endpoints
สร้างการชำระเงิน
POST /v1/payments
Request Body:
{
"amount": 10000,
"currency": "THB",
"description": "ค่าสินค้า Order #1234",
"payment_methods": ["credit_card", "promptpay"],
"return_url": "https://yourdomain.com/success",
"webhook_url": "https://yourdomain.com/webhook"
}
Response:
{
"id": "pay_xxxxxxxxxxxxx",
"amount": 10000,
"currency": "THB",
"status": "pending",
"payment_url": "https://payment.trushpayment.com/pay/xxxxx",
"created_at": "2024-01-15T10:30:00Z"
}
ตรวจสอบสถานะการชำระเงิน
GET /v1/payments/{payment_id}
Response:
{
"id": "pay_xxxxxxxxxxxxx",
"amount": 10000,
"currency": "THB",
"status": "successful",
"payment_method": "credit_card",
"paid_at": "2024-01-15T10:35:00Z"
}
Refund การชำระเงิน
POST /v1/refunds
Request Body:
{
"payment_id": "pay_xxxxxxxxxxxxx",
"amount": 10000,
"reason": "ยกเลิกคำสั่งซื้อ"
}
Webhook
การตั้งค่า Webhook
Webhook จะถูกส่งเมื่อมีการเปลี่ยนแปลงสถานะของการชำระเงิน
Events:
payment.successful- ชำระเงินสำเร็จpayment.failed- ชำระเงินไม่สำเร็จrefund.created- สร้าง refund แล้วrefund.completed- refund สำเร็จ
ตัวอย่างการรับ Webhook
Node.js/Express
app.post('/webhook', express.json(), (req, res) => {
const event = req.body;
// ตรวจสอบ Signature
const signature = req.headers['x-trush-signature'];
const isValid = trush.webhooks.verify(event, signature);
if (!isValid) {
return res.status(400).send('Invalid signature');
}
switch (event.type) {
case 'payment.successful':
console.log('Payment successful:', event.data);
// Update order status
break;
case 'payment.failed':
console.log('Payment failed:', event.data);
break;
}
res.json({ received: true });
});
Webhook Payload:
{
"type": "payment.successful",
"data": {
"id": "pay_xxxxxxxxxxxxx",
"amount": 10000,
"currency": "THB",
"status": "successful",
"payment_method": "credit_card",
"paid_at": "2024-01-15T10:35:00Z"
},
"created_at": "2024-01-15T10:35:00Z"
}
Testing
Test Cards
ใช้บัตรทดสอบเหล่านี้ใน Test Mode:
| Card Number | Result |
|---|---|
| 4242 4242 4242 4242 | สำเร็จ |
| 4000 0000 0000 0002 | ถูกปฏิเสธ |
| 4000 0000 0000 9995 | ไม่เพียงพอ |
Test Data:
- CVV: 123 (ใดๆ)
- วันหมดอายุ: ใดๆในอนาคต
- ชื่อ: ใดๆ
Sandbox Environment
API Endpoint: https://sandbox-api.trushpayment.com/v1/
Payment URL: https://sandbox-payment.trushpayment.com/
Plugins และ Integration
WooCommerce
# ติดตั้งผ่าน WordPress Admin
1. Plugins > Add New > Search "Trush Payment"
2. Install และ Activate
3. WooCommerce > Settings > Payments > Trush Payment
4. ใส่ API Key
Shopify
# ติดตั้งผ่าน Shopify App Store
1. ไปที่ Shopify App Store
2. ค้นหา "Trush Payment"
3. Install App
4. ตั้งค่า API Key ใน Settings
ช่วยเหลือเพิ่มเติม
มีคำถาม? ติดต่อ Technical Support