You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP调用PhonePe API查支付状态时遇‘Api Mapping Not Found’错误求助

解决PhonePe交易状态查询返回"Bad Request - Api Mapping Not Found"的问题

可能的原因及修复方案

1. API端点路径与请求方法错误

PhonePe的交易状态查询API要求使用POST请求,且正确的端点路径并非将商户ID和交易ID直接拼接在URL中,而是通过请求体传递参数。错误的请求方法或路径会直接导致映射找不到的错误。

2. BaseUrl配置错误

确保$baseUrl使用对应环境的正确地址:

  • 沙箱环境:https://api-preprod.phonepe.com/apis/pg-sandbox
  • 生产环境:https://api.phonepe.com/apis/hermes

3. 签名计算规则错误

签名需要基于请求的完整内容(包括请求体)生成,而非仅拼接路径和saltkey。正确的签名生成逻辑需要包含请求体的JSON字符串、端点路径和saltkey。

修正后的代码

function checkstatus() {
    global $MID, $txnid, $baseUrl, $saltkey, $keyindex;

    $merchantId = $MID;
    $transactionId = $txnid;

    // 正确的API端点路径
    $endpoint = '/v1/transaction/status';
    $url = $baseUrl . $endpoint;

    // 构造请求体
    $requestBody = json_encode([
        'merchantId' => $merchantId,
        'transactionId' => $transactionId
    ]);

    // 生成签名:请求体JSON + 端点路径 + saltkey
    $str_forSha256 = $requestBody . $endpoint . $saltkey;
    $sha_value = hash('sha256', $str_forSha256);
    $x_verify = $sha_value . '###' . $keyindex;

    $headers = array(
        "Content-Type: application/json",
        "X-VERIFY: $x_verify",
        "X-MERCHANT-ID: $merchantId" // 部分环境需要添加该头部
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true); // 设置为POST请求
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); // 传递请求体

    // 开启调试可查看详细请求信息(可选)
    // curl_setopt($ch, CURLOPT_VERBOSE, true);

    $res = curl_exec($ch);

    // 检查curl错误
    if(curl_errno($ch)){
        $res = 'Curl Error: ' . curl_error($ch);
    }

    curl_close($ch);

    return $res;
}

echo checkstatus();

额外检查项

  • 确认$saltkey和$keyindex是PhonePe提供的有效密钥和索引,且与当前环境(沙箱/生产)匹配。
  • 确保服务器可以访问PhonePe的API域名,没有被防火墙或安全组拦截。

内容的提问来源于stack exchange,提问作者shivchandra kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:17:25