php微信h5支付经验之谈
2018-01-24 23:33
309 查看
首先 先去商户平台申请开通 H5支付。
官方是给我们提供了案例的大家可以移步查看--->微信官方体验链接:http://wxpay.wxutil.com/mch/pay/h5.v2.php,请在微信外浏览器打开。
官方提供的流程,大家可以看看是不是你想要的样子,以防止写错 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_3
大概思路:前台传过来参数后台接收比如金额(注意这里的金额,微信传的是分),订单号,客户端ip等。
用到的最重要的接口是 统一下单

$subject = $data['subject']; //商品描述
$total_amount = $data['total_amount']*100; //金额
$additional = $data['additional']; ////附加数据
$order_id = $data['order_id']; ////订单号
$nonce_str=MD5($order_id);//随机字符串
$spbill_create_ip = $data['spbill_create_ip']; //终端ip
//以上参数接收不必纠结,按照正常接收就行,相信大家都看得懂
//$spbill_create_ip = '118.144.37.98'; //终端ip测试
$trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
$notify_url = 'http://www.xxx.com'; //回调地址
$scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.xxx.com","wap_name":"测试支付"}}'; //场景信息
//对参数按照key=value的格式,并按照参数名ASCII字典序排序生成字符串
$signA = "appid=$appid&body=$subject&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$order_id
&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_amount&trade_type=$trade_type";
$strSignTmp = $signA."&key=$key"; //拼接字符串
$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
$post_data = "<xml>
<appid>$appid</appid>
<body>$subject</body>
<mch_id>$mch_id</mch_id>
<nonce_str>$nonce_str</nonce_str>
<notify_url>$notify_url</notify_url>
<out_trade_no>$order_id</out_trade_no>
<scene_info>$scene_info</scene_info>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<total_fee>$total_amount</total_fee>
<trade_type>$trade_type</trade_type>
<sign>$sign</sign>
</xml>";//拼接成XML 格式
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
$dataxml = $this->http_post($url,$post_data); //后台POST微信传参地址 同时取得微信返回的参数,http_post方法请看下文
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
if($objectxml['return_code'] == 'SUCCESS') {
if($objectxml['result_code'] == 'SUCCESS'){//如果这两个都为此状态则返回mweb_url,详情看‘统一下单’接口文档
return $objectxml['mweb_url']; //mweb_url是微信返回的支付连接要把这个连接分配到前台
}
if($objectxml['result_code'] == 'FAIL'){
return $err_code_des = $objectxml['err_code_des'];
}}
微信支付接口签名校验工具:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=20_1
大家可以使用以上工具,检测您的签名是不是正确。
function http_post($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
以上就是微信H5支付,原理类似,看文档即可,不懂可以联系我QQ:570803231为你指导
官方是给我们提供了案例的大家可以移步查看--->微信官方体验链接:http://wxpay.wxutil.com/mch/pay/h5.v2.php,请在微信外浏览器打开。
官方提供的流程,大家可以看看是不是你想要的样子,以防止写错 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_3
大概思路:前台传过来参数后台接收比如金额(注意这里的金额,微信传的是分),订单号,客户端ip等。
用到的最重要的接口是 统一下单

$subject = $data['subject']; //商品描述
$total_amount = $data['total_amount']*100; //金额
$additional = $data['additional']; ////附加数据
$order_id = $data['order_id']; ////订单号
$nonce_str=MD5($order_id);//随机字符串
$spbill_create_ip = $data['spbill_create_ip']; //终端ip
//以上参数接收不必纠结,按照正常接收就行,相信大家都看得懂
//$spbill_create_ip = '118.144.37.98'; //终端ip测试
$trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
$notify_url = 'http://www.xxx.com'; //回调地址
$scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.xxx.com","wap_name":"测试支付"}}'; //场景信息
//对参数按照key=value的格式,并按照参数名ASCII字典序排序生成字符串
$signA = "appid=$appid&body=$subject&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$order_id
&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_amount&trade_type=$trade_type";
$strSignTmp = $signA."&key=$key"; //拼接字符串
$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
$post_data = "<xml>
<appid>$appid</appid>
<body>$subject</body>
<mch_id>$mch_id</mch_id>
<nonce_str>$nonce_str</nonce_str>
<notify_url>$notify_url</notify_url>
<out_trade_no>$order_id</out_trade_no>
<scene_info>$scene_info</scene_info>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<total_fee>$total_amount</total_fee>
<trade_type>$trade_type</trade_type>
<sign>$sign</sign>
</xml>";//拼接成XML 格式
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
$dataxml = $this->http_post($url,$post_data); //后台POST微信传参地址 同时取得微信返回的参数,http_post方法请看下文
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
if($objectxml['return_code'] == 'SUCCESS') {
if($objectxml['result_code'] == 'SUCCESS'){//如果这两个都为此状态则返回mweb_url,详情看‘统一下单’接口文档
return $objectxml['mweb_url']; //mweb_url是微信返回的支付连接要把这个连接分配到前台
}
if($objectxml['result_code'] == 'FAIL'){
return $err_code_des = $objectxml['err_code_des'];
}}
微信支付接口签名校验工具:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=20_1
大家可以使用以上工具,检测您的签名是不是正确。
function http_post($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
以上就是微信H5支付,原理类似,看文档即可,不懂可以联系我QQ:570803231为你指导
相关文章推荐
- php微信h5支付demo
- PHP微信H5支付开发实例
- php微信H5支付
- 微信H5支付完整版含PHP回调页面.代码精简2018年2月
- 微信H5支付:网络环境未能通过安全验证,请稍后再试。解决办法(PHP版)
- Android通过外部浏览器调用微信H5支付,Android+PHP详解
- Android通过外部浏览器调用微信H5支付,Android+PHP详解
- php实现微信H5支付开发记录(非微信浏览器)
- 微信H5支付开发指南
- 微信h5支付接口开发,出现错误‘商家参数格式有误,请联系商家解决’,访问无法mweb_url
- 分答项目_知识点:微信h5支付1
- [置顶] 实现APP支付QQ,微信,支付宝三方的后端服务------php实现QQAPP支付
- 微信扫码支付 php代码
- 微信小程序支付PHP
- asp.net mvc实现微信外H5支付方法
- 微信H5支付: APP应用内部h5调起微信支付
- 微信js支付 php
- 转载【微信支付】jsapi支付之传参问题(使用微信官方SDK之PHP版本) V3之WxpayPubHelper 亲测有效,V3WxpayAPI_php_v3.zip版未测试,理论上也是一样的。
- PHP 接入微信扫码支付总结(总结篇)
- php版微信js-sdk支付接口类用法示例