196 lines
8.7 KiB
PHP
196 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\account\Withdraws;
|
|
use app\common\traits\ServiceTrait;
|
|
use think\Db;
|
|
use think\Log;
|
|
|
|
class AccountWithdrawService
|
|
{
|
|
use ServiceTrait;
|
|
|
|
/** @var Withdraws */
|
|
public static $Model = Withdraws::class;
|
|
|
|
//转账类型:0=默认,1=微信,2=支付宝,3=线下转款
|
|
const TYPE_WECHAT = 1;
|
|
const TYPE_ALIPAY = 2;
|
|
const TYPE_OFFLINE = 3;
|
|
|
|
const TYPE_MAP = [
|
|
self::TYPE_WECHAT => '微信',
|
|
self::TYPE_ALIPAY => '支付宝',
|
|
self::TYPE_OFFLINE => '线下转款',
|
|
];
|
|
|
|
const STATUS_MAP = [
|
|
CommonService::ONGOING => '审核中',
|
|
CommonService::SUCCESS => '审核通过',
|
|
CommonService::FAILED => '审核驳回',
|
|
];
|
|
|
|
//提现限制(与小程序端展示的「提现规则」保持一致)
|
|
const WITHDRAW_MIN_AMOUNT = 0.1; //单笔最低(元)
|
|
const WITHDRAW_MAX_AMOUNT = 200; //单笔最高(元)
|
|
const WITHDRAW_DAY_MAX_COUNT = 10; //每个自然日最多申请次数
|
|
const WITHDRAW_DAY_MAX_AMOUNT = 2000; //每个自然日累计提现上限(元)
|
|
const WITHDRAW_AMOUNT_SCALE = 2; //金额小数位数
|
|
|
|
|
|
/**
|
|
* 用户申请提现
|
|
* @param int|string $userId 用户id
|
|
* @param int|float $amount 余额
|
|
* @param string $name 名字
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public static function applyWithdraw($userId, $amount, string $name = '')
|
|
{
|
|
//单笔金额校验(0.1~200元,最多两位小数)
|
|
$amount = self::checkWithdrawAmount($amount);
|
|
|
|
//每个自然日次数限制(已驳回的不计入)
|
|
$todayCount = self::todayQuery($userId)->count();
|
|
if ($todayCount >= self::WITHDRAW_DAY_MAX_COUNT) {
|
|
throw new \Exception('每个自然日最多可发起' . self::WITHDRAW_DAY_MAX_COUNT . '次提现申请,请明日再试');
|
|
}
|
|
|
|
//每个自然日累计金额限制(已驳回的不计入)
|
|
$todayAmount = (float)self::todayQuery($userId)->sum('amount');
|
|
if ($todayAmount + $amount > self::WITHDRAW_DAY_MAX_AMOUNT) {
|
|
throw new \Exception('每个自然日累计提现金额不超过' . self::WITHDRAW_DAY_MAX_AMOUNT
|
|
. '元,今日已申请' . sprintf('%.2f', $todayAmount) . '元,请明日再试');
|
|
}
|
|
|
|
//开启事务 抛出异常时自动回滚
|
|
Db::transaction(function () use ($userId, $amount, $name) {
|
|
if($name){
|
|
$user = UserService::getOne(['id' => $userId]);
|
|
if ($user->username != $name) {
|
|
$user->username = $name;
|
|
$user->save();
|
|
}
|
|
}
|
|
//添加提现记录
|
|
$data = [
|
|
'apply_sn' => generate_order_sn('TX'),
|
|
'user_id' => $userId,
|
|
'amount' => $amount,
|
|
'status' => AccountWithdrawService::$Common::ONGOING,
|
|
];
|
|
$recordRes = self::create($data);
|
|
if (!($recordRes)) {
|
|
throw new \Exception('提现申请失败');
|
|
}
|
|
//记录提现日志
|
|
$title = '用户发起提现';
|
|
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '-余额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $recordRes->id);
|
|
$log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额增加', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_INCREASE, $recordRes->id);
|
|
//减去余额,加到冻结里面
|
|
AccountService::updateData($userId, -$amount, $amount, 0,0, $log_res->id);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 单笔提现金额校验
|
|
* @param int|float|string $amount
|
|
* @return float 校验通过返回规范化后的金额(保留2位小数)
|
|
* @throws \Exception
|
|
*/
|
|
public static function checkWithdrawAmount($amount): float
|
|
{
|
|
if (!is_numeric($amount)) {
|
|
throw new \Exception('提现金额必须为数字');
|
|
}
|
|
$amount = floatval($amount);
|
|
if ($amount < self::WITHDRAW_MIN_AMOUNT || $amount > self::WITHDRAW_MAX_AMOUNT) {
|
|
throw new \Exception('单笔提现金额需在' . self::WITHDRAW_MIN_AMOUNT . '~'
|
|
. self::WITHDRAW_MAX_AMOUNT . '元之间');
|
|
}
|
|
if (round($amount, self::WITHDRAW_AMOUNT_SCALE) != $amount) {
|
|
throw new \Exception('提现金额最多保留' . self::WITHDRAW_AMOUNT_SCALE . '位小数');
|
|
}
|
|
|
|
return round($amount, self::WITHDRAW_AMOUNT_SCALE);
|
|
}
|
|
|
|
/**
|
|
* 当日提现查询对象(已驳回的不计入次数与累计金额)
|
|
* @param int $userId
|
|
* @return \think\db\Query
|
|
*/
|
|
private static function todayQuery($userId)
|
|
{
|
|
return self::$Model::where('user_id', $userId)
|
|
->whereTime('createtime', 'today')
|
|
->where('status', '<>', CommonService::FAILED);
|
|
}
|
|
|
|
/**
|
|
* 审核提现
|
|
* @param $withdraw_record_id int 提现申请id
|
|
* @param $approval bool 是否同意
|
|
* @param $remark string 驳回意见
|
|
* @param $type int 转款方式
|
|
* @throws \Exception
|
|
*/
|
|
public static function approvalWithdraw($withdraw_record_id, $approval = false, $remark = '', $type = 0)
|
|
{
|
|
$withdraw_record = AccountWithdrawService::get($withdraw_record_id);
|
|
if (!$withdraw_record) {
|
|
throw new \Exception('提现申请不存在');
|
|
}
|
|
if ($withdraw_record->status == AccountWithdrawService::$Common::SUCCESS) {
|
|
throw new \Exception('已经提现成功,请勿重复操作');
|
|
}
|
|
$userId = $withdraw_record->user_id;
|
|
$amount = $withdraw_record->amount;
|
|
$user = UserService::get($userId);
|
|
|
|
//检测收支是否相同
|
|
// if(!AccountService::checkBreakEven($userId, $user->is_merchant)){
|
|
// throw new \Exception('该用户余额收支异常');
|
|
// }
|
|
//提现通过
|
|
if ($approval) {
|
|
//记录提现日志
|
|
$title = '用户提现成功(' . self::TYPE_MAP[$type] .')';
|
|
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $withdraw_record->id);
|
|
//减去冻结
|
|
AccountService::updateData($userId, 0, -$amount, 0,0, $log_res->id);
|
|
|
|
if($type == AccountWithdrawService::TYPE_WECHAT){
|
|
//免确认收款授权校验:未授权无法以授权单发起转账
|
|
$auth = TransferAuthorizationService::getByUserId($userId);
|
|
if (!TransferAuthorizationService::isAuthorized($userId)) {
|
|
throw new \Exception('该用户未完成免确认收款授权,无法发起转账,请通知用户在小程序余额页完成授权后重新审核');
|
|
}
|
|
//发起微信商家转账到用户零钱(v3授权转账:以授权单定位收款人,免用户确认收款)
|
|
$payRes = (new EasyWeChatService())->mchPay($withdraw_record->apply_sn, $user->username, $amount, $auth->out_authorization_no);
|
|
//以 transfer_bill_no(微信转账单号) 判断单据是否创建成功
|
|
if (empty($payRes['transfer_bill_no'])) {
|
|
throw new \Exception($payRes['message'] ?? '商家转账发起失败');
|
|
}
|
|
//授权转账免用户确认,状态记为微信返回的 state,最终以回调/查单为准
|
|
$withdraw_record->result_status = $payRes['state'] ?? '';
|
|
$withdraw_record->save();
|
|
}
|
|
} else {
|
|
if (!$remark) {
|
|
throw new \Exception('驳回原因必填');
|
|
}
|
|
if ($withdraw_record->status == AccountWithdrawService::$Common::FAILED) {
|
|
throw new \Exception('已经审核驳回,请勿重复操作');
|
|
}
|
|
//记录提现日志
|
|
$title = '提现失败(' . $remark .')';
|
|
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $withdraw_record->id);
|
|
$log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '-余额增加', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_INCREASE, $withdraw_record->id);
|
|
//减去冻结,返回到余额
|
|
AccountService::updateData($userId, $amount, -$amount, 0,0, $log_res->id);
|
|
}
|
|
}
|
|
} |