wesley.chen преди 7 години
родител
ревизия
c6de1ae2b3
променени са 3 файла, в които са добавени 156 реда и са изтрити 10 реда
  1. 1 1
      app/Http/Controllers/Api/V1/Controller.php
  2. 145 9
      app/Http/Controllers/Api/V1/HomeController.php
  3. 10 0
      routes/api.php

+ 1 - 1
app/Http/Controllers/Api/V1/Controller.php

xqd
@@ -21,7 +21,7 @@ class Controller extends BaseController
         $this->middleware('auth:api', [
             'except' => [
                 'upload', 'getCode', 'reset', 'login', 'get', 'register', 'alipayNotify', 'wechatpayNotify',
-                'get', 'area', 'get_province', 'get_city', 'get_county', 'test','getIntroduction','getBaseData','getQueried','agreeIntroduction','getPaidMajors'
+                'get', 'area', 'get_province', 'get_city', 'get_county', 'test','getIntroduction','getBaseData','getQueried','agreeIntroduction','getPaidMajors','pay'
 
             ]
         ]);

+ 145 - 9
app/Http/Controllers/Api/V1/HomeController.php

xqd xqd xqd xqd xqd xqd xqd xqd
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\V1;
 
 use App\Models\IntroductionInfoModel;
 use App\Models\MajorInfoModel;
+use App\Models\OrderInfoModel;
 use App\Models\PaidSettingModel;
 use App\Models\QueryInfoModel;
 use App\Models\StudentCountModel;
@@ -40,9 +41,9 @@ class HomeController extends Controller
      * @apiGroup 高考助手
      * @apiPermission none
      * @apiVersion 0.1.0
-     * @apiParam {int}    [code](必填)
-     * @apiParam {int}    [nickName]
-     * @apiParam {int}    [avatar]
+     * @apiParam {string}    [code](必填)
+     * @apiParam {string}    [nickName]
+     * @apiParam {string}    [avatar]
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -233,9 +234,10 @@ class HomeController extends Controller
      * @apiParam {string}    [cnumber]   考号(必填)
      * @apiParam {string}    [class]   科类(必填)
      * @apiParam {int}    [grade]   高考成绩(必填)
-     * @apiParam {int}    [batch]   批次(必填)
-     * @apiParam {int}    [province]   省份(必填)
-     * @apiParam {int}    [code]   推荐码(选填)
+     * @apiParam {int}    [mobile]   手机号(必填)
+     * @apiParam {string}    [batch]   批次(必填)
+     * @apiParam {string}    [province]   省份(必填)
+     * @apiParam {string}    [code]   推荐码(选填)
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -267,11 +269,13 @@ class HomeController extends Controller
                 'cnumber' => 'required',
                 'username' => 'required',
                 'grade' => 'required|integer',
+                'mobile' => 'required'
             ],
             [
                 'cnumber.required' => '考号不能为空!',
                 'username.required' => '姓名不能为空!',
                 'username.required' => '姓名不能为空!',
+                'mobile.required' => '手机号不能为空!',
                 'grade.required' => '成绩不能为空!',
                 'grade.integer' => '请输入正确格式的成绩!',
             ]
@@ -403,6 +407,7 @@ class HomeController extends Controller
 
             $count = count($major);
 
+            $this->createQueryInfo($_user, 1);
             return $this->api(compact('count', 'major'));
         } else {
             $price = PaidSettingModel::first();
@@ -414,14 +419,133 @@ class HomeController extends Controller
         }
     }
 
+    /**
+     * @api {post} /api/home/pay   获取微信支付签名信息
+     * @apiDescription  获取微信支付签名信息
+     * @apiGroup 高考助手
+     * @apiPermission none
+     * @apiVersion 0.1.0
+     * @apiParam {int}    [userid]   用户ID(必填)
+     * @apiParam {string}    [price]  付费金额(必填)
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     * {
+     *     "status": true,
+     *     "status_code": 0,
+     *     "message": "",
+     *     "data": {
+     *              "appId":"wx1c2357232cd25f65",
+     *              "timeStamp":"1524907589",
+     *              "nonceStr":"5ae43e45eb499",
+     *              "package":"prepay_id=wx28172629917401724160128f0238805782",
+     *              "signType":"MD5",
+     *              "paySign":"8E9CF26B2B83C22471D023CBBDC36EDF"
+     *          }
+     * }
+     * @apiErrorExample {json} Error-Response:
+     * HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     * 可能出现的错误代码:
+     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
+     */
+    public function pay(Request $request)
+    {
+        $validator = Validator::make($request->all(),
+            [
+                'userid' => 'required',
+                'price' => 'required',
+            ],
+            [
+                'userid.required' => 'userid不能为空!',
+                'price.required' => 'price不能为空!',
+
+            ]
+        );
+
+
+        if ($validator->fails()) {
+            return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
+        }
+        $money = $request->input('price');
+        $user = UserInfoModel::find(request('userid'));
+
+        $trade_no = date("YmdHis");
+
+        \Log::info($this->options());
+
+        $app = Factory::payment($this->options());
+
+        $result = $app->order->unify([
+            'body' => '高考志愿助手 - 付费查询',
+            'out_trade_no' => $trade_no,
+            'total_fee' => $money * 100,
+            'trade_type' => 'JSAPI',
+            'notify_url' => url('/api/home/pay'),
+            'openid' => $user->get('openid')
+        ]);
+
+        \Log::info($result);
+
+        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
+            $payment = Factory::payment($this->options());
+            $jssdk = $payment->jssdk;
+
+            $json = $jssdk->bridgeConfig($result['prepay_id']);
+            \Log::info($json);
+            return $this->api(compact('json'));
+        }else{
+            $msg = "签名失败,请稍后再试!";
+
+            return $this->api(compact('msg'));
+        }
+    }
+
+
+//下面是回调函数
+    public function notify()
+    {
+        $app = Factory::payment($this->options());
+        \Log::info("wechat notify start!");
+        return $app->handlePaidNotify(function ($notify, $successful) {
+            \Log::info($notify);
+            if ($notify['result_code'] == 'SUCCESS') {
+                $user = UserInfoModel::where('openid',$notify['openid'])->first();
+                $data['order_no'] = $notify['out_trade_no'];
+                $data['price'] = $notify['total_fee'] / 100;
+                $data['user_id'] = $user->id;
+                $data['code'] = $user->code;
+                $user->paid_end_time = Carbon::parse("+1 year")->toDateTimeString();
+
+                OrderInfoModel::create($data);
+                $user->save();
+            } else {
+                return $successful('通信失败,请稍后再通知我');
+            }
+            $msg = "支付成功";
+            return $this->api(compact('msg'));
+        });
+
+    }
+
+
     public function checkPaid()
     {
         $userid = request('userid');
         $_user = UserInfoModel::find($userid);
 
         $hasPaid = QueryInfoModel::where('user_id', $_user->id)->where('grade', $_user->grade)->where('is_paid', 1)->count();
+        $not_expire = Carbon::now() < $_user->paid_end_time;
 
-        return $hasPaid;
+        if($hasPaid && $not_expire){
+            return 1;
+        }else{
+            return 0;
+        }
     }
 
     public function updateUserinfo()
@@ -437,9 +561,9 @@ class HomeController extends Controller
 
     }
 
-    public function createQueryInfo($userinfo)
+    public function createQueryInfo($userinfo, $ispaid = 0)
     {
-        $data['is_paid'] = 0;
+        $data['is_paid'] = $ispaid;
         $data['cnumber'] = $userinfo->cnumber;
         $data['grade'] = $userinfo->grade;
         $data['code'] = $userinfo->code;
@@ -447,4 +571,16 @@ class HomeController extends Controller
     }
 
 
+    public function options()
+    {
+        return [
+            'app_id' => "wx1c2357232cd25f65",
+            'mch_id' => "1398823402",
+            'key' => "c1891122765718911227657189112276",
+            'notify_url' => url('/api/home/pay'),
+            'sandbox' => false,
+        ];
+    }
+
+
 }

+ 10 - 0
routes/api.php

xqd
@@ -94,4 +94,14 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($a
         'uses' => 'HomeController@getPaidMajors',
     ]);
 
+    $api->post('home/pay', [
+        'as' => 'home.pay',
+        'uses' => 'HomeController@pay',
+    ]);
+
+    $api->any('home/notify', [
+        'as' => 'home.notify',
+        'uses' => 'HomeController@notify',
+    ]);
+
 });