官方文档
https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_2&index=2 参考文档
*不需要证书
参数说明
appid: "wx********f8"
bank_type: "OTHERS" //付款银行
cash_fee: "1" //现金支付金额
fee_type: "CNY" //现金支付货币类型
is_subscribe: "Y" //是否关注公众账号
mch_id: "163*****2" //商户号
nonce_str: "mxov******6m6" //随机字符串
openid: "oyC7G********5I" //用户标识
out_trade_no: "NW*****38" //商户订单号
result_code: "SUCCESS" //返回状态码
return_code: "SUCCESS" //返回状态码
sign: "5BD61D*******DCFF02" //签名
time_end: "20221219144714" //支付完成时间
total_fee: "1" //订单总金额,单位为分
trade_type: "MWEB" //交易类型
transaction_id: "42000*********1694" //微信支付订单号
支付demo
- wxh5.html--前端跳转提交支付
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>微信支付</title>
<link rel="stylesheet" href="">
</head>
<body>
<a href="wxh5.php" title="">微信H5--统一下单支付</a>
</body>
</html>
- wxh5.php后台跳转支付
<?php
function dump($arr){
echo "<pre>";
var_dump($arr);
echo "<pre>";die;
}
//随机字符串
function createNoncestr($length = 32){
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
//微信支付签名生成函数
function wxqmzxy($arr,$key){
ksort($arr);
$str='';
foreach($arr as $k=>$v){
$str.=$k.'='.$v.'&';
}
$str.='key='.$key;
return strtoupper(md5($str));
}
//给微信发送请求函数
function postXmlCurl($data, $url, $second = 30)
{
$curl = curl_init();
//设置url
curl_setopt($curl, CURLOPT_URL,$url);
//设置发送方式:post
curl_setopt($curl, CURLOPT_POST, true);
//试试手气新增,增加之后 curl 不报 60# 错误,可以请求到微信的响应
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证 SSL 证书
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//不验证 SSL 证书域名
//设置发送数据
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$return_xml = curl_exec($curl);
//关闭cURL资源,并且释放系统资源
curl_close($curl);
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
//先把xml转换为simplexml对象,再把simplexml对象转换成 json,再将 json 转换成数组。
$value_array = json_decode(json_encode(simplexml_load_string($return_xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
// dump($value_array);die;
if ($return_xml) {
return $value_array;
} else {
$error = curl_errno($curl);
return "curl error, error code ".$error;
//throw new WxPayException("curl出错,错误码:$error");
}
}
function xml_to_array($xml){
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
// 微信H5统一订单支付
// --微信支付需要跳转支付,会进行refer判断,所以不能直接提交支付订单
function wxpay(){
// 模拟数据
$_POST = [
'name' => '张三',
'sex' => 1,
'hometel' => '135****5658',
'url' => 'http://****.com/'
];
//接收表单参数
$name = $_POST['name'];
$sex = $_POST['sex'];
$tel = $_POST['hometel'];
$url = $_POST['url']; //这个是刚刚那个html页面的网址
//微信h5支付
$param['appid'] = "wx7********f8";//微信支付的appid
$param['body'] = "测试";//微信支付的商品描述
$param['mch_id'] = "163****";//微信支付的mch_id
$param['nonce_str'] = createNoncestr(); //随机字符串
$param['notify_url'] = "http://******.com/notify.php"; //回调地址 这个地址需要微信支付后台配置白名单 上限貌似是五个
$param['out_trade_no'] = "N20221122".date("YmdHis");//商户订单号
$param['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];//终端ip
$param['total_fee'] = 1; //价格 100 等于 1元钱 单位分
$param['trade_type'] = "MWEB"; //交易类型 h5支付的交易类型必须为MWEB
$param['scene_info'] = '{"h5_info": {"type":"Wap","app_url": "http://******.com","app_name": "公司"}}'; //场景信息 https://xxxx.com 单位官网
//按照微信规则生成签名
$key = "z**********************"; //微信商户API密钥
$sign = wxqmzxy($param,$key); //key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
// echo $sign;die;
//拼接xml数据
$send = "<xml>
<appid>{$param['appid']}</appid>
<attach>{$param['attach']}</attach>
<body>{$param['body']}</body>
<mch_id>{$param['mch_id']}</mch_id>
<nonce_str>{$param['nonce_str']}</nonce_str>
<notify_url>{$param['notify_url']}</notify_url>
<out_trade_no>{$param['out_trade_no']}</out_trade_no>
<spbill_create_ip>{$param['spbill_create_ip']}</spbill_create_ip>
<total_fee>{$param['total_fee']}</total_fee>
<trade_type>{$param['trade_type']}</trade_type>
<scene_info>{$param['scene_info']}</scene_info>
<sign>{$sign}</sign>
</xml>";
// echo "<pre>";
// var_dump($send);
// echo "<pre>";die;
//向微信发送请求
// $result = postXmlCurl($send,"https://api.mch.weixin.qq.com/v3/pay/transactions/h5");
$result = postXmlCurl($send,"https://api.mch.weixin.qq.com/pay/unifiedorder");
$return_arr =$result;
//设置支付成功跳转地址 数据写入数据库的后台地址,域名和中间页要同一个,不然会报错
$urlstr = "http://******.com/wxh52get.php?name=".$name."&hometel=".$tel."&sex=".$sex."&url=".$url;
$return_url = urlencode($urlstr); //支付完成后你要跳转的地址 getenv("HTTP_REFERER") 是原封不动的跳回去
// echo $return_arr['mweb_url']."&redirect_url={$return_url}";die;
//跳转此链接拉起微信支付
header("location:".$return_arr['mweb_url']."&redirect_url={$return_url}"); //拉起微信支付链接(并且附上$return_url)
exit;
}
// 订单查询
function getTrade(){
//我这里为了方便,这个查单的流程要单独写函数哈
//补充,如果用户取消支付,需要去查询当前生成的订单有没有付款,微信这个接口需要支付后2-3s再进行查询验证,需要写一个弹窗或者什么中间页来引导用户去触发这个
//构造请求微信接口的参数 查询订单支付状态
$param = [
'appid' => "wx7********f8",// APP ID
'mch_id' => "1********2",// 商户号
'out_trade_no' => "N20221122****************1", //订单号 就是上面的单号
'nonce_str' => createNoncestr(),
'sign_type' => 'MD5',
];
//按照微信规则生成签名
$key = "z*********************"; //微信商户API密钥
$sign = wxqmzxy($param,$key); //key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
//拼接xml数据
$send = "<xml>
<appid>{$param['appid']}</appid>
<mch_id>{$param['mch_id']}</mch_id>
<nonce_str>{$param['nonce_str']}</nonce_str>
<out_trade_no>{$param['out_trade_no']}</out_trade_no>
<sign_type>{$param['sign_type']}</sign_type>
<sign>{$sign}</sign>
</xml>";
//向微信发送请求
$result = postXmlCurl($send,"https://api.mch.weixin.qq.com/pay/orderquery");
dump($result);
// array(21) {
// ["return_code"]=>
// string(7) "SUCCESS"
// ["return_msg"]=>
// string(2) "OK"
// ["result_code"]=>
// string(7) "SUCCESS"
// ["mch_id"]=>
// string(10) "1********"
// ["appid"]=>
// string(18) "wx7********f8"
// ["openid"]=>
// string(28) "oy******************"
// ["is_subscribe"]=>
// string(1) "Y"
// ["trade_type"]=>
// string(4) "MWEB"
// ["trade_state"]=>
// string(7) "SUCCESS"
// ["bank_type"]=>
// string(10) "CMB_CREDIT"
// ["total_fee"]=>
// string(3) "100"
// ["fee_type"]=>
// string(3) "CNY"
// ["cash_fee"]=>
// string(3) "100"
// ["cash_fee_type"]=>
// string(3) "CNY"
// ["transaction_id"]=>
// string(28) "4&****************4"
// ["out_trade_no"]=>
// string(23) "N20221122****************1"
// ["attach"]=>
// array(0) {
// }
// ["time_end"]=>
// string(14) "20221202102458"
// ["trade_state_desc"]=>
// string(12) "支付成功"
// ["nonce_str"]=>
// string(16) "GXljlDNSaKIJw9om"
// ["sign"]=>
// string(32) "5F**********************5688F1"
// }
if($result['trade_state']=="SUCCESS"){
echo "订单支付成功";
}else{
echo "订单支付失败";
}
//$jsonArray['trade_state'] //支付状态
//官方 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_2&index=2
}
wxpay();
//getTrade();
?>
回调接口数据
参考链接
https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=8
扫描二维码关注公众号,回复: 15505990 查看本文章注意:
V2的通知是xml格式 xml的数据不会保存在body里,而是保存在raw-body里,通过后台语言处理raw-body获取回调数据
- Notify.php回调接口
<?php
header('Content-Type: text/html; charset=utf-8');
// file_put_contents("./log/13.log", time()."\r\n", FILE_APPEND)
// $data = $_SERVER;
// $data = $_REQUEST;
// $logData = json_encode($data);
// file_put_contents("./log/13.log", $logData."\r\n", FILE_APPEND);
//xml的数据不会保存在body里,而是保存在raw-body里,通过后台语言处理raw-body获取回调数据
//无法通过获取body($_SERVER|$_REQUEST)来获取回调数据
$xml = file_get_contents("php://input");
libxml_disable_entity_loader(true); //禁用加载外部实体的功能
$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
file_put_contents("./log/13.log", json_encode($data)."\r\n", FILE_APPEND);
?>
- 回调参数
appid: "wx**********"
bank_type: "OTHERS" //付款银行
cash_fee: "1" //现金支付金额
fee_type: "CNY" //现金支付货币类型
is_subscribe: "Y" //是否关注公众账号
mch_id: "1**********" //商户号
nonce_str: "mxv*****************" //随机字符串
openid: "o*****************" //用户标识
out_trade_no: "NW*************" //商户订单号
result_code: "SUCCESS" //返回状态码
return_code: "SUCCESS" //返回状态码
sign: "5B**********************CFF02" //签名
time_end: "20221219144714" //支付完成时间
total_fee: "1" //订单总金额,单位为分
trade_type: "MWEB" //交易类型
transaction_id: "420**********************4" //微信支付订单号