$fee = 0.01; //举例充值0.01 $appid = '' ; //如果是公众号 就是公众号的appid $body = '名片升级-高级名片' ; $mch_id = '' ; $nonce_str = $this ->nonce_str(); //随机字符串 $notify_url = 'http://192.168.188.103:8888/public/api/notify' ; $openid = 'oLVPv0HrT89G_27JWj7fvQzk3O2I' ; $out_trade_no = $this ->order_number( $openid ); //商户订单号 $spbill_create_ip = '192.168.188.103' ; $total_fee = $fee *100; //因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100 $trade_type = 'JSAPI' ; //交易类型 默认 //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错 $post [ 'appid' ] = $appid ; $post [ 'body' ] = $body ; $post [ 'mch_id' ] = $mch_id ; $post [ 'nonce_str' ] = $nonce_str ; //随机字符串 $post [ 'notify_url' ] = $notify_url ; $post [ 'openid' ] = $openid ; $post [ 'out_trade_no' ] = $out_trade_no ; $post [ 'spbill_create_ip' ] = $spbill_create_ip ; //终端的ip $post [ 'total_fee' ] = $total_fee ; //总金额 最低为一块钱 必须是整数 $post [ 'trade_type' ] = $trade_type ; $sign = $this ->sign( $post ); //签名 $post_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> '; //统一接口prepay_id $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' ; $xml = $this ->http_request( $url , $post_xml ); dd( $xml ); |
private function sign( $data ){ $stringA = '' ; foreach ( $data as $key => $value ){ if (! $value ) continue ; if ( $stringA ) $stringA .= '&' . $key . "=" . $value ; else $stringA = $key . "=" . $value ; } $wx_key = '' ; //申请支付后有给予一个商户账号和密码,登陆后自己设置key $stringSignTemp = $stringA . '&key=' . $wx_key ; return strtoupper (MD5( $stringSignTemp )); } |
function http_request( $url , $data = null, $headers = array ()) { $curl = curl_init(); if ( count ( $headers ) >= 1 ){ curl_setopt( $curl , CURLOPT_HTTPHEADER, $headers ); } curl_setopt( $curl , CURLOPT_URL, $url ); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, FALSE); if (! empty ( $data )){ curl_setopt( $curl , CURLOPT_POST, 1); curl_setopt( $curl , CURLOPT_POSTFIELDS, $data ); } curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); $output = curl_exec( $curl ); curl_close( $curl ); return $output ; } |
你好,请按照以下几点仔细检查:
1) 使用微信的在线签名工具检查签名是否和程序生成的一致
https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=20_1 签名工具用谷歌打开。
选择MD5,XML,然后把请求参数xml放进去,就能校验签名。
2)如果和微信的在线签名工具一致,说明程序没有错误,确定是API密钥错误(被别人改动或者记错了)
在商户平台的账户信息中更改API密钥(账户设置-安全设置-API安全), 15分钟后生效
2.1)统一下单用的是A商户号,也必须是A商户号登陆商户平台设置key才对。
2.2)要注意统一下单请求参数中total_fee参数的类型是int类型。
3) 如果和微信的在线签名工具不一致,说明程序有错误,常见的错误可能是:
3.1) 编码问题,确保所有的都是utf-8的. 如果有中文, 可以先把中文改成英文重新签名,看是否签名错误,如果英文不会错中文才会错,基本肯定是编码问题
3.2:)消息中字段大小写和文档中完全一致
4 )统一下单和调起支付签名类型需要一致。
5)参数的类型和格式,长度限制需要完全符合文档的要求