修改余额支付

This commit is contained in:
luofei 2024-06-04 17:21:57 +08:00
parent 14bc0c1539
commit e4c6c74f1a
4 changed files with 93 additions and 81 deletions

View File

@ -8,33 +8,35 @@ use app\api\controller\BaseApiController;
use app\api\lists\order\CartList; use app\api\lists\order\CartList;
use app\common\model\order\Cart; use app\common\model\order\Cart;
use hg\apidoc\annotation as ApiDoc; use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
#[ApiDoc\NotParse()]
class CartController extends BaseApiController class CartController extends BaseApiController
{ {
public function list(){ public function list()
{
return $this->dataLists(new CartList()); return $this->dataLists(new CartList());
} }
/** /**
* @notes 添加购物车 * @notes 添加购物车
*/ */
public function create(){ public function create()
{
$params = (new CartValidate())->post()->goCheck('add'); $params = (new CartValidate())->post()->goCheck('add');
$params['uid']=$this->request->userId; $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(); $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(); $count = Cart::where(['uid' => $params['uid'], 'delete_time' => null, 'is_pay' => 0])->count();
if($count>100){ if ($count > 100) {
return $this->fail('购物车商品不能大于100个请先结算'); return $this->fail('购物车商品不能大于100个请先结算');
} }
if($result){ if ($result) {
$res=CartLogic::edit($params); $res = CartLogic::edit($params);
}else{ } else {
$res=CartLogic::add($params); $res = CartLogic::add($params);
} }
if($res){ if ($res) {
return $this->success('添加成功'); return $this->success('添加成功');
}else{ } else {
return $this->fail(CartLogic::getError()); return $this->fail(CartLogic::getError());
} }
} }
@ -42,30 +44,32 @@ class CartController extends BaseApiController
/** /**
* @notes 修改购物车 * @notes 修改购物车
*/ */
public function change(){ public function change()
{
$params = (new CartValidate())->post()->goCheck('change'); $params = (new CartValidate())->post()->goCheck('change');
$params['uid']=$this->request->userId; $params['uid'] = $this->request->userId;
$res=CartLogic::edit($params,'dec'); $res = CartLogic::edit($params, 'dec');
if($res){ if ($res) {
return $this->success('修改成功'); return $this->success('修改成功');
}else{ } else {
return $this->fail(CartLogic::getError()); return $this->fail(CartLogic::getError());
} }
} }
/** /**
* @notes 删除购物车 * @notes 删除购物车
*/ */
public function delete(){ public function delete()
{
$params = (new CartValidate())->post()->goCheck('delete'); $params = (new CartValidate())->post()->goCheck('delete');
$params['uid']=$this->request->userId; $params['uid'] = $this->request->userId;
$res=CartLogic::delete($params); $res = CartLogic::delete($params);
if($res){ if ($res) {
return $this->success('删除成功'); return $this->success('删除成功');
}else{ } else {
return $this->fail(CartLogic::getError()); return $this->fail(CartLogic::getError());
} }
} }
} }

View File

@ -128,17 +128,8 @@ class OrderController extends BaseApiController
switch ($pay_type) { switch ($pay_type) {
case PayEnum::BALANCE_PAY: case PayEnum::BALANCE_PAY:
//余额支付 //余额支付
$user = User::where('id', $this->request->userId)->find(); PayNotifyLogic::handle('balancePay', $order['number']);
OrderLogic::payBalance($user, $order); return $this->success('余额支付成功');
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('余额支付成功');
}
case PayEnum::CASH_PAY: case PayEnum::CASH_PAY:
//现金支付 //现金支付
PayNotifyLogic::handle('cash_pay', $order['number']); PayNotifyLogic::handle('cash_pay', $order['number']);
@ -216,18 +207,8 @@ class OrderController extends BaseApiController
switch ($pay_type) { switch ($pay_type) {
case PayEnum::BALANCE_PAY: case PayEnum::BALANCE_PAY:
//余额支付 //余额支付
$user = User::where('id', $this->request->userId)->find(); PayNotifyLogic::handle('balancePay', $order['order_id']);
$res = OrderLogic::payBalance($user, $order); return $this->success('余额支付成功');
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;
case PayEnum::CASH_PAY: case PayEnum::CASH_PAY:
//现金支付 //现金支付
PayNotifyLogic::handle('cash_pay', $order['number']); PayNotifyLogic::handle('cash_pay', $order['number']);

View File

@ -143,7 +143,7 @@ class OrderLogic extends BaseLogic
} }
(new StoreOrderCartInfo())->saveAll($goods_list); (new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0]; $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(); Db::commit();
return $order; return $order;
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -6,6 +6,7 @@ use app\common\enum\OrderEnum;
use app\common\enum\PayEnum; use app\common\enum\PayEnum;
use app\common\model\store_finance_flow\StoreFinanceFlow; use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_order\StoreOrder; use app\common\model\store_order\StoreOrder;
use app\common\model\user\User;
use app\common\service\PushService; use app\common\service\PushService;
use support\Log; use support\Log;
use think\facade\Db; use think\facade\Db;
@ -23,7 +24,7 @@ class PayNotifyLogic extends BaseLogic
{ {
Db::startTrans(); Db::startTrans();
try { try {
if ($action != 'cash_pay') { if ($action != 'cash_pay' && $action != 'balancePay') {
$payNotifyLogLogic = new PayNotifyLogLogic(); $payNotifyLogLogic = new PayNotifyLogLogic();
$payNotifyLogLogic->insert($action, $extra); $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 微信通用回调 * @notes 微信通用回调
@ -61,6 +96,7 @@ class PayNotifyLogic extends BaseLogic
if ($order->pay_type != 10) { if ($order->pay_type != 10) {
$order->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2); $order->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->paid = 1; $order->paid = 1;
$order->pay_time = time();
$order->status = 1; $order->status = 1;
$order->save(); $order->save();
} else { } else {
@ -69,14 +105,7 @@ class PayNotifyLogic extends BaseLogic
if ($order->pay_type == 9) { if ($order->pay_type == 9) {
$order->status = 2; $order->status = 2;
} }
$financeLogic = new StoreFinanceFlowLogic(); self::afterPay($order);
$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();
if ($order->pay_type == 9) { if ($order->pay_type == 9) {
$extra['create_time'] = $order['create_time']; $extra['create_time'] = $order['create_time'];
@ -103,23 +132,10 @@ class PayNotifyLogic extends BaseLogic
return true; return true;
} }
$order->paid = 1; $order->paid = 1;
$order->pay_time = time();
$order->status = 2; $order->status = 2;
$order->save(); $order->save();
//商户获得流水 self::afterPay($order);
$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);
} }
/** /**
@ -139,6 +155,7 @@ class PayNotifyLogic extends BaseLogic
if ($order->pay_type != 10) { if ($order->pay_type != 10) {
$order->money = $extra['buyer_pay_amount']; $order->money = $extra['buyer_pay_amount'];
$order->paid = 1; $order->paid = 1;
$order->pay_time = time();
$order->status = 1; $order->status = 1;
$order->save(); $order->save();
} else { } else {
@ -147,15 +164,7 @@ class PayNotifyLogic extends BaseLogic
if ($order->pay_type == 9) { if ($order->pay_type == 9) {
$order->status = 2; $order->status = 2;
} }
$financeLogic = new StoreFinanceFlowLogic(); self::afterPay($order);
$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();
if ($order->pay_type == 9) { if ($order->pay_type == 9) {
$extra['create_time'] = $order['create_time']; $extra['create_time'] = $order['create_time'];
@ -166,4 +175,22 @@ class PayNotifyLogic extends BaseLogic
} }
return true; 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();
}
} }