thinkphp5实现微信扫码支付

吾爱主题 阅读:150 2021-09-24 14:26:00 评论:0

本文实例为大家分享了thinkphp5微信扫码支付的具体代码,供大家参考,具体内容如下

配置WxPay.Config.php

控制器

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 //微信支付 //参数 订单 价格   public function wxPay( $order_number , $money )   {    header( "Content-type:text/html;charset=utf-8" );     //require_once VENDOR_PATH.'/alipaymobile/config.php';    require_once VENDOR_PATH. '/wxpay/WxPay.Api.php' ; //引入微信支付    require_once VENDOR_PATH. '/wxpay/WxPay.Notify.php' ;    require_once VENDOR_PATH. '/wxpay/phpqrcode/phpqrcode.php' ;      $input = new \WxPayUnifiedOrder(); //统一下单    $config = new \WxPayConfig(); //配置参数    $notify = new \QRcode();    //$paymoney = input('post.paymoney'); //支付金额    $paymoney = $money ; //测试写死    //$paymoney = 0.01; //测试写死    //$str = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);//生成订单号    $out_trade_no = $order_number ; //商户订单号(自定义)    $goods_name = '扫码支付' . $paymoney . '元' ; //商品名称(自定义)    $input ->SetBody( $goods_name );    $input ->SetAttach( $goods_name );    $input ->SetOut_trade_no( $out_trade_no );    $input ->SetTotal_fee( $paymoney *100); //金额乘以100    $input ->SetTime_start( date ( "YmdHis" ));    $input ->SetTime_expire( date ( "YmdHis" , time() + 600));    $input ->SetGoods_tag( "test" );    //回调地址 注意能访问    $input ->SetNotify_url( "http://www.域名.com/index.php/index/index/wxpaynotifyurl" ); //回调地址    $input ->SetTrade_type( "NATIVE" );    $input ->SetProduct_id( '123456789' ); //商品id    $result = \WxPayApi::unifiedOrder( $config , $input );    if ( $result [ 'result_code' ]== 'SUCCESS' && $result [ 'return_code' ]== 'SUCCESS' ) {     $url = $result [ "code_url" ];     $this ->assign( 'money' , $paymoney );     $this ->assign( 'url' , $url );     $this ->assign( 'num' , $out_trade_no );     //映射视图 微信二维码需要自己生成     return view( "../../../template/wxpay" );    } else {     $this ->error( '参数错误' );    }    // return view();     }

视图 生成二维码 进行扫描付款

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 <!doctype html> < html > < head > < meta charset = "utf-8" > < title >微信支付页面</ title > < style > .wxsm{ width:1200px; margin:0 auto; overflow:hidden;} .wxsmt{ width:100%; height:40px; border-bottom:1px solid #ccc;} .wxsmt h2{ font-size:14px; line-height:40px; color:#232323; font-weight:400; text-align:center;} .wxsmc{ width:100%; overflow:hidden;} .smcr{ overflow:hidden; margin:0 auto; } .smcr h2{ font-size:14px; font-weight:400; line-height:40px; text-align:center;} .smcr h2 span{ font-size:20px; color:#f23535;} .smcrb{ width:100%; overflow:hidden;;} .smm{ width:218px; height:284px; border:1px solid #3cb035; background:#3cb035; margin:0 auto} .smm img{ width:218px; height:218px; background:#fff;} .smm span{ display:block; color:#fff; line-height:66px; text-align:center;}   </ style > < script src = "__TEMP__/js/jquery-1.10.1.min.js" ></ script >   </ head >   < body >   < div class = "wxsm" >   < div class = "wxsmt" >    < h2 >订单提交成功,请尽快付款</ h2 >   </ div >   < div class = "wxsmc" >    < div class = "smcr" >     < h2 >应付金额:< span >{$money}</ span >元</ h2 >     < div class = "smcrb" >      < div class = "smm" >       < img src="/vendor/wxpay/qrcode.php?data=<?php echo urlencode($url);?>"/>       < span >打开微信,扫描二维码支付</ span >      </ div >     </ div >    </ div >   </ div > </ div >   < script >   //设置每隔1000毫秒执行一次load() 方法   var myIntval=setInterval(function(){loads()},1000);   function loads(){       var xmlhttp;      // 轮询的浏览器设置      if (window.XMLHttpRequest){       // code for IE7+, Firefox, Chrome, Opera, Safari       xmlhttp=new XMLHttpRequest();      }else{       // code for IE6, IE5       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");      }      xmlhttp.onreadystatechange=function(){       if (xmlhttp.readyState==4 && xmlhttp.status==200){        trade_state=JSON.parse(xmlhttp.responseText);        console.log(trade_state);//打印查看轮询的状态,也可以关闭。        //判断订单支付状态 并用document.getElementById方法赋值到myDiv中;        if(trade_state.code=='SUCCESS'){                 //延迟3000毫秒执行tz() 方法         clearInterval(myIntval);         //进行跳转。          var url = "{:url('index/center')}";          var http = window.location.protocol;          var zhu = window.location.host;          setTimeout("location.href='"+http+"//"+zhu+""+url+"'",1500);        }else if(trade_state.code=='REFUND'){                 clearInterval(myIntval);        }else if(trade_state.code=='NOTPAY'){                  }else if(trade_state.code=='CLOSED'){                 clearInterval(myIntval);        }else if(trade_state.code=='REVOKED'){                 clearInterval(myIntval);        }else if(trade_state.code=='USERPAYING'){                }else if(trade_state.code=='PAYERROR'){                 clearInterval(myIntval);        }               }      }    //orderquery.php 文件返回订单状态,通过订单状态确定支付状态    xmlhttp.open("POST","/vendor/wxpay/orderquery.php",false);    //下面这句话必须有    //把标签/值对添加到要发送的头文件。    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");    xmlhttp.send("out_trade_no=<? php echo $num;?>");       } </ script >   </ body > </ html >

支付回调

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public function wxpaynotifyurl()   {    //$xml = $GLOBALS['HTTP_RAW_POST_DATA']; //返回的xml   $xml = file_get_contents ( "php://input" );   //$results = db('fund') -> where('id',1) -> update(['a'=>$xml]);exit();    $xmlArr = $this ->Init( $xml );    file_put_contents (dirname( __FILE__ ). '/xml.txt' , $xml ); //记录日志 支付成功后查看xml.txt文件是否有内容 如果有xml格式文件说明回调成功       $out_trade_no = $xmlArr [ 'out_trade_no' ]; //订单号    $total_fee = $xmlArr [ 'total_fee' ]/100; //回调回来的xml文件中金额是以分为单位的    $result_code = $xmlArr [ 'result_code' ]; //状态    //$result = db('order') -> where(['order' => $out_trade_no]) -> find();    //if($result['price'] == $total_fee){     if ( $result_code == 'SUCCESS' ){ //数据库操作      //处理数据库操作 例如修改订单状态 给账户充值等等        echo 'SUCCESS' ; //返回成功给微信端 一定要带上不然微信会一直回调8次      exit ;     } else { //失败      return false;      exit ;     }         }   public function Init( $xml )   {    $array_data = json_decode(json_encode(simplexml_load_string( $xml , 'SimpleXMLElement' , LIBXML_NOCDATA)), true);    return $array_data ;   }

简单的写了一下 能实现简单的thinkphp5+微信扫码支付的大体流程 若有有问题的地方 还请各位大神指点

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43866089/article/details/103641380

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容