🔗 基础信息
API地址 | http://您的域名/ |
请求方式 | POST |
返回格式 | JSON |
字符编码 | UTF-8 |
💡 重要:所有API请求都需要包含通讯密钥(key参数)进行验证
💰 创建订单
POST
/createOrder
创建新的支付订单
请求参数
参数 | 类型 | 必填 | 说明 | 示例 |
payId | string | 是 | 商户订单号 | ORDER_001 |
type | int | 是 | 1=微信 2=支付宝 | 1 |
price | float | 是 | 支付金额(元) | 10.00 |
key | string | 是 | 通讯密钥 | your_key |
notifyUrl | string | 否 | 异步通知地址 | http://... |
returnUrl | string | 否 | 同步跳转地址 | http://... |
PHP示例
$data = [
'payId' => 'ORDER_' . time(),
'type' => 1, // 1=微信 2=支付宝
'price' => 10.00,
'key' => 'your_communication_key',
'notifyUrl' => 'http://your-site.com/notify.php'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://your-domain.com/createOrder');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['code'] == 1) {
echo '支付链接:' . $result['data']['payUrl'];
} else {
echo '错误:' . $result['msg'];
}
返回示例
{
"code": 1,
"msg": "成功",
"data": {
"payId": "ORDER_001",
"orderId": "20241201123456",
"payType": 1,
"price": 10.00,
"reallyPrice": 10.01,
"payUrl": "wxp://f2f0...",
"state": 0,
"timeOut": 300
}
}
🔍 查询订单
请求参数
参数 | 类型 | 必填 | 说明 | 示例 |
payId | string | 是 | 商户订单号 | ORDER_001 |
key | string | 是 | 通讯密钥 | your_key |
JavaScript示例
async function checkOrder(payId) {
const data = new FormData();
data.append('payId', payId);
data.append('key', 'your_communication_key');
const response = await fetch('/getOrder', {
method: 'POST',
body: data
});
const result = await response.json();
if (result.code === 1) {
if (result.data.state === 1) {
alert('支付成功!');
} else {
console.log('等待支付...');
}
}
}
// 每5秒查询一次订单状态
setInterval(() => checkOrder('ORDER_001'), 5000);
订单状态说明
🔔 异步通知
支付成功后,系统会向您的 notifyUrl
发送POST通知:
通知参数
{
"payId": "ORDER_001",
"orderId": "20241201123456",
"price": 10.00,
"reallyPrice": 10.01,
"param": "custom_data",
"state": 1,
"payDate": 1701234567
}
PHP处理示例
// notify.php
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if ($data && $data['state'] == 1) {
// 验证订单
$payId = $data['payId'];
$price = $data['price'];
// 处理业务逻辑
// 例如:更新订单状态、发货等
// 返回成功标识
echo 'success';
} else {
echo 'fail';
}
⚠️ 重要:必须返回字符串 "success" 表示处理成功,否则系统会重复发送通知
🚀 完整示例
一个完整的支付流程示例:
<!-- 支付页面 -->
<form id="payForm">
<input type="text" id="amount" placeholder="支付金额" value="10.00">
<button type="button" onclick="createOrder(1)">微信支付</button>
<button type="button" onclick="createOrder(2)">支付宝支付</button>
</form>
<div id="qrcode" style="display:none;">
<img id="qrImg" src="" alt="支付二维码">
<p>请使用手机扫码支付</p>
</div>
<script>
let currentOrderId = '';
async function createOrder(type) {
const amount = document.getElementById('amount').value;
const payId = 'ORDER_' + Date.now();
const data = new FormData();
data.append('payId', payId);
data.append('type', type);
data.append('price', amount);
data.append('key', 'your_communication_key');
data.append('notifyUrl', 'http://your-site.com/notify.php');
const response = await fetch('/createOrder', {
method: 'POST',
body: data
});
const result = await response.json();
if (result.code === 1) {
currentOrderId = payId;
showQRCode(result.data.payUrl);
startPolling();
} else {
alert('创建订单失败:' + result.msg);
}
}
function showQRCode(payUrl) {
// 生成二维码(需要引入二维码库)
document.getElementById('qrImg').src =
'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=' +
encodeURIComponent(payUrl);
document.getElementById('qrcode').style.display = 'block';
}
function startPolling() {
const interval = setInterval(async () => {
const data = new FormData();
data.append('payId', currentOrderId);
data.append('key', 'your_communication_key');
const response = await fetch('/getOrder', {
method: 'POST',
body: data
});
const result = await response.json();
if (result.code === 1 && result.data.state === 1) {
clearInterval(interval);
alert('支付成功!');
window.location.href = '/success.html';
}
}, 3000);
// 5分钟后停止轮询
setTimeout(() => clearInterval(interval), 300000);
}
</script>