Przeglądaj źródła

fix(controller): 优化支付

xiansin 4 lat temu
rodzic
commit
c1fb423226

+ 6 - 3
app/Http/Controllers/PaymentController.php → app/Http/Controllers/Api/PaymentController.php

xqd xqd xqd xqd
@@ -6,10 +6,11 @@
  * @description
  */
 
-namespace App\Http\Controllers;
+namespace App\Http\Controllers\Api;
 
 use App\Models\User;
 use App\Models\UserVip;
+use Carbon\Carbon;
 use EasyWeChat\Factory;
 use EasyWeChat\Kernel\Exceptions\Exception;
 
@@ -19,6 +20,7 @@ class PaymentController extends Controller
     {
         $app = Factory::payment(config('wechat.payment.default'));
         $response = $app->handlePaidNotify(function ($message, $fail) {
+            trace(json_encode($message,JSON_UNESCAPED_UNICODE),'debug');
             // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
             $order = UserVip::where('order_id', $message['out_trade_no'])->first();
 
@@ -27,8 +29,9 @@ class PaymentController extends Controller
             }
             if ($message['return_code'] === 'SUCCESS') {
                 if ($message['result_code'] === 'SUCCESS') {
-                    $order->pay_at = time(); // 更新支付时间为当前时间
+                    $order->pay_at = Carbon::now()->toDateTimeString(); // 更新支付时间为当前时间
                     $order->status = 1;
+                    $order->serial_number = $message['transaction_id'];
                 }elseif ($message['result_code'] === 'FAIL') {
                     $order->status = 2;
                 }
@@ -39,7 +42,7 @@ class PaymentController extends Controller
             if($order->status == 1){
                 $user = User::find($order->user_id);
                 $user->is_vip = 1;
-                $user->become_vip_at = time();
+                $user->become_vip_at = Carbon::now()->toDateTimeString();
                 $user->save();
             }
 

+ 22 - 12
app/Http/Controllers/Api/UserController.php

xqd xqd xqd xqd
@@ -27,11 +27,11 @@ class UserController extends Controller
 
         $record = UserVip::where('user_id',$user['id'])
             ->where('status',1)
-            ->first()
-            ->toArray();
+            ->first();
         if(empty($record)){
             return out();
         }
+        $record = $record->toArray();
 
         return out([
             'order_fee' => $record['order_fee'],
@@ -74,7 +74,6 @@ class UserController extends Controller
         $user = auth('api')->user();
         $shareConfig = Setting::first()->toArray();
 
-        $orderId = get_order_id($user['id']);
         \DB::beginTransaction();
         $app = Factory::payment(config('wechat.payment.default'));
         $jssdk = $app->jssdk;
@@ -83,16 +82,21 @@ class UserController extends Controller
 
             $order = UserVip::where('user_id', $user['id'])->first();
             if(empty($order) || empty($order->prepay_id)){
-                $order = UserVip::create([
-                    'order_id' => $orderId,
-                    'user_id' => $user['id'],
-                    'order_fee' => $shareConfig['member_price']
-                ]);
+                $orderId = get_order_id($user['id']);
+                if(empty($order)){
+                    $order = UserVip::create([
+                        'order_id' => $orderId,
+                        'user_id' => $user['id'],
+                        'order_fee' => $shareConfig['member_price']
+                    ]);
+                }else{
+                    $order->order_id = $orderId;
+                }
                 $payment = $app->order->unify([
-                    'body' => '三哥桥梁-会员充值',
+                    'body' => '三哥桥梁-购买会员',
                     'out_trade_no' => $orderId,
                     'total_fee' => $shareConfig['member_price']*100, // 分
-                    'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
+                    'notify_url' => config('wechat.payment.default.notify_url'), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                     'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                     'openid' => $user['openid'],
                 ]);
@@ -124,10 +128,16 @@ class UserController extends Controller
     public function query()
     {
         $user = auth('api')->user();
-        $order = UserVip::where('user_id', $user['id'])->first()->toArray();
-        return out($order);
+        $order = UserVip::where('user_id', $user['id'])->first();
+        if(!$order){
+            return out(2);
+        }
+        $order = $order->toArray();
+
+        return out($order['status']);
     }
 
+    // 推广人数
     public function shares()
     {
         $user = auth('api')->user();

+ 1 - 2
routes/api.php

xqd
@@ -19,8 +19,7 @@ $api->version('v1', [
     'prefix'    => 'api',
     //'middleware' => ['serializer:array', 'bindings']
 ], function($api) {
-    // 上传不验证
-    // $api->post('Common/uploadFile', 'CommonController@uploadFile');
+     $api->post('payment/notice', 'PaymentController@notice');
 
     // 限制登录和验证码接口请求频率
     $api->group([