//生成随机字符串
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;
}
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";
}
}
$out_trade_no = date("Ymd").getNonceNum(); //订单号
$appid = ''; //APPID
$mch_id = ''; //商户号
$key = ''; //商户支付密钥
$total_fee = 1; //支付金额
$notify_url = 'http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php'; //异步回调地址
$getNonceNumstring = getNonceNum();
$body = 'H5支付测试';
$ip = get_client_ip();
$trade_type = 'MWEB';
$param['appid'] = $appid;
$param['mch_id'] = $mch_id;
$param['nonce_str'] = $getNonceNumstring;
$param['body'] = $body;
$param['out_trade_no'] = $out_trade_no;
$param['total_fee'] = $total_fee;
$param['spbill_create_ip'] = $ip;
$param['notify_url'] = $notify_url;
$param['trade_type'] = $trade_type;
$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=".$key;
//生成签名
$sign = strtoupper(md5($sign_raw));
$xml = <<<EOF
<xml>
<appid>$appid</appid>
<body>$body</body>
<mch_id>$mch_id</mch_id>
<nonce_str>$getNonceNumstring</nonce_str>
<notify_url>$notify_url</notify_url>
<out_trade_no>$out_trade_no</out_trade_no>
<spbill_create_ip>$ip</spbill_create_ip>
<total_fee>$total_fee</total_fee>
<trade_type>$trade_type</trade_type>
<scene_info>{"h5_info": "h5_info" {"type": "IOS","app_name": "H5支付测试","bundle_id": ""}}</scene_info>
<sign>$sign</sign>
</xml>
EOF;
//调用接口,返回xml数据包含跳转url
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$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>
转载请注明出处。