การแจ้งเตือน Webhook
รับการอัปเดตสถานะการชำระเงินและการถอนแบบเรียลไทม์ผ่าน Webhook ที่ลงนามด้วย HMAC
ระบบ 2328.io ส่ง Webhook ไปยัง url_callback ของคุณทุกครั้งที่สถานะการชำระเงินเปลี่ยน นี่คือวิธีที่แนะนำในการรับแจ้งเตือนเกี่ยวกับการชำระเงินที่สำเร็จ
รูปแบบของคำขอ
- Method:
POST - Content-Type:
application/json - Signature: ฟิลด์
signใน body ของคำขอ
Payload
body ของ Webhook เหมือนกับการตอบกลับ /v1/payment/info พร้อมเพิ่มฟิลด์ sign สำหรับใช้ตรวจสอบลายเซ็น
การชำระเงินที่สำเร็จ
{
"uuid": "db17d490-15b6-47b9-9015-91d1d8b119f2",
"order_id": "ORDER-12345",
"amount": "180.00000000",
"currency": "RUB",
"url": "https://go.2328.io/db17d490-15b6-47b9-9015-91d1d8b119f2",
"expires_at": "2026-05-09T16:56:58+03:00",
"created_at": "2026-05-09T15:56:58+03:00",
"payer_currency": "TON",
"payer_amount": "0.95256917",
"network": "TON",
"address": "UQA0RevhkCQx-EltyNgPPeG8dqtnCz7ZslOzMdNQlLxVaNBb",
"payment_status": "paid",
"txid": "41c2a327323480af8e705d05deb09c238a41779928832abef4bb77c862357b11",
"payment_amount": "0.95256917",
"merchant_amount": "0.949711462490000000",
"amount_usd": "2.41324380",
"exchange_rate": "0.01340691",
"sign": "6f8c15b6e53b506d5bfa38ed3fb3b50697af73434262153c02e412541372f04d"
}การชำระเงินที่ยกเลิก / ล้มเหลว
เมื่อการชำระเงินไม่ได้อยู่ในสถานะสุดท้าย paid ฟิลด์ txid, payment_amount และ merchant_amount จะเป็น null:
{
"uuid": "48edaf2d-2c49-4638-8f86-88636f661c1f",
"order_id": "ORDER-12345",
"amount": "2800.00000000",
"currency": "RUB",
"url": "https://go.2328.io/48edaf2d-2c49-4638-8f86-88636f661c1f",
"expires_at": "2026-05-09T06:19:04+03:00",
"created_at": "2026-05-09T05:19:04+03:00",
"payer_currency": "ETH",
"payer_amount": "0.01620968",
"network": "ETH-ERC20",
"address": "0x37c20d6d96d130Bc5B33D832e43b8e16aACe0c59",
"payment_status": "cancel",
"txid": null,
"payment_amount": null,
"merchant_amount": null,
"amount_usd": "37.53934800",
"exchange_rate": "0.01340691",
"sign": "40ce68ad9691ad54e684329d75ab5adaf5b01409a2d18d3e0110b8c1be605342"
}อ้างอิงฟิลด์
| Field | Type | Description |
|---|---|---|
uuid | string | UUID ของการชำระเงิน |
order_id | string | order ID ของคุณ |
amount | decimal (8 dp) | จำนวนเงิน fiat ใน currency |
currency | string | สกุลเงิน fiat ที่ผู้ค้าร้องขอ |
url | string | URL หน้า checkout ที่โฮสต์ไว้ |
expires_at | string (ISO 8601) | เวลาที่เซสชันการชำระเงินจะหมดอายุ |
created_at | string (ISO 8601) | เวลาที่สร้างเซสชันการชำระเงิน |
payer_currency | string | คริปโตที่ผู้ชำระจ่าย |
payer_amount | decimal (8 dp) | จำนวนคริปโตที่คาดว่าจะได้รับ |
network | string | เครือข่ายบล็อกเชน |
address | string | ที่อยู่ฝากเงิน |
payment_status | string | หนึ่งใน: pending, check, paid, underpaid_check, underpaid, overpaid, cancel, aml_lock (ดู References) |
txid | string | null | hash ธุรกรรมบล็อกเชน ปรากฏเฉพาะหลังการชำระเงินยืนยันแล้ว |
payment_amount | decimal | null | จำนวนเงินที่จ่ายจริง ปรากฏเฉพาะหลังการชำระเงิน |
merchant_amount | decimal (18 dp) | null | จำนวนที่เครดิตให้ผู้ค้าหลังหักค่าธรรมเนียม |
amount_usd | decimal (8 dp) | จำนวนเงินใน USD ณ เวลาที่สร้าง |
exchange_rate | decimal | อัตราแลกเปลี่ยน crypto / fiat ที่ใช้ |
sign | string (hex) | ลายเซ็น HMAC-SHA256 ของ payload |
การตรวจสอบลายเซ็น
ขั้นตอนการตรวจสอบลายเซ็น Webhook:
- ดึงฟิลด์
signออกจาก payload - ลบฟิลด์
signออกจากออบเจกต์ - เข้ารหัสฟิลด์ที่เหลือเป็น JSON
- เข้ารหัส JSON เป็น Base64
- คำนวณ HMAC-SHA256 จากสตริง Base64 โดยใช้ API_KEY ของคุณ
- เปรียบเทียบลายเซ็นที่คำนวณได้กับค่า
signด้วยการเปรียบเทียบแบบ constant-time
<?php
function verifyWebhookSign(array $data, string $apiKey): bool {
$receivedSign = $data['sign'] ?? '';
unset($data['sign']);
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$base64 = base64_encode($json);
$calculated = hash_hmac('sha256', $base64, $apiKey);
return hash_equals($calculated, $receivedSign);
}
$apiKey = 'YOUR_API_KEY';
$payload = json_decode(file_get_contents('php://input'), true);
if (!verifyWebhookSign($payload, $apiKey)) {
http_response_code(401);
exit;
}
switch ($payload['payment_status']) {
case 'paid':
case 'overpaid':
// Credit the order — check idempotency by order_id first
break;
case 'underpaid_check':
case 'underpaid':
case 'cancel':
break;
}
http_response_code(200);import crypto from "crypto";
import express from "express";
const app = express();
app.use(express.json());
function verifyWebhookSign(payload, apiKey) {
const { sign, ...rest } = payload;
const json = JSON.stringify(rest);
const base64 = Buffer.from(json).toString("base64");
const calculated = crypto
.createHmac("sha256", apiKey)
.update(base64)
.digest("hex");
return crypto.timingSafeEqual(
Buffer.from(calculated),
Buffer.from(sign || ""),
);
}
app.post("/webhook", (req, res) => {
if (!verifyWebhookSign(req.body, process.env.API_KEY)) {
return res.sendStatus(401);
}
const { order_id, payment_status, txid } = req.body;
if (payment_status === "paid" || payment_status === "overpaid") {
// Credit the order — check idempotency by order_id first
}
res.sendStatus(200);
});import json
import hmac
import hashlib
import base64
from fastapi import FastAPI, Request, HTTPException
app = FastAPI()
API_KEY = "YOUR_API_KEY"
def verify_webhook_sign(payload: dict, api_key: str) -> bool:
received = payload.pop("sign", "")
body = json.dumps(payload, separators=(",", ":"), ensure_ascii=False)
b64 = base64.b64encode(body.encode("utf-8")).decode()
calculated = hmac.new(api_key.encode(), b64.encode(), hashlib.sha256).hexdigest()
return hmac.compare_digest(calculated, received)
@app.post("/webhook")
async def webhook(request: Request):
payload = await request.json()
if not verify_webhook_sign(payload, API_KEY):
raise HTTPException(401)
if payload["payment_status"] in ("paid", "overpaid"):
# Credit the order — check idempotency by order_id first
pass
return {"ok": True}package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"io"
"net/http"
)
func verifyWebhookSign(body []byte, apiKey string) (map[string]any, bool) {
var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
return nil, false
}
received, _ := payload["sign"].(string)
delete(payload, "sign")
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.Encode(payload)
reencoded := bytes.TrimRight(buf.Bytes(), "\n")
b64 := base64.StdEncoding.EncodeToString(reencoded)
h := hmac.New(sha256.New, []byte(apiKey))
h.Write([]byte(b64))
calculated := hex.EncodeToString(h.Sum(nil))
return payload, hmac.Equal([]byte(calculated), []byte(received))
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
payload, ok := verifyWebhookSign(body, apiKey)
if !ok {
http.Error(w, "invalid signature", http.StatusUnauthorized)
return
}
status, _ := payload["payment_status"].(string)
if status == "paid" || status == "overpaid" {
// Credit the order — check idempotency first
}
w.WriteHeader(http.StatusOK)
}require "json"
require "openssl"
require "base64"
require "sinatra"
API_KEY = "YOUR_API_KEY"
def verify_webhook_sign(payload, api_key)
received = payload.delete("sign") || ""
body = payload.to_json
b64 = Base64.strict_encode64(body)
calculated = OpenSSL::HMAC.hexdigest("SHA256", api_key, b64)
OpenSSL.fixed_length_secure_compare(calculated, received)
end
post "/webhook" do
payload = JSON.parse(request.body.read)
halt 401 unless verify_webhook_sign(payload, API_KEY)
if %w[paid overpaid].include?(payload["payment_status"])
# Credit the order — check idempotency by order_id first
end
status 200
endตรวจสอบลายเซ็นเสมอ ก่อนเครดิตเงินใด ๆ ให้ผู้ใช้ Webhook ที่ไม่มีลายเซ็นหรือลงนามไม่ถูกต้องอาจเป็นคำขอปลอมแปลง
Webhook ของการถอน
เมื่อ status ของการถอนเปลี่ยน ระบบจะส่ง Webhook แบบ POST ไปยัง URL url_callback ที่ระบุตอนสร้างการถอน หากไม่ได้ระบุ url_callback จะไม่มีการส่ง Webhook สำหรับการถอนนั้น
Webhook ของการถอนต้องตรวจสอบด้วย Payout API key — ไม่ใช่ API key ปกติ อัลกอริทึมการลงลายเซ็นเหมือนกับ Webhook ของการชำระเงิน (ตัด sign ออก, แปลงเป็น JSON, base64, HMAC-SHA256) แตกต่างกันแค่คีย์ที่ใช้
Payload
{
"uuid": "019dff1f-0dbd-7277-8d45-271e7775388f",
"order_id": "4dfdcc84402b1185b71cbe399321533e",
"status": "completed",
"currency": "TRX",
"network": "TRX-TRC20",
"amount": "3.00",
"merchant_amount": "3.00",
"network_amount": "3.00",
"amount_usd": "1.04",
"to_address": "THauRv5tcucQRohXg8NiyGTk16DX1XQG5x",
"memo": null,
"txid": "9242e533703704ef3eaba840f70b4a26333e72c943377ee375fea17badb53def",
"block_number": null,
"error_type": null,
"created_at": "2026-05-07T00:08:38+03:00",
"updated_at": "2026-05-07T00:08:54+03:00",
"from_currency": "USDT",
"debited_amount": "1.050735",
"debited_currency": "USDT",
"sign": "925ad7bf3d6841864101f7cc2c7e30652e70a06cdb04dbe07a0129480000ce4a"
}อ้างอิงฟิลด์
| Field | Type | Description |
|---|---|---|
uuid | string | UUID ของการถอน |
order_id | string | ID อ้างอิง / idempotency ของคุณ ถ้าระบุไว้ |
status | string | pending, completed, failed, cancelled (ดู References) |
currency | string | สกุลเงินที่ถอน |
network | string | เครือข่ายบล็อกเชน |
amount | decimal | จำนวนเงินที่ถอน (ใน currency) |
merchant_amount | decimal | จำนวนที่หักจากยอดคงเหลือผู้ค้า |
network_amount | decimal | จำนวนที่ส่งจริงบนเชน |
amount_usd | decimal | มูลค่า USD ณ เวลาของการถอน |
to_address | string | ที่อยู่บล็อกเชนของผู้รับ |
memo | string | null | Memo / destination tag ถ้าใช้ |
txid | string | null | hash ของธุรกรรมบล็อกเชน ตั้งค่าเมื่อ completed |
block_number | integer | null | ความสูงของบล็อกของธุรกรรมบนเชน |
error_type | string | null | สาเหตุเมื่อ status = failed (เช่น aml_risk ดู References) |
created_at | string (ISO 8601) | เวลาที่สร้างการถอน |
updated_at | string (ISO 8601) | เวลาที่สถานะเปลี่ยนล่าสุด |
from_currency | string | ยอดคงเหลือต้นทางที่ถูกตัดเมื่อใช้การแปลงอัตโนมัติ (เช่น USDT สำหรับการถอนเป็น BTC) |
debited_amount | decimal | จำนวนที่ตัดจากยอดคงเหลือ from_currency |
debited_currency | string | สกุลเงินของยอดที่ตัด |
sign | string (hex) | ลายเซ็น HMAC-SHA256 ของ payload ลงนามด้วย Payout API key |
แนวปฏิบัติที่ดี
- Idempotency — ตรวจสอบว่าการชำระเงินถูกประมวลผลแล้วหรือไม่ (ด้วย
order_idหรือuuid) เสมอ Webhook อาจมาถึงหลายครั้ง - ตอบกลับเร็ว — คืน HTTP 200 ให้เร็วที่สุดเท่าที่จะทำได้ ย้ายงานหนักไปยังคิวเบื้องหลัง
- การลองใหม่ — หากระบบไม่ได้รับ HTTP 200 Webhook จะถูกส่งใหม่หลัง 2 นาที สูงสุด 5 ครั้ง
- การประมวลผลแบบ async — จัดการอีเวนต์ Webhook แบบ asynchronous เพื่อไม่ให้บล็อกการตอบกลับ
- ความปลอดภัย — ตรวจสอบลายเซ็น
signเสมอก่อนเชื่อ payload
Webhook อาจมาไม่เป็นลำดับ อย่าสันนิษฐานว่า Webhook แรกที่คุณได้รับคือสถานะสุดท้าย — ดึงข้อมูลใหม่ผ่าน /v1/payment/info (หรือ /v1/payout/status/{uuid}) เสมอหากต้องการความแน่นอน