From 2a190a0b78eec8d9c881f5e919c90c0b074d4a91 Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Tue, 22 May 2018 16:44:14 +0800 Subject: [PATCH] order --- app/Common/function.php | 10 +-- app/Http/Controllers/Api/UserController.php | 1 + .../Api/UserRechargeController.php | 3 +- app/Http/Controllers/Home/IndexController.php | 21 ++++++ .../Controllers/Weixin/OrderController.php | 4 +- .../Controllers/Weixin/UserController.php | 4 +- .../Controllers/Weixin/WxPayController.php | 71 +++++++++++------- app/Http/Model/User.php | 1 + resources/org/wxpay/WxPayConfig.php | 2 +- resources/org/wxpay/cert/apiclient_cert.p12 | Bin 0 -> 2876 bytes resources/org/wxpay/cert/apiclient_cert.pem | 48 ++++++------ resources/org/wxpay/cert/apiclient_key.pem | 52 ++++++------- .../org/wxpay/cert/证书使用说明.txt | 18 +++++ .../views/weixin/order/orderWxpay.blade.php | 8 +- resources/views/weixin/user/index.blade.php | 6 +- .../weixin/user/userRechargeOrder.blade.php | 4 +- .../user/userRechargeOrderDetail.blade.php | 8 +- routes/web.php | 7 +- 18 files changed, 164 insertions(+), 104 deletions(-) create mode 100644 resources/org/wxpay/cert/apiclient_cert.p12 create mode 100644 resources/org/wxpay/cert/证书使用说明.txt diff --git a/app/Common/function.php b/app/Common/function.php index d30b44e..d77dcf6 100644 --- a/app/Common/function.php +++ b/app/Common/function.php @@ -33,11 +33,11 @@ if (! function_exists('curl_request')) curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_HEADER, 0); - + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - + $response = curl_exec($curl); - + if ($response === FALSE) { $error = curl_error($curl); @@ -50,9 +50,9 @@ if (! function_exists('curl_request')) $response = trim($response,chr(239).chr(187).chr(191)); $response = json_decode($response, true); } - + curl_close($curl); - + return $response; } } diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 4b86e32..1396f26 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -218,6 +218,7 @@ class UserController extends CommonController return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在'); } + $data['add_time'] = time(); //添加用户 $res = User::wxRegister($data); diff --git a/app/Http/Controllers/Api/UserRechargeController.php b/app/Http/Controllers/Api/UserRechargeController.php index 6c3e71e..8719de1 100644 --- a/app/Http/Controllers/Api/UserRechargeController.php +++ b/app/Http/Controllers/Api/UserRechargeController.php @@ -61,6 +61,7 @@ class UserRechargeController extends CommonController $data['status'] = UserRecharge::UN_PAY; //0未处理,1已完成 $data['pay_type'] = $request->input('pay_type',''); //充值类型:1微信,2支付宝 $data['user_id'] = Token::$uid; + $data['created_at'] = time(); if($data['money']=='' || $data['pay_type']=='') { @@ -84,7 +85,7 @@ class UserRechargeController extends CommonController $data['trade_no'] = $request->input('trade_no',''); $data['pay_time'] = $request->input('pay_time',''); $data['status'] = UserRecharge::COMPLETE_PAY; - $data['updated_at'] = date('Y-m-d H:i:s',time()); + $data['updated_at'] = time(); if($id=='' || $data['trade_no']=='' || $data['pay_time']=='') { diff --git a/app/Http/Controllers/Home/IndexController.php b/app/Http/Controllers/Home/IndexController.php index 03e5a51..46c81b6 100644 --- a/app/Http/Controllers/Home/IndexController.php +++ b/app/Http/Controllers/Home/IndexController.php @@ -457,6 +457,27 @@ class IndexController extends CommonController return view('home.404'); } + //验证消息的确来自微信服务器 + public function checksignature() + { + $signature = $_GET["signature"]; + $timestamp = $_GET["timestamp"]; + $nonce = $_GET["nonce"]; + $echoStr = $_GET["echostr"]; + + $token = 'fanli'; + $tmpArr = array($token, $timestamp, $nonce); + sort($tmpArr, SORT_STRING); + $tmpStr = implode( $tmpArr ); + $tmpStr = sha1( $tmpStr ); + + if( $tmpStr == $signature ){ + exit($echoStr); + }else{ + return false; + } + } + //测试页面 public function test() {return view('home.index.test'); diff --git a/app/Http/Controllers/Weixin/OrderController.php b/app/Http/Controllers/Weixin/OrderController.php index 6c2afb1..6c802a1 100644 --- a/app/Http/Controllers/Weixin/OrderController.php +++ b/app/Http/Controllers/Weixin/OrderController.php @@ -237,7 +237,7 @@ class OrderController extends CommonController $this->success_jump('支付成功',route('weixin_order_list')); } - //订单余额支付 + //订单-微信支付 public function orderWxpay(Request $request) { $order_id = $request->input('order_id',''); @@ -262,7 +262,7 @@ class OrderController extends CommonController $body = '订单支付';//订单详情 $out_trade_no = $data['order_detail']['order_sn'];//订单号 $total_fee = floatval($data['order_detail']['order_amount']*100);//价格0.01 - $attach = 'pay_type=2'; //pay_type=2订单支付 + $attach = 'pay_type=2'; //附加数据,pay_type=2订单支付,示例:xxx=1&yyy=2 $notify_url = route('weixin_wxpay_notify');//通知地址 $wxconfig= \WxPayConfig::wxconfig(); diff --git a/app/Http/Controllers/Weixin/UserController.php b/app/Http/Controllers/Weixin/UserController.php index d49fb9e..ca0184d 100644 --- a/app/Http/Controllers/Weixin/UserController.php +++ b/app/Http/Controllers/Weixin/UserController.php @@ -203,7 +203,7 @@ class UserController extends CommonController $html .= '
  • '; $html .= '+ '.$v['money'].''; $html .= '

    充值

    '; - $html .= '

    '.$v['created_at'].'

    '; + $html .= '

    '.date('Y-m-d H:i:s', $v['created_at']).'

    '; $html .= '
  • '; } } @@ -236,7 +236,7 @@ class UserController extends CommonController $body = '充值';//订单详情 $out_trade_no = $user_recharge['recharge_sn'];//订单号 $total_fee = floatval($user_recharge['money']*100);//价格0.01 - $attach = 'pay_type=1'; //pay_type=1充值支付 + $attach = 'pay_type=1'; //附加数据,pay_type=1充值支付,示例:xxx=1&yyy=2 $notify_url = route('weixin_wxpay_notify');//通知地址 $wxconfig= \WxPayConfig::wxconfig(); diff --git a/app/Http/Controllers/Weixin/WxPayController.php b/app/Http/Controllers/Weixin/WxPayController.php index f67f9b1..2feca89 100644 --- a/app/Http/Controllers/Weixin/WxPayController.php +++ b/app/Http/Controllers/Weixin/WxPayController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Weixin; use App\Http\Controllers\Weixin\CommonController; use Illuminate\Http\Request; use DB; +use Log; class WxPayController extends CommonController { @@ -12,77 +13,91 @@ class WxPayController extends CommonController parent::__construct(); } - //微信支付回调 + /** + * 微信支付回调 + */ public function wxpayNotify(Request $request) { - file_put_contents("1.txt",$GLOBALS['HTTP_RAW_POST_DATA']); + $res = "SUCCESS"; //支付成功返回SUCCESS,失败返回FAILE + + //file_put_contents("1.txt",$GLOBALS['HTTP_RAW_POST_DATA']); + Log::info('微信支付回调数据:'.$GLOBALS['HTTP_RAW_POST_DATA']); + //获取通知的数据 $xml = $GLOBALS['HTTP_RAW_POST_DATA']; //将XML转为array //禁止引用外部xml实体 libxml_disable_entity_loader(true); $post_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); - $get_arr = explode('&',$post_data['attach']); - foreach($get_arr as $value) + + if(isset($post_data['attach']) && !empty($post_data['attach'])) { - $tmp_arr = explode('=',$value); - $post_data[$tmp_arr[0]] = $tmp_arr[1]; - } + $get_arr = explode('&',$post_data['attach']); + foreach($get_arr as $value) + { + $tmp_arr = explode('=',$value); + $post_data[$tmp_arr[0]] = $tmp_arr[1]; + } + } if($post_data['result_code'] == 'SUCCESS') { - $pay_money = $post_data['total_fee']/100; - $pay_time_timestamp = strtotime(date_format(date_create($post_data['time_end']),"Y-m-d H:i:s")); - //$post_data['out_trade_no'] - //$post_data['transaction_id'] + $pay_money = $post_data['total_fee']/100; //支付金额 + $pay_time_timestamp = strtotime(date_format(date_create($post_data['time_end']),"Y-m-d H:i:s")); //支付完成时间,时间戳格式 + $pay_time_date = date_format(date_create($post_data['time_end']),"Y-m-d H:i:s"); //支付完成时间,date格式Y-m-d H:i:s + //$post_data['out_trade_no'] //商户订单号 + //$post_data['transaction_id'] //微信支付订单号 //附加参数pay_type:1充值支付,2订单支付 if($post_data['pay_type'] == 1) { //获取充值支付记录 $user_recharge = DB::table('user_recharge')->where(array('recharge_sn'=>$post_data['out_trade_no'],'status'=>0))->first(); - if(!$user_recharge){exit;} - if($pay_money < $user_recharge->money){exit;} //如果支付金额小于要充值的金额 + if(!$user_recharge){Log::info('充值记录不存在');echo "FAILE";exit;} + if($pay_money < $user_recharge->money){Log::info('充值金额不匹配');echo "FAILE";exit;} //如果支付金额小于要充值的金额 //更新充值支付记录状态 - DB::table('user_recharge')->where(array('recharge_sn'=>$post_data['out_trade_no'],'status'=>0))->update(array('pay_time'=>$pay_time_timestamp,'pay_type'=>1,'status'=>1,'trade_no'=>$post_data['transaction_id'],'pay_money'=>$pay_money)); + DB::table('user_recharge')->where(array('recharge_sn'=>$post_data['out_trade_no'],'status'=>0))->update(array('pay_time'=>$pay_time_timestamp,'updated_at'=>time(),'pay_type'=>1,'status'=>1,'trade_no'=>$post_data['transaction_id'],'pay_money'=>$pay_money)); //增加用户余额 DB::table('user')->where(array('id'=>$user_recharge->user_id))->increment('money', $pay_money); //添加用户余额记录 DB::table('user_money')->insert(array('user_id'=>$user_recharge->user_id,'type'=>0,'money'=>$pay_money,'des'=>'充值','user_money'=>DB::table('user')->where(array('id'=>$user_recharge->user_id))->value('money'),'add_time'=>time())); } - if($post_data['pay_type'] == 2) + elseif($post_data['pay_type'] == 2) { //获取订单记录 $order = DB::table('order')->where(array('order_sn'=>$post_data['out_trade_no'],'order_status'=>0,'pay_status'=>0))->first(); - if(!$order){exit;} - if($pay_money < $order->order_amount){exit;} //如果支付金额小于订单金额 + if(!$order){Log::info('订单不存在');echo "FAILE";exit;} + if($pay_money < $order->order_amount){Log::info('订单金额不匹配');exit;} //如果支付金额小于订单金额 //修改订单状态 $order_update_data['pay_status'] = 1; $order_update_data['pay_money'] = $pay_money; //支付金额 $order_update_data['pay_id'] = 2; $order_update_data['pay_time'] = $pay_time_timestamp; - $order_update_data['pay_name'] = '微信'; + $order_update_data['pay_name'] = 'wxpay_jsapi'; $order_update_data['out_trade_no'] = $post_data['transaction_id']; + $order_update_data['updated_at'] = time(); + DB::table('order')->where(array('order_sn'=>$post_data['out_trade_no'],'order_status'=>0,'pay_status'=>0))->update($order_update_data); } - if($post_data['pay_type'] == 3) + elseif($post_data['pay_type'] == 3) { - + $res = "FAILE"; } - if($post_data['pay_type'] == 4) + elseif($post_data['pay_type'] == 4) { - + $res = "FAILE"; + } + else + { + $res = "FAILE"; } - file_put_contents("2.txt",$post_data['total_fee'].'--'.$post_data['out_trade_no'].'--'.$post_data['attach'].'--'.$post_data['pay_type']); - echo "SUCCESS"; - } - else - { - echo "FAILE"; + //file_put_contents("2.txt",$post_data['total_fee'].'--'.$post_data['out_trade_no'].'--'.$post_data['attach'].'--'.$post_data['pay_type']); } + + echo $res; } /** diff --git a/app/Http/Model/User.php b/app/Http/Model/User.php index cb0ce3b..cd8ff7e 100644 --- a/app/Http/Model/User.php +++ b/app/Http/Model/User.php @@ -188,6 +188,7 @@ class User extends BaseModel if(isset($sex)){$data['sex'] = $sex;} if(isset($head_img)){$data['head_img'] = $head_img;} if(isset($nickname)){$data['nickname'] = $nickname;} + $data['add_time'] = time(); if (isset($data) && $id = self::add($data)) { diff --git a/resources/org/wxpay/WxPayConfig.php b/resources/org/wxpay/WxPayConfig.php index 5d19268..f9ba7d6 100644 --- a/resources/org/wxpay/WxPayConfig.php +++ b/resources/org/wxpay/WxPayConfig.php @@ -25,7 +25,7 @@ class WxPayConfig const APPID = 'wx3d216119fe23ef10'; const MCHID = '1503321381'; const KEY = '7357733e71993a94ce392b9b5523c1ae'; - const APPSECRET = '7357733e71993a94ce392b9b5523c1ae'; + const APPSECRET = 'dc8a69f061f595ea4db0d7f0e52fc971'; //=======【证书路径设置】===================================== /** diff --git a/resources/org/wxpay/cert/apiclient_cert.p12 b/resources/org/wxpay/cert/apiclient_cert.p12 new file mode 100644 index 0000000000000000000000000000000000000000..261b766bfb164118b1a749c3014159357aa63653 GIT binary patch literal 2876 zcmY+^cQ_P|9|!O|-5t&zNA|vqaAsx>*_TyEl9B9nl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-xI{w zPLV(3YCh#`KS+M_MV|y?S_$PJXD;^ILi(MWGfm_ z$B^-GrW|_Gdb0axPZh`M@n$3=l9KM|Hz8*Ap=btIsPrd01Mr0j%VC% zaO>aRxlEnR#b%vs!#1KXl9;7ElFgy-mRmNouR)q!lv!_R59T$B?Hbjdsc_!UJZ7RT z!^r28S%Nhg;n~xN;`p6sk7nLR4ZDZ8I;h|##bKv@ee#sfSWd))m3;8ScnXj}0EUk- zPtEK;x3Nasg;g&MZddwo zFcgJs_WM{bqi8oXn1WHy>p&;v>2HG7=XU?f@Q`+iN4o|*x#Iv z9#y;PfjnE{8zFZz`GP-=8ILHnD(FS~G+Dt*-MYQBApnDgv78F~)20N^J5kEgzrx1X zX&YWiR=-Bcd~ie
    -

    订单已于 提交成功,请您尽快付款!

    +

    订单已于 提交成功,请您尽快付款!

    订单号:
    应付金额:
    diff --git a/resources/views/weixin/user/index.blade.php b/resources/views/weixin/user/index.blade.php index cd36047..cf926a4 100644 --- a/resources/views/weixin/user/index.blade.php +++ b/resources/views/weixin/user/index.blade.php @@ -193,7 +193,7 @@
    -
    +
    - +
    diff --git a/resources/views/weixin/user/userRechargeOrderDetail.blade.php b/resources/views/weixin/user/userRechargeOrderDetail.blade.php index 1db5da2..ae823db 100644 --- a/resources/views/weixin/user/userRechargeOrderDetail.blade.php +++ b/resources/views/weixin/user/userRechargeOrderDetail.blade.php @@ -16,14 +16,14 @@ function jsApiCall() if(res.err_msg=='get_brand_wcpay_request:ok') { - alert('支付成功!'); + alert('支付成功'); } else { - alert('支付失败!'); + alert('支付失败'); } - setTimeout("location.href = ''",2000); + setTimeout("location.href = ''",1000); } ); } @@ -54,7 +54,7 @@ function callpay()
    -

    订单已于 提交成功,请您尽快付款!

    +

    充值订单已于 提交成功,请您尽快付款!

    订单号:
    应付金额:

    diff --git a/routes/web.php b/routes/web.php index d412c3c..a7218ab 100644 --- a/routes/web.php +++ b/routes/web.php @@ -44,7 +44,8 @@ Route::group(['namespace' => 'Home'], function () { Route::get('/goodslist', 'IndexController@goodslist')->name('home_goodslist'); //产品分类页 Route::get('/brandlist', 'IndexController@brandList')->name('home_brandlist'); //品牌列表 Route::get('/sitemap.xml', 'IndexController@sitemap')->name('home_sitemap');//sitemap - + Route::get('/wx_checksignature', 'IndexController@checksignature')->name('home_wx_checksignature'); + Route::get('/test', 'IndexController@test')->name('home_test'); //测试 Route::get('/aaa', function () { dd('wap'); @@ -80,6 +81,7 @@ Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin'], function () { Route::get('/test', 'IndexController@test')->name('weixin_test'); //测试 }); + //微信路由,需登录,全局 Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin', 'middleware' => ['web','wxlogin']], function () { //个人中心 @@ -112,8 +114,8 @@ Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin', 'middleware' => ['w Route::post('/order_dopay', 'OrderController@dopay')->name('weixin_order_dopay'); //订单支付 Route::get('/order_list', 'OrderController@orderList')->name('weixin_order_list'); //全部订单列表 Route::get('/order_detail', 'OrderController@orderDetail')->name('weixin_order_detail'); //订单详情 - Route::get('/order_wxpay', 'OrderController@orderWxpay')->name('weixin_order_wxpay'); //订单微信支付 Route::get('/order_yuepay', 'OrderController@orderYuepay')->name('weixin_order_yuepay'); //订单余额支付 + Route::get('/order_wxpay', 'OrderController@orderWxpay')->name('weixin_order_wxpay'); //订单微信支付 Route::any('/order_comment', 'OrderController@orderComment')->name('weixin_order_comment'); //订单评价 //收货地址 Route::get('/user_address', 'AddressController@index')->name('weixin_user_address_list'); @@ -132,6 +134,7 @@ Route::group(['middleware' => ['web']], function () { Route::post('/dataapi/', 'Api\UserController@signin'); //签到 }); + //API接口路由,无需token验证 Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web']], function () { //轮播图