wallet tron transactions

This commit is contained in:
cano
2024-03-25 06:15:36 +08:00
parent 4b8f205e86
commit 489090382f
83 changed files with 5424 additions and 1343 deletions

View File

@ -67,57 +67,70 @@ class WalletPlatformBalanceModel extends WalletBaseModel
}
return $resModel;
}
//增加平台余额
function incAvailableBalance($id, $amount): bool
function frozenAmount($id,$amount): bool|int
{
$amount = abs($amount);
$oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'available_amount']);
if (!$oModel) throw new ModelException('找不到id');
$oModel->frozen_amount = Db::raw('total_amount + ' . $amount);
$oModel->available_amount = Db::raw('available_amount + ' . $amount);
return $oModel->save();
$updateItem = [
'id' => $id,
'frozen_amount' => DB::raw('frozen_amount + '. $amount),
'available_amount' => DB::raw('available_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
//扣除平台余额
function decAvailableBalance($id, $amount): bool
function unFrozenAmount($id,$amount): bool|int
{
$amount = abs($amount);
$oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'available_amount']);
if (!$oModel) throw new ModelException('找不到id');
$oModel->frozen_amount = Db::raw('total_amount - ' . $amount);
$oModel->available_amount = Db::raw('available_amount - ' . $amount);
return $oModel->save();
$updateItem = [
'id' => $id,
'frozen_amount' => DB::raw('frozen_amount - '. $amount),
'available_amount' => DB::raw('available_amount + '. $amount),
];
return $this->updateItem($updateItem);
}
function decFrozenAmountById($id, $amount): int
function subFrozenAmount($id, $amount): bool|int
{
$amount = abs($amount);
$oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'frozen_amount']);
if (!$oModel) throw new ModelException('找不到id');
$oModel->frozen_amount = Db::raw('frozen_amount - ' . $amount);
$oModel->total_amount = Db::raw('total_amount - ' . $amount);
return $oModel->save();
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount - '. $amount),
'frozen_amount' => DB::raw('frozen_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
function unFrozenAmountById($id, $amount): int
function addFrozenAmount($id, $amount): bool|int
{
$amount = abs($amount);
$oModel = $this->newQuery()->where('id', $id)->first(['frozen_amount', 'available_amount']);
if (!$oModel) throw new ModelException('找不到id');
$oModel->frozen_amount = Db::raw('frozen_amount - ' . $amount);
$oModel->available_amount = Db::raw('available_amount + ' . $amount);
return $oModel->save();
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount + '. $amount),
'frozen_amount' => DB::raw('frozen_amount + '. $amount),
];
return $this->updateItem($updateItem);
}
function frozenAmountById($id, $amount): int
function subAvailableAmount($id, $amount): bool|int
{
$amount = abs($amount);
$oModel = $this->newQuery()->where('id', $id)->first(['frozen_amount', 'available_amount']);
if (!$oModel) throw new ModelException('找不到id');
$oModel->frozen_amount = Db::raw('frozen_amount + ' . $amount);
$oModel->available_amount = Db::raw('available_amount - ' . $amount);
return $oModel->save();
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount - '. $amount),
'available_amount' => DB::raw('available_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
function addAvailableAmount($id, $amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount + '. $amount),
'available_amount' => DB::raw('available_amount + '. $amount),
];
return $this->updateItem($updateItem);
}

View File

@ -2,255 +2,453 @@
namespace App\Models\Wallet\Platform;
use App\Bean\Model\Wallet\Platform\WalletPlatformBalanceTransactionBean;
use App\Bean\Queue\Wallet\QueueNotifyToPlatformBean;
use App\Bean\Queue\Wallet\QueueWalletPlatformWithdrawTransferBean;
use App\Cache\Table\Wallet\TableWalletSettingCache;
use App\Exceptions\ModelException;
use App\Jobs\Wallet\WalletNotifyToPlatformQueue;
use App\Jobs\Wallet\WalletPlatformWithdrawTransferQueue;
use App\Models\Wallet\Base\WalletBaseModel;
use App\Models\Wallet\Wallet\WalletCurrencyModel;
use App\Models\Wallet\Wallet\WalletAddressModel;
use App\Tools\Logs;
use App\Tools\Math;
use App\Tools\Times;
use App\Tools\Tools;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Symfony\Contracts\Service;
class WalletPlatformBalanceTransactionModel extends WalletBaseModel
{
protected $table = 'wallet_platform_balance_transaction';
protected $table = 'wallet_address_transaction';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'sn',
'type',
'status',
'platform_id',
'currency_id',
'currency_code',
'currency_type',
'from_wallet_addr_id',
'from_user_transaction_id',
'from_uid',
'wallet_addr',
'from_wallet_transaction_id',
'received_amount',
'entered_amount',
'balance_id',
'block_transaction_id',
'bind_wallet_address_id',
'bind_wallet_address',
'from_wallet_address_id',
'from_wallet_address',
'to_wallet_address_id',
'to_wallet_address',
'uid',
'amount',
'fee_amount',
'before_total_amount',
'after_total_amount',
'desc_key',
'desc',
'remark',
'sign',
'source_wallet_id',
'callback_time',
'callback_wallet_address_transaction_id',
'is_notify',
'created_at',
'updated_at',
];
const TYPE_USER_RECHARGE = 1;
const TYPE_USER_WITHDRAW = 2;
const TYPE_ADMIN_ADD = 3;
const TYPE_ADMIN_DEC = 4;
const TYPE_RECHARGE_ADD = 1;
const TYPE_WITHDRAW_DEC = 2;
const TYPE_TRANSACTION_FEE_SUB = 10;
const TYPE_TRANSACTION_FEE_ADD = 11;
const TYPE_ADMIN_ADD = 12;
const TYPE_ADMIN_SUB = 13;
const TYPE = [
self::TYPE_USER_RECHARGE => '用户充值',
self::TYPE_USER_WITHDRAW => '用户提现',
self::TYPE_ADMIN_ADD => '管理员加',
self::TYPE_ADMIN_DEC => '管理员减',
self::TYPE_RECHARGE_ADD => '充值',
self::TYPE_WITHDRAW_DEC => '提现',
self::TYPE_TRANSACTION_FEE_SUB => '交易手续费扣款',
self::TYPE_TRANSACTION_FEE_ADD => '交易手续费加款',
self::TYPE_ADMIN_ADD => '管理员加款',
self::TYPE_ADMIN_SUB => '管理员扣款',
];
const STATUS_WAITING_QUEUE = 1;
const STATUS_CHAIN_WAITING_CALLBACK = 2;
const STATUS_WAIT = 1;
const STATUS_PROCESSING = 2;
const STATUS_SUCCESS = 3;
const STATUS_FAIL = 4;
const STATUS = [
self::STATUS_WAITING_QUEUE => '等待队列',
self::STATUS_CHAIN_WAITING_CALLBACK => '链上等待回调',
self::STATUS_WAIT => '等待处理',
self::STATUS_PROCESSING => '处理中',
self::STATUS_SUCCESS => '成功',
self::STATUS_FAIL => '失败',
];
function addPlatformTransaction(
$type,
$status,
$received_amount,
$entered_amount,
$platform_id,
$currency_code,
$from_uid = '',
$from_user_transaction_id = '',
$from_wallet_addr_id = '',
$wallet_addr = '',
$desc_key = '',
$desc = '',
$remark = '',
): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
$received_amount = abs($received_amount);
$entered_amount = abs($entered_amount);
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$oWalletCurrencyModel = new WalletCurrencyModel();
$resWalletPlatformBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalance($platform_id, $currency_code);
if (!$resWalletPlatformBalanceModel) throw new ModelException('platform balance not found');
$resWalletCurrencyModel = $oWalletCurrencyModel->findByCode($currency_code);
if (!$resWalletCurrencyModel) throw new ModelException('currency_code error');
if (in_array($type, [self::TYPE_ADMIN_DEC, self::TYPE_USER_WITHDRAW])) {
if (Math::bcComp($resWalletPlatformBalanceModel->available_amount, $received_amount) == -1) throw new ModelException('platform balance not enough');
}
//提现费率计算
$fee_amount = 0;
if ($type == self::TYPE_USER_WITHDRAW) {
$fee_amount = $oWalletCurrencyModel->computeTransferRate($received_amount, $oWalletCurrencyModel->transfer_rate);
$entered_amount = Math::bcSub($received_amount, $fee_amount);
}
if($type == self::TYPE_USER_RECHARGE){
$fee_amount = Math::bcSub($received_amount, $entered_amount);
}
$insert = [
'type' => $type,
'status' => $status,
'platform_id' => $platform_id,
'currency_id' => $resWalletPlatformBalanceModel->currency_id,
'currency_code' => $resWalletPlatformBalanceModel->currency_code,
'currency_type' => $resWalletPlatformBalanceModel->currency_type,
'from_wallet_addr_id' => $from_wallet_addr_id,
'from_user_transaction_id' => $from_user_transaction_id,
'from_uid' => $from_uid,
'wallet_addr' => $wallet_addr,
'received_amount' => $received_amount,
'entered_amount' => $entered_amount,
'fee_amount' => $fee_amount,
'before_total_amount' => $resWalletPlatformBalanceModel->total_amount,
'after_total_amount' => Math::bcAdd($resWalletPlatformBalanceModel->total_amount, $entered_amount),
'desc_key' => $desc_key,
'desc' => $desc,
'remark' => $remark,
];
// $insert['sign'] = '';
$insert['created_at'] = Times::getNowDateTime();
$insert['updated_at'] = Times::getNowDateTime();
return $this->addItem($insert);
}
//增加提现账变并且冻结平台金额
function newWithdrawPlatformTransaction(
$amount,
$platform_id,
$currency_code,
$uid,
): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
const IS_NOTIFY_WAIT = 1;
const IS_NOTIFY_PROCESS = 2;
const IS_NOTIFY_FINISH = 3;
const IS_NOTIFY_FAIL = 4;
const IS_NOTIFY = [
self::IS_NOTIFY_WAIT => '待处理',
self::IS_NOTIFY_PROCESS => '处理中',
self::IS_NOTIFY_FINISH => '处理完成',
self::IS_NOTIFY_FAIL => '处理失败',
];
//管理员扣款
function typeAdminSub($platform_id, $uid, $currency_code, $amount, $remark = null): bool
{
$bean = new WalletPlatformBalanceTransactionBean();
$bean->setPlatformId($platform_id);
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //负数
$bean->setType(self::TYPE_ADMIN_SUB);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
Db::beginTransaction();
$resModel = $this->addPlatformTransaction(self::TYPE_USER_WITHDRAW, self::STATUS_WAITING_QUEUE, $amount,$amount, $platform_id, $currency_code,$uid);
if(!$resModel) throw new ModelException('addPlatformTransaction error');
DB::beginTransaction();
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$res = $oWalletPlatformBalanceModel->findPlatformBalance($platform_id,$currency_code);
if (!$res) throw new ModelException('findPlatformBalance error');
$res = $oWalletPlatformBalanceModel->frozenAmountById($oWalletPlatformBalanceModel->id, $amount); //变更平台余额
if (!$res) throw new ModelException('frozenAmountById error');
Db::commit();
return $resModel;
}catch (\Exception $e) {
Db::rollBack();
Log::error('newWithdrawPlatformTransaction', ['code' => $e->getCode(), 'error' => $e->getMessage()]);
return false;
}
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
}
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcSub($resBalanceModel->total_amount, abs($amount)));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oWalletPlatformBalanceModel->subAvailableAmount($bean->getBalanceId(), abs($amount));
if (!$res) throw new ModelException('subAvailableAmount fail');
//成功提现处理
function withdrawSuccByUserTransactionId($user_transaction_id,$entered_amount, $remark = ''): bool
{
try{
Db::beginTransaction();
$resModel = $this->findItemByWhere(['from_user_transaction_id' => $user_transaction_id, 'type' => self::TYPE_USER_WITHDRAW]);
if (!$resModel) throw new ModelException('user transaction not found');
if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) throw new ModelException('status error');
$updateItems = [
'id' => $resModel->id,
'status' => self::STATUS_SUCCESS,
'updated_at' => Times::getNowDateTime(),
'entered_amount' => $entered_amount,
'fee_amount' => Db::raw('received_amount - '.$entered_amount),
];
if(!empty($remark)) $updateItems['remark'] = $remark;
$res = $this->updateItem($updateItems);
if(!$res) throw new ModelException('save error');
//扣除平台冻结余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resWalletPlatformBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalance($resModel->platform_id, $resModel->currency_code);
if (!$resWalletPlatformBalanceModel) throw new ModelException('platform balance not found');
$res = $oWalletPlatformBalanceModel->decFrozenAmountById($resWalletPlatformBalanceModel->id, $resModel->received_amount);
if (!$res) throw new ModelException('incBalance error');
Db::commit();
return true;
}catch (\Exception $e){
Db::rollBack();
Log::error('withdrawSuccByUserTransactionIdErr', ['code' => $e->getCode(), 'error' => $e->getMessage()]);
return false;
}
}
//失败提现处理
function withdrawErrByUserTransactionId($user_transaction_id, $remark = ''): bool
{
try {
Db::beginTransaction();
$resModel = $this->findItemByWhere(['from_user_transaction_id' => $user_transaction_id, 'type' => self::TYPE_USER_WITHDRAW]);
if (!$resModel) throw new ModelException('user transaction not found');
if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) throw new ModelException('status error');
//更改账变状态
$updateItems = [
'id' => $resModel->id,
'status' => self::STATUS_FAIL,
'updated_at' => Times::getNowDateTime(),
];
if(!empty($remark)) $updateItems['remark'] = $remark;
$res = $this->updateItem($updateItems);
if (!$res) throw new ModelException('save error');
//解冻平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resWalletPlatformBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalance($resModel->platform_id, $resModel->currency_code);
if (!$resWalletPlatformBalanceModel) throw new ModelException('platform balance not found');
$res = $oWalletPlatformBalanceModel->unFrozenAmountById($resWalletPlatformBalanceModel->id, $resModel->received_amount);
if (!$res) throw new ModelException('incBalance error');
Db::commit();
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
Db::rollBack();
Log::error('withdrawErrByUserTransactionIdErr', ['code' => $e->getCode(), 'error' => $e->getMessage()]);
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' rollBack', $e, func_get_args());
return false;
}
}
function newRechargePlatformTransaction(
$resWalletAddrTransactionModel,
$resWalletPlatformUserTransactionModel,
$amount,
$platform_id,
$currency_code,
): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
//管理员加款
function typeAdminAdd($platform_id, $uid, $currency_code, $amount, $remark = null): bool
{
$bean = new WalletPlatformBalanceTransactionBean();
$bean->setPlatformId($platform_id);
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setType(self::TYPE_ADMIN_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
Db::beginTransaction();
$resModel = $this->addPlatformTransaction(self::TYPE_USER_RECHARGE, self::STATUS_SUCCESS, $amount,$amount, $platform_id, $currency_code,$resWalletPlatformUserTransactionModel->uid,$resWalletPlatformUserTransactionModel->id,$resWalletAddrTransactionModel->wallet_addr_id,$resWalletAddrTransactionModel->wallet_addr);
if(!$resModel) throw new ModelException('addPlatformTransaction error');
DB::beginTransaction();
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resWalletPlatformBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalance($platform_id,$currency_code);
if (!$resWalletPlatformBalanceModel) throw new ModelException('findPlatformBalance error');
$res = $oWalletPlatformBalanceModel->incAvailableBalance($resWalletPlatformBalanceModel->id, $amount); //变更平台余额
if (!$res) throw new ModelException('frozenAmountById error');
Db::commit();
return $resModel;
}catch (\Exception $e) {
Db::rollBack();
Log::error('newWithdrawPlatformTransaction', ['code' => $e->getCode(), 'error' => $e->getMessage()]);
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oWalletPlatformBalanceModel->addAvailableAmount($bean->getBalanceId(), $bean->getAmount());
if (!$res) throw new ModelException('addAvailableAmount fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' rollBack', $e, func_get_args());
return false;
}
}
//手续费扣除
function typeTransactionFeeSub($platform_id, $uid, $currency_code, $amount, $remark = null): bool
{
$bean = new WalletPlatformBalanceTransactionBean();
$bean->setPlatformId($platform_id);
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //负数
$bean->setType(self::TYPE_TRANSACTION_FEE_SUB);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
DB::beginTransaction();
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcSub($resBalanceModel->total_amount, abs($amount)));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oWalletPlatformBalanceModel->subAvailableAmount($bean->getBalanceId(), abs($amount));
if (!$res) throw new ModelException('subAvailableAmount fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' rollBack', $e, func_get_args());
return false;
}
}
//手续费增加
function typeTransactionFeeAdd($platform_id, $uid, $currency_code, $amount, $remark = null): bool
{
$bean = new WalletPlatformBalanceTransactionBean();
$bean->setPlatformId($platform_id);
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setType(self::TYPE_TRANSACTION_FEE_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
DB::beginTransaction();
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oWalletPlatformBalanceModel->addAvailableAmount($bean->getBalanceId(), $bean->getAmount());
if (!$res) throw new ModelException('addAvailableAmount fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' rollBack', $e, func_get_args());
return false;
}
}
//充值
function typeRecharge($platform_id, $uid, $currency_code, $amount, $to_wallet_address_id = null, $tid = null, $callback_wallet_address_transaction_id = null): bool
{
$bean = new WalletPlatformBalanceTransactionBean();
$bean->setBlockTransactionId($tid);
$bean->setPlatformId($platform_id);
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setType(self::TYPE_RECHARGE_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setCallbackWalletAddressTransactionId($callback_wallet_address_transaction_id);
$bean->setToWalletAddressId($to_wallet_address_id);
try {
DB::beginTransaction();
//检测钱包地址和当前绑定地址是否一致
$oWalletPlatformBindModel = new WalletPlatformBindModel();
$resBindModel = $oWalletPlatformBindModel->findBind($platform_id, $uid, $currency_code);
if (!$resBindModel) throw new ModelException('WalletPlatformBindModel findBind error');
//查找钱包地址
$oWalletAddressModel = new WalletAddressModel();
$resWalletAddressModel = $oWalletAddressModel->findItem($resBindModel->wallet_address_id);
if (!$resWalletAddressModel) throw new ModelException('WalletAddressModel findItem error');
//检测异步到账地址是否和绑定地址一致
if (!empty($wallet_address_id)) {
if ($resWalletAddressModel->id != $to_wallet_address_id) throw new ModelException('wallet_address_id not same');
}
$bean->setBindWalletAddressId($resWalletAddressModel->id);
$bean->setBindWalletAddress($resWalletAddressModel->address_base58);
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oWalletPlatformBalanceModel->addAvailableAmount($bean->getBalanceId(), $bean->getAmount());
if (!$res) throw new ModelException('addAvailableAmount fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' rollBack', $e);
return false;
}
}
//发起提现
function typeWithdrawFirst($platform_id, $uid, $currency_code, $amount, $to_address = null): bool
{
$bean = new WalletPlatformBalanceTransactionBean();
$bean->setPlatformId($platform_id);
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //负数
$bean->setType(self::TYPE_WITHDRAW_DEC);
$bean->setStatus(self::STATUS_WAIT);
$bean->setToWalletAddress($to_address);
try {
DB::beginTransaction();
//获取提现手续费
$oTableWalletSettingCache = new TableWalletSettingCache();
$withdraw_fee = $oTableWalletSettingCache->getWithdrawFeeAmount();
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//检查余额是否足够
if (Math::bcComp(Math::bcSub($resBalanceModel->available_amount, $withdraw_fee), abs($bean->getAmount())) == -1) throw new ModelException('available_amount not enough');
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcSub($resBalanceModel->total_amount, $bean->getAmount()));
$bean->setFeeAmount($withdraw_fee);
//查询当前绑定地址
$oWalletPlatformBindModel = new WalletPlatformBindModel();
$resBindModel = $oWalletPlatformBindModel->findBind($platform_id, $uid, $currency_code);
if (!$resBindModel) throw new ModelException('findBind error');
//查找钱包地址
$oWalletAddressModel = new WalletAddressModel();
$resWalletAddressModel = $oWalletAddressModel->findItem($resBindModel->wallet_address_id);
if (!$resWalletAddressModel) throw new ModelException('WalletAddressModel findItem error');
$bean->setBindWalletAddressId($resWalletAddressModel->id);
$bean->setBindWalletAddress($resWalletAddressModel->address_base58);
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//先扣除手续费
$res = $this->typeTransactionFeeSub($platform_id, $uid, $currency_code, $withdraw_fee, $res->id .'withdraw fee');
if (!$res) throw new ModelException('withdraw fee fail');
//变更余额
//先冻结,等回调成功再扣减
$res = $oWalletPlatformBalanceModel->frozenAmount($bean->getBalanceId(), $bean->getAmount());
if (!$res) throw new ModelException('frozenAmount fail');
//投递到提现处理队列
$oQueueWalletPlatformWithdrawBean = new QueueWalletPlatformWithdrawTransferBean();
$oQueueWalletPlatformWithdrawBean->setTransactionId($bean->getId());
WalletPlatformWithdrawTransferQueue::putToQueue($oQueueWalletPlatformWithdrawBean);
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . 'rollBack', $e);
return false;
}
}
//提现回调
function typeWithdrawSecondCallback($id, $status, $remark = null, $callback_wallet_address_transaction_id = null): bool
{
try {
DB::beginTransaction();
//获取账变
$resModel = $this->findItem($id);
if (!$resModel) throw new ModelException('findItem error');
if ($resModel->status != self::STATUS_PROCESSING) throw new ModelException('status error');
//查询平台余额
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findItem($resModel->balance_id);
if (!$resBalanceModel) throw new ModelException('WalletPlatformBalanceModel findItem error');
if ($status == self::STATUS_SUCCESS) {
//变更余额
$res = $oWalletPlatformBalanceModel->subFrozenAmount($resModel->balance_id, $resModel->amount);
if (!$res) throw new ModelException('subFrozenAmount error');
//更新账变状态
$updateItem = [
'id' => $id,
'status' => self::STATUS_SUCCESS,
'remark' => $remark,
'updated_at' => Times::getNowDateTime(),
'callback_time' => Times::getNowDateTime(),
'after_total_amount' => Math::bcAdd($resBalanceModel->total_amount, $resModel->amount),
'before_total_amount' => $resBalanceModel->total_amount,
'callback_wallet_address_transaction_id' => $callback_wallet_address_transaction_id,
];
if (!empty($remark)) $updateItem['remark'] = $remark;
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('updateItem error');
//投递到通知平台队列
$oQueueNotifyToPlatformBean = new QueueNotifyToPlatformBean();
$oQueueNotifyToPlatformBean->setType(QueueNotifyToPlatformBean::TYPE_WITHDRAW);
$oQueueNotifyToPlatformBean->setPlatformId($resModel->platform_id);
$oQueueNotifyToPlatformBean->setCurrencyCode($resModel->currency_code);
$oQueueNotifyToPlatformBean->setAmount($resModel->amount);
$oQueueNotifyToPlatformBean->setStatus(self::STATUS_SUCCESS);
$oQueueNotifyToPlatformBean->setSn($resModel->sn);
WalletNotifyToPlatformQueue::putToQueue($oQueueNotifyToPlatformBean);
} else {
//变更余额
$res = $oWalletPlatformBalanceModel->unFrozenAmount($resModel->balance_id, $resModel->amount);
if (!$res) throw new ModelException('unFrozenAmount error');
//更新账变状态
$updateItem = [
'id' => $id,
'status' => self::STATUS_FAIL,
'updated_at' => Times::getNowDateTime(),
'callback_time' => Times::getNowDateTime(),
'callback_wallet_address_transaction_id' => $callback_wallet_address_transaction_id,
];
if (!empty($remark)) $updateItem['remark'] = $remark;
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('updateItem error');
//返还手续费
$res = $this->typeTransactionFeeAdd($resModel->platform_id, $resModel->uid, $resModel->currency_code, $resModel->fee_amount, $resModel->id . 'withdraw fail return fee');
if (!$res) throw new ModelException('withdraw fail return fee fail');
}
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' rollBack', $e, func_get_args());
return false;
}
}
function addTransaction(WalletPlatformBalanceTransactionBean &$bean): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
if (!$bean->getPlatformId()) throw new ModelException('addTransaction getPlatformId is required');
if (!$bean->getCurrencyCode()) throw new ModelException('addTransaction getCurrencyCode is required');
if (!$bean->getType()) throw new ModelException('addTransaction getType is required');
if (!$bean->getStatus()) throw new ModelException('addTransaction getStatus is required');
if (!$bean->getBalanceId()) throw new ModelException('addTransaction getBalanceId is required');
if (!$bean->getBindWalletAddressId()) throw new ModelException('addTransaction getBindWalletAddressId is required');
if (empty($bean->getSn())) $bean->setSn(Tools::genUuid());
$bean->setCreatedAt(Times::getNowDateTime());
return $this->addItem($bean->toArrayNotNull());
}
function findByBlockTransactionId(string $tid, $type): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
if (!in_array($type, [self::TYPE_RECHARGE_ADD, self::TYPE_WITHDRAW_DEC])) throw new ModelException('type error');
return $this->findItemByWhere(['block_transaction_id' => $tid, 'type' => $type]);
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Models\Wallet\Platform;
use App\Bean\Model\Wallet\Wallet\WalletPlatformBindBean;
use App\Exceptions\ModelException;
use App\Models\Wallet\Base\WalletBaseModel;
use App\Models\Wallet\Wallet\WalletAddressModel;
use App\Models\Wallet\Wallet\WalletCurrencyModel;
use App\Tools\Logs;
use App\Tools\Math;
use App\Tools\Times;
use App\Tools\Tools;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class WalletPlatformBindHistoryModel extends WalletBaseModel
{
protected $table = 'wallet_platform_bind_history';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'orgi_id',
'platform_id',
'wallet_address_id',
'currency_code',
'uid',
'orgi_created_at',
'remark',
'created_at',
];
function addHistory($aItem,$remark = ''): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
$aItem['orgi_created_at'] = $aItem['created_at'];
$aItem['orgi_id'] = $aItem['id'];
unset($aItem['id']);
unset($aItem['created_at']);
$aItem['remark'] = $remark;
$aItem['created_at'] = Times::getNowDateTime();
return $this->addItem($aItem);
}
}

View File

@ -0,0 +1,169 @@
<?php
namespace App\Models\Wallet\Platform;
use App\Bean\Model\Wallet\Wallet\WalletPlatformBindBean;
use App\Exceptions\ModelException;
use App\Models\Wallet\Base\WalletBaseModel;
use App\Models\Wallet\Wallet\WalletAddressModel;
use App\Models\Wallet\Wallet\WalletCurrencyModel;
use App\Tools\Logs;
use App\Tools\Math;
use App\Tools\Times;
use App\Tools\Tools;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class WalletPlatformBindModel extends WalletBaseModel
{
protected $table = 'wallet_platform_bind';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'platform_id',
'wallet_address_id',
'currency_code',
'uid',
'created_at',
];
public function getBindInfo($platformId, $walletAddressId, $currencyCode, $uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
{
return $this->newQuery()
->where('platform_id', $platformId)
->where('wallet_address_id', $walletAddressId)
->where('currency_code', $currencyCode)
->where('uid', $uid)
->first();
}
function checkIsBind($platformId, $uid, $currencyCode): bool
{
$res = $this->findBind($platformId, $uid, $currencyCode,['id']);
if($res) return true;
return false;
}
function findBind($platformId, $uid, $currencyCode,$cols = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
{
if(empty($uid)) $uid = 0; //平台默认充提地址
return $this->newQuery()
->where('platform_id', $platformId)
->where('currency_code', $currencyCode)
->where('uid', $uid)
->first($cols)
;
}
//查找空余钱包地址分配给平台
function addBindWallet($platformId, $uid = null, $currencyCode = WalletCurrencyModel::CODE_USDT_TRC20): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
$res = $this->checkIsBind($platformId, $uid, $currencyCode);
if($res) return false;
try {
DB::beginTransaction();
//检测是否已经绑定
$this->getBindInfo( $platformId, $uid, $currencyCode);
$oWalletAddressModel = new WalletAddressModel();
$resWalletAddressModel = $oWalletAddressModel->findFreeAddress($currencyCode);
if ($resWalletAddressModel) {
throw new ModelException("can not find free address");
}
$bean = new WalletPlatformBindBean();
$bean->setPlatformId($platformId);
$bean->setCurrencyCode($currencyCode);
$bean->setUid($uid);
$bean->setWalletAddressId($resWalletAddressModel->id);
$bean->setCreatedAt(Times::getNowDateTime());
$res = $this->addItem($bean->toArrayNotNull());
if(!$res) throw new ModelException("add bind fail");
//更新钱包地址状态
$updateItem = [
'id' => $resWalletAddressModel->id,
'use_status' => WalletAddressModel::USE_STATUS_USING,
];
$res = $oWalletAddressModel->updateItem($updateItem);
if(!$res) throw new ModelException("change use_status fail wallet_address_id:{$resWalletAddressModel->id}");
DB::commit();
Logs::SuccLog(__FUNCTION__, $bean->toArrayNotNull());
return true;
}catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
return false;
}
}
//解绑
function unBindWithUid($platformId, $uid, $currencyCode = WalletCurrencyModel::CODE_USDT_TRC20,$remark = ''): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
$res = $this->checkIsBind($platformId, $uid, $currencyCode);
if(!$res) return true;
try {
DB::beginTransaction();
$oWalletAddressModel = new WalletAddressModel();
$oWalletPlatformBindModel = new WalletPlatformBindModel();
$resWalletPlatformBindModel = $oWalletPlatformBindModel->newQuery()
->where('platform_id', $platformId)
->where('currency_code', $currencyCode)
->where('uid', $uid)
->first();
if (!$resWalletPlatformBindModel) return true;
//废弃原有地址
$updateItem = [
'id' => $resWalletPlatformBindModel->id,
'use_status' => WalletAddressModel::USE_STATUS_USED,
];
$res = $oWalletAddressModel->updateItem($updateItem);
if(!$res) throw new ModelException("change used use_status fail wallet_address_id:{$resWalletPlatformBindModel->wallet_address_id}");
//删除原有绑定记录
$res = $oWalletPlatformBindModel->delItem($resWalletPlatformBindModel->id);
if(!$res) throw new ModelException("delete bind fail");
//原有绑定记录添加到历史
$oWalletPlatformBindHistoryModel = new WalletPlatformBindHistoryModel();
$res = $oWalletPlatformBindHistoryModel->addHistory($resWalletPlatformBindModel->toArray(),$remark);
if(!$res) throw new ModelException("add history fail");
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
}catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
return false;
}
}
//绑定检测是否已绑定,未绑定则绑定,已绑定则解绑再绑定新地址
function bindWallet( $platformId, $currencyCode = WalletCurrencyModel::CODE_USDT_TRC20, $uid = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
try {
DB::beginTransaction();
$res = $this->unBindWithUid($platformId, $uid, $currencyCode);
if(!$res) throw new ModelException("unBindWithUid fail");
$res = $this->addBindWallet($platformId, $uid, $currencyCode);
if(!$res) throw new ModelException("addBindWallet fail");
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
}catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
return false;
}
}
}