接入教程
1. 停止使用支付Webhooks端点
2. 访问会计Webhooks文档页面
3. 配置新的Webhooks接收地址
4. 验证新Webhooks的通知格式
5. 更新系统处理逻辑
6. 全面切换至会计Webhooks
Python Webhook 验证示例
import json
import hmac
import hashlib
from flask import Flask, request
app = Flask(__name__)
WEBHOOK_SECRET = 'YOUR_WEBHOOK_SECRET'
@app.route('/webhook', methods=['POST'])
def handle_webhook():
payload = request.get_data()
signature = request.headers.get('X-Signature')
# 验证签名
expected_signature = hmac.new(
WEBHOOK_SECRET.encode(),
payload,
hashlib.sha256
).hexdigest()
if not hmac.compare_digest(signature, expected_signature):
return 'Invalid signature', 401
data = json.loads(payload)
# 处理webhook数据
print(f"Received webhook: {data}")
return 'OK', 200
if __name__ == '__main__':
app.run(port=3000)
PHP Webhook 处理示例
<?php
$webhookSecret = 'YOUR_WEBHOOK_SECRET';
// 获取原始请求体
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';
// 验证签名
$expectedSignature = hash_hmac('sha256', $payload, $webhookSecret);
if (!hash_equals($expectedSignature, $signature)) {
http_response_code(401);
echo 'Invalid signature';
exit;
}
// 解析JSON数据
$data = json_decode($payload, true);
// 处理webhook事件
if (isset($data['eventType'])) {
switch ($data['eventType']) {
case 'payment.created':
// 处理支付创建事件
break;
case 'payment.updated':
// 处理支付更新事件
break;
default:
// 处理其他事件
}
}
http_response_code(200);
header('Content-Type: application/json');
echo json_encode(['status' => 'success']);
Node.js Webhook 接收示例
const express = require('express');
const crypto = require('crypto');
const app = express();
const WEBHOOK_SECRET = 'YOUR_WEBHOOK_SECRET';
app.use(express.raw({ type: 'application/json' }));
app.post('/webhook', (req, res) => {
const signature = req.headers['x-signature'];
const payload = req.body;
// 验证签名
const expectedSignature = crypto
.createHmac('sha256', WEBHOOK_SECRET)
.update(payload)
.digest('hex');
if (signature !== expectedSignature) {
return res.status(401).send('Invalid signature');
}
const data = JSON.parse(payload.toString());
// 处理webhook事件
console.log('Received webhook:', data);
// 根据事件类型处理
if (data.eventType) {
switch (data.eventType) {
case 'payment.created':
// 处理支付创建
break;
case 'transfer.completed':
// 处理转账完成
break;
}
}
res.status(200).json({ received: true });
});
app.listen(3000, () => {
console.log('Webhook server listening on port 3000');
});
常见问题
这个API为什么被标记为已弃用?
支付Webhooks API已被弃用,建议使用新的会计Webhooks API替代。弃用原因包括功能整合、性能优化和安全性增强。原有功能已迁移到新API中。
如何迁移到新的会计Webhooks API?
迁移步骤包括:1. 注册新的会计Webhooks API密钥;2. 更新webhook端点URL;3. 调整事件处理逻辑以适应新的数据格式;4. 测试新webhook的签名验证机制。详细迁移指南请参考官方文档。
已弃用的API还能继续使用多久?
根据官方公告,已弃用的支付Webhooks API将在未来12个月内继续运行,但不再接收功能更新和安全补丁。建议尽快迁移到新的会计Webhooks API以确保服务连续性和安全性。
Aitishiku.com