zilong 4 år sedan
förälder
incheckning
5632804cd9
1 ändrade filer med 89 tillägg och 220 borttagningar
  1. 89 220
      app/Http/Controllers/Api/V1/OrderController.php

+ 89 - 220
app/Http/Controllers/Api/V1/OrderController.php

xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd
@@ -48,7 +48,6 @@ class OrderController extends AuthController
             'symptoms' => 'required_if:product_type,2|max:2000',
             'medical_imgs' => 'required_if:product_type,2|json|max:3000',
             'order_pack_id' => 'required_if:payment_type,3|integer',
-            'pay_password|支付密码' => 'integer',
         ]);
         $user = $this->user;
 
@@ -58,15 +57,6 @@ class OrderController extends AuthController
             return out(null, 10011, '当前不在医生服务时间内,不能下单');
         }
 
-        if (!empty($req['pay_password'])) {
-            if (empty($user['pay_password'])) {
-                return out(null, 60010, '未设置支付密码');
-            }
-            if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
-                return out(null, 10001, '密码错误');
-            }
-        }
-
         $discount_amount = 0;
         if (!empty($req['user_coupon_id'])) {
             //计算优惠金额
@@ -75,33 +65,14 @@ class OrderController extends AuthController
         $payment_amount = $req['total_amount'] - $discount_amount;
         $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
 
-        if (in_array($req['payment_type'], [2,3]) && $payment_amount > 0 && empty($req['pay_password'])) {
-            return out(null, 10011, '请输入支付密码');
-        }
-
         //图文咨询订单未结束时不能针对同一医生再次下图文订单
         if ($req['product_type'] == 2 && Order::where('docter_id', $req['docter_id'])->where('product_type', 2)->where('user_id', $user['id'])->whereIn('order_status', [2,3])->exists()) {
             return out(null, 10012, '您已经下过该医生的图文订单了,并且订单还未完成');
         }
 
-        if ($req['payment_type'] == 2) {
-            if ($user['balance'] < $payment_amount) {
-                return out(null, 601, '余额不足');
-            }
-        }
-
-        if ($req['payment_type'] == 3) {
-            OrderPack::checkUserServicePack($req['order_pack_id'], $user['id'], $req['product_type']);
-        }
-
         $order_status = $payment_status = 1;
         $payment_time = 0;
-        if ($payment_amount == 0 || in_array($req['payment_type'], [2,3])) {
-            $order_status = $payment_status = 2;
-            $payment_time = time();
-        }
 
-        $config = null;
         DB::beginTransaction();
         try {
             //保存订单数据
@@ -138,50 +109,6 @@ class OrderController extends AuthController
             }
             OrderPatient::create($addPatient);
 
-            //判断是微信支付
-            if ($req['payment_type'] == 1) {
-                //生成支付交易单
-                if ($payment_amount > 0) {
-                    $trade_sn = build_sn($order['id'], 3, 'T');
-                    $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
-                    Payment::create([
-                        'user_id' => $user['id'],
-                        'order_id' => $order['id'],
-                        'trade_sn' => $trade_sn,
-                        'amount' => $payment_amount,
-                        'remark' => $payBody,
-                    ]);
-                    //请求支付
-                    $payment = Factory::payment(config('config.wechat_pay'));
-                    $result = $payment->order->unify([
-                        'body' => $payBody,
-                        'out_trade_no' => $trade_sn,
-                        'total_fee' => $payment_amount,
-                        'trade_type' => 'JSAPI',
-                        'openid' => $user['openid'],
-                    ]);
-
-                    if (empty($result['prepay_id'])) {
-                        $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
-                        return out(null, 702, $errorMsg);
-                    }
-
-                    $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
-                }
-            }
-            //判断是余额支付
-            elseif ($req['payment_type'] == 2) {
-                if ($payment_amount > 0) {
-                    //改变用户余额
-                    $change_amount = 0 - $payment_amount;
-                    User::changeBalance($user['id'], $change_amount, 1, $order['id'], '咨询订单消费');
-                }
-            }
-
-            if ($payment_amount == 0 || in_array($req['payment_type'], [2, 3])) {
-                Order::payCompletedHandle($order['id']);
-            }
-
             //如果有优惠券就标记优惠券为已使用
             if (!empty($req['user_coupon_id'])) {
                 UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
@@ -193,7 +120,7 @@ class OrderController extends AuthController
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
 
-        return out($config);
+        return out(['order_id' => $order['id']]);
     }
 
     public function appointPlaceOrder()
@@ -212,19 +139,9 @@ class OrderController extends AuthController
             'vaccine_id' => 'required_if:product_type,4|integer',
             'nurse_ids' => 'required_if:product_type,5|json',
             'order_pack_id' => 'required_if:payment_type,3|integer',
-            'pay_password|支付密码' => 'integer',
         ]);
         $user = $this->user;
 
-        if (!empty($req['pay_password'])) {
-            if (empty($user['pay_password'])) {
-                return out(null, 60010, '未设置支付密码');
-            }
-            if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
-                return out(null, 10001, '密码错误');
-            }
-        }
-
         $product_type = $req['product_type'];
 
         $discount_amount = 0;
@@ -235,10 +152,6 @@ class OrderController extends AuthController
         $payment_amount = $req['total_amount'] - $discount_amount;
         $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
 
-        if (in_array($req['payment_type'], [2,3]) && $payment_amount > 0 && empty($req['pay_password'])) {
-            return out(null, 10011, '请输入支付密码');
-        }
-
         //检查号源
         if ($product_type == 3) {
             $schedulePeriod = SchedulePeriod::where('docter_id', $req['docter_id'])->where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->where('schedule_type', 1)->first();
@@ -262,12 +175,6 @@ class OrderController extends AuthController
             }
         }
 
-        if ($req['payment_type'] == 2) {
-            if ($user['balance'] < $payment_amount) {
-                return out(null, 601, '余额不足');
-            }
-        }
-
         //疫苗预约检查库存是否足够
         if ($req['product_type'] == 4) {
             $stock = OrganizationVaccine::where('org_id', $req['organization_id'])->where('vaccine_id', $req['vaccine_id'])->value('stock');
@@ -276,19 +183,9 @@ class OrderController extends AuthController
             }
         }
 
-        if ($req['payment_type'] == 3) {
-            OrderPack::checkUserServicePack($req['order_pack_id'], $user['id'], $req['product_type']);
-        }
-
         $order_status = $payment_status = 1;
         $payment_time = 0;
-        if ($payment_amount == 0 || in_array($req['payment_type'], [2,3])) {
-            $order_status = 7;
-            $payment_status = 2;
-            $payment_time = time();
-        }
 
-        $config = null;
         DB::beginTransaction();
         try {
             //保存订单数据
@@ -341,50 +238,6 @@ class OrderController extends AuthController
                 }
             }
 
-            //判断是微信支付
-            if ($req['payment_type'] == 1) {
-                //生成支付交易单
-                if ($payment_amount > 0) {
-                    $trade_sn = build_sn($order['id'], 3, 'T');
-                    $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
-                    Payment::create([
-                        'user_id' => $user['id'],
-                        'order_id' => $order['id'],
-                        'trade_sn' => $trade_sn,
-                        'amount' => $payment_amount,
-                        'remark' => $payBody,
-                    ]);
-                    //请求支付
-                    $payment = Factory::payment(config('config.wechat_pay'));
-                    $result = $payment->order->unify([
-                        'body' => $payBody,
-                        'out_trade_no' => $trade_sn,
-                        'total_fee' => $payment_amount,
-                        'trade_type' => 'JSAPI',
-                        'openid' => $user['openid'],
-                    ]);
-
-                    if (empty($result['prepay_id'])) {
-                        $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
-                        return out(null, 702, $errorMsg);
-                    }
-
-                    $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
-                }
-            }
-            //判断是余额支付
-            elseif ($req['payment_type'] == 2) {
-                if ($payment_amount > 0) {
-                    //改变用户余额
-                    $change_amount = 0 - $payment_amount;
-                    User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
-                }
-            }
-
-            if ($payment_amount == 0 || in_array($req['payment_type'], [2, 3])) {
-                Order::payCompletedHandle($order['id']);
-            }
-
             //如果有优惠券就标记优惠券为已使用
             if (!empty($req['user_coupon_id'])) {
                 UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
@@ -396,7 +249,7 @@ class OrderController extends AuthController
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
 
-        return out($config);
+        return out(['order_id' => $order['id']]);
     }
 
     public function packPlaceOrder()
@@ -411,20 +264,10 @@ class OrderController extends AuthController
             'is_security' => 'required|in:0,1',
             'guardian_name' => 'required|max:50',
             'relationship_type' => 'required|integer',
-            'pay_password|支付密码' => 'integer',
             'is_need_insurance' => 'in:0,1',
         ]);
         $user = $this->user;
 
-        if (!empty($req['pay_password'])) {
-            if (empty($user['pay_password'])) {
-                return out(null, 60010, '未设置支付密码');
-            }
-            if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
-                return out(null, 10001, '密码错误');
-            }
-        }
-
         $discount_amount = 0;
         if (!empty($req['user_coupon_id'])) {
             //计算优惠金额
@@ -436,25 +279,9 @@ class OrderController extends AuthController
         $payment_amount = $req['total_amount'] - $discount_amount;
         $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
 
-        if ($req['payment_type'] == 2 && $payment_amount > 0 && empty($req['pay_password'])) {
-            return out(null, 10011, '请输入支付密码');
-        }
-
-        if ($req['payment_type'] == 2) {
-            if ($user['balance'] < $payment_amount) {
-                return out(null, 601, '余额不足');
-            }
-        }
-
         $order_status = $payment_status = 1;
         $payment_time = 0;
-        if ($payment_amount == 0 || $req['payment_type'] == 2) {
-            $order_status = 3;
-            $payment_status = 2;
-            $payment_time = time();
-        }
 
-        $config = null;
         DB::beginTransaction();
         try {
             //保存订单数据
@@ -494,50 +321,6 @@ class OrderController extends AuthController
             $addPack['is_need_insurance'] = !empty($req['is_need_insurance']) ? $req['is_need_insurance'] : 0;
             OrderPack::create($addPack);
 
-            //判断是微信支付
-            if ($req['payment_type'] == 1) {
-                //生成支付交易单
-                if ($payment_amount > 0) {
-                    $trade_sn = build_sn($order['id'], 3, 'T');
-                    $payBody = '超级宝妈-'.config('config.product_type_map')[6];
-                    Payment::create([
-                        'user_id' => $user['id'],
-                        'order_id' => $order['id'],
-                        'trade_sn' => $trade_sn,
-                        'amount' => $payment_amount,
-                        'remark' => $payBody,
-                    ]);
-                    //请求支付
-                    $payment = Factory::payment(config('config.wechat_pay'));
-                    $result = $payment->order->unify([
-                        'body' => $payBody,
-                        'out_trade_no' => $trade_sn,
-                        'total_fee' => $payment_amount,
-                        'trade_type' => 'JSAPI',
-                        'openid' => $user['openid'],
-                    ]);
-
-                    if (empty($result['prepay_id'])) {
-                        $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
-                        return out(null, 702, $errorMsg);
-                    }
-
-                    $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
-                }
-            }
-            //判断是余额支付
-            elseif ($req['payment_type'] == 2) {
-                if ($payment_amount > 0) {
-                    //改变用户余额
-                    $change_amount = 0 - $payment_amount;
-                    User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
-                }
-            }
-
-            if ($payment_amount == 0 || $req['payment_type'] == 2) {
-                Order::payCompletedHandle($order['id']);
-            }
-
             //如果有优惠券就标记优惠券为已使用
             if (!empty($req['user_coupon_id'])) {
                 UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
@@ -549,7 +332,7 @@ class OrderController extends AuthController
             return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
         }
 
-        return out($config);
+        return out(['order_id' => $order['id']]);
     }
 
     public function orderList()
@@ -756,4 +539,90 @@ class OrderController extends AuthController
 
         return out($orderPacks);
     }
+
+    public function orderPay()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'order_id' => 'required|integer',
+            'pay_password' => 'max:20',
+        ]);
+        $user = $this->user;
+
+        $order = Order::where('id', $req['order_id'])->first()->toArray();
+        if (in_array($order['payment_type'], [2,3]) && $order['payment_amount'] > 0 && empty($req['pay_password'])) {
+            return out(null, 10011, '请输入支付密码');
+        }
+        if (!empty($req['pay_password'])) {
+            if (empty($user['pay_password'])) {
+                return out(null, 60010, '未设置支付密码');
+            }
+            if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
+                return out(null, 10001, '密码错误');
+            }
+        }
+        if ($order['payment_type'] == 2) {
+            if ($user['balance'] < $order['payment_amount']) {
+                return out(null, 601, '余额不足');
+            }
+        }
+        if ($order['payment_type'] == 3) {
+            OrderPack::checkUserServicePack($order['pay_order_pack_id'], $user['id'], $order['product_type']);
+        }
+
+        $config = null;
+        DB::beginTransaction();
+        try {
+            //判断是微信支付
+            if ($order['payment_type'] == 1) {
+                //生成支付交易单
+                if ($order['payment_amount'] > 0) {
+                    $trade_sn = build_sn($order['id'], 3, 'T');
+                    $payBody = '超级宝妈-'.config('config.product_type_map')[$order['product_type']];
+                    Payment::create([
+                        'user_id' => $user['id'],
+                        'order_id' => $order['id'],
+                        'trade_sn' => $trade_sn,
+                        'amount' => $order['payment_amount'],
+                        'remark' => $payBody,
+                    ]);
+                    //请求支付
+                    $payment = Factory::payment(config('config.wechat_pay'));
+                    $result = $payment->order->unify([
+                        'body' => $payBody,
+                        'out_trade_no' => $trade_sn,
+                        'total_fee' => $order['payment_amount'],
+                        'trade_type' => 'JSAPI',
+                        'openid' => $user['openid'],
+                    ]);
+
+                    if (empty($result['prepay_id'])) {
+                        $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
+                        return out(null, 702, $errorMsg);
+                    }
+
+                    $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
+                }
+            }
+            //判断是余额支付
+            elseif ($order['payment_type'] == 2) {
+                if ($order['payment_amount'] > 0) {
+                    //改变用户余额
+                    $change_amount = 0 - $order['payment_amount'];
+                    User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
+                }
+            }
+
+            if ($order['payment_amount'] == 0 || in_array($order['payment_type'], [2, 3])) {
+                Order::payCompletedHandle($order['id']);
+            }
+
+            DB::commit();
+        } catch (Exception $e) {
+            DB::rollBack();
+            return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
+        }
+
+        return out($config);
+    }
 }