浏览代码

充值提现

dyjh 6 年之前
父节点
当前提交
733a0dc03a

+ 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', 'indexMenu', 'userRelease', 'indexTag', 'indexMessage', 'indexGetMessage', 'userEditFavorite', 'userFavorite', 'userMyMessage', 'userMessageDetail', 'userGetMessages', 'recharge'
+                'get', 'area', 'get_province', 'get_city', 'get_county', 'test', 'indexMenu', 'userRelease', 'indexTag', 'indexMessage', 'indexGetMessage', 'userEditFavorite', 'userFavorite', 'userMyMessage', 'userMessageDetail', 'userGetMessages', 'recharge', 'cash'
             ]
         ]);
 

+ 136 - 13
app/Http/Controllers/Api/V1/PayController.php

xqd xqd xqd xqd xqd xqd
@@ -9,10 +9,13 @@
 namespace App\Http\Controllers\Api\V1;
 
 
+use App\Models\PaymentInfoModel;
 use App\Models\UserInfoModel;
 use App\Models\WechatAppModel;
+use App\User;
 use EasyWeChat\Factory;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Auth,Validator;
 use App\Services\Base\ErrorCode;
@@ -20,10 +23,12 @@ use App\Services\Base\ErrorCode;
 class PayController extends Controller
 {
     private $config;
+    private $wechat;
     public function __construct()
     {
-        //parent::__construct();
+        parent::__construct();
         $wechat = WechatAppModel::find(1);
+        $this->wechat = $wechat;
         $this->config = [
             // 必要配置
             'app_id'             => $wechat->appId,
@@ -38,6 +43,33 @@ class PayController extends Controller
         ];
     }
 
+
+    /**
+     * @api {post} /api/pay/recharge 充值(recharge)
+     * @apiDescription 充值(recharge)recharge
+     * @apiGroup Pay
+     * @apiPermission none
+     * @apiVersion 0.1.0
+     * @apiParam {decimal}  price    内容
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     * {
+     * "status": true,
+     * "status_code": 0,
+     * "message": "",
+     * "data": {
+     *      "msg": "已成功发起提现,请等待后台审核"
+     *    }
+     *}
+     * @apiErrorExample {json} Error-Response:
+     * HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     */
     public function recharge(Request $request)
     {
         $validator = Validator::make($request->all(),
@@ -54,7 +86,16 @@ class PayController extends Controller
         $userAuth = Auth('api')->user();
         $money = $request->input('price');
         $trade_no = 'Pay'.date('YmdHis').rand(1000,9999);
-
+        $order['user_id'] = $userAuth->id;
+        $order['out_trade_no'] = $trade_no;
+        $order['price'] = $money;
+        $order['to_user'] = 0;
+        $order['state'] = 0;
+        $order['msg_id'] = 0;
+        $order['poundage'] = 0;
+        $order['openid'] = $userAuth->openid;
+        $order['type'] = 0;
+        PaymentInfoModel::create($order);
         $app = Factory::payment($this->config);
 
         $result = $app->order->unify([
@@ -73,7 +114,7 @@ class PayController extends Controller
             $jssdk = $payment->jssdk;
 
             $json = $jssdk->bridgeConfig($result['prepay_id']);
-            dd($json);
+            //dd($json);
             return $this->api(compact('json'));
         }else{
             $msg = "签名失败,请稍后再试!";
@@ -86,27 +127,109 @@ class PayController extends Controller
 //下面是回调函数
     public function notify()
     {
-        $app = Factory::payment($this->options());
+        $app = Factory::payment($this->config);
         \Log::info("wechat notify start!");
-        return $app->handlePaidNotify(function ($notify, $successful) {
+        $response = $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;
-                \Log::info($data);
-                $user->paid_end_time = Carbon::parse("+1 year")->toDateTimeString();
-                $this->createQueryInfo($user,1);
-
-                OrderInfoModel::create($data);
-                $user->save();
+                $wechat_pay = PaymentInfoModel::where([['user_id',$user->id],['out_trade_no',$notify['out_trade_no']],['state',0]])->first();
+                if($wechat_pay){
+                    $user->money += $data['price'];
+                    $user->save();
+                    $wechat_pay->state = 1;
+                    $wechat_pay->save();
+                }
             } else {
                 return $successful('通信失败,请稍后再通知我');
             }
             return true;
         });
+        return $response->send();
+    }
+
+
+    /**
+     * @api {post} /api/pay/cash 提现(cash)
+     * @apiDescription 提现(cash)cash
+     * @apiGroup Pay
+     * @apiPermission none
+     * @apiVersion 0.1.0
+     * @apiParam {decimal}  price    内容
+     * @apiParam {string}  name    真实姓名
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     * {
+     * "status": true,
+     * "status_code": 0,
+     * "message": "",
+     * "data": {
+     *      "msg": "已成功发起提现,请等待后台审核"
+     *    }
+     *}
+     * @apiErrorExample {json} Error-Response:
+     * HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     */
+
+    public function cash(Request $request)
+    {
+        $validator = Validator::make($request->all(),
+            [
+                'price' => 'required',
+                'name' => 'required',
+            ],[
+                'price.required' => 'price不能为空!',
+                'name.required' => 'name不能为空!'
+            ]
+        );
+        if ($validator->fails()) {
+            return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
+        }
+        $data = $request->input();
+        $userAuth = Auth('api')->user();
+        $trade_no = 'Cash'.date('YmdHis').rand(1000,9999);
+        $poundage = $this->wechat->poundage * $data['price'];
+        $total = $data['price'] + $poundage;
+        if($userAuth->money < $total){
+            $msg = '您的余额不足';
+            return $this->api($msg);
+        }
+        DB::beginTransaction();
+        $user = UserInfoModel::find($userAuth->id);
+        $user->money -= $total;
+        $res = $user->save();
+        if($res){
+            $order['user_id'] = 0;
+            $order['out_trade_no'] = $trade_no;
+            $order['price'] = $data['price'];
+            $order['to_user'] = $userAuth->id;
+            $order['state'] = 0;
+            $order['name'] = $data['name'];
+            $order['msg_id'] = 0;
+            $order['poundage'] = 0;
+            $order['openid'] = $userAuth->openid;
+            $order['type'] = 1;
+            $query = PaymentInfoModel::create($order);
+            if($query){
+                $msg = '已成功发起提现,请等待后台审核';
+                DB::commit();
+            } else {
+                $msg = '提现失败';
+                DB::rollback();
+            }
+        } else {
+            $msg = '提现失败';
+            DB::rollback();
+        }
+        return $this->api($msg);
 
     }
 

+ 4 - 1
app/Models/PaymentInfoModel.php

xqd
@@ -43,7 +43,10 @@ class PaymentInfoModel extends BaseModel
         'price',
         'type',
         'msg_id',
-        'to_user'
+        'to_user',
+        'state',
+        'poundage',
+        'name'
     ];
 
 }

+ 34 - 0
database/migrations/2018_07_26_025613_add_state_to_table_payment_info.php

xqd
@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddStateToTablePaymentInfo extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payment_info', function (Blueprint $table) {
+            //
+            $table->unsignedInteger('state')->comment('支付状态');
+            $table->decimal('poundage')->default(null)->comment('手续费');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payment_info', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 33 - 0
database/migrations/2018_07_26_054123_add_name_to_table_payment_info.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddNameToTablePaymentInfo extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payment_info', function (Blueprint $table) {
+            //
+            $table->string('name')->nullable()->default(null)->comment('真实姓名');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payment_info', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 1 - 0
routes/api.php

xqd
@@ -41,6 +41,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($a
     $api->get('/user/get_message', 'UserController@userGetMessages');
     $api->get('/user/message_detail', 'UserController@userMessageDetail');
     $api->post('/pay/recharge', 'PayController@recharge');
+    $api->post('/pay/cash', 'PayController@cash');
     $api->post('/attachment/upload', 'AttachmentController@upload');
 //    $api->post('auth/logout', [
 //        'as' => 'auth.logout',