首先在商户平台通开H5支付功能,然后帮后绑定,支付完成之后需要跳转的地址,开通之后就可以开发H5支付;
首先是签名,卧槽,说到这个就想骂人,
官方文档的解说;文科生哪能看得懂什么是集合;
下面就来签名:
<?php //生成随机字符串 function getNonceNum($numLen=16){ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $numLen; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } //获取用户ip地址 function get_client_ip(){ $cip = "unknown"; if($_SERVER[‘REMOTE_ADDR‘]){ $cip = $_SERVER[‘REMOTE_ADDR‘]; }elseif(getenv("REMOTE_ADDR")){ $cip = getenv("REMOTE_ADDR"); } return $cip; } $getNonceNumstring = getNonceNum(); $out_trade_no = date("Ymd").getNonceNum(); $ip = get_client_ip(); $param[‘appid‘] = "############"; $param[‘mch_id‘] = ""############";"; $param[‘nonce_str‘] = $getNonceNumstring; $param[‘body‘] = "###"; $param[‘out_trade_no‘] = $out_trade_no; $param[‘total_fee‘] = 1; $param[‘spbill_create_ip‘] = $ip; $param[‘notify_url‘] = "http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php"; $param[‘trade_type‘] = "MWEB"; $param[‘scene_info‘] = ‘{"h5_info": "h5_info" {"type": "IOS","app_name": "h5支付测试","bundle_id": ""}}‘; //字典排序 ksort($param); $sign_raw = ""; foreach($param as $k => $v){ $sign_raw .= $k."=".$v."&"; } $sign_raw .= "key=############"; //生成签名 $sign = strtoupper(md5($sign_raw));
将上面的###替换成你们的参数;太敏感就不贴出来了
然后就是发送xml数据请求接口;
$xml = <<<EOF <xml> <appid>###</appid> <body>###</body> <mch_id>###</mch_id> <nonce_str>$getNonceNumstring</nonce_str> <notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url> <out_trade_no>$out_trade_no</out_trade_no> <spbill_create_ip>$ip</spbill_create_ip> <total_fee>1</total_fee> <trade_type>MWEB</trade_type> <scene_info>{"h5_info": "h5_info" {"type": "IOS","app_name": "h5支付测试","bundle_id": ""}}</scene_info> <sign>$sign</sign> </xml> EOF; $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //调用接口,返回xml数据包含跳转url; $result = postXmlCurl($xml, $url); //解析xml $xml = simplexml_load_string($result); $bigarr = array(); //循环生成数组 foreach($xml->children() as $child){ $key = $child->getName(); $bigarr["$key"] = "$child";//必须加引号 } $tiaozhuanurl = $bigarr[‘mweb_url‘]."&redirect_url=####"; ?> <a href="<?php echo $tiaozhuanurl;?>"> <h1>h5支付</h1> </a> <?php function postXmlCurl($xml, $url, $useCert = false, $second = 30){ $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch,CURLOPT_URL, $url); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); }else{ curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 } //设置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if($useCert == true){ //设置证书 //使用证书:cert 与 key 分别属于两个.pem文件 curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘PEM‘); curl_setopt($ch,CURLOPT_SSLCERT, "###.pem"); curl_setopt($ch,CURLOPT_SSLKEYTYPE,‘PEM‘); curl_setopt($ch,CURLOPT_SSLKEY, "###.pem"); curl_setopt($ch, CURLOPT_CAINFO, "###.pem‘); } //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); echo "error"; } }
其中要注意的就是xml解析;这个不需要证书;