MICUU
心情
所有
图集
登录
搜索
原创
Tp5框架整合微信支付详细版带源码
韩宇
发布于:2018-06-21
<h1> thinkphp5.0框架整合微信支付 </h1> <p> 微信支付相信是很多地方都要用到的!这里不多说,再thinkphp3框架下已经有很多源码了!但是再tp5的框架下不是很多! </p> <p> 这里我还是讲解下载composer工具下整合微信支付! </p> <p> 我的PHP版本是5.4的,所以很多composer版本不能用!我选用的是 </p> <pre><span style="color:#660e7a;font-weight:bold;">"yurunsoft/pay-sdk"</span>: <span style="color:#008000;font-weight:bold;">"1.0.*" 这个版本</span></pre> <pre><span><b>大家可以再你的composer中添加代码</b></span></pre> <pre><span><b><img src="/data/upload/editer/image/2018/06/21/5b2b38992d9f1.jpg" alt="" /> </b></span></pre> <pre><span><b> </b></span></pre> <pre><span><b>然后再运行语句</b></span></pre> <pre><span><b><img src="/data/upload/editer/image/2018/06/21/5b2b38d6494a0.jpg" alt="" /> </b></span></pre> <pre><span><b>这样支付框架就载入到您的系统了!tp5框架会自动加载的!</b></span></pre> <pre><span><b> </b></span></pre> <pre><span><b> </b></span></pre> <pre><span><b>下面我们来看看创建订单代码</b></span></pre> <pre><span><b><img src="/data/upload/editer/image/2018/06/21/5b2b392841baa.jpg" alt="" /> </b></span></pre> <pre><span><b>具体代码</b></span></pre> <pre><span><b> <pre class="prettyprint lang-js"> public function order() { /***************************************/ $orderid = Request::instance()->param("orderid"); if($orderid){ $res = Db::name("order")->where("orderid","=",$orderid)->find(); $data = $res; if(time() > $data["add_time"]+(15*60)){ $this->error("订单超过15分钟未支付,请重新下单"); } }else{ $d = Request::instance()->post(); if($d['word_num'] < 1){ $this->error("订单数不能小于1"); } if(!Session::get("index_user.uid")){ $this->error("用户登录状态异常,请重新登录再试"); } $uid = Session::get("index_user.uid"); $data["uid"] = $uid; $data["add_time"] = time(); $data["orderid"] = create_order(); $data["num"] = $d["word_num"]; $data["num_left"] = $d["word_num"]; $data["fee"] = $d["word_num"] * 50; $d = date("d"); $data["end_time"] = (date("Y")+1).(date("m")).($d-1); $res = Db::name("order")->insert($data); } if($res){ //支付操作 $params = new \Yurun\PaySDK\Weixin\Params\PublicParams; $params->appID = "wx72d4738afa4a1fce"; $params->mch_id ="1492050462"; $params->key = "2jjdw2o3wefdfsewirewrwerer223dsf"; $pay = new \Yurun\PaySDK\Weixin\SDK($params); $request = new \Yurun\PaySDK\Weixin\Native\Params\Pay\Request; $request->body = '驱动中国大数据研究院'; // 商品描述 $request->out_trade_no =$data["orderid"]; // 订单号 $request->total_fee = $data["fee"]*100; // 订单总金额,单位为:分 $request->spbill_create_ip = $_SERVER['REMOTE_ADDR'] ; // 客户端ip $request->notify_url = "http://data.qudong.com/index/notify/"; // 异步通知地址 // 调用接口 $result = $pay->execute($request); $shortUrl = $result['code_url']; $this->assign("url",$shortUrl); $this->assign("order",$data); return $this->fetch(); }else{ $this->error("出错了,请重试"); } }</pre> 可以看到我的回调地址是<b> <pre class="prettyprint lang-js">http://xxxxxxxxx.com/index/notify/</pre> <pre class="prettyprint lang-js">下面具体看下notify控制器的代码</pre> <pre class="prettyprint lang-js">如下</pre> <pre class="prettyprint lang-js"> <pre class="prettyprint lang-js"><?php namespace app\index\controller; use think\Controller; use think\Cookie; use think\Db; use think\Request; use think\Session; class Notify extends Controller { public function _initialize() { } public function index() { $params = new \Yurun\PaySDK\Weixin\Params\PublicParams; $params->appID = "wx72d4738afa4a1fce"; $params->mch_id ="1492050462"; $params->key = "2jjdw2o3wefdfsewirewrwerer223dsf"; $sdk = new \Yurun\PaySDK\Weixin\SDK($params); $payNotify = new PayNotify; try{ $sdk->notify($payNotify); }catch(Exception $e){ file_put_contents(__DIR__ . '/notify_result.txt', $e->getMessage() . ':' . var_export($payNotify->data, true)); } } } class PayNotify extends \Yurun\PaySDK\Weixin\Notify\Pay { /** * 后续执行操作 * @return void */ protected function __exec() { // 支付成功处理,一般做订单处理,$this->data 是从微信发送来的数据 file_put_contents(__DIR__ . '/notify_result.txt', date('Y-m-d H:i:s') . ':' . var_export($this->data, true)); $res = $this->data; $data['pay_time'] = time(); $data['ispayed'] =1; $data['status'] =1; $db = Db::name("order"); $db->where("orderid","=",$res['out_trade_no'])->update($data); $order = $db->where("orderid","=",$res['out_trade_no'])->find(); Db::name("user")->where(["id"=>$order["uid"]])->setInc("residue_degree",$order['num_left']); // 告诉微信我处理过了,不要再通过了 $this->reply(true, 'OK'); } } </pre> <br /> </pre> <pre class="prettyprint lang-js">这样就完成了订单的创建和异步通知[微信支付服务器会通知我们的服务器]!</pre> <pre class="prettyprint lang-js"> </pre> <pre class="prettyprint lang-js">接下来就是前台页面怎么在支付成功后自动跳转了</pre> <pre class="prettyprint lang-js">页面代码如下</pre> <pre class="prettyprint lang-js"> <pre class="prettyprint lang-js"><!DOCTYPE html> <html> <head> <title>支付订单</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <meta http-equiv="Cache-Control" content="no-cache"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="-1"> <meta name="keywords" content=""> <meta name="description" content=""> <!--layui--> <link rel="stylesheet" href="__STATIC__/layui/css/layui.css"> <script src="__STATIC__/layui/layui.all.js"></script> <script src="__STATIC__/layer/layer.js"></script> <!--layui end--> </head> {include file="public/head" /} <body style="background-color: #9c9c9c;"> <div class="layui-container" style="min-height: 300px;background-color: #FFF; padding: 30px;"> {include file="public/top_bar" /} <table class="am-table"> <thead> <tr> <th>名称</th> <th>类型</th> <th>周期</th> <th>费用</th> <th>订单号</th> </tr> </thead> <tbody> <tr> <td>关键词条数</td> <td>{$order.num}</td> <td>{$order.end_time}</td> <td>{$order.fee}</td> <td><span id="orderid">{$order.orderid}</span></td> </tr> </tbody> </table> <div style="width: 100%; text-align: center"> <img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data=<?php echo urlencode($url);?>" style="width:150px;height:150px;"/> <h2>微信扫码支付</h2> </div> </div> <script> //检测订单支付状态,支付成功则自动跳转 function check() { var orderid = $("#orderid").html(); var url = "{:url('vip/check')}"; $.post(url,{orderid:orderid},function(e){ if(e.status == "1"){ alert("支付成功,确定后跳转到订单列表"); redirect(e.url); }else { console.log("支付失败或者还未支付"); } }); } setInterval("check()",3000); </script> </body> </html></pre> 可以看到,页面插入了订单号,每3秒执行一次检测,当后台支付成功数据库被改变后,前台检测出订单支付完成,进行跳转!<br /> </pre> <pre class="prettyprint lang-js">可能说的不是特别明白,下面是我打包的原代码!其中只有APPlication目录下的文件!</pre> <pre class="prettyprint lang-js"><a href="http://micuer.com/data/upload/application.rar" target="_blank">大家自行下载放到自己的框架研究下!其实很简单的!</a></pre> <pre class="prettyprint lang-js">http://micuer.com/data/upload/application.rar </pre> <pre class="prettyprint lang-js"> </pre> <pre class="prettyprint lang-js"> </pre> </b><br /> </b></span></pre> <p> <br /> </p>
注:原创不易,转载请注明出处(
https://micuu.com/new/290.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-02-22
上一篇:
虚拟主机如何建立多个子网站,每个网站在子目录下?
下一篇:
搞笑段子背景音乐,配音合集
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>