From 96a0e234d0cbeafdc95836f788a8416e7849171d Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Wed, 8 Nov 2017 17:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=85=E5=80=BC=E5=BE=AE=E4=BF=A1=E6=94=AF?= =?UTF-8?q?=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Weixin/UserController.php | 42 ++++++++- .../Controllers/Weixin/WxPayController.php | 21 +++++ app/Http/Model/User.php | 2 +- resources/org/wxpay/WxPayConfig.php | 87 ++++++++++++++++++ resources/org/wxpay/WxPayPubHelper.class.php | 33 ++++++- .../wxpay/{wxt.html => 微信支付demo.html} | 0 resources/views/weixin/index/index.blade.php | 22 ++--- .../views/weixin/user/userRecharge.blade.php | 2 +- .../user/userRechargeOrderDetail.blade.php | 71 +++++++++++++++ .../weixin/user/userRechargeTwo.blade.php | 91 ------------------- routes/web.php | 5 +- 11 files changed, 268 insertions(+), 108 deletions(-) create mode 100644 app/Http/Controllers/Weixin/WxPayController.php create mode 100644 resources/org/wxpay/WxPayConfig.php rename resources/org/wxpay/{wxt.html => 微信支付demo.html} (100%) create mode 100644 resources/views/weixin/user/userRechargeOrderDetail.blade.php delete mode 100644 resources/views/weixin/user/userRechargeTwo.blade.php diff --git a/app/Http/Controllers/Weixin/UserController.php b/app/Http/Controllers/Weixin/UserController.php index a11876f..72f331a 100644 --- a/app/Http/Controllers/Weixin/UserController.php +++ b/app/Http/Controllers/Weixin/UserController.php @@ -63,8 +63,9 @@ class UserController extends CommonController } //用户充值第二步,支付 - public function userRechargeTwo($id) + public function userRechargeOrderDetail($id) { + //获取充值记录详情 $postdata = array( 'id' => $id, 'access_token' => $_SESSION['weixin_user_info']['access_token'] @@ -73,7 +74,44 @@ class UserController extends CommonController $res = curl_request($url,$postdata,'GET'); $data['post'] = $res['data']; - return view('weixin.user.userRechargeTwo', $data); + //微信支付-start + require_once(resource_path('org/wxpay/WxPayConfig.php')); // 导入微信配置类 + require_once(resource_path('org/wxpay/WxPayPubHelper.class.php')); // 导入微信支付类 + + $body = '商品购买';//订单详情 + $out_trade_no = '2017787878';//订单号 + $total_fee = floatval(0.01*100);//价格0.01 + $notify_url = route('weixin_wxpay_notify');//通知地址 + $wxconfig= \WxPayConfig::wxconfig(); + + //=========步骤1:网页授权获取用户openid============ + $jsApi = new \JsApi_pub($wxconfig); + $openid = $jsApi->getOpenid(); + //=========步骤2:使用统一支付接口,获取prepay_id============ + //使用统一支付接口 + $unifiedOrder = new \UnifiedOrder_pub($wxconfig); + //设置统一支付接口参数 + //设置必填参数 + //appid已填,商户无需重复填写 + //mch_id已填,商户无需重复填写 + //noncestr已填,商户无需重复填写 + //spbill_create_ip已填,商户无需重复填写 + //sign已填,商户无需重复填写 + $unifiedOrder->setParameter("openid","$openid");//微信用户 + $unifiedOrder->setParameter("body","$body");//商品描述 + $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 + $unifiedOrder->setParameter("total_fee","$total_fee");//总金额 + $unifiedOrder->setParameter("notify_url","$notify_url");//通知地址 + $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型 + //$unifiedOrder->setParameter("attach","test"); //附加数据,选填,在查询API和支付通知中原样返回,可作为自定义参数使用 + $prepay_id = $unifiedOrder->getPrepayId(); + //=========步骤3:使用jsapi调起支付============ + $jsApi->setPrepayId($prepay_id); + $jsApiParameters = $jsApi->getParameters(); + + $data['jsApiParameters'] = $jsApiParameters; + $data['returnUrl'] = route('weixin_user'); //支付完成要跳转的url + return view('weixin.user.userRechargeOrderDetail', $data); } //余额明细 diff --git a/app/Http/Controllers/Weixin/WxPayController.php b/app/Http/Controllers/Weixin/WxPayController.php new file mode 100644 index 0000000..a102b09 --- /dev/null +++ b/app/Http/Controllers/Weixin/WxPayController.php @@ -0,0 +1,21 @@ +$openid))->first(); } diff --git a/resources/org/wxpay/WxPayConfig.php b/resources/org/wxpay/WxPayConfig.php new file mode 100644 index 0000000..d5910f0 --- /dev/null +++ b/resources/org/wxpay/WxPayConfig.php @@ -0,0 +1,87 @@ +createOauthUrlForOpenid(); //初始化curl @@ -838,8 +838,39 @@ class JsApi_pub extends Common_util_pub //取出openid $data = json_decode($res,true); $this->openid = $data['openid']; + return $this->openid; } + + /** + * + * 通过跳转获取用户的openid,跳转流程如下: + * 1、设置自己需要调回的url及其其他参数,跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize + * 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code + * + * @return 用户的openid + */ + function getOpenid() + { + //通过code获得openid + if (!isset($_GET['code'])) + { + //触发微信返回code码 + $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']); + $url = $this->createOauthUrlForCode($baseUrl); + Header("Location: $url"); + exit(); + } + else + { + //获取code码,以获取openid + $code = $_GET['code']; + $this->setCode($code); + $openid = $this->GetOpenidFromMp(); + + return $openid; + } + } /** * 作用:设置prepay_id diff --git a/resources/org/wxpay/wxt.html b/resources/org/wxpay/微信支付demo.html similarity index 100% rename from resources/org/wxpay/wxt.html rename to resources/org/wxpay/微信支付demo.html diff --git a/resources/views/weixin/index/index.blade.php b/resources/views/weixin/index/index.blade.php index 88fa49b..1f8ea68 100644 --- a/resources/views/weixin/index/index.blade.php +++ b/resources/views/weixin/index/index.blade.php @@ -85,35 +85,35 @@ var swiper = new Swiper('.swiper-nav', {