diff --git a/app/api/controller/order/CartController.php b/app/api/controller/order/CartController.php index 29538966..d0fd2da3 100644 --- a/app/api/controller/order/CartController.php +++ b/app/api/controller/order/CartController.php @@ -8,33 +8,35 @@ use app\api\controller\BaseApiController; use app\api\lists\order\CartList; use app\common\model\order\Cart; use hg\apidoc\annotation as ApiDoc; -#[ApiDoc\NotParse()] +#[ApiDoc\NotParse()] class CartController extends BaseApiController { - public function list(){ + public function list() + { return $this->dataLists(new CartList()); } /** * @notes 添加购物车 */ - public function create(){ + public function create() + { $params = (new CartValidate())->post()->goCheck('add'); - $params['uid']=$this->request->userId; - $result=Cart::where(['uid'=>$params['uid'],'store_id'=>$params['store_id'],'product_id'=>$params['product_id'],'is_fail'=>0,'delete_time'=>null])->find(); - $count=Cart::where(['uid'=>$params['uid'],'delete_time'=>null,'is_pay'=>0])->count(); - if($count>100){ + $params['uid'] = $this->request->userId; + $result = Cart::where(['uid' => $params['uid'], 'store_id' => $params['store_id'], 'product_id' => $params['product_id'], 'is_fail' => 0, 'delete_time' => null])->find(); + $count = Cart::where(['uid' => $params['uid'], 'delete_time' => null, 'is_pay' => 0])->count(); + if ($count > 100) { return $this->fail('购物车商品不能大于100个,请先结算'); } - if($result){ - $res=CartLogic::edit($params); - }else{ - $res=CartLogic::add($params); + if ($result) { + $res = CartLogic::edit($params); + } else { + $res = CartLogic::add($params); } - if($res){ + if ($res) { return $this->success('添加成功'); - }else{ + } else { return $this->fail(CartLogic::getError()); } } @@ -42,30 +44,32 @@ class CartController extends BaseApiController /** * @notes 修改购物车 */ - public function change(){ + public function change() + { $params = (new CartValidate())->post()->goCheck('change'); - $params['uid']=$this->request->userId; - $res=CartLogic::edit($params,'dec'); - if($res){ + $params['uid'] = $this->request->userId; + $res = CartLogic::edit($params, 'dec'); + if ($res) { return $this->success('修改成功'); - }else{ + } else { return $this->fail(CartLogic::getError()); } } + /** * @notes 删除购物车 */ - public function delete(){ + public function delete() + { $params = (new CartValidate())->post()->goCheck('delete'); - $params['uid']=$this->request->userId; - $res=CartLogic::delete($params); - if($res){ + $params['uid'] = $this->request->userId; + $res = CartLogic::delete($params); + if ($res) { return $this->success('删除成功'); - }else{ + } else { return $this->fail(CartLogic::getError()); } } - } diff --git a/app/api/controller/order/OrderController.php b/app/api/controller/order/OrderController.php index fb4e7845..0f3be0fa 100644 --- a/app/api/controller/order/OrderController.php +++ b/app/api/controller/order/OrderController.php @@ -128,17 +128,8 @@ class OrderController extends BaseApiController switch ($pay_type) { case PayEnum::BALANCE_PAY: //余额支付 - $user = User::where('id', $this->request->userId)->find(); - OrderLogic::payBalance($user, $order); - if (OrderLogic::hasError()) { - return $this->fail(OrderLogic::getError()); - } else { - OrderLogic::paySuccess($order, ['money' => $order['actual']]); - if (OrderLogic::hasError()) { - return $this->fail(OrderLogic::getError()); - } - return $this->success('余额支付成功'); - } + PayNotifyLogic::handle('balancePay', $order['number']); + return $this->success('余额支付成功'); case PayEnum::CASH_PAY: //现金支付 PayNotifyLogic::handle('cash_pay', $order['number']); @@ -216,18 +207,8 @@ class OrderController extends BaseApiController switch ($pay_type) { case PayEnum::BALANCE_PAY: //余额支付 - $user = User::where('id', $this->request->userId)->find(); - $res = OrderLogic::payBalance($user, $order); - if (!OrderLogic::hasError()) { - $res = OrderLogic::paySuccess($order, ['money' => $order['actual']]); - if (OrderLogic::hasError()) { - return $this->fail(OrderLogic::getError()); - } - return $this->success('余额支付成功'); - } else { - return $this->fail(OrderLogic::getError()); - } - break; + PayNotifyLogic::handle('balancePay', $order['order_id']); + return $this->success('余额支付成功'); case PayEnum::CASH_PAY: //现金支付 PayNotifyLogic::handle('cash_pay', $order['number']); diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index 6bb59e13..121213fd 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -143,7 +143,7 @@ class OrderLogic extends BaseLogic } (new StoreOrderCartInfo())->saveAll($goods_list); $where = ['is_pay' => 0]; - Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1]); + Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1, 'delete_time' => time()]); Db::commit(); return $order; } catch (\Exception $e) { diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index e73a84df..c4c717e0 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -6,6 +6,7 @@ use app\common\enum\OrderEnum; use app\common\enum\PayEnum; use app\common\model\store_finance_flow\StoreFinanceFlow; use app\common\model\store_order\StoreOrder; +use app\common\model\user\User; use app\common\service\PushService; use support\Log; use think\facade\Db; @@ -23,7 +24,7 @@ class PayNotifyLogic extends BaseLogic { Db::startTrans(); try { - if ($action != 'cash_pay') { + if ($action != 'cash_pay' && $action != 'balancePay') { $payNotifyLogLogic = new PayNotifyLogLogic(); $payNotifyLogLogic->insert($action, $extra); } @@ -44,6 +45,40 @@ class PayNotifyLogic extends BaseLogic } } + /** + * 余额支付 + * @param $orderSn + * @param $extra + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + static function balancePay($orderSn, $extra = []) + { + $order = StoreOrder::where('order_id', $orderSn)->findOrEmpty(); + $user = User::where('id', $order['uid'])->find(); + if ($user['now_money'] < $order['pay_price']) { + throw new \Exception('余额不足'); + } + Db::startTrans(); + try { + $order->money = $order['pay_price']; + $order->paid = 1; + $order->pay_time = time(); + $order->save(); + $capitalFlowDao = new CapitalFlowLogic($user); + $capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order['pay_price']); + self::afterPay($order); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine()); + self::setError('支付失败' . $e->getMessage()); + return false; + } + } /** * @notes 微信通用回调 @@ -61,6 +96,7 @@ class PayNotifyLogic extends BaseLogic if ($order->pay_type != 10) { $order->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2); $order->paid = 1; + $order->pay_time = time(); $order->status = 1; $order->save(); } else { @@ -69,14 +105,7 @@ class PayNotifyLogic extends BaseLogic if ($order->pay_type == 9) { $order->status = 2; } - $financeLogic = new StoreFinanceFlowLogic(); - $financeLogic->order = $order; - $financeLogic->user = ['uid' => $order['uid']]; - if ($order->pay_type != 9 || $order->pay_type != 10) { - $financeLogic->in($order['pay_price'], OrderEnum::USER_ORDER_PAY); - } - $financeLogic->out($order['pay_price'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0); - $financeLogic->save(); + self::afterPay($order); if ($order->pay_type == 9) { $extra['create_time'] = $order['create_time']; @@ -103,23 +132,10 @@ class PayNotifyLogic extends BaseLogic return true; } $order->paid = 1; + $order->pay_time = time(); $order->status = 2; $order->save(); - //商户获得流水 - $record[] = [ - 'financial_record_sn' => time(), - 'order_id' => $order['id'], - 'order_sn' => $order['order_id'], - 'user_id' => $order['uid'], - 'financial_type' => OrderEnum::CASHIER_CASH_ORDER_PAY, - 'financial_pm' => OrderEnum::INCOME, - 'number' => $order['pay_price'], - 'status' => 1, - 'type' => OrderEnum::MERCHANT, - 'store_id' => $order['store_id'], - 'staff_id' => $order['staff_id'], - ]; - (new StoreFinanceFlow())->saveAll($record); + self::afterPay($order); } /** @@ -139,6 +155,7 @@ class PayNotifyLogic extends BaseLogic if ($order->pay_type != 10) { $order->money = $extra['buyer_pay_amount']; $order->paid = 1; + $order->pay_time = time(); $order->status = 1; $order->save(); } else { @@ -147,15 +164,7 @@ class PayNotifyLogic extends BaseLogic if ($order->pay_type == 9) { $order->status = 2; } - $financeLogic = new StoreFinanceFlowLogic(); - $financeLogic->order = $order; - $financeLogic->user = ['uid' => $order['uid']]; - if ($order->pay_type != 9 || $order->pay_type != 10) { - $financeLogic->in($order['pay_price'], OrderEnum::USER_ORDER_PAY); - } - $financeLogic->out($order['pay_price'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0); - $financeLogic->save(); - + self::afterPay($order); if ($order->pay_type == 9) { $extra['create_time'] = $order['create_time']; @@ -166,4 +175,22 @@ class PayNotifyLogic extends BaseLogic } return true; } + + /** + * 支付后逻辑 + * @param $order + * @return void + */ + public static function afterPay($order) + { + $financeLogic = new StoreFinanceFlowLogic(); + $financeLogic->order = $order; + $financeLogic->user = ['uid' => $order['uid']]; + if ($order->pay_type != 9 || $order->pay_type != 10) { + $financeLogic->in($order['pay_price'], OrderEnum::USER_ORDER_PAY); + } + $financeLogic->out($order['pay_price'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0); + $financeLogic->save(); + } + }