事前准备:在微信商户平台申请微信H5支付功能,审核需要3-5天时间;H5支付开发文档
一、获取支付网址
<?php
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
//生成商户订单号
$trade = date('YmdHis') . substr(microtime(), 2, 6) . rand(1000, 9999);
//应用APPID
$appid = "XXXXXX";
//微信支付商户号
$mch_id = "XXXXXX";
//微信商户API密钥
$key = "XXXXXX";
//随机字符串
$nonce_str = createNoncestr();
//内容
$body = "支付测试";
//商户内部订单号
$out_trade_no = $trade;
//付款金额(单位:分)
$total_fee = 1;
//IP
$spbill_create_ip = get_client_ip();
//回调地址
$notify_url = "http://www.example.com/wxpay/notify.php";
//交易类型 具体看API 里面有详细介绍
$trade_type = 'MWEB';
//场景信息
$scene_info = '{"h5_info": {"type":"Wap","wap_url": "http://www.example.com","wap_name": "微信支付测试"}}';
//sign签名
$signA = "appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
//拼接字符串
$signB = $signA . "&key=$key";
// MD5 后转换成大写
$sign = strtoupper(md5($signB));
//拼接成XML 格式
$postData = "<xml>
<appid>$appid</appid>
<mch_id>$mch_id</mch_id>
<body>$body</body>
<out_trade_no>$out_trade_no</out_trade_no>
<total_fee>$total_fee</total_fee>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<notify_url>$notify_url</notify_url>
<trade_type>$trade_type</trade_type>
<scene_info>$scene_info</scene_info>
<nonce_str>$nonce_str</nonce_str>
<sign>$sign</sign>
</xml>";
//微信传参地址
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//后台POST微信传参地址 同时取得微信返回的参数
$dataxml = postCurl($postData , $url);
//将微信返回的XML 转换成数组
$content = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);
$status = $content['return_code'];
if ($status == 'SUCCESS') {
//支付网址
$mweb_url = $content['mweb_url'];
}
//获取客户端IP函数
function get_client_ip()
{
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_REAL_IP')) {
$ip = getenv('HTTP_X_REAL_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
$ips = explode(',', $ip);
$ip = $ips[0];
} elseif (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = 'unknown';
}
return $ip;
}
//CURL post请求数据
function postCurl($xml, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//生成随机数
function createNonceStr()
{
$string = "abcdefghijkl0123456789mnopqrstuvwxyz";
$str = str_shuffle($string);
$nonce = substr($str, 0, 32);
return $nonce;
}
二、回调页面(notify.php)
<?php
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
//接收微信参数
$postXml = file_get_contents("php://input");;
if (empty($postXml)) {
return false;
}
$data = xmlToArray($postXml);
//将xml格式转换成数组
function xmlToArray($xml)
{
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$encode = json_encode($xmlstring);
$val = json_decode($encode, true);
file_put_contents('log.html', $encode . PHP_EOL, FILE_APPEND);
return $val;
}
//返回状态码 SUCCESS/FAIL
$return_code = $data['return_code'];
if ($return_code == 'SUCCESS') {
//支付结果 SUCCESS/FAIL
$result_code = $data['result_code'];
if ($result_code == 'SUCCESS') {
//验证签名
$appid = $data['appid'];
$bank_type = $data['bank_type'];
$cash_fee = $data['cash_fee'];
$fee_type = $data['fee_type'];
$is_subscribe = $data['is_subscribe'];
$mch_id = $data['mch_id'];
$nonce_str = $data['nonce_str'];
$openid = $data['openid'];
//商户订单号
$out_trade_no = $data['out_trade_no'];
//签名
$sign = $data['sign'];
//支付完成时间
$time_end = $data['time_end'];
//订单总金额,单位为分
$total_fee = $data['total_fee'];
$trade_type = $data['trade_type'];
//微信支付订单号
$transaction_id = $data['transaction_id'];
//APPKEY
$key = "XXXXXX";
//生成签名
$signA = "appid=$appid&bank_type=$bank_type&cash_fee=$cash_fee&fee_type=$fee_type&is_subscribe=$is_subscribe&mch_id=$mch_id&nonce_str=$nonce_str&openid=$openid&out_trade_no=$out_trade_no&result_code=$result_code&return_code=$return_code&time_end=$time_end&total_fee=$total_fee&trade_type=$trade_type&transaction_id=$transaction_id";
$singB = $signA . '&key=' . $key;
$singC = md5($singB);
$mySign = strtoupper($singC);
//验证签名
if ($mySign == $sign) {
//签名通过 处理业务逻辑
}
}
}
//返回成功消息
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
三、查询订单 (所用函数同“一”)
<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("PRC");
//商户订单号,商户网站订单系统中唯一订单号,必填
$trade = XXXXXX;
//应用APPID
$appid = "XXXXXX";
//微信支付商户号
$mch_id = "XXXXXX";
//微信商户API密钥
$key = "XXXXXX";
//随机字符串
$nonce_str = createNonceStr();
$signA = "appid=$appid&mch_id=$mch_id&nonce_str=$nonce_str&out_trade_no=$trade";
//拼接字符串
$strSignTmp = $signA . "&key=$key";
// MD5 后转换成大写
$sign = strtoupper(md5($strSignTmp));
//拼接成XML 格式
$xml = "<xml>
<appid>$appid</appid>
<mch_id>$mch_id</mch_id>
<out_trade_no>$trade</out_trade_no>
<nonce_str>$nonce_str</nonce_str>
<sign>$sign</sign>
</xml>";
//微信传参地址
$url = "https://api.mch.weixin.qq.com/pay/orderquery";
//后台POST微信传参地址 同时取得微信返回的参数
$cont = postCurl($url,$xml);
//将微信返回的XML 转换成数组
$result= (array)simplexml_load_string($cont, 'SimpleXMLElement', LIBXML_NOCDATA);
$return_code = $result['return_code'];
$result_code = $result['result_code'];
if ($result_code == 'SUCCESS' && $return_code == 'SUCCESS') {
$trade_state = $result['trade_state'];
if ($trade_state == 'SUCCESS') {
$data['code'] = 200;
$data['msg'] = '支付成功';
} else {
$data['code'] = 201;
$data['msg'] = '支付失败,请重新支付!';
}
} else {
$data['code'] = 202;
$data['msg'] = '支付失败,请重新支付!';
}
echo json_encode($data, JSON_UNESCAPED_UNICODE);