收藏
回答

Native下单API 返回签名错误?

public function index()
    {
        list($msec, $sec) = explode(' ', microtime());

        $method = 'POST';
        $url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/native';
        $body = [
            'appid' => $this->appid,
            'mchid' => $this->mchid,
            'description' => '关系你咨询服务',
            'out_trade_no' => $sec,
            'notify_url' => 'www.baidudd.com',
            'amount' => [
                'total' => 1,
                'currency' => 'CNY',
            ],
        ];

        $option = [
            'headers' => [
                'Accept'     => 'application/json',
                'Authorization' => $this->createAuthorization($method, $url, json_encode($body)),
            ],
            'json' => $body,
        ];
        var_dump($option);

        $ret = $this->httpclient->request($method, $url, $option);
//        $ret = json_decode($ret, true);

        return $ret;
    }

    protected function createAuthorization($method, $url, $body)
    {
        $random = uniqid();
        $timestamp= time();
        $authorization =  $this->encrypType . ' '
            . 'mchid="' . $this->mchid . '",'
            . 'nonce_str="' . $random . '",'
            . 'timestamp="' . $timestamp . '",'
            . 'serial_no="' . $this->serialNo . '",'
            . 'signature="' . $this->sign([$method, $url, $timestamp, $random, $body]) .'"';
        return $authorization;
    }

    protected function sign($array)
    {
        $priKey =openssl_pkey_get_private($this->priKey);
        if (!$priKey) {
            return false;
        }
        $str = '';
        foreach ($array as $item) {
            $str = $str . $item . "\n";
        }
        openssl_sign($str, $encrypted, $priKey, 'sha256WithRSAEncryption');
        $encrypted = base64_encode($encrypted);
        var_dump($encrypted);
        return $encrypted;
    }

 以上是我的代码有大佬可以看下怎么就签名错误了嘛?照着是列写的啊

 签名Authorization:WECHATPAY2-SHA256-RSA2048 mchid="1615086712",nonce_str="617e624e2be98",timestamp="1635672654",serial_no="1E868F1B8E4E65A1620C15B82703590F52271659",signature="pS5YpotqcgU+ccwqO85biucB9rpKmx+l4Zg88KPfvQ7mubnpt4VwqLy8+fbg4QO8/h5W0uQ6DqSlGJIIE2U2hEg4bjC4+Dc9ShycKX72C4D6FDl+xi9/EoZqg4DLMPIYitK2cw/8vlMe9Crr2By0kzrY4u9eAhxqWO96NKAX0ffwDk7DWTReJ6UvxGWYQag53wkhf98eVn8NZubiBCqS1e2j+6ERoojzMgr4OZFNr6gVh/8MbNjjVw7TbSTNjJO8wkDjaZ8Ia/lScF7EThckvGAHFKUjvfolCANunXnFKGu86TmuKSJfN++uOz06J6HPk1U//M4gjZ3XQKBie2Mugw=="
报错
{"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_information":{"method":"GET","sign_message_length":47,"truncated_sign_message":"GET\n/v3/certificates\n1635672654\n617e624e2be98\n\n","url":"/v3/certificates"}},"message":"错误的签名,验签失败"}

最后一次编辑于  2021-10-31
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容