-
-
- (扫码模式二,微信浏览器内部支付(jsapi模式),手机端浏览器支付 (H5支付或者wap支付)
-
-
-
-
-
- 先进微信https://pay.weixin.qq.com/index.php做参数配置工作
-
-
-
- 产品中心--开发者配置
-
-
-
- A 支付授权目录(支付发起的页面url 必须精确到最后一个目录/下 必须有 / 否则不行)
-
-
-
- http://www.qq.com/abd/sdfs/1.html 就要在后台配置http://www.qq.com/abd/sdfs/
-
-
-
- 如果url是http://www.qq.com/abd/sdfs?1.html 那就把url改成 http://www.qq.com/abd/sdfs/?1.html 否则验证不通过无法调用微信支付
-
-
-
- B 回调地址 就是付款完成了微信会回调你的url告诉你处理结果(我这里没用上,微信死活不回调我,我就js轮询主动去查询微信那边的订单状态做处理)
-
-
-
- C H5支付需要开通 填写顶级域名即可
-
-
-
-
-
- 1.扫码模式二
-
-
-
- //微信支付
-
-
-
- $wxpay = new Wxpay();
-
-
-
- $order_info['body']='画品购买';
-
-
-
- $order_info['order_number']=$order_id;
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $order_info['money']=$money;//画品总价
-
-
-
- }else{
-
-
-
- $order_info['money']='1';
-
-
-
- }
-
-
-
- $order_info['id']=$order_id;
-
-
-
- $result=$wxpay->do_nativepay($order_info);
-
-
-
- if($result){
-
-
-
- //这个if里面的就是自己定义的内容,比如生成二维码之后你要插入一条数据进入数据库之类 上面
-
-
-
- // 组装是必须要有的 下面这个是自己自定义的
-
-
-
- $data['url']=$result;
-
-
-
- $data['order_id']=$order_id;
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $data['money']=$money;//画品总价
-
-
-
- }else{
-
-
-
- $data['money']='1';
-
-
-
- }
-
-
-
-
-
- $data['pay_type']=2;
-
-
-
- }
-
-
-
- return ajax_success($data); 这里包含微信返回的支付信息
-
-
-
-
-
- 下面是在魔板和js里面进行的处理:
-
-
-
-
-
- 这里就生成了二维码可以扫码付款
-
-
-
-
-
- setTimeout(function(){
-
-
-
- getoneorder(data.info.order_id);
-
-
-
- },2000);
-
-
-
- //微信支付轮询js开始
-
-
-
- function getoneorder(pr){
-
-
-
- if(pr) {
-
-
-
- $.ajax({
-
-
-
- url: '/wechat/checkwxorderstatus', //这个方法是查询微信那边的订单处理状结果 因为的回调微信没有触发.
-
-
-
- data: {order_id: pr},
-
-
-
- type: 'GET',
-
-
-
- // dataType : 'json',
-
-
-
- success: function (msg) {
-
-
-
- if (msg.status==1) {
-
-
-
- // istry = false;
-
-
-
- layer.msg('支付成功');
-
-
-
- window.location.href='/member';
-
-
-
- }else if (msg.status==2 ) {
-
-
-
- layer.msg('支付失败');
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }else{ //暂未支付
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
-
-
- },
-
-
-
- error: function () {
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
- })
-
-
-
- }
-
-
-
- }
-
-
-
- //微信支付轮询js结束
-
-
-
-
-
- //查询微信的订单状态并处理订单业务逻辑返回处理结果
-
-
-
- public function checkWxOrderStatus(Request $request){
-
-
-
- $data = $request->all();
-
-
-
- $order_id=$data['order_id'];
-
-
-
- $wxpay=new Wxpay();
-
-
-
- $result=$wxpay->query_order($order_id);
-
-
-
- if(!empty($result['trade_state'])&&$result['trade_state']=='SUCCESS'){//去微信查询订单状态是ok
-
-
-
- $doorder=Order::doOrder($order_id,2); //传订单id处理订单
-
-
-
- if($doorder=='success'){
-
-
-
- return array(
-
-
-
- 'trade_state'=>'SUCCESS',
-
-
-
- 'order_status'=>'SUCCESS',
-
-
-
- 'status'=>'1' //支付成功
-
-
-
- );
-
-
-
- }else{
-
-
-
- return array(
-
-
-
- 'trade_state'=>'SUCCESS',
-
-
-
- 'order_status'=>'FAIL',
-
-
-
- 'status'=>'2' //付款成功但是没改订单状态
-
-
-
- );
-
-
-
- }
-
-
-
- }else{
-
-
-
- return array(
-
-
-
- 'trade_state'=>'FAIL',
-
-
-
- 'order_status'=>'FAIL',
-
-
-
- 'status'=>'3' //付款不成功订单也没修改
-
-
-
- );
-
-
-
- }
-
-
-
- }
-
-
-
-
-
-
-
- 2.微信内部浏览器支付(jsapi模式)和手机浏览器模式参见下面合并在一起判断处理的,我这里是是根据UA进行判断用户进入哪种支付模式,如果是微信打开的进入第一个否则进入第二个H5支付
-
-
-
- //手机UA判断
-
-
-
- if($this->isWeixin()){ //微信内部浏览器,这个方法网上很多自己搜索
-
-
-
- $wxpay = new Mobilewxpay();
-
-
-
- $order_info['uatype']='2';
-
-
-
- $order_info['body']='画品购买';
-
-
-
- $order_info['opend_id']=Auth::user()->openid;
-
-
-
- $order_info['order_number']=$parent_order_id;
-
-
-
- //区分生产和测试环境的支付金额
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $order_info['money']=$total_money;
-
-
-
- }else{
-
-
-
- $order_info['money']='1';
-
-
-
- }
-
-
-
- $order_info['id']=$parent_order_id;
-
-
-
- $order_info['pay_type']=2;
-
-
-
- //未付款的订单才可以进行付款
-
-
-
- $wxpay->unifiedOrder($order_info);
-
-
-
- $resulturl = $wxpay->GetJsApiParameters($order_info);
-
-
-
- if (!empty($resulturl)){
-
-
-
- //这一步是把微信返回的json格式的数据转换成数组
-
-
-
- $resulturl = json_decode($resulturl,true);
-
-
-
- }
-
-
-
- $order_info['resulturl']= $resulturl;
-
-
-
- }else{ //手机自带浏览器
-
-
-
- $wxpay = new Mobilewxpay();
-
-
-
- $order_info['uatype']='1';
-
-
-
- $order_info['body']='画品购买';
-
-
-
- $order_info['order_number']=$parent_order_id;
-
-
-
- //区分生产和测试环境的支付金额
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $order_info['money']=$total_money;
-
-
-
- }else{
-
-
-
- $order_info['money']='1';
-
-
-
- }
-
-
-
- $order_info['id']=$parent_order_id;
-
-
-
- //未付款的订单才可以进行付款
-
-
-
- $h5result= $wxpay->MobileH5Pay($order_info);
-
-
-
- $order_info['pay_type']=2;
-
-
-
- $order_info['h5resulturl']=!empty($h5result['mweb_url'])?$h5result['mweb_url']:'';
-
-
-
- }
-
-
-
- return ajax_success($order_info);
-
-
-
- 提交订单的通过上面的ajax返回数据
-
-
-
- 下面的js里面的处理:
-
-
-
- if(data.info.pay_type==2){//微信支付
-
-
-
- if(data.info.uatype == 1){//手机自带浏览器H5支付走这里
-
-
-
- var h5resulturlss=data.info.h5resulturl.replace(/amp;/, "");//这里进入了一个坑IOS手机上url会被转义这里是处理这个情况的,请注意支付跳转的url里面的&是否被转义成& 切记大坑. 如果是赋值在a链接的一个链接的话不会有这种情况发生.如果变量赋值给js的话会有这种情况.
-
-
-
- var h5resulturl12=decodeURI(h5resulturlss);
-
-
-
- window.open(h5resulturl12);//这里是走微信支付协议调用微信弹框
-
-
-
- }else{//微信内部浏览器jsapi支付走这里
-
-
-
- callpay(data.info.resulturl);//这里是呼唤微信支付弹框
-
-
-
- }
-
-
-
- setTimeout(function(){
-
-
-
- getoneorder(data.info.id);
-
-
-
- },2000);
-
-
-
- return false;
-
-
-
- }
-
-
-
-
-
-
-
-
-
- //调用微信JS api 支付
-
-
-
- function jsApiCall(pr)
-
-
-
- {
-
-
-
- WeixinJSBridge.invoke(
-
-
-
- 'getBrandWCPayRequest',
-
-
-
- pr,
-
-
-
- function(res){
-
-
-
- WeixinJSBridge.log(res.err_msg);
-
-
-
- // alert(res.err_code+res.err_desc+res.err_msg);
-
-
-
- }
-
-
-
- );
-
-
-
- }
-
-
-
- function callpay(pr)
-
-
-
- {
-
-
-
- if (typeof WeixinJSBridge == "undefined"){
-
-
-
- if( document.addEventListener ){
-
-
-
- document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
-
-
-
- }else if (document.attachEvent){
-
-
-
- document.attachEvent('WeixinJSBridgeReady', jsApiCall);
-
-
-
- document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
-
-
-
- }
-
-
-
- }else{
-
-
-
- jsApiCall(pr);
-
-
-
- }
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
- //微信支付轮询js开始
-
-
-
- function getoneorder(pr){
-
-
-
- if(pr) {
-
-
-
- $.ajax({
-
-
-
- url: '/wechat/checkwxorderstatus',//这里还是查询微信那边的订单状态做判断处理
-
-
-
- data: {order_id: pr},
-
-
-
- type: 'GET',
-
-
-
- // dataType : 'json',
-
-
-
- success: function (msg) {
-
-
-
- if (msg.status==1) {
-
-
-
- // istry = false;
-
-
-
- layer.open({
-
-
-
- content: ''支付成功
- content: '
-
-
-
- ,skin: 'msg'
-
-
-
- ,time: 2 //2秒后自动关闭
-
-
-
- });
-
-
-
- // window.setTimeout('window.location.href="/buy/paysuccess?type=1&oid='+pr+'"',2000);
-
-
-
- window.setTimeout('window.location.href="/member/myorder"',2000);
-
-
-
- }else if (msg.status==2 ) {
-
-
-
- layer.open({
-
-
-
- content: ''支付失败
- content: '
-
-
-
- ,skin: 'msg'
-
-
-
- ,time: 2 //2秒后自动关闭
-
-
-
- });
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }else{ //暂未支付
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
-
-
- },
-
-
-
- error: function () {
-
-
-
- setTimeout(function () {
-
-
-
- getoneorder(pr);
-
-
-
- }, 1000);
-
-
-
- }
-
-
-
- })
-
-
-
- }
-
-
-
- }
-
-
-
- //微信支付轮询js结束
-
-
-
- $(document).ready(function(){
-
-
-
- getoneorder();
-
-
-
- });
-
-
-
-
-
-
-
- 附上我的支付类文件 (扫描支付模式二使用的)
-
-
-
-
-
- namespace App\Tools\weixin;
-
-
-
- /**
-
-
-
-
- 微信支付
-
-
-
-
-
-
- */
-
-
-
-
-
- class Wxpay {
-
-
-
-
-
-
-
- //扫码支付模式一
-
-
-
- public function do_nativepay1($productId){
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.NativePay.php';
-
-
-
- $notify = new NativePay();
-
-
-
- $url1 = $notify->GetPrePayUrl($productId);
-
-
-
- return $url1;
-
-
-
- }
-
-
-
-
-
- //扫描支付模式二
-
-
-
- public function do_nativepay($order_info) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.NativePay.php';
-
-
-
- $notify = new \NativePay();
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody($order_info['body']);
-
-
-
- $input->SetAttach("画品购买");
-
-
-
- $input->SetOut_trade_no($order_info['id']);
-
-
-
- //$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
-
-
-
- if($_SERVER['HTTP_HOST']=='www.qq.cn'){
-
-
-
- $input->SetTotal_fee($order_info['money']*100);
-
-
-
- }else{
-
-
-
- $input->SetTotal_fee($order_info['money']);
-
-
-
- }
-
-
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag("test");
-
-
-
- $input->SetNotify_url('http://artflyer.qq.com/buy/getpay');
-
-
-
- // $input->SetNotify_url(url('http://artflyer.qq.com/wechat/getpay'));
-
-
-
- $input->SetTrade_type("NATIVE");
-
-
-
- $input->SetProduct_id($order_info['id']);
-
-
-
- $result = $notify->GetPayUrl($input);
-
-
-
- $url2 = $result["code_url"];
-
-
-
- return $url2;
-
-
-
- }
-
-
-
-
-
- // 支付付款
-
-
-
- public function do_wxpay($order_info) {
-
-
-
- ini_set('date.timezone', 'Asia/Shanghai');
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
- //①、获取用户openid
-
-
-
- $tools = new \JsApiPay();
-
-
-
- $openId = $tools->GetOpenid();
-
-
-
- //②、统一下单
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody($order_info['goods_name']);
-
-
-
- $input->SetAttach('');
-
-
-
- $input->SetOut_trade_no($order_info['order_sn']);
-
-
-
- $input->SetTotal_fee($order_info['order_amount']);
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag('');
-
-
-
- $input->SetNotify_url(url('http://artflyer.qq.com/wechat/getpay'));
-
-
-
- // $input->SetNotify_url(WxPayConfig::NOTIFY_URL);
-
-
-
- $input->SetTrade_type("JSAPI");
-
-
-
- $input->SetOpenid($openId);
-
-
-
- $order = \WxPayApi::unifiedOrder($input);
-
-
-
- $jsApiParameters = $tools->GetJsApiParameters($order);
-
-
-
- //生成带参数的同步放回地址
-
-
-
- include_once('../app/Tools/weixin/lib/WxPay.Api.php');
-
-
-
- $url_arr = json_decode($jsApiParameters, TRUE);
-
-
-
- unset($url_arr['signType']);
-
-
-
- unset($url_arr['paySign']);
-
-
-
- $url_arr['order_sn'] = $order_info['order_sn'];
-
-
-
- $notify = new \WxPayResults();
-
-
-
- $notify->FromArray($url_arr);
-
-
-
- $sign = $notify->SetSign();
-
-
-
- $url_arr['sign'] = $sign;
-
-
-
- $return_url = \WxPayConfig::RETURN_URL . '?' . http_build_query($url_arr);
-
-
-
- //支付页面
-
-
-
- $pay_url = \WxPayConfig::PAY_URL . '?order_sn=' . $order_info['order_sn'];
-
-
-
- include_once 'weixin.html';
-
-
-
- }
-
-
-
-
-
- // 同步回调验证
-
-
-
- public function do_return($arr = array()) {
-
-
-
- include_once('../app/Tools/weixin/lib/WxPay.Api.php');
-
-
-
- $notify = new \WxPayResults();
-
-
-
- if (empty($arr)) {
-
-
-
- $arr = $_GET;
-
-
-
- }
-
-
-
- $notify->FromArray($arr);
-
-
-
- if ($notify->CheckSign() == true) {
-
-
-
- return TRUE;
-
-
-
- } else {
-
-
-
- return FALSE;
-
-
-
- }
-
-
-
- }
-
-
-
-
-
- // 异步回调验证
-
-
-
- public function do_notify($xml) {
-
-
-
- include_once('../app/Tools/weixin/lib/WxPay.Api.php');
-
-
-
- $notify = new WxPayResults();
-
-
-
- $notify->FromXml($xml);
-
-
-
- if ($notify->CheckSign() == true) {
-
-
-
- return TRUE;
-
-
-
- } else {
-
-
-
- return FALSE;
-
-
-
- }
-
-
-
- }
-
-
-
-
-
- //xml转化成数组
-
-
-
- public function FromXml($xml) {
-
-
-
- include_once('../app/Tools/weixin/lib/notify.php');
-
-
-
- $notify = new PayNotifyCallBack();
-
-
-
- return $notify->FromXml($xml);
-
-
-
- }
-
-
-
-
-
- /**
-
-
-
-
- 交易查询 查询订单交易状态
-
-
-
-
-
-
-
- @param string $order_sn 07073订单号
-
-
-
-
-
- @return [type] [array]
-
-
-
-
- */
-
-
-
-
-
- public function query_order($order_sn) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.Api.php';
-
-
-
- if (isset($order_sn) && $order_sn != "") {
-
-
-
- $input = new \WxPayOrderQuery();
-
-
-
- $input->SetOut_trade_no($order_sn);
-
-
-
- $result_arr = \WxPayApi::orderQuery($input);
-
-
-
- }
-
-
-
- return $result_arr;
-
-
-
- }
-
-
-
-
-
- // 退款
-
-
-
- public function wx_refund($order_info) {
-
-
-
- require_once "../app/Tools/weixin/lib/WxPay.Api.php";
-
-
-
-
-
- $input = new WxPayRefund();
-
-
-
- $input->SetTransaction_id($order_info['transaction_id']);
-
-
-
- $input->SetTotal_fee($order_info['total_fee']);
-
-
-
- $input->SetRefund_fee($order_info['refund_fee']);
-
-
-
- $input->SetOut_refund_no($order_info['batch_no']);
-
-
-
- $input->SetOp_user_id(WxPayConfig::MCHID);
-
-
-
- $res = WxPayApi::refund($input);
-
-
-
- return $res;
-
-
-
- }
-
-
-
-
-
- // 退款状态查询
-
-
-
- public function wx_refund_query($order_info) {
-
-
-
- require_once '../app/Tools/weixin/lib/WxPay.Api.php';
-
-
-
-
-
- $input = new WxPayRefundQuery();
-
-
-
- $input->SetTransaction_id($order_info['transaction_id']);
-
-
-
- $res = WxPayApi::refundQuery($input);
-
-
-
-
-
- return $res;
-
-
-
- }
-
-
-
-
-
- // 支付付款
-
-
-
- public function do_wappay($order_info) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.WapPay.php';
-
-
-
- // 统一下单
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody($order_info['goods_name']);
-
-
-
- $input->SetAttach("");
-
-
-
- $input->SetOut_trade_no($order_info['order_sn']);
-
-
-
- $input->SetTotal_fee($order_info['order_amount']);
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag("");
-
-
-
- $input->SetNotify_url(\WxPayConfig::NOTIFY_URL);
-
-
-
- $input->SetTrade_type("WAP");
-
-
-
- $input->SetProduct_id($order_info['id']);
-
-
-
-
-
- $wxpay = new \WapPay();
-
-
-
- // 预支付订单信息
-
-
-
- $prepay_res = $wxpay->GetPrepayInfo($input);
-
-
-
- var_dump($prepay_res);
-
-
-
- $wxpay->GetWapPayUrl($prepay_res['prepayid']);
-
-
-
- }
-
-
-
-
-
- // 微信登录
-
-
-
- public function wxlogin() {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
-
-
- // 获取用户openid
-
-
-
- $tools = new JsApiPay();
-
-
-
- $data = $tools->GetOpenData();
-
-
-
- return $data;
-
-
-
- }
-
-
-
-
-
- // 获得微信用户信息
-
-
-
- public function wxuser($access_token, $openid) {
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
-
-
- // 获取用户openid
-
-
-
- $tools = new JsApiPay();
-
-
-
- $data = $tools->getUserInfo($access_token, $openid);
-
-
-
- return $data;
-
-
-
- }
-
-
-
-
-
- //将xml转为array
-
-
-
- public function xmlToArray($xml){
-
-
-
- $array_data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
-
-
-
- return $array_data;
-
-
-
- }
-
-
-
-
-
- /**
-
-
-
-
- 作用:array转xml
-
-
-
-
- */
-
-
-
- function arrayToXml($arr)
-
-
-
- {
-
-
-
- $xml = "
";
- $xml = "
-
-
-
- foreach ($arr as $key=>$val)
-
-
-
- {
-
-
-
- if (is_numeric($val))
-
-
-
- {
-
-
-
- $xml.="<".$key.">".$val."".$key.">";
-
-
-
-
-
- }
-
-
-
- else
-
-
-
- $xml.="<".$key.">".$key.">";
-
-
-
- }
-
-
-
- $xml.="";
-
-
-
- return $xml;
-
-
-
- }
-
-
-
- /*
-
-
-
-
- 移动端H5支付
-
-
-
-
- */
-
-
-
- public function mobile_h5pay(){
-
-
-
- //①、获取用户openid
-
-
-
- include_once '../app/Tools/weixin/lib/WxPay.JsApiPay.php';
-
-
-
- $tools = new \JsApiPay();
-
-
-
- $openId = $tools->GetOpenid();
-
-
-
- //②、统一下单
-
-
-
- $input = new \WxPayUnifiedOrder();
-
-
-
- $input->SetBody("test");
-
-
-
- $input->SetAttach("test");
-
-
-
- $input->SetOut_trade_no(\WxPayConfig::MCHID.date("YmdHis"));
-
-
-
- $input->SetTotal_fee("1");
-
-
-
- $input->SetTime_start(date("YmdHis"));
-
-
-
- $input->SetTime_expire(date("YmdHis", time() + 600));
-
-
-
- $input->SetGoods_tag("test");
-
-
-
- $input->SetNotify_url("http://www1.qq.cn/wechat/getpay");
-
-
-
- $input->SetTrade_type("MWEB"); //H5支付的交易类型为MWEB
-
-
-
- // $input->SetOpenid($openId); //这里要注释掉
-
-
-
- $order = \WxPayApi::unifiedOrder($input);
-
-
-
- return $order;
-
-
-
- }
-
-
-
-
-
- }
-
-
-
-
-
- 下面这个是H5支付和微信内部浏览器支付使用的类文件
-
-
-
-
-
- namespace App\Tools\weixin;
-
-
-
- header('Content-type:text/html;charset=utf-8');
-
-
-
- /**微信支付
-
-
-
-
- Class Mobilewxpay
-
-
-
-
-
- @package App\Tools\weixin
-
-
-
-
- */
-
-
-
- class Mobilewxpay {
-
-
-
- public $APPID = 'wx811a9fc';
-
-
-
- public $MCHID = '14912';
-
-
-
- public $KEY = '68e4b3eae5b171f17fc5';
-
-
-
- public $APPSECRET = '6bce72a750664d61a3b47c';
-
-
-
- /**
-
-
-
- *
-
-
-
-
- 拼接签名字符串
-
-
-
-
-
- @param array $urlObj
-
-
-
-
-
- @return 返回已经拼接好的字符串
-
-
-
-
- */
-
-
-
- function ToUrlParams($urlObj)
-
-
-
- {
-
-
-
- $buff = "";
-
-
-
- foreach ($urlObj as $k => $v)
-
-
-
- {
-
-
-
- if($k != "sign"){
-
-
-
- $buff .= $k . "=" . $v . "&";
-
-
-
- }
-
-
-
- }
-
-
-
-
-
- $buff = trim($buff, "&");
-
-
-
- return $buff;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
-
-
-
-
-
- appid、mchid、spbill_create_ip、nonce_str不需要填入
-
-
-
-
-
- @param WxPayUnifiedOrder $inputObj
-
-
-
-
-
- @param int $timeOut
-
-
-
-
-
- @throws WxPayException
-
-
-
-
-
- @return 成功时返回,其他抛异常
-
-
-
-
- */
-
-
-
- function unifiedOrder($order_info,$timeOut = 6)
-
-
-
- {
-
-
-
- $datas = array();
-
-
-
- $datas['body'] = '购买';
-
-
-
- $datas['out_trade_no'] = $order_info['id'];//订单号
-
-
-
- $datas['total_fee'] = $order_info['money'];
-
-
-
- $datas['time_start'] = date("YmdHis");
-
-
-
- $datas['time_expire'] = date("YmdHis", time() + 600);
-
-
-
- $datas['notify_url'] = 'http://artwap.qq.com/wechat/getpay';//没有用上这个url微信死活不回调我,用的js轮询查询支付和订单状态
-
-
-
- $datas['trade_type'] = 'JSAPI';//微信内部浏览器支付
-
-
-
- // $datas['openid'] = $order_info['opend_id'];
-
-
-
- $datas['openid'] = 'o_rTz0bCFLXRx5XSmE_ijh9xaC3U';
-
-
-
- $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
-
-
-
- $datas['appid'] = $this->APPID;//公众账号ID
-
-
-
- $datas['mch_id'] = $this->MCHID;//商户号
-
-
-
- $datas['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];//ip
-
-
-
- $datas['nonce_str'] = $this->getNonceStr();//随机字符串
-
-
-
- //签名步骤一:按字典序排序参数
-
-
-
- ksort($datas);
-
-
-
- $string = $this->ToUrlParamss($datas);
-
-
-
- //签名步骤二:在string后加入KEY
-
-
-
- $string = $string . "&key=".$this->KEY;
-
-
-
- //签名步骤三:MD5加密
-
-
-
- $string = md5($string);
-
-
-
- //签名步骤四:所有字符转为大写
-
-
-
- $result = strtoupper($string);
-
-
-
- $datas['sign'] = $result;//签名
-
-
-
- $xml = $this->ToXml($datas);
-
-
-
- $response = $this->postXmlCurl($xml, $url, false, $timeOut);
-
-
-
- $data = $this->FromXml($response);
-
-
-
- return $data;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 产生随机字符串,不长于32位
-
-
-
-
-
- @param int $length
-
-
-
-
-
- @return 产生的随机字符串
-
-
-
-
- */
-
-
-
- function getNonceStr($length = 32)
-
-
-
- {
-
-
-
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
-
-
-
- $str ="";
-
-
-
- for ( $i = 0; $i < $length; $i++ ) {
-
-
-
- $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
-
-
-
- }
-
-
-
- return $str;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 输出xml字符
-
-
-
-
- **/
-
-
-
- function ToXml($datas)
-
-
-
- {
-
-
-
- $xml = "
";
- $xml = "
-
-
-
- foreach ($datas as $key=>$val)
-
-
-
- {
-
-
-
- if (is_numeric($val)){
-
-
-
- $xml.="<".$key.">".$val."".$key.">";
-
-
-
- }else{
-
-
-
- $xml.="<".$key.">".$key.">";
-
-
-
- }
-
-
-
- }
-
-
-
- $xml.="";
-
-
-
- return $xml;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 格式化参数格式化成url参数
-
-
-
-
- */
-
-
-
- function ToUrlParamss($datas)
-
-
-
- {
-
-
-
- $buff = "";
-
-
-
- foreach ($datas as $k => $v)
-
-
-
- {
-
-
-
- if($k != "sign" && $v != "" && !is_array($v)){
-
-
-
- $buff .= $k . "=" . $v . "&";
-
-
-
- }
-
-
-
- }
-
-
-
- $buff = trim($buff, "&");
-
-
-
- return $buff;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 以post方式提交xml到对应的接口url
-
-
-
-
- *
-
-
-
-
- @param string $xml 需要post的xml数据
-
-
-
-
-
- @param string $url url
-
-
-
-
-
- @param bool $useCert 是否需要证书,默认不需要
-
-
-
-
-
- @param int $second url执行超时时间,默认30s
-
-
-
-
- */
-
-
-
- function postXmlCurl($xml, $url, $useCert = false, $second = 30)
-
-
-
- {
-
-
-
- $ch = curl_init();
-
-
-
- //设置超时
-
-
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
-
-
-
- curl_setopt($ch,CURLOPT_URL, $url);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//严格校验
-
-
-
- //设置header
-
-
-
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
-
-
-
- //要求结果为字符串且输出到屏幕上
-
-
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
-
-
- //post提交方式
-
-
-
- curl_setopt($ch, CURLOPT_POST, TRUE);
-
-
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
-
-
-
- //运行curl
-
-
-
- $data = curl_exec($ch);
-
-
-
- //返回结果
-
-
-
- curl_close($ch);
-
-
-
- return $data;
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 将xml转为array
-
-
-
-
-
- @param string $xml
-
-
-
-
- */
-
-
-
- public function FromXml($xml) {
-
-
-
- //将XML转为array
-
-
-
- return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 获取jsapi支付的参数
-
-
-
-
-
- @param array $UnifiedOrderResult 统一支付接口返回的数据
-
-
-
-
-
- @return json数据,可直接填入js函数作为参数
-
-
-
-
- */
-
-
-
- function GetJsApiParameters($order_info,$timeOut = 6){
-
-
-
-
-
- $UnifiedOrderResult = $this->unifiedOrder($order_info,$timeOut = 6);
-
-
-
- if(!array_key_exists("appid", $UnifiedOrderResult)
-
-
-
- || !array_key_exists("prepay_id", $UnifiedOrderResult)
-
-
-
- || $UnifiedOrderResult['prepay_id'] == "")
-
-
-
- {
-
-
-
- echo $UnifiedOrderResult['err_code_des'];
-
-
-
- exit;
-
-
-
- }
-
-
-
- $da = array();
-
-
-
- $da['appId'] = $UnifiedOrderResult["appid"];
-
-
-
- $timeStamp = time();
-
-
-
- $da['timeStamp'] = "$timeStamp";
-
-
-
- $da['nonceStr'] = $this->getNonceStr();
-
-
-
- $da['package'] = "prepay_id=" . $UnifiedOrderResult['prepay_id'];
-
-
-
- $da['signType'] = 'MD5';
-
-
-
- //签名步骤一:按字典序排序参数
-
-
-
- ksort($da);
-
-
-
- $string = $this->ToUrlParamss($da);
-
-
-
- //签名步骤二:在string后加入KEY
-
-
-
- $string = $string . "&key=".$this->KEY;
-
-
-
- //签名步骤三:MD5加密
-
-
-
- $string = md5($string);
-
-
-
- //签名步骤四:所有字符转为大写
-
-
-
- $result = strtoupper($string);
-
-
-
- $da['paySign'] = $result;
-
-
-
- $parameters = json_encode($da);
-
-
-
- return $parameters;
-
-
-
- }
-
-
-
- /*
-
-
-
-
- 微信H5相关处理开始
-
-
-
-
- */
-
-
-
- function get_client_ip() {
-
-
-
- static $realip;
-
-
-
- if (isset($_SERVER)) {
-
-
-
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
-
-
-
- $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
-
-
-
- } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
-
-
-
- $realip = $_SERVER["HTTP_CLIENT_IP"];
-
-
-
- } else {
-
-
-
- $realip = $_SERVER["REMOTE_ADDR"];
-
-
-
- }
-
-
-
- } else {
-
-
-
- if (getenv("HTTP_X_FORWARDED_FOR")) {
-
-
-
- $realip = getenv("HTTP_X_FORWARDED_FOR");
-
-
-
- } else if (getenv("HTTP_CLIENT_IP")) {
-
-
-
- $realip = getenv("HTTP_CLIENT_IP");
-
-
-
- } else {
-
-
-
- $realip = getenv("REMOTE_ADDR");
-
-
-
- }
-
-
-
- }
-
-
-
- return $realip;
-
-
-
- }
-
-
-
-
-
- //H5支付开始(手机自带浏览器处理)
-
-
-
- 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;
-
-
-
- }
-
-
-
- public function getIp(){
-
-
-
- $ip = '';
-
-
-
- if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
-
-
-
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
-
-
-
- }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
-
-
-
- $ip = $_SERVER['HTTP_CLIENT_IP'];
-
-
-
- }else{
-
-
-
- $ip = $_SERVER['REMOTE_ADDR'];
-
-
-
- }
-
-
-
- $ip_arr = explode(',', $ip);
-
-
-
- return $ip_arr[0];
-
-
-
- }
-
-
-
- public function postXmlCurlh5($xml,$url,$second = 30){
-
-
-
- $ch = curl_init();
-
-
-
- //设置超时
-
-
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
-
-
-
- curl_setopt($ch,CURLOPT_URL, $url);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
-
-
-
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
-
-
-
- //设置header
-
-
-
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
-
-
-
- //要求结果为字符串且输出到屏幕上
-
-
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
-
-
- //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 "curl出错,错误码:$error"."
";
- echo "curl出错,错误码:$error"."
-
-
-
- }
-
-
-
- }
-
-
-
- /**
-
-
-
-
- 生成签名
-
-
-
-
-
- @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
-
-
-
-
- */
-
-
-
- protected function MakeSign($arr) {
-
-
-
- //签名步骤一:按字典序排序参数
-
-
-
- ksort($arr);
-
-
-
- $string = $this->ToUrlParams($arr);
-
-
-
- //签名步骤二:在string后加入KEY
-
-
-
- $string = $string . "&key=" . $this->KEY;
-
-
-
- //签名步骤三:MD5加密
-
-
-
- $string = md5($string);
-
-
-
- //签名步骤四:所有字符转为大写
-
-
-
- $result = strtoupper($string);
-
-
-
- return $result;
-
-
-
- }
-
-
-
-
-
-
-
- public function MobileH5Pay($order_info){
-
-
-
- $headers =array();
-
-
-
- $headers[] ='Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8';
-
-
-
- $headers[] ='Connection: Keep-Alive';
-
-
-
- $headers[] ='Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3';
-
-
-
- $headers[] ='Accept-Encoding: gzip, deflate';
-
-
-
- $headers[] ='User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101Firefox/22.0';
-
-
-
- $appid = $this->APPID; //应用APPID
-
-
-
- $mch_id = $this->MCHID; //微信支付商户号
-
-
-
- $key = $this->KEY; //微信商户API密钥
-
-
-
-
-
- $rand = rand(00000,99999);
-
-
-
- $attach='artflyer.cn';
-
-
-
- $out_trade_no = $order_info['id'];//平台内部订单号
-
-
-
- $nonce_str = $this->createNoncestr();//随机字符串
-
-
-
- $body = "artflyer.cn";//内容
-
-
-
- $total_fee = $order_info['money']; //金额
-
-
-
- $spbill_create_ip = $this->getIp(); //IP
-
-
-
- $notify_url = "http://artwap.qq.com/wechat/getpay";//没有用上这个url微信死活不回调我,用的js轮询查询支付和订单状态
-
-
-
- $trade_type = 'MWEB';//交易类型 因为是h5支付所以交易类型必须是MWEB
-
-
-
- $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.qq.com/wechat/getpay","wap_name":"artflyer.cn"}}';//场景信息 必要参数
-
-
-
- //以下信息可以不改
-
-
-
- $signA ="appid=$appid&attach=$out_trade_no&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";
-
-
-
- $strSignTmp = $signA."&key=$key"; //拼接字符串 注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 //包括下面XML 是否正确
-
-
-
- $sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
-
-
-
- $post_data = "
- $post_data = "
-
-
-
$appid
-
-
-
$mch_id
-
-
-
- $body
-
-
-
$out_trade_no
-
-
-
$total_fee
-
-
-
$spbill_create_ip
-
-
-
$notify_url
-
-
-
$trade_type
-
-
-
$scene_info
-
-
-
$out_trade_no
-
-
-
$nonce_str
-
-
-
$sign
-
-
-
- ";//拼接成XML 格式
-
-
-
-
-
- $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
-
-
-
- $dataxml = $this->postXmlCurlh5($post_data,$url,$headers); //后台POST微信传参地址 同时取得微信返回的参数 POST 方法我写下面了
-
-
-
- $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
-
-
-
- return $objectxml;
-
-
-
- }
-
-
-
-
-
-
-
- }
-
-
-
- 有问题也可以发邮件给微信技术 [email protected]
-
-
-
- 搞的比较乱别骂...... 有问题可以QQ交流 245864009
-
-
-