企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
1. 在上节的基础上做修改,在上节源码中,根目录下在Shop/View文件夹下的order.html中的name改为username ![](https://box.kancloud.cn/218faee67c186550856b181c7d2a41dc_350x67.png) 2. 将更改根目录下在Shop/Controller文件夹下的IndexController.class.php为 ~~~ <?php namespace Shop\Controller; use Think\Controller; use Com\Wechat; use Com\WechatAuth; class IndexController extends Controller { public function index(){ $m=M('goods'); $data=$m->select(); $this->assign('data',$data); $this->display(); } public function detail(){ $m=M('goods'); $data=$m->find($_GET['id']); $this->assign('data',$data); $this->display(); } public function order(){ $this->assign('data',$_POST); $this->display(); } public function addOrder(){ header('Content-type:text/html;charset=utf-8'); if(empty($_GET['isnull'])){ header('Location:http://zyw.gzzysd.com/wxpay/example/jsapi.php'); $data['gid']=$_POST['id']; $data['name']=$_POST['name']; $data['username']=$_POST['username']; $data['pirce']=$_POST['pirce']*$_POST['num']; $data['num']=$_POST['num']; $data['email']=$_POST['email']; $data['mobile']=$_POST['mobile']; $data['city']=$_POST['city']; $data['detail']=$_POST['detail']; session('data',$data); }else{ $m=M('orders'); $res=$m->add(session('data')); if($res){ echo '<script>alert("生成订单成功");location.href="http://zyw.gzzysd.com/index.php/Shop/Index";</script>'; }else{ echo '<script>alert("生成订单失败");history.go(-2)</script>'; } } } } ~~~ 3. 将根目录下的wxpay/example/jsapi.php更改为 ~~~ <?php session_start();//*** header('Content-type:text/html;charset=utf-8'); ini_set('date.timezone','Asia/Shanghai'); error_reporting(E_ERROR); require_once "../lib/WxPay.Api.php"; require_once "WxPay.JsApiPay.php"; require_once 'log.php'; //初始化日志 $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log'); $log = Log::Init($logHandler, 15); //打印输出数组信息//*** /*function printf_info($data) { foreach($data as $key=>$value){ echo "<font color='#00ff55;'>$key</font> : $value <br/>"; } }*/ //①、获取用户openid $tools = new JsApiPay(); $openId = $tools->GetOpenid(); //②、统一下单 $input = new WxPayUnifiedOrder(); $input->SetBody($_SESSION['data']['name']);//商品名称 $input->SetAttach($_SESSION['data']['name']);//附加参数 $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//订单号 $input->SetTotal_fee($_SESSION['data']['pirce']*100);//支付金额 $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://zyw.gzzysd.com/wxpay/example/notify.php");//支付验证链接 $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input); //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';//*** //printf_info($order);//*** $jsApiParameters = $tools->GetJsApiParameters($order); //获取共享收货地址js函数参数 $editAddress = $tools->GetEditAddressParameters(); //③、在支持成功回调通知中处理成功之后的事宜,见 notify.php /** * 注意: * 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认支付是否成功 * 2、jsapi支付时需要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信公众平台“网页授权接口”, * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html) */ ?> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>微信支付样例-支付</title> <script type="text/javascript"> //调用微信JS api 支付 callpay();//**** function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $jsApiParameters; ?>, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert('支付成功'); location.href="http://zyw.gzzysd.com/index.php/Shop/Index/addOrder/isnull/1"; }else{ alert('支付失败'); location.href="http://zyw.gzzysd.com/index.php/Shop/Index/detail/id/<?php echo $_SESSION['data']['gid'];?>"; } } ); } function callpay() { 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(); } } </script> <script type="text/javascript"> //获取共享地址 // function editAddress() // { // WeixinJSBridge.invoke( // 'editAddress', // <?php echo $editAddress; ?>, // function(res){ // var value1 = res.proviceFirstStageName; // var value2 = res.addressCitySecondStageName; // var value3 = res.addressCountiesThirdStageName; // var value4 = res.addressDetailInfo; // var tel = res.telNumber; // alert(value1 + value2 + value3 + value4 + ":" + tel); // } // ); // } // window.onload = function(){ // if (typeof WeixinJSBridge == "undefined"){ // if( document.addEventListener ){ // document.addEventListener('WeixinJSBridgeReady', editAddress, false); // }else if (document.attachEvent){ // document.attachEvent('WeixinJSBridgeReady', editAddress); // document.attachEvent('onWeixinJSBridgeReady', editAddress); // } // }else{ // editAddress(); // } // }; </script> </head> <body> <!-- <br/> <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px">1分</span>钱</b></font><br/><br/> <div align="center"> <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onClick="callpay()" >立即支付</button> </div>--> </body> </html> ~~~ 4. 打开域名/Shop/index即可测试,展示结果为 ![](https://box.kancloud.cn/5fb5cf27b4ca86d17cce5abe5bd84261_419x745.png) ![](https://box.kancloud.cn/332f594515152c91287984319fa856bc_401x765.png) 5. 本节源码下载(下载密码:hron)点击[下载](https://pan.baidu.com/s/1Gvydmn1jencWMaICvsloOg)