refactor(PayNotifyLogic): 优化支付回调处理逻辑,提高代码可读性

This commit is contained in:
mkm 2024-06-08 22:06:30 +08:00
parent f5185ea377
commit e6405cd648

@ -33,19 +33,18 @@ class PayNotifyLogic extends BaseLogic
try { try {
if ($action != 'cash_pay' && $action != 'balancePay') { if ($action != 'cash_pay' && $action != 'balancePay') {
$payNotifyLogLogic = new PayNotifyLogLogic(); $payNotifyLogLogic = new PayNotifyLogLogic();
if($action == 'refund'){ if ($action == 'refund') {
$payNotifyLogLogic->insert($action, $extra,PayNotifyLog::TYPE_REFUND); $payNotifyLogLogic->insert($action, $extra, PayNotifyLog::TYPE_REFUND);
}else{ } else {
$payNotifyLogLogic->insert($action, $extra); $payNotifyLogLogic->insert($action, $extra);
} }
} }
self::$action($orderSn, $extra); self::$action($orderSn, $extra);
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
Log::error('支付回调处理失败' . $e->getMessage().',lien:'.$e->getLine().',file:'.$e->getFile()); Log::error('支付回调处理失败' . $e->getMessage() . ',lien:' . $e->getLine() . ',file:' . $e->getFile());
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage());
} }
} }
@ -59,7 +58,7 @@ class PayNotifyLogic extends BaseLogic
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public static function balancePay($orderSn, $extra = []) public static function balancePay($orderSn, $extra = [])
{ {
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty(); $order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
$user = User::where('id', $order['uid'])->find(); $user = User::where('id', $order['uid'])->find();
@ -102,16 +101,16 @@ class PayNotifyLogic extends BaseLogic
$user = User::where('id', $order['uid'])->find(); $user = User::where('id', $order['uid'])->find();
if ($order->pay_type == 9) { if ($order->pay_type == 9) {
$order->status = 2; $order->status = 2;
}else{ } else {
$capitalFlowDao = new CapitalFlowLogic($user); $capitalFlowDao = new CapitalFlowLogic($user);
//微信支付和用户余额无关 //微信支付和用户余额无关
$capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order->pay_price,'',1); $capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order->pay_price, '', 1);
} }
self::afterPay($order,$extra['transaction_id']); self::afterPay($order, $extra['transaction_id']);
//活动期间消费 //活动期间消费
$check = DictType::where('type','activities')->find(); $check = DictType::where('type', 'activities')->find();
if(isset($check) && $check['status'] == 1 && $user['user_ship'] == 0){ if (isset($check) && $check['status'] == 1 && $user['user_ship'] == 0) {
self::dealChange($order['uid']); self::dealChange($order['uid']);
} }
@ -133,7 +132,7 @@ class PayNotifyLogic extends BaseLogic
public static function refund($orderSn, $extra = []) public static function refund($orderSn, $extra = [])
{ {
//更新状态 //更新状态
$order = StoreOrder::where('order_id',$orderSn)->findOrEmpty(); $order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->status == OrderEnum::REFUND_PAY) { if ($order->isEmpty() || $order->status == OrderEnum::REFUND_PAY) {
return true; return true;
} }
@ -144,36 +143,37 @@ class PayNotifyLogic extends BaseLogic
$order->refund_num += 1; $order->refund_num += 1;
$order->save(); $order->save();
// self::afterPay($order,$extra['transaction_id']); // self::afterPay($order,$extra['transaction_id']);
} }
public static function recharge($orderSn, $extra = []) public static function recharge($orderSn, $extra = [])
{ {
$order = UserRecharge::where('order_id', $orderSn)->findOrEmpty(); $order = UserRecharge::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) { if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
}
$price = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->price = $price;
$order->paid = 1;
$order->pay_time = time();
$order->save();
$uid = $order->uid;
$user = User::where('id',$uid)->findOrEmpty();
if ($user->isEmpty()) {
return true;
}
bcscale(2);
$user->now_money = bcadd($user->now_money, $price, 2);
$user->total_recharge_amount = bcadd($user->total_recharge_amount, $price, 2);
$user->save();
//用户的财务add
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userIncome('user_balance_recharge', 'user_recharge', $order['id'], $price);
//更新等级
self::dealLevel($uid,$user->total_recharge_amount);
return true; return true;
}
$price = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->price = $price;
$order->paid = 1;
$order->pay_time = time();
$order->save();
$uid = $order->uid;
$user = User::where('id', $uid)->findOrEmpty();
//用户的财务add
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userIncome('user_balance_recharge', 'user_recharge', $order['id'], $price);
if ($user->isEmpty()) {
return true;
}
bcscale(2);
$user->now_money = bcadd($user->now_money, $price, 2);
$user->total_recharge_amount = bcadd($user->total_recharge_amount, $price, 2);
$user->save();
//更新等级
self::dealLevel($uid, $user->total_recharge_amount);
return true;
} }
/** /**
@ -239,7 +239,7 @@ class PayNotifyLogic extends BaseLogic
* @param $order * @param $order
* @return void * @return void
*/ */
public static function afterPay($order,$transaction_id=0) public static function afterPay($order, $transaction_id = 0)
{ {
$financeLogic = new StoreFinanceFlowLogic(); $financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order; $financeLogic->order = $order;
@ -252,44 +252,42 @@ class PayNotifyLogic extends BaseLogic
$fees = bcdiv(bcmul($order['pay_price'], '0.02', 2), 1, 2); $fees = bcdiv(bcmul($order['pay_price'], '0.02', 2), 1, 2);
$financeLogic->in($transaction_id, $fees, OrderEnum::ORDER_COMMITION, $order['store_id']); //手续费入账 $financeLogic->in($transaction_id, $fees, OrderEnum::ORDER_COMMITION, $order['store_id']); //手续费入账
$financeLogic->out($transaction_id, $order['profit'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0);//商户获得的 $financeLogic->out($transaction_id, $order['profit'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0); //商户获得的
//冻结金额的 //冻结金额的
$frozen = bcsub($order->profit, $fees, 2); $frozen = bcsub($order->profit, $fees, 2);
//缴纳齐全了就加商户没有就加到平台 //缴纳齐全了就加商户没有就加到平台
$money_limt = SystemStore::where('id', $order['store_id'])->field('paid_deposit,security_deposit')->find(); $money_limt = SystemStore::where('id', $order['store_id'])->field('paid_deposit,security_deposit')->find();
$deposit = bcsub($money_limt['security_deposit'], $money_limt['paid_deposit'], 2);//剩余额度 $deposit = bcsub($money_limt['security_deposit'], $money_limt['paid_deposit'], 2); //剩余额度
if ($deposit > 0 && $frozen > 0) { if ($deposit > 0 && $frozen > 0) {
$amount = min($deposit, $frozen); $amount = min($deposit, $frozen);
$financeLogic->in($transaction_id, $amount, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id']); $financeLogic->in($transaction_id, $amount, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id']);
} }
} }
$financeLogic->save(); $financeLogic->save();
} }
} }
//等级处理 //等级处理
public static function dealLevel($uid,$total_money) public static function dealLevel($uid, $total_money)
{ {
$userShip = UserShip::where('limit', '<=', $total_money) $userShip = UserShip::where('limit', '<=', $total_money)
->field('id,title,limit') ->field('id,title,limit')
->order('limit', 'desc') ->order('limit', 'desc')
->find(); ->find();
$info = User::where('id',$uid)->findOrEmpty(); $info = User::where('id', $uid)->findOrEmpty();
if($info && $userShip){ if ($info && $userShip) {
if ($info->user_ship != $userShip['id']) { if ($info->user_ship != $userShip['id']) {
$info->user_ship = $userShip['id']; $info->user_ship = $userShip['id'];
$info->save(); $info->save();
} }
} }
return true; return true;
} }
//处理活动期间直接改用户的等级 //处理活动期间直接改用户的等级
public static function dealChange($uid) public static function dealChange($uid)
{ {
User::where('id',$uid)->update(['user_ship'=>UserShipEnum::LEVEL_ONE]); User::where('id', $uid)->update(['user_ship' => UserShipEnum::LEVEL_ONE]);
return true; return true;
} }
@ -300,18 +298,17 @@ class PayNotifyLogic extends BaseLogic
* @param $extra * @param $extra
* @return void * @return void
*/ */
public static function notifyLog($order,$extra) public static function notifyLog($order, $extra)
{ {
$data = [ $data = [
'pay_type'=>'wechat', 'pay_type' => 'wechat',
'type'=>OrderEnum::PAY, 'type' => OrderEnum::PAY,
'amount'=>$extra['amount']['payer_total'],//分 'amount' => $extra['amount']['payer_total'], //分
'order_sn'=>$order, 'order_sn' => $order,
'out_trade_no'=>$extra['transaction_id'], 'out_trade_no' => $extra['transaction_id'],
'attach'=>$extra['attach'], 'attach' => $extra['attach'],
'create_time'=>date('Y-m-d H:i:s',time()), 'create_time' => date('Y-m-d H:i:s', time()),
]; ];
PayNotify::create($data); PayNotify::create($data);
} }
} }