商户API接口文档

用于网站/系统对接的支付接口,几行代码即可接入

如果您在开发手机APP并需要对接,请查看 APP开发API文档

接口概述

所有接口统一使用 JSON 格式返回,HTTP状态码配合业务code码使用。

接口地址为您的网站域名,例如:https://您的域名.com/api/pay.php

通用返回格式:

{
    "code": 200,       // 200=成功 400=参数错误 401=认证失败 404=不存在 500=服务器错误
    "message": "xxx",  // 提示信息
    "data": {}         // 返回数据(可选)
}

商户API支付接口

请求地址:POST /api/pay.php

参数名必填类型说明
app_idstring应用ID
order_nostring商户订单号
pay_typeint支付方式:1=支付宝,2=微信
amountfloat支付金额(元),如 100.00
signstringMD5签名

签名算法:

// 1. 将所有参数(除sign外)按key排序
// 2. 拼接为 key=value&key=value 格式
// 3. 末尾追加 apikey
// 4. MD5加密
MD5(order_no=xxx&pay_type=1&app_id=xxx&apikey=xxx)

返回示例:

{
    "code": 200,                       // int    业务状态码
    "message": "创建订单成功",           // string 提示信息
    "data": {
        "order_no": "20240101120000",   // string 商户订单号
        "trade_no": "SK20240101120000", // string 平台订单号
        "pay_url": "https://...",       // string 支付页面URL
        "qrcode_url": "https://...",    // string 收款二维码URL
        "amount": "100.00",             // string 订单金额(元)
        "actual_amount": "99.98",       // string 实际收款金额(元)
        "pay_type": 1,                  // int    支付方式:1=支付宝 2=微信
        "pay_type_name": "支付宝",       // string 支付方式名称
        "expired_at": "2024-01-01 12:30:00", // string 订单过期时间
        "status": 0                     // int    订单状态:0=未支付, 1=已支付, 2=已关闭(无法支付)
    }
}

商户API订单查询接口

请求地址:POST/GET /api/query.php

参数名必填类型说明
app_idstring应用ID
order_nostring商户订单号
signstringMD5签名(与支付接口签名方式相同)

返回示例:

{
    "code": 200,                       // int    业务状态码
    "message": "查询成功",              // string 提示信息
    "data": {
        "order_no": "20240101120000",   // string 商户订单号
        "trade_no": "SK20240101120000", // string 平台订单号
        "amount": "100.00",             // string 订单金额(元)
        "actual_amount": "99.98",       // string 实际收款金额(元)
        "pay_type": 1,                  // int    支付方式:1=支付宝 2=微信
        "pay_type_name": "支付宝",       // string 支付方式名称
        "status": 1,                    // int    订单状态:0=未支付, 1=已支付, 2=已关闭(无法支付)
        "status_name": "已支付",         // string 状态名称
        "callback_status": 1,           // int    回调状态:0=未回调 1=已确认
        "callback_status_name": "确认成功", // string 回调状态名称
        "paid_at": "2024-01-01 12:05:00",  // string|null 支付时间,未支付为null
        "created_at": "2024-01-01 12:00:00", // string 创建时间
        "expired_at": "2024-01-01 12:30:00"  // string 过期时间
    }
}

接入方式订单状态查询(轮询)

不提供异步回调。请在创建订单后定时调用订单查询接口轮询支付状态。

推荐轮询策略:

阶段间隔说明
创建订单后每 2 秒等待用户扫码支付
5 分钟后停止订单过期,无需再查
// PHP 轮询示例(每2秒查询一次,最多等5分钟)
function pollOrderStatus($tradeNo, $apiKey, $appId, $maxWait = 300) {
    $url = 'https://yourdomain.com/api/check_order.php';
    $start = time();
    
    while (time() - $start < $maxWait) {
        $params = [
            'app_id' => $appId,
            'trade_no' => $tradeNo
        ];
        ksort($params);
        $signStr = '';
        foreach ($params as $k => $v) {
            $signStr .= $k . '=' . $v . '&';
        }
        $signStr .= 'apikey=' . $apiKey;
        $params['sign'] = md5($signStr);
        
        $url2 = $url . '?' . http_build_query($params);
        $resp = json_decode(file_get_contents($url2), true);
        
        if ($resp['code'] == 200 && $resp['data']['status'] == 1) {
            return '支付成功';
        }
        sleep(2);
    }
    return '支付超时';
}

错误码说明

错误码说明
200请求成功
400请求参数错误(缺少参数、金额无效等)
401认证失败(签名错误、app_id无效等)
403账户被禁用
404订单不存在
405请求方式错误(需使用POST)
500服务器内部错误