|
@@ -9,9 +9,10 @@ use think\exception\HttpResponseException;
|
|
|
use app\common\model\Withdraw as WithdrawModel;
|
|
|
use app\common\model\WithdrawLog as WithdrawLogModel;
|
|
|
use app\common\library\Operator;
|
|
|
+use app\common\library\BcMath;
|
|
|
use app\common\model\ThirdOauth;
|
|
|
use app\common\Service\Wallet as WalletService;
|
|
|
-use app\common\Pay\PayService;
|
|
|
+use app\common\Service\Pay\PayService;
|
|
|
use app\common\model\User;
|
|
|
|
|
|
class Withdraw
|
|
@@ -32,10 +33,10 @@ class Withdraw
|
|
|
|
|
|
// 提现规则
|
|
|
$config = shop_config('shop.withdraw');
|
|
|
- $config['min_amount'] = $config['min_amount'] == 0 ? $config['min_amount'] : number_format(floatval($config['min_amount']), 2, '.', '');
|
|
|
- $config['max_amount'] = $config['max_amount'] == 0 ? $config['max_amount'] : number_format(floatval($config['max_amount']), 2, '.', '');
|
|
|
+ $config['min_amount'] = BcMath::isZero($config['min_amount']) ? $config['min_amount'] : BcMath::format($config['min_amount'], 2);
|
|
|
+ $config['max_amount'] = BcMath::isZero($config['max_amount']) ? $config['max_amount'] : BcMath::format($config['max_amount'], 2);
|
|
|
$config['charge_rate_format'] = round(floatval($config['charge_rate']), 1); // 1 位小数
|
|
|
- $config['charge_rate'] = round((floatval($config['charge_rate']) * 0.01), 3);
|
|
|
+ $config['charge_rate'] = BcMath::div($config['charge_rate'], '100', 3);
|
|
|
$config['num_unit'] = $config['num_unit'] == 'day' ? '天' : '月';
|
|
|
|
|
|
$this->config = $config;
|
|
@@ -46,11 +47,11 @@ class Withdraw
|
|
|
public function apply($params)
|
|
|
{
|
|
|
$type = $params['type'];
|
|
|
- $money = $params['money'] ?? 0;
|
|
|
+ $money = $params['amount'] ?? 0; // money 改为 amount
|
|
|
$money = (string)$money;
|
|
|
|
|
|
// 手续费
|
|
|
- $charge = bcmul($money, (string)$this->config['charge_rate'], 2);
|
|
|
+ $charge = BcMath::mul($money, $this->config['charge_rate'], 2);
|
|
|
|
|
|
// 检查提现规则
|
|
|
$this->checkApply($type, $money, $charge);
|
|
@@ -71,7 +72,7 @@ class Withdraw
|
|
|
$withdraw->save();
|
|
|
|
|
|
// 佣金钱包变动
|
|
|
- WalletService::change($this->user, 'commission', -bcadd($charge, $money, 2), 'withdraw', [
|
|
|
+ WalletService::change($this->user, 'commission', '-' . BcMath::add($charge, $money, 2), 'withdraw', [
|
|
|
'withdraw_id' => $withdraw->id,
|
|
|
'amount' => $withdraw->amount,
|
|
|
'charge_fee' => $withdraw->charge_fee,
|
|
@@ -94,10 +95,10 @@ class Withdraw
|
|
|
->find();
|
|
|
|
|
|
if (!$withdraw) {
|
|
|
- throw new ShoproException('提现记录不存在');
|
|
|
+ throw new BusinessException('提现记录不存在');
|
|
|
}
|
|
|
if (request()->header('platform') !== $withdraw->platform) {
|
|
|
- throw new ShoproException('请在发起该次提现的平台操作');
|
|
|
+ throw new BusinessException('请在发起该次提现的平台操作');
|
|
|
}
|
|
|
|
|
|
$this->handleLog($withdraw, '用户重新发起提现申请');
|
|
@@ -115,18 +116,18 @@ class Withdraw
|
|
|
->find();
|
|
|
|
|
|
if (!$withdraw) {
|
|
|
- throw new ShoproException('提现记录不存在');
|
|
|
+ throw new BusinessException('提现记录不存在');
|
|
|
}
|
|
|
|
|
|
if (request()->header('platform') !== $withdraw->platform) {
|
|
|
- throw new ShoproException('请在发起该次提现的平台操作');
|
|
|
+ throw new BusinessException('请在发起该次提现的平台操作');
|
|
|
}
|
|
|
|
|
|
// 实时检查提现单状态
|
|
|
$withdraw = $this->checkWechatTransferResult($withdraw);
|
|
|
|
|
|
if ($withdraw->wechat_transfer_state !== 'WAIT_USER_CONFIRM') {
|
|
|
- throw new ShoproException('该提现单暂无法发起取消操作,请联系客服');
|
|
|
+ throw new BusinessException('该提现单暂无法发起取消操作,请联系客服');
|
|
|
}
|
|
|
// 提交微信撤销单
|
|
|
$payService = new PayService($withdraw->withdraw_type, $withdraw->platform);
|
|
@@ -138,7 +139,7 @@ class Withdraw
|
|
|
} catch (\Exception $e) {
|
|
|
\think\Log::error('撤销提现失败: ' . ': 行号: ' . $e->getLine() . ': 文件: ' . $e->getFile() . ': 错误信息: ' . $e->getMessage());
|
|
|
$this->handleLog($withdraw, '撤销微信商家转账失败: ' . $e->getMessage());
|
|
|
- throw new ShoproException($e->getMessage());
|
|
|
+ throw new BusinessException($e->getMessage());
|
|
|
}
|
|
|
|
|
|
$withdraw->wechat_transfer_state = $result['state'];
|
|
@@ -181,7 +182,7 @@ class Withdraw
|
|
|
// $withdraw->wechat_transfer_state = 'NOT_FOUND';
|
|
|
$withdraw->save();
|
|
|
$this->handleFail($withdraw, $e->getMessage());
|
|
|
- throw new ShoproException($e->getMessage());
|
|
|
+ throw new BusinessException($e->getMessage());
|
|
|
}
|
|
|
|
|
|
// 发起提现失败,自动处理退还佣金
|
|
@@ -199,16 +200,16 @@ class Withdraw
|
|
|
$withdraw = $this->handleWithdraw($withdraw, $this->user);
|
|
|
|
|
|
Db::commit();
|
|
|
- } catch (ShoproException $e) {
|
|
|
+ } catch (BusinessException $e) {
|
|
|
Db::commit(); // 不回滚,记录错误日志
|
|
|
- throw new ShoproException($e->getMessage());
|
|
|
+ throw new BusinessException($e->getMessage());
|
|
|
} catch (HttpResponseException $e) {
|
|
|
$data = $e->getResponse()->getData();
|
|
|
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
|
|
- throw new ShoproException($message);
|
|
|
+ throw new BusinessException($message);
|
|
|
} catch (\Exception $e) {
|
|
|
Db::rollback();
|
|
|
- throw new ShoproException($e->getMessage());
|
|
|
+ throw new BusinessException($e->getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -218,7 +219,7 @@ class Withdraw
|
|
|
public function handleAgree($withdraw, $oper = null)
|
|
|
{
|
|
|
if ($withdraw->status != 0) {
|
|
|
- throw new ShoproException('请勿重复操作');
|
|
|
+ throw new BusinessException('请勿重复操作');
|
|
|
}
|
|
|
$withdraw->status = 1;
|
|
|
$withdraw->save();
|
|
@@ -230,7 +231,7 @@ class Withdraw
|
|
|
{
|
|
|
$withDrawStatus = false;
|
|
|
if ($withdraw->status != 1) {
|
|
|
- throw new ShoproException('请勿重复操作');
|
|
|
+ throw new BusinessException('请勿重复操作');
|
|
|
}
|
|
|
if ($withdraw->withdraw_type !== 'bank') {
|
|
|
$withDrawStatus = $this->handleTransfer($withdraw);
|
|
@@ -250,13 +251,13 @@ class Withdraw
|
|
|
public function handleRefuse($withdraw, $refuse_msg)
|
|
|
{
|
|
|
if ($withdraw->status != 0 && $withdraw->status != 1) {
|
|
|
- throw new ShoproException('请勿重复操作');
|
|
|
+ throw new BusinessException('请勿重复操作');
|
|
|
}
|
|
|
$withdraw->status = -1;
|
|
|
$withdraw->save();
|
|
|
|
|
|
// 退回用户佣金
|
|
|
- WalletService::change($this->user, 'commission', bcadd($withdraw->charge_fee, $withdraw->amount, 2), 'withdraw_error', [
|
|
|
+ WalletService::change($this->user, 'commission', BcMath::add($withdraw->charge_fee, $withdraw->amount, 2), 'withdraw_error', [
|
|
|
'withdraw_id' => $withdraw->id,
|
|
|
'amount' => $withdraw->amount,
|
|
|
'charge_fee' => $withdraw->charge_fee,
|
|
@@ -271,13 +272,13 @@ class Withdraw
|
|
|
public function handleFail($withdraw, $refuse_msg, $status = -2)
|
|
|
{
|
|
|
if ($withdraw->status != 0 && $withdraw->status != 1) {
|
|
|
- throw new ShoproException('请勿重复操作');
|
|
|
+ throw new BusinessException('请勿重复操作');
|
|
|
}
|
|
|
$withdraw->status = $status;
|
|
|
$withdraw->save();
|
|
|
|
|
|
// 退回用户佣金
|
|
|
- WalletService::change($this->user, 'commission', bcadd($withdraw->charge_fee, $withdraw->amount, 2), 'withdraw_error', [
|
|
|
+ WalletService::change($this->user, 'commission', BcMath::add($withdraw->charge_fee, $withdraw->amount, 2), 'withdraw_error', [
|
|
|
'withdraw_id' => $withdraw->id,
|
|
|
'amount' => $withdraw->amount,
|
|
|
'charge_fee' => $withdraw->charge_fee,
|
|
@@ -293,17 +294,17 @@ class Withdraw
|
|
|
{
|
|
|
$withdraw = WithdrawModel::where('withdraw_sn', $withdrawSn)->find();
|
|
|
if (!$withdraw) {
|
|
|
- throw new ShoproException('提现记录不存在');
|
|
|
+ throw new BusinessException('提现记录不存在');
|
|
|
}
|
|
|
|
|
|
if ($withdraw->status !== 1) {
|
|
|
- throw new ShoproException('提现记录状态不正确');
|
|
|
+ throw new BusinessException('提现记录状态不正确');
|
|
|
}
|
|
|
|
|
|
$this->user = User::get($withdraw->user_id);
|
|
|
|
|
|
if (!$this->user) {
|
|
|
- throw new ShoproException('用户不存在');
|
|
|
+ throw new BusinessException('用户不存在');
|
|
|
}
|
|
|
|
|
|
$withdraw->wechat_transfer_state = $action;
|
|
@@ -331,7 +332,7 @@ class Withdraw
|
|
|
public function checkWechatTransferResult($withdraw)
|
|
|
{
|
|
|
if ($withdraw->createtime < strtotime('-30 day')) {
|
|
|
- throw new ShoproException('提现单据已过期,请联系客服解决');
|
|
|
+ throw new BusinessException('提现单据已过期,请联系客服解决');
|
|
|
}
|
|
|
$payService = new PayService($withdraw->withdraw_type, $withdraw->platform);
|
|
|
|
|
@@ -349,7 +350,6 @@ class Withdraw
|
|
|
// 企业付款提现
|
|
|
private function handleTransfer($withdraw)
|
|
|
{
|
|
|
- operate_disabled();
|
|
|
$type = $withdraw->withdraw_type;
|
|
|
$platform = $withdraw->platform;
|
|
|
|
|
@@ -378,15 +378,15 @@ class Withdraw
|
|
|
$withdraw->save();
|
|
|
return true;
|
|
|
}
|
|
|
- throw new ShoproException(json_encode($response, JSON_UNESCAPED_UNICODE));
|
|
|
+ throw new BusinessException(json_encode($response, JSON_UNESCAPED_UNICODE));
|
|
|
} catch (HttpResponseException $e) {
|
|
|
$data = $e->getResponse()->getData();
|
|
|
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
|
|
- throw new ShoproException($message);
|
|
|
+ throw new BusinessException($message);
|
|
|
} catch (\Exception $e) {
|
|
|
\think\Log::error('提现失败:' . ' 行号:' . $e->getLine() . '文件:' . $e->getFile() . '错误信息:' . $e->getMessage());
|
|
|
$this->handleLog($withdraw, '提现失败:' . $e->getMessage());
|
|
|
- throw new ShoproException($e->getMessage()); // 弹出错误信息
|
|
|
+ throw new BusinessException($e->getMessage()); // 弹出错误信息
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
@@ -412,25 +412,28 @@ class Withdraw
|
|
|
private function checkApply($type, $money, $charge)
|
|
|
{
|
|
|
if (!in_array($type, $this->config['methods'])) {
|
|
|
- throw new ShoproException('暂不支持该提现方式');
|
|
|
+ throw new BusinessException('暂不支持该提现方式');
|
|
|
}
|
|
|
|
|
|
- if ($money <= 0) {
|
|
|
- throw new ShoproException('请输入正确的提现金额');
|
|
|
+ // 使用BC数学函数检查金额是否大于0
|
|
|
+ if (!BcMath::isPositive($money)) {
|
|
|
+ throw new BusinessException('请输入正确的提现金额');
|
|
|
}
|
|
|
|
|
|
// 检查最小提现金额
|
|
|
- if ($this->config['min_amount'] > 0 && $money < $this->config['min_amount']) {
|
|
|
- throw new ShoproException('提现金额不能少于 ' . $this->config['min_amount'] . '元');
|
|
|
+ if (BcMath::isPositive($this->config['min_amount']) && BcMath::comp($money, $this->config['min_amount']) < 0) {
|
|
|
+ throw new BusinessException('提现金额不能少于 ' . $this->config['min_amount'] . '元');
|
|
|
}
|
|
|
+
|
|
|
// 检查最大提现金额
|
|
|
- if ($this->config['max_amount'] > 0 && $money > $this->config['max_amount']) {
|
|
|
- throw new ShoproException('提现金额不能大于 ' . $this->config['max_amount'] . '元');
|
|
|
+ if (BcMath::isPositive($this->config['max_amount']) && BcMath::comp($money, $this->config['max_amount']) > 0) {
|
|
|
+ throw new BusinessException('提现金额不能大于 ' . $this->config['max_amount'] . '元');
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- if ($this->user->commission < bcadd($charge, $money, 2)) {
|
|
|
- throw new ShoproException('可提现佣金不足');
|
|
|
+ // 检查用户佣金余额是否足够
|
|
|
+ $totalAmount = BcMath::add($charge, $money, 2);
|
|
|
+ if (BcMath::comp($this->user->commission, $totalAmount) < 0) {
|
|
|
+ throw new BusinessException('可提现佣金不足');
|
|
|
}
|
|
|
|
|
|
// 检查最大提现次数
|
|
@@ -440,7 +443,7 @@ class Withdraw
|
|
|
$num = WithdrawModel::where('user_id', $this->user->id)->where('createtime', '>=', $start_time)->count();
|
|
|
|
|
|
if ($num >= $this->config['max_num']) {
|
|
|
- throw new ShoproException('每' . ($this->config['num_unit'] == 'day' ? '日' : '月') . '提现次数不能大于 ' . $this->config['max_num'] . '次');
|
|
|
+ throw new BusinessException('每' . ($this->config['num_unit'] == 'day' ? '日' : '月') . '提现次数不能大于 ' . $this->config['max_num'] . '次');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -449,6 +452,17 @@ class Withdraw
|
|
|
// 获取提现账户信息
|
|
|
private function getAccountInfo($type, $params)
|
|
|
{
|
|
|
+ // 根据账户ID查询账户信息
|
|
|
+ $account = \app\common\model\user\Account::where([
|
|
|
+ 'id' => $params['account_id'],
|
|
|
+ 'user_id' => $this->user->id,
|
|
|
+ 'type' => $type
|
|
|
+ ])->find();
|
|
|
+
|
|
|
+ if (!$account) {
|
|
|
+ throw new BusinessException('账户信息不存在');
|
|
|
+ }
|
|
|
+
|
|
|
$platform = request()->header('platform');
|
|
|
|
|
|
switch ($type) {
|
|
@@ -460,30 +474,30 @@ class Withdraw
|
|
|
}
|
|
|
$thirdOauth = ThirdOauth::where('provider', 'wechat')->where('platform', $platform)->where('user_id', $this->user->id)->find();
|
|
|
if (!$thirdOauth) {
|
|
|
- throw new ShoproException('请先绑定微信账号', -1);
|
|
|
+ throw new BusinessException('请先绑定微信账号', -1);
|
|
|
}
|
|
|
$withdrawInfo = [
|
|
|
- '真实姓名' => $params['account_name'],
|
|
|
+ '真实姓名' => $account->account_name,
|
|
|
'微信用户' => $thirdOauth['nickname'] ?: $this->user['nickname'],
|
|
|
'微信ID' => $thirdOauth['openid'],
|
|
|
];
|
|
|
break;
|
|
|
case 'alipay':
|
|
|
$withdrawInfo = [
|
|
|
- '真实姓名' => $params['account_name'],
|
|
|
- '支付宝账户' => $params['account_no']
|
|
|
+ '真实姓名' => $account->account_name,
|
|
|
+ '支付宝账户' => $account->account_no
|
|
|
];
|
|
|
break;
|
|
|
case 'bank':
|
|
|
$withdrawInfo = [
|
|
|
- '真实姓名' => $params['account_name'],
|
|
|
- '开户行' => $params['account_header'] ?? '',
|
|
|
- '银行卡号' => $params['account_no']
|
|
|
+ '真实姓名' => $account->account_name,
|
|
|
+ '开户行' => $account->account_header,
|
|
|
+ '银行卡号' => $account->account_no
|
|
|
];
|
|
|
break;
|
|
|
}
|
|
|
if (!isset($withdrawInfo)) {
|
|
|
- throw new ShoproException('您的提现信息有误');
|
|
|
+ throw new BusinessException('您的提现信息有误');
|
|
|
}
|
|
|
|
|
|
return $withdrawInfo;
|