收藏
回答

微信支付v2,签名错误,请检查后再试?

原有代码 使用 Yansongda\Pay\Pay 接入小程序,这次不行报错?都能用,不知道为啥现在就不行了!!!

<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名错误,请检查后再试]]></return_msg></xml>

于是我手动码curl也不行,key我v2v3都设置一样的,求大神指教,下面是代码:

    public function paytst()
    {
        $config = [
            'app_id' => "wxa***ca7",
            'appid' => "wxa***ca7",
            'cert_client' => "",
            'cert_key' => "",
            'http' => [
                'timeout' => 5,
                'connect_timeout' => 5,
            ],
            'key' => "bId***P5e",
            'log' => [
                'file' => "./logs/wechat.log",
                'level' => "debug",
                'type' => "single",
                'max_file' => 30,
            ],
            'mch_id' => "1647531410",
            'miniapp_id' => "wxa***ca7",
            'notify_url' => "https://***/notify/wechat",
        ];
        $order = [
            'out_trade_no' => 'kp' . time(),
            'total_fee' => '1',
            'body' => '蓝牙音乐手表',
            'openid' => 'oGg***ORU',
        ];
        $appid = $config['appid'];
        $body = $order['body'];
        $mch_id = $config['mch_id'];
        $nonce_str = (string)mt_rand(10000, 99999) . time(); //随机字符串 
        $notify_url = $config['notify_url'];
        $openid = $order['openid'];
        $out_trade_no = $order['out_trade_no'];
        $spbill_create_ip = '*43.2*4.15*.*37'; //服务器终端的ip $_SERVER['SERVER_ADDR']
        $total_fee = intval($order['total_fee']); //总金额 最低为一分钱 必须是整数 
        $trade_type = 'JSAPI'; //交易类型 默认JSAPI
        $signA = compact('appid', 'body', 'mch_id', 'nonce_str', 'notify_url', 'openid', 'out_trade_no', 'spbill_create_ip', 'total_fee', 'trade_type');
        ksort($signA); //签名步骤一:按字典序排序数组参数
        $signTA = []; //参数进行拼接key=value&k=v
        foreach ($signA as $key => $val) $signTA[] = $key . '=' . $val;
        $sign = implode("&", $signTA);
        $sign .= "&key=" . $config['key']; //签名步骤二:在string后加入KEY
        $sign = md5($sign); //签名步骤三:MD5加密
        $sign = strtoupper($sign); //签名步骤四:所有字符转为大写
        $xml = "<xml><appid>$appid</appid><body>$body</body><mch_id>$mch_id</mch_id><nonce_str>$nonce_str</nonce_str><notify_url>$notify_url</notify_url><openid>$openid</openid><out_trade_no>$out_trade_no</out_trade_no><spbill_create_ip>$spbill_create_ip</spbill_create_ip><total_fee>$total_fee</total_fee><trade_type>$trade_type</trade_type><sign>$sign</sign></xml>";
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; //统一下单接口prepay_id
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        $parse = xml_parser_create();
        xml_parse_into_struct($parse, $res, $vals, $idx);
        xml_parser_free($parse);
        $arr = [];
        foreach ($idx as $key => $val) {
            if ($key == 'xml' || $key == 'XML') continue;
            $tag = $vals[$val[0]]['tag'];
            $val = $vals[$val[0]]['value'];
            $arr[$tag] = $val;
        }
        return compact('config', 'order', 'sign', 'xml', 'res');
    }
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容