用户账变 用户钱包

This commit is contained in:
cano
2024-03-10 00:44:58 +08:00
parent 025dab4c2c
commit 6d242ae973
17 changed files with 1617 additions and 16 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace App\Models\Api\Order;
use App\Models\Api\Base\ApiBaseModel;
use Spatie\FlareClient\Api;
class CustomerUserPostOrderDisputeModel extends ApiBaseModel
{
protected $table = 'customer_user_post_order_dispute';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'post_order_id',
'status',
'chat_group_id',
'seller_submit_datetime',
'buyer_submit_datetime',
'result_status',
'buyer_pay_dispute_status',
'buyer_pay_amount',
'buyer_pay_transaction_id',
'seller_pay_dispute_status',
'seller_pay_amount',
'seller_pay_transaction_id',
'seller_desc_key',
'seller_desc',
'admin_remark',
'admin_uid',
'created_at',
'updated_at',
];
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Models\Api\Order;
use App\Models\Api\Base\ApiBaseModel;
use Spatie\FlareClient\Api;
class CustomerUserPostOrderModel extends ApiBaseModel
{
protected $table = 'customer_user_post_order';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'sn',
'pay_status',
'buyer_uid',
'seller_uid',
'pid',
'post_history_id',
'amount',
'buyer_wallet_transaction_id',
'seller_wallet_transaction_id',
'wallet_checkbook_id',
'dispute_status',
'dispute_result_status',
'created_at',
'updated_at',
];
const PAY_STATUS_UNPAID = 1;
const PAY_STATUS_PAID = 2;
const PAY_STATUS_CANCEL = 3;
const DISPUTE_STATUS_UN_DISPUTE = 1;
const DISPUTE_STATUS_WAITING = 2;
const DISPUTE_STATUS_PROCESSING = 3;
const DISPUTE_STATUS_FINISH = 4;
const DISPUTE_STATUS = [
self::DISPUTE_STATUS_UN_DISPUTE => '未申诉',
self::DISPUTE_STATUS_WAITING => '申诉待处理',
self::DISPUTE_STATUS_PROCESSING => '申诉处理中',
self::DISPUTE_STATUS_FINISH => '申诉完成',
];
const DISPUTE_RESULT_STATUS_NO_MISTAKE = 1;
const DISPUTE_RESULT_STATUS_HAVE_MISTAKE = 2;
const DISPUTE_RESULT_STATUS = [
self::DISPUTE_RESULT_STATUS_NO_MISTAKE => '无过错',
self::DISPUTE_RESULT_STATUS_HAVE_MISTAKE => '有过错',
];
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Models\Api\Wallet;
use App\Models\Api\Base\ApiBaseModel;
use Spatie\FlareClient\Api;
class CustomerPostOrderWalletCheckbookModel extends ApiBaseModel
{
protected $table = 'customer_post_order_wallet_checkbook';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'post_order_id',
'type',
'status',
'pay_uid',
'receive_uid',
'amount',
'currency_id',
'currency_code',
'pay_time',
'created_at',
'updated_at',
'exec_time',
'remark',
];
}

View File

@ -0,0 +1,130 @@
<?php
namespace App\Models\Api\Wallet;
use App\Models\Api\Base\ApiBaseModel;
use Illuminate\Support\Facades\DB;
class CustomerWalletBalanceModel extends ApiBaseModel
{
protected $table = 'customer_wallet_balance';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'uid',
'currency_code',
'total_amount',
'frozen_amount',
'available_amount',
'security_amount',
'recharge_wallet_addr',
'created_at',
'updated_at',
];
function frozenAmount($id,$amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'frozen_amount' => DB::raw('frozen_amount + '. $amount),
'available_amount' => DB::raw('available_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
function unFrozenAmount($id,$amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'frozen_amount' => DB::raw('frozen_amount - '. $amount),
'available_amount' => DB::raw('available_amount + '. $amount),
];
return $this->updateItem($updateItem);
}
function subFrozenAmount($id, $amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount - '. $amount),
'frozen_amount' => DB::raw('frozen_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
function addFrozenAmount($id, $amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount + '. $amount),
'frozen_amount' => DB::raw('frozen_amount + '. $amount),
];
return $this->updateItem($updateItem);
}
function subAvailableAmount($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);
}
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);
}
function balanceToSecurity($id, $amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount - '. $amount),
'available_amount' => DB::raw('available_amount - '. $amount),
'security_amount' => DB::raw('security_amount + '. $amount),
];
return $this->updateItem($updateItem);
}
function securityToBalance($id, $amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'total_amount' => DB::raw('total_amount + '. $amount),
'available_amount' => DB::raw('available_amount + '. $amount),
'security_amount' => DB::raw('security_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
function subSecurityAmount($id, $amount): bool|int
{
$amount = abs($amount);
$updateItem = [
'id' => $id,
'security_amount' => DB::raw('security_amount - '. $amount),
];
return $this->updateItem($updateItem);
}
function findByUidCurrencyCode($uid, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->findItemByWhere(['uid' => $uid, 'currency_code' => $currency_code]);
}
}

View File

@ -0,0 +1,712 @@
<?php
namespace App\Models\Api\Wallet;
use App\Bean\Model\Api\Order\CustomerUserPostOrderBean;
use App\Bean\Model\Api\Wallet\CustomerWalletBalanceTransactionBean;
use App\Exceptions\ModelException;
use App\Models\Api\Base\ApiBaseModel;
use App\Models\Api\Order\CustomerUserPostOrderModel;
use App\Tools\Logs;
use App\Tools\Math;
use App\Tools\Times;
use Illuminate\Support\Facades\DB;
class CustomerWalletBalanceTransactionModel extends ApiBaseModel
{
protected $table = 'customer_wallet_balance_transactions';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'type',
'status',
'wallet_id',
'uid',
'currency_code',
'amount',
'before_total_amount',
'after_total_amount',
'source_params',
'remark',
'target_uid',
'target_post_order_id',
'sign',
'callback_time',
'delay_payment_time',
'created_at',
'updated_at',
];
const TYPE_RECHARGE_ADD = 1;
const TYPE_WITHDRAW_DEC = 2;
const TYPE_ORDER_PAY_DEC = 3;
const TYPE_ORDER_RECEIVE_ADD = 4;
const TYPE_ORDER_REFUND_SUB = 5;
const TYPE_BALANCE_TO_SECURITY = 6;
const TYPE_SECURITY_TO_BALANCE = 7;
const TYPE_DISPUTE_ADD = 8;
const TYPE_DISPUTE_SUB = 9;
const TYPE_TRANSACTION_FEE_SUB = 10;
const TYPE_TRANSACTION_FEE_ADD = 11;
const TYPE_ADMIN_ADD = 12;
const TYPE_ADMIN_SUB = 13;
const TYPE_TRANSFER_TO_USER_DEC = 14;
const TYPE_TRANSFER_RECEIVE_ADD = 15;
const TYPE = [
self::TYPE_RECHARGE_ADD => '充值',
self::TYPE_WITHDRAW_DEC => '提现',
self::TYPE_ORDER_PAY_DEC => '订单支付',
self::TYPE_ORDER_RECEIVE_ADD => '订单收款',
self::TYPE_ORDER_REFUND_SUB => '订单退款',
self::TYPE_BALANCE_TO_SECURITY => '余额转保证金',
self::TYPE_SECURITY_TO_BALANCE => '保证金转余额',
self::TYPE_DISPUTE_ADD => '纠纷加款',
self::TYPE_DISPUTE_SUB => '纠纷扣款',
self::TYPE_TRANSACTION_FEE_SUB => '交易手续费扣除',
self::TYPE_TRANSACTION_FEE_ADD => '交易手续费增加',
self::TYPE_ADMIN_ADD => '管理员加款',
self::TYPE_ADMIN_SUB => '管理员扣款',
self::TYPE_TRANSFER_TO_USER_DEC => '转账给用户',
self::TYPE_TRANSFER_RECEIVE_ADD => '转账收款',
];
const STATUS_WAIT = 1;
const STATUS_PROCESSING = 2;
const STATUS_SUCCESS = 3;
const STATUS_FAIL = 4;
const STATUS_DELAY_PAYMENT = 5;
const STATUS = [
self::STATUS_WAIT => '等待处理',
self::STATUS_PROCESSING => '处理中',
self::STATUS_SUCCESS => '成功',
self::STATUS_FAIL => '失败',
self::STATUS_DELAY_PAYMENT => '延期到账',
];
const SOURCE_PARAMS_BALANCE = 1;
const SOURCE_PARAMS_SECURITY = 2;
const SOURCE_PARAMS = [
self::SOURCE_PARAMS_BALANCE => '余额',
self::SOURCE_PARAMS_SECURITY => '保证金',
];
//转账给用户
function typeTransferToUser($uid, $target_uid, $currency_code, $amount, $remark = null): bool
{
$senderWalletTransactionBean = new CustomerWalletBalanceTransactionBean();
$receiverWalletTransactionBean = new CustomerWalletBalanceTransactionBean();
$senderWalletTransactionBean->setUid($uid);
$senderWalletTransactionBean->setTargetUid($target_uid);
$senderWalletTransactionBean->setCurrencyCode($currency_code);
$senderWalletTransactionBean->setAmount(-abs($amount)); //账变负数
$senderWalletTransactionBean->setType(self::TYPE_TRANSFER_TO_USER_DEC);
$senderWalletTransactionBean->setStatus(self::STATUS_SUCCESS);
$senderWalletTransactionBean->setRemark($remark);
$receiverWalletTransactionBean->setUid($target_uid);
$receiverWalletTransactionBean->setTargetUid($uid);
$receiverWalletTransactionBean->setCurrencyCode($currency_code);
$receiverWalletTransactionBean->setAmount(abs($amount)); //账变正数
$receiverWalletTransactionBean->setType(self::TYPE_TRANSFER_RECEIVE_ADD);
$receiverWalletTransactionBean->setStatus(self::STATUS_SUCCESS);
$receiverWalletTransactionBean->setRemark($remark);
try {
DB::beginTransaction();
//查询发送人余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resSenderWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($senderWalletTransactionBean->getUid(), $senderWalletTransactionBean->getCurrencyCode());
if (!$resSenderWalletBalanceModel) throw new ModelException('sender wallet not found');
//查询接收人余额
$resReceiverWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($receiverWalletTransactionBean->getUid(), $receiverWalletTransactionBean->getCurrencyCode());
if (!$resReceiverWalletBalanceModel) throw new ModelException('receiver wallet not found');
//检查发送人余额是否足够
if (Math::bcComp($resSenderWalletBalanceModel->available_amount, abs($senderWalletTransactionBean->getAmount())) != -1) throw new ModelException('balance not enough');
$senderWalletTransactionBean->setWalletId($resSenderWalletBalanceModel->id);
$senderWalletTransactionBean->setBeforeTotalAmount($resSenderWalletBalanceModel->total_amount);
$senderWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resSenderWalletBalanceModel->total_amount, $senderWalletTransactionBean->getAmount()));
//发送人新增账变
$res = $this->addTransaction($senderWalletTransactionBean);
if (!$res) throw new ModelException('addTransaction fail');
//发送人变更余额
$res = $oCustomerWalletBalanceModel->subAvailableAmount($senderWalletTransactionBean->getWalletId(), $senderWalletTransactionBean->getAmount());
if (!$res) throw new ModelException('transfer to user dec fail');
//接收人账变
$receiverWalletTransactionBean->setWalletId($resReceiverWalletBalanceModel->id);
$receiverWalletTransactionBean->setBeforeTotalAmount($resReceiverWalletBalanceModel->total_amount);
$receiverWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resReceiverWalletBalanceModel->total_amount, $receiverWalletTransactionBean->getAmount()));
//接收人新增账变
$res = $this->addTransaction($receiverWalletTransactionBean);
if (!$res) throw new ModelException('addTransaction fail');
//接收人变更余额
$res = $oCustomerWalletBalanceModel->addAvailableAmount($receiverWalletTransactionBean->getWalletId(), $receiverWalletTransactionBean->getAmount());
if (!$res) throw new ModelException('transfer receive add 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 typeAdminSub($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$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();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) throw new ModelException('balance not enough');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('admin sub 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 typeAdminAdd($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$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();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('admin add 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($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$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();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('transaction fee add 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($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$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();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
//检查余额是否足够
if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) { //从余额中扣除
$bean->setSourceParams(self::SOURCE_PARAMS_BALANCE);
$res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount());
} else if (Math::bcComp($resWalletBalanceModel->security_amount, abs($bean->getAmount())) != -1) { //从保证金中扣除
$bean->setSourceParams(self::SOURCE_PARAMS_SECURITY);
$res = $oCustomerWalletBalanceModel->subSecurityAmount($bean->getWalletId(), $bean->getAmount());
} else {
throw new ModelException('balance and security not enough');
}
if (!$res) throw new ModelException('transaction fee sub 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 typeDisputeSub($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //账变负数
$bean->setType(self::TYPE_DISPUTE_SUB);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
//检查余额是否足够
if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) { //从余额中扣除
$bean->setSourceParams(self::SOURCE_PARAMS_BALANCE);
$res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount());
} else if (Math::bcComp($resWalletBalanceModel->security_amount, abs($bean->getAmount())) != -1) { //从保证金中扣除
$bean->setSourceParams(self::SOURCE_PARAMS_SECURITY);
$res = $oCustomerWalletBalanceModel->subSecurityAmount($bean->getWalletId(), $bean->getAmount());
} else {
throw new ModelException('balance and security not enough');
}
if (!$res) throw new ModelException('dispute sub 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 typeDisputeAdd($uid, $currency_code, $amount, $remark = null): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setType(self::TYPE_DISPUTE_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
$bean->setRemark($remark);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('dispute add 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 typeSecurityToBalance($uid, $currency_code, $amount): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setType(self::TYPE_SECURITY_TO_BALANCE);
$bean->setStatus(self::STATUS_SUCCESS);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//检查保证金是否足够
if (Math::bcComp($resWalletBalanceModel->security_amount, abs($bean->getAmount())) == -1) throw new ModelException('security not enough');
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$res = $oCustomerWalletBalanceModel->securityToBalance($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('securityToBalance 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 typeBalanceToSecurity($uid, $currency_code, $amount): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //账变负数
$bean->setType(self::TYPE_BALANCE_TO_SECURITY);
$bean->setStatus(self::STATUS_SUCCESS);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//检查余额是否足够
if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) == -1) throw new ModelException('balance not enough');
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$res = $oCustomerWalletBalanceModel->balanceToSecurity($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('balanceToSecurity 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 typeOrderPay(CustomerUserPostOrderBean $orderBean): bool
{
//初始检测
if ($orderBean->getBuyerCurrencyCode() != $orderBean->getSellerCurrencyCode()) throw new ModelException('currency_code not match');
if ($orderBean->getBuyerUid() == $orderBean->getSellerUid()) throw new ModelException('buyer_uid can not match seller_uid');
if ($orderBean->getAmount() <= 0) throw new ModelException('amount error');
$orderBean->setAmount(abs($orderBean->getAmount())); //限制正数
$buyerWalletTransactionBean = new CustomerWalletBalanceTransactionBean();
$sellerWalletTransactionBean = new CustomerWalletBalanceTransactionBean();
//买家初始化
$buyerWalletTransactionBean->setType(self::TYPE_ORDER_PAY_DEC);
$buyerWalletTransactionBean->setStatus(self::STATUS_SUCCESS);
$buyerWalletTransactionBean->setAmount(-$orderBean->getAmount()); //负数
$buyerWalletTransactionBean->setUid($orderBean->getBuyerUid());
$buyerWalletTransactionBean->setCurrencyCode($orderBean->getBuyerCurrencyCode());
$buyerWalletTransactionBean->setTargetPostOrderId($orderBean->getId());
//卖家初始化
$sellerWalletTransactionBean->setType(self::TYPE_ORDER_RECEIVE_ADD);
$sellerWalletTransactionBean->setStatus(self::STATUS_DELAY_PAYMENT);
$sellerWalletTransactionBean->setAmount($orderBean->getAmount()); //正数
$sellerWalletTransactionBean->setUid($orderBean->getSellerUid());
$sellerWalletTransactionBean->setCurrencyCode($orderBean->getSellerCurrencyCode());
$sellerWalletTransactionBean->setTargetPostOrderId($orderBean->getId());
//@@计算订单延期到账时间
$delay_payment_time = Times::getNowDateTimeAddDays(1);
$sellerWalletTransactionBean->setDelayPaymentTime($delay_payment_time);
try {
DB::beginTransaction();
//查询买家余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resBuyerWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($buyerWalletTransactionBean->getUid(), $buyerWalletTransactionBean->getCurrencyCode());
//检查买家余额是否足够
if (Math::bcComp($resBuyerWalletBalanceModel->available_amount, abs($orderBean->getAmount())) == -1) throw new ModelException('buyer balance not enough');
//设置买家卖家钱包id
$buyerWalletTransactionBean->setWalletId($resBuyerWalletBalanceModel->id);
$buyerWalletTransactionBean->setBeforeTotalAmount($resBuyerWalletBalanceModel->total_amount);
$buyerWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resBuyerWalletBalanceModel->total_amount, $buyerWalletTransactionBean->getAmount()));
//设置卖家卖家钱包id
$resSellerWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($sellerWalletTransactionBean->getUid(), $sellerWalletTransactionBean->getCurrencyCode());
if (!$resSellerWalletBalanceModel) throw new ModelException('seller wallet not found');
$sellerWalletTransactionBean->setWalletId($resSellerWalletBalanceModel->id);
$sellerWalletTransactionBean->setBeforeTotalAmount($resSellerWalletBalanceModel->total_amount);
$sellerWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resSellerWalletBalanceModel->total_amount, $sellerWalletTransactionBean->getAmount()));
//买家新增账变
$resBuyerTransaction = $this->addTransaction($buyerWalletTransactionBean);
if (!$resBuyerTransaction) throw new ModelException('buyer addTransaction fail');
$buyerWalletTransactionBean->setId($resBuyerTransaction->id);
//买家变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$res = $oCustomerWalletBalanceModel->subAvailableAmount($buyerWalletTransactionBean->getWalletId(), $buyerWalletTransactionBean->getAmount());
if (!$res) throw new ModelException('buyer order pay fail');
//卖家新增账变
$resSellerTransaction = $this->addTransaction($sellerWalletTransactionBean);
if (!$resSellerTransaction) throw new ModelException('seller addTransaction fail');
$sellerWalletTransactionBean->setId($resSellerTransaction->id);
//卖家变更余额
$res = $oCustomerWalletBalanceModel->addFrozenAmount($sellerWalletTransactionBean->getWalletId(), $sellerWalletTransactionBean->getAmount());
if (!$res) throw new ModelException('seller order receive fail');
//更新订单状态
$oCustomerUserPostOrderModel = new CustomerUserPostOrderModel();
$updateItem = [
'id' => $orderBean->getId(),
'pay_status' => CustomerUserPostOrderModel::PAY_STATUS_PAID,
'buyer_wallet_transaction_id' => $buyerWalletTransactionBean->getId(),
'seller_wallet_transaction_id' => $sellerWalletTransactionBean->getId(),
'updated_at' => Times::getNowDateTime(),
];
$res = $oCustomerUserPostOrderModel->updateItem($updateItem);
if (!$res) throw new ModelException('updateItem 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 typeWithdrawFirst($uid, $currency_code, $amount): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(-abs($amount)); //负数
$bean->setUid($uid);
$bean->setType(self::TYPE_WITHDRAW_DEC);
$bean->setStatus(self::STATUS_PROCESSING);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount($resWalletBalanceModel->total_amount);
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
//先冻结,等回调成功再扣减
$res = $oCustomerWalletBalanceModel->frozenAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('withdraw 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 typeWithdrawSecondCallback($id, $status, $remark = null): bool
{
try {
DB::beginTransaction();
$resModel = $this->findItem($id);
if (!$resModel) throw new ModelException('transaction not found');
if ($resModel->status != self::STATUS_PROCESSING) throw new ModelException('transaction status error');
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findItem($resModel->wallet_id);
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
if ($status == self::STATUS_SUCCESS) {
//扣减冻结金额
$res = $oCustomerWalletBalanceModel->subFrozenAmount($resModel->wallet_id, $resModel->amount);
if (!$res) throw new ModelException('withdraw fail');
//更新账变状态
$updateItem = [
'id' => $id,
'status' => self::STATUS_SUCCESS,
'updated_at' => Times::getNowDateTime(),
'before_total_amount' => $resWalletBalanceModel->total_amount,
'after_total_amount' => Math::bcAdd($resWalletBalanceModel->total_amount, $resModel->amount)
];
if (!empty($remark)) $updateItem['remark'] = $remark;
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('updateItem fail');
} else { //失败
//解冻
$res = $oCustomerWalletBalanceModel->unFrozenAmount($resModel->wallet_id, $resModel->amount);
if (!$res) throw new ModelException('withdraw fail');
//更新账变状态
$updateItem = [
'id' => $id,
'status' => self::STATUS_FAIL,
'updated_at' => Times::getNowDateTime(),
];
if (!empty($remark)) $updateItem['remark'] = $remark;
$res = $this->updateItem($updateItem);
if (!$res) throw new ModelException('updateItem 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($uid, $currency_code, $amount): bool
{
$bean = new CustomerWalletBalanceTransactionBean();
$bean->setUid($uid);
$bean->setCurrencyCode($currency_code);
$bean->setAmount(abs($amount)); //正数
$bean->setUid($uid);
$bean->setType(self::TYPE_RECHARGE_ADD);
$bean->setStatus(self::STATUS_SUCCESS);
try {
DB::beginTransaction();
//查询用户余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setWalletId($resWalletBalanceModel->id);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount()));
//新增账变
$res = $this->addTransaction($bean);
if (!$res) throw new ModelException('addTransaction fail');
//变更余额
$oCustomerWalletBalanceModel = new CustomerWalletBalanceModel();
$res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount());
if (!$res) throw new ModelException('recharge fail');
DB::commit();
Logs::SuccLog(__FUNCTION__, func_get_args());
return true;
} catch (\Exception $e) {
DB::rollBack();
Logs::ErrLog('typeRecharge error rollBack', $e);
return false;
}
}
function addTransaction(CustomerWalletBalanceTransactionBean &$bean)
{
if (!$bean->getWalletId()) throw new ModelException('wallet_id is required');
if (!$bean->getType()) throw new ModelException('type is required');
if (!$bean->getStatus()) $bean->setStatus(self::STATUS_WAIT);
if (empty($bean->getUid()) || empty($bean->getBeforeTotalAmount())) {
$oWalletBalanceModel = new CustomerWalletBalanceModel();
$resWalletBalanceModel = $oWalletBalanceModel->findItem($bean->getWalletId());
if (!$resWalletBalanceModel) throw new ModelException('wallet not found');
$bean->setUid($resWalletBalanceModel->uid);
$bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount);
}
$bean->setCreatedAt(Times::getNowDateTime());
$bean->setUpdatedAt(Times::getNowDateTime());
return $this->addItem($bean->toArrayNotNull());
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Models\Api\Wallet;
use App\Models\Api\Base\ApiBaseModel;
use Spatie\FlareClient\Api;
class CustomerWalletCurrencyModel extends ApiBaseModel
{
protected $table = 'customer_wallet_currency';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'type',
'name',
'code',
'token',
'transfer_rate',
'desc',
'created_at',
];
const TYPE_CASH = 1;
const TYPE_CRYPTO_COIN = 2;
const TYPE = [
self::TYPE_CASH => '现金',
self::TYPE_CRYPTO_COIN => '加密货币',
];
const CODE_USDT_TRC20 = 'USDT_TRC20';
}

View File

@ -10,6 +10,7 @@ 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
{
@ -156,17 +157,22 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
}
//成功提现处理
function withdrawSuccByUserTransactionId($user_transaction_id, $remark = ''): bool
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) return false;
if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) return false;
$resModel->status = self::STATUS_SUCCESS;
if(!empty($remark)) $resModel->remark = $remark;
$resModel->updated_at = Times::getNowDateTime();
$res = $resModel->save();
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');
//扣除平台冻结余额
@ -196,9 +202,13 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
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');
//更改账变状态
$resModel->status = self::STATUS_FAIL;
if(!empty($remark)) $resModel->remark = $remark;
$res = $resModel->save();
$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');
//解冻平台余额

View File

@ -303,13 +303,22 @@ class WalletPlatformUserTransactionModel extends WalletBaseModel
if ($resWalletPlatformUserTransactionModel->type != self::TYPE_WITHDRAW) throw new ModelException('type error');
if ($resWalletPlatformUserTransactionModel->status != self::STATUS_CHAIN_WAITING) throw new ModelException('status error');
//用户账变处理
$resWalletPlatformUserTransactionModel->status = $status;
$resWalletPlatformUserTransactionModel->save();
$updateItems = [
'id' => $resWalletPlatformUserTransactionModel->id,
'status' => $status,
'entered_amount' => $resWalletAddrTransactionModel->entered_amount,
'fee_amount' => Db::raw('received_amount - '.$resWalletAddrTransactionModel->entered_amount),
];
$res = $this->updateItem($updateItems);
if(!$res) {
Log::error('listenWithdrawCallbackErr', $updateItems);
return;
}
//平台余额和账变处理
$oWalletPlatformBalanceTransactionModel = new WalletPlatformBalanceTransactionModel();
if ($status == self::STATUS_SUCCESS) { //成功
$oWalletPlatformBalanceTransactionModel->withdrawSuccByUserTransactionId($id);
$oWalletPlatformBalanceTransactionModel->withdrawSuccByUserTransactionId($id,$resWalletAddrTransactionModel->entered_amount);
} else { //失败
$oWalletPlatformBalanceTransactionModel->withdrawErrByUserTransactionId($id);
}

View File

@ -247,10 +247,15 @@ class WalletAddrModel extends WalletBaseModel
}
if ($type == WalletAddrTransactionModel::TYPE_TRANSFER && $resWalletAddrTransactionModel) { //提现已有账变,修改账变状态即可
$res = $oWalletAddrTransactionModel->updateItem([
$updateItems = [
'id' => $resWalletAddrTransactionModel->id,
'status' => $status,
]);
];
if($status == WalletAddrTransactionModel::STATUS_SUCCESS){
$updateItems['entered_amount'] = $amount;
$updateItems['fee_amount'] = Db::raw('received_amount - '.$amount);
}
$res = $oWalletAddrTransactionModel->updateItem($updateItems);
if (!$res) {
Log::error('listenWalletAddrCallbackErr', ['token' => $token, 'addr' => $addr, 'amount' => $amount, 'status' => $status, 'wallet_addr_id' => $resModel->id, 'wallet_transaction_id' => $resWalletAddrTransactionModel->id, 'error' => '修改账变状态失败']);
throw new ModelException('修改账变状态失败');

View File

@ -19,6 +19,12 @@ class WalletCurrencyModel extends WalletBaseModel
'created_at',
];
const TYPE_CASH = 1;
const TYPE_CRYPTO_COIN = 2;
const TYPE = [
self::TYPE_CASH => '现金',
self::TYPE_CRYPTO_COIN => '加密货币',
];
const CODE_USDT_TRC20 = 'USDT_TRC20';
function findByCode($code,$columns = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null