Browse Source

充值微信支付

master
ZLW-PC\Administrator 7 years ago
parent
commit
96a0e234d0
  1. 42
      app/Http/Controllers/Weixin/UserController.php
  2. 21
      app/Http/Controllers/Weixin/WxPayController.php
  3. 2
      app/Http/Model/User.php
  4. 87
      resources/org/wxpay/WxPayConfig.php
  5. 33
      resources/org/wxpay/WxPayPubHelper.class.php
  6. 0
      resources/org/wxpay/微信支付demo.html
  7. 22
      resources/views/weixin/index/index.blade.php
  8. 2
      resources/views/weixin/user/userRecharge.blade.php
  9. 71
      resources/views/weixin/user/userRechargeOrderDetail.blade.php
  10. 91
      resources/views/weixin/user/userRechargeTwo.blade.php
  11. 5
      routes/web.php

42
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);
}
//余额明细

21
app/Http/Controllers/Weixin/WxPayController.php

@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers\Weixin;
use App\Http\Controllers\Weixin\CommonController;
use Illuminate\Http\Request;
class WxPayController extends CommonController
{
public function __construct()
{
parent::__construct();
}
//微信支付回调
public function wxpayNotify(Request $request)
{
require_once(resource_path('org/wxpay/WxPayPubHelper.class.php'));
return view('weixin.cart.index', $data);
}
}

2
app/Http/Model/User.php

@ -199,7 +199,7 @@ class User extends BaseModel
{
extract($param); //参数
if(isset($openid))
if(isset($openid) && !empty($openid))
{
$user = self::where(array('openid'=>$openid))->first();
}

87
resources/org/wxpay/WxPayConfig.php

@ -0,0 +1,87 @@
<?php
/**
* 微信支付配置账号信息
*/
class WxPayConfig
{
//=======【基本信息设置】=====================================
//
/**
* TODO: 修改这里配置为您自己申请的商户信息
* 微信公众号信息配置
*
* APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
*
* MCHID:商户号(必须配置,开户邮件中可查看)
*
* KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
* 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
*
* APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
* 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
* @var string
*/
const APPID = 'wx1c7946b5734199d0';
const MCHID = '1331184301';
const KEY = '93aa64d6552bf09401af7e7e6f9b3be7';
const APPSECRET = '93aa64d6552bf09401af7e7e6f9b3be7';
//=======【证书路径设置】=====================================
/**
* TODO:设置商户证书路径
* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要,可登录商户平台下载,
* API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书)
* @var path
*/
const SSLCERT_PATH = '../cert/apiclient_cert.pem';
const SSLKEY_PATH = '../cert/apiclient_key.pem';
//=======【curl代理设置】===================================
/**
* TODO:这里设置代理机器,只有需要代理的时候才设置,不需要代理,请设置为0.0.0.0和0
* 本例程通过curl使用HTTP POST方法,此处可修改代理服务器,
* 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置)
* @var unknown_type
*/
const CURL_PROXY_HOST = "0.0.0.0";//"10.152.18.220";
const CURL_PROXY_PORT = 0;//8080;
//=======【上报信息配置】===================================
/**
* TODO:接口调用上报等级,默认紧错误上报(注意:上报超时间为【1s】,上报无论成败【永不抛出异常】,
* 不会影响接口调用流程),开启上报之后,方便微信监控请求调用的质量,建议至少
* 开启错误上报。
* 上报等级,0.关闭上报; 1.仅错误出错上报; 2.全量上报
* @var int
*/
const REPORT_LEVENL = 1;
public static function wxconfig()
{
//=======【基本信息设置】=====================================
//微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
$wxconfig['APPID'] = self::APPID;
//受理商ID,身份标识
$wxconfig['MCHID'] = self::MCHID;
//商户支付密钥Key。审核通过后,在微信发送的邮件中查看
$wxconfig['KEY'] = self::KEY;
//JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
$wxconfig['APPSECRET'] = self::APPSECRET;
//=======【证书路径设置】=====================================
//证书路径,注意应该填写绝对路径
$wxconfig['SSLCERT_PATH'] = self::SSLCERT_PATH;
$wxconfig['SSLKEY_PATH'] = self::SSLKEY_PATH;
//=======【异步通知url设置】===================================
//异步通知url,商户根据实际开发过程设定
//$wxconfig['NOTIFY_URL'] = 'http://'.$_SERVER['HTTP_HOST'].U('Wxpay/notify_url'); //放在下单时,添加
//=======【curl超时设置】===================================
//本例程通过curl使用HTTP POST方法,此处可修改其超时时间,默认为30秒
$wxconfig['CURL_TIMEOUT'] = 30;
return $wxconfig;
}
}

33
resources/org/wxpay/WxPayPubHelper.class.php

@ -820,7 +820,7 @@ class JsApi_pub extends Common_util_pub
/**
* 作用:通过curl向微信提交code,以获取openid
*/
function getOpenid()
function GetOpenidFromMp()
{
$url = $this->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

0
resources/org/wxpay/wxt.html → resources/org/wxpay/微信支付demo.html

22
resources/views/weixin/index/index.blade.php

@ -85,35 +85,35 @@ var swiper = new Swiper('.swiper-nav', {
<div class="floor home_menu">
<nav>
<a href="/index.php/mobile/Goods/categoryList.html">
<img src="images/weixin/icon_03.png" alt="全部分类" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_03.png" alt="全部分类" />
<span>全部分类</span>
</a>
<a href="http://www.shop.com/index.php/mobile/Goods/integralMall.html">
<img src="images/weixin/icon_05.png" alt="积分商城" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_05.png" alt="积分商城" />
<span>积分商城</span>
</a>
<a href="http://www.shop.com/index.php/mobile/Goods/brandstreet.html">
<img src="images/weixin/icon_07.png" alt="品牌街" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_07.png" alt="品牌街" />
<span>品牌街</span>
</a>
<a href="http://www.shop.com/index.php/mobile/Activity/promote_goods.html">
<img src="images/weixin/icon_09.png" alt="优惠活动" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_09.png" alt="优惠活动" />
<span>优惠活动</span>
</a>
<a href="http://www.shop.com/index.php/mobile/Activity/group_list.html">
<img src="images/weixin/icon_15.png" alt="团购" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_15.png" alt="团购" />
<span>团购</span>
</a>
<a href="http://www.shop.com/index.php/mobile/User/order_list.html">
<img src="images/weixin/icon_16.png" alt="我的订单" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_16.png" alt="我的订单" />
<span>我的订单</span>
</a>
<a href="<?php echo route('weixin_cart'); ?>">
<img src="images/weixin/icon_17.png" alt="购物车" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_17.png" alt="购物车" />
<span>购物车</span>
</a>
<a href="<?php echo route('weixin_user'); ?>">
<img src="images/weixin/icon_19.png" alt="个人中心" />
<img src="<?php echo env('APP_URL'); ?>/images/weixin/icon_19.png" alt="个人中心" />
<span>个人中心</span>
</a>
</nav>
@ -123,7 +123,7 @@ var swiper = new Swiper('.swiper-nav', {
<!--资讯头条-start-->
<div class="bggrey">
<div class="home_toutiao">
<div class="home_toutiao_tit"><img src="images/weixin/ad_tit.png"></div>
<div class="home_toutiao_tit"><img src="<?php echo env('APP_URL'); ?>/images/weixin/ad_tit.png"></div>
<div class="home_toutiao_box">
<ul>
<marquee id="mar1" scrollamount="1" direction="up" height="30" style="height: 30px;">
@ -137,7 +137,7 @@ var swiper = new Swiper('.swiper-nav', {
<!--猜您喜欢-start-->
<div class="floor guesslike">
<div class="banner_tit"><img src="images/weixin/ind_52.jpg" alt="猜您喜欢"/></div>
<div class="banner_tit"><img src="<?php echo env('APP_URL'); ?>/images/weixin/ind_52.jpg" alt="猜您喜欢"/></div>
<div class="likeshop">
<ul class="goods_list" id="goods_list">
<?php if($goods_list){foreach($goods_list as $k=>$v){ ?>
@ -150,7 +150,7 @@ var swiper = new Swiper('.swiper-nav', {
<!--猜您喜欢-start-->
<div class="floor guesslike">
<div class="banner_tit"><img src="images/weixin/ind_52.jpg" alt="猜您喜欢"/></div>
<div class="banner_tit"><img src="<?php echo env('APP_URL'); ?>/images/weixin/ind_52.jpg" alt="猜您喜欢"/></div>
<ul class="goods_list_s cl">
<?php if($goods_list){foreach($goods_list as $k=>$v){ ?>
<a href="<?php echo $v['goods_detail_url']; ?>"><li><span class="goods_thumb"><img alt="<?php echo $v['title']; ?>" src="<?php echo $v['litpic']; ?>"></span>

2
resources/views/weixin/user/userRecharge.blade.php

@ -77,7 +77,7 @@ function chongzhi()
if(res.code==0)
{
location.href = '<?php echo substr(route('weixin_user_recharge_two',array('id'=>1)), 0, -1); ?>' + res.data;
location.href = '<?php echo substr(route('weixin_user_recharge_order_detail',array('id'=>1)), 0, -1); ?>' + res.data;
}
else
{

71
resources/views/weixin/user/userRechargeOrderDetail.blade.php

@ -0,0 +1,71 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>充值-支付</title><meta name="keywords" content="关键词"><meta name="description" content="描述"><meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<link href="<?php echo env('APP_URL'); ?>/css/weixin/style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/weixin/mobile.js"></script>
<link href="<?php echo env('APP_URL'); ?>/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<script>
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsApiParameters},
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_code+res.err_desc+res.err_msg);
if(res.err_msg=='get_brand_wcpay_request:ok')
{
alert('支付成功!');
}
else
{
alert('支付失败!');
}
setTimeout("location.href = '{$returnUrl}'",2000);
}
);
}
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>
</head><body onload="callpay();">
<div class="classreturn loginsignup">
<div class="ds-in-bl return"><a href="javascript:history.back(-1);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/return.png" alt="返回"></a></div>
<div class="ds-in-bl tit center"><span>充值</span></div>
</div>
<style>
.account{text-align:center;margin-top:30px;}
.account .icon{color:#FFCC00;font-size:100px;}
.account .tit{color:#000;font-size:18px;}
.bottoma{display:block;font-size:18px;padding:10px;border-radius:2px;}
</style>
<div class="floor account">
<div class="icon"><i class="fa fa-google-wallet"></i></div>
<div style="margin:10px;text-align:left;">
<p style="color:#7abd65;">订单已于 <b style="color:#fea700;"><?php echo $post['created_at']; ?></b> 提交成功,请您尽快付款!</p>
订单号:<?php echo $post['id']; ?><br>
应付金额:<strong style="color:#D03737;"><?php echo $post['money']; ?></strong> 元<br><br>
<p style="color:#999;font-size:.875em">请您在提交订单后30分钟内完成支付,否则订单会自动取消。</p>
</div>
<a style="margin:0 10px 10px 10px;background-color:#1aad19;text-align:center;color:#fff;border:1px solid #179e16;" class="bottoma" href="javascript:chongzhi();">去支付</a>
</div>
</body></html>

91
resources/views/weixin/user/userRechargeTwo.blade.php

@ -1,91 +0,0 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>充值-支付</title><meta name="keywords" content="关键词"><meta name="description" content="描述"><meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<link href="<?php echo env('APP_URL'); ?>/css/weixin/style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/weixin/mobile.js"></script>
<link href="<?php echo env('APP_URL'); ?>/css/font-awesome.min.css" type="text/css" rel="stylesheet"></head><body>
<div class="classreturn loginsignup">
<div class="ds-in-bl return"><a href="javascript:history.back(-1);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/return.png" alt="返回"></a></div>
<div class="ds-in-bl tit center"><span>充值</span></div>
</div>
<style>
.account{text-align:center;margin-top:30px;}
.account .icon{color:#FFCC00;font-size:100px;}
.account .tit{color:#000;font-size:18px;}
.bottoma{display:block;font-size:18px;padding:10px;border-radius:2px;}
</style>
<div class="floor account">
<div class="icon"><i class="fa fa-google-wallet"></i></div>
<div style="margin:10px;padding:10px;text-align:left;">
订单已于 <span style="color:#390;"><?php echo $post['created_at']; ?></span> 提交成功,请您尽快付款!<br>
订单号:<?php echo $post['id']; ?><br>
应付金额:<span style="color:#ff5500;font-size:18px;"><?php echo $post['money']; ?></span> 元<br><br>
请您在提交订单后30分钟内完成支付,否则订单会自动取消。<br><br>
</div>
<a style="margin:0 10px 10px 10px;background-color:#1aad19;text-align:center;color:#fff;border:1px solid #179e16;" class="bottoma" href="javascript:chongzhi();">去支付</a>
</div>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/layer/mobile/layer.js"></script>
<script>
function chongzhi()
{
var money = $('#money').val();
var re = /^[0-9]+$/; //判断字符串是否为数字
if(money == '')
{
//提示
layer.open({
content: '请输入充值金额'
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
return false;
}
if(!re.test(money))
{
//提示
layer.open({
content: '金额格式不正确'
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
return false;
}
//询问框
layer.open({
content: '确定要充值吗?'
,btn: ['确定', '取消']
,yes: function(){
var url = '<?php echo env('APP_API_URL')."/user_recharge_add"; ?>';
var pay_type = $('#pay_type').val();
$.post(url,{access_token:'<?php echo $_SESSION['weixin_user_info']['access_token']; ?>',money:money,pay_type:pay_type},function(res)
{
//提示
layer.open({
content: res.msg
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
if(res.code==0)
{
location.href = '<?php echo substr(route('weixin_user_recharge_two',array('id'=>1)), 0, -1); ?>' + res.data;
}
else
{
}
},'json');
}
});
}
</script>
</body></html>

5
routes/web.php

@ -64,6 +64,7 @@ Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin'], function () {
Route::get('/goods/{id}', 'GoodsController@goodsDetail')->name('weixin_goods_detail'); //商品详情页
Route::get('/goodslist', 'GoodsController@goodsList')->name('weixin_goods_list'); //产品分类页
Route::any('/wxpay_notify', 'WxPayController@wxpayNotify')->name('weixin_wxpay_notify'); //微信回调
Route::any('/wxoauth', 'UserController@oauth')->name('weixin_wxoauth'); //微信网页授权
Route::any('/login', 'UserController@login')->name('weixin_login');
Route::any('/register', 'UserController@register')->name('weixin_register');
@ -83,8 +84,10 @@ Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin', 'middleware' => ['w
Route::get('/user_account', 'UserController@userAccount')->name('weixin_user_account');
Route::get('/user_money_list', 'UserController@userMoneyList')->name('weixin_user_money_list');
Route::get('/user_point_list', 'UserController@userPointList')->name('weixin_user_point_list');
//用户充值
Route::get('/user_recharge', 'UserController@userRecharge')->name('weixin_user_recharge');
Route::get('/user_recharge_two/{id}', 'UserController@userRechargeTwo')->name('weixin_user_recharge_two');
Route::get('/user_recharge_order', 'UserController@userRechargeOrder')->name('weixin_user_recharge_order');
Route::get('/user_recharge_order_detail/{id}', 'UserController@userRechargeOrderDetail')->name('weixin_user_recharge_order_detail');
//优惠券、红包
Route::get('/user_bonus_list', 'UserController@userBonusList')->name('weixin_user_bonus_list');
//浏览记录

Loading…
Cancel
Save