用户账变 用户钱包

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,201 @@
<?php
namespace App\Bean\Model\Api\Order;
use App\Bean\Model\Base\BaseBean;
use App\Models\Api\Wallet\CustomerWalletCurrencyModel;
class CustomerUserPostOrderBean extends BaseBean
{
protected $id;
protected $sn;
protected $pay_status;
protected $buyer_uid;
protected $buyer_currency_code = CustomerWalletCurrencyModel::CODE_USDT_TRC20;
protected $seller_uid;
protected $seller_currency_code = CustomerWalletCurrencyModel::CODE_USDT_TRC20;
protected $pid;
protected $post_history_id;
protected $amount;
protected $buyer_wallet_transaction_id;
protected $seller_wallet_transaction_id;
protected $wallet_checkbook_id;
protected $dispute_status;
protected $dispute_result_status;
protected $created_at;
protected $updated_at;
public function getBuyerCurrencyCode()
{
return $this->buyer_currency_code;
}
public function setBuyerCurrencyCode($buyer_currency_code): void
{
$this->buyer_currency_code = $buyer_currency_code;
}
public function getSellerCurrencyCode()
{
return $this->seller_currency_code;
}
public function setSellerCurrencyCode($seller_currency_code): void
{
$this->seller_currency_code = $seller_currency_code;
}
public function getId()
{
return $this->id;
}
public function setId($id): void
{
$this->id = $id;
}
public function getSn()
{
return $this->sn;
}
public function setSn($sn): void
{
$this->sn = $sn;
}
public function getPayStatus()
{
return $this->pay_status;
}
public function setPayStatus($pay_status): void
{
$this->pay_status = $pay_status;
}
public function getBuyerUid()
{
return $this->buyer_uid;
}
public function setBuyerUid($buyer_uid): void
{
$this->buyer_uid = $buyer_uid;
}
public function getSellerUid()
{
return $this->seller_uid;
}
public function setSellerUid($seller_uid): void
{
$this->seller_uid = $seller_uid;
}
public function getPid()
{
return $this->pid;
}
public function setPid($pid): void
{
$this->pid = $pid;
}
public function getPostHistoryId()
{
return $this->post_history_id;
}
public function setPostHistoryId($post_history_id): void
{
$this->post_history_id = $post_history_id;
}
public function getAmount()
{
return $this->amount;
}
public function setAmount($amount): void
{
$this->amount = $amount;
}
public function getBuyerWalletTransactionId()
{
return $this->buyer_wallet_transaction_id;
}
public function setBuyerWalletTransactionId($buyer_wallet_transaction_id): void
{
$this->buyer_wallet_transaction_id = $buyer_wallet_transaction_id;
}
public function getSellerWalletTransactionId()
{
return $this->seller_wallet_transaction_id;
}
public function setSellerWalletTransactionId($seller_wallet_transaction_id): void
{
$this->seller_wallet_transaction_id = $seller_wallet_transaction_id;
}
public function getWalletCheckbookId()
{
return $this->wallet_checkbook_id;
}
public function setWalletCheckbookId($wallet_checkbook_id): void
{
$this->wallet_checkbook_id = $wallet_checkbook_id;
}
public function getDisputeStatus()
{
return $this->dispute_status;
}
public function setDisputeStatus($dispute_status): void
{
$this->dispute_status = $dispute_status;
}
public function getDisputeResultStatus()
{
return $this->dispute_result_status;
}
public function setDisputeResultStatus($dispute_result_status): void
{
$this->dispute_result_status = $dispute_result_status;
}
public function getCreatedAt()
{
return $this->created_at;
}
public function setCreatedAt($created_at): void
{
$this->created_at = $created_at;
}
public function getUpdatedAt()
{
return $this->updated_at;
}
public function setUpdatedAt($updated_at): void
{
$this->updated_at = $updated_at;
}
}

View File

@ -0,0 +1,121 @@
<?php
namespace App\Bean\Model\Api\Wallet;
use App\Bean\Model\Base\BaseBean;
class CustomerWalletBalanceBean extends BaseBean
{
protected $id;
protected $uid;
protected $currency_code;
protected $total_amount;
protected $frozen_amount;
protected $available_amount;
protected $security_amount;
protected $recharge_wallet_addr;
protected $created_at;
protected $updated_at;
public function getId()
{
return $this->id;
}
public function setId($id): void
{
$this->id = $id;
}
public function getUid()
{
return $this->uid;
}
public function setUid($uid): void
{
$this->uid = $uid;
}
public function getCurrencyCode()
{
return $this->currency_code;
}
public function setCurrencyCode($currency_code): void
{
$this->currency_code = $currency_code;
}
public function getTotalAmount()
{
return $this->total_amount;
}
public function setTotalAmount($total_amount): void
{
$this->total_amount = $total_amount;
}
public function getFrozenAmount()
{
return $this->frozen_amount;
}
public function setFrozenAmount($frozen_amount): void
{
$this->frozen_amount = $frozen_amount;
}
public function getAvailableAmount()
{
return $this->available_amount;
}
public function setAvailableAmount($available_amount): void
{
$this->available_amount = $available_amount;
}
public function getSecurityAmount()
{
return $this->security_amount;
}
public function setSecurityAmount($security_amount): void
{
$this->security_amount = $security_amount;
}
public function getRechargeWalletAddr()
{
return $this->recharge_wallet_addr;
}
public function setRechargeWalletAddr($recharge_wallet_addr): void
{
$this->recharge_wallet_addr = $recharge_wallet_addr;
}
public function getCreatedAt()
{
return $this->created_at;
}
public function setCreatedAt($created_at): void
{
$this->created_at = $created_at;
}
public function getUpdatedAt()
{
return $this->updated_at;
}
public function setUpdatedAt($updated_at): void
{
$this->updated_at = $updated_at;
}
}

View File

@ -0,0 +1,212 @@
<?php
namespace App\Bean\Model\Api\Wallet;
use App\Bean\Model\Base\BaseBean;
use App\Models\Api\Wallet\CustomerWalletCurrencyModel;
use App\Models\Api\Wallet\CustomerWalletBalanceTransactionModel;
class CustomerWalletBalanceTransactionBean extends BaseBean
{
protected $id;
protected $type;
protected $status = CustomerWalletBalanceTransactionModel::STATUS_WAIT;
protected $wallet_id;
protected $uid;
protected $currency_code = CustomerWalletCurrencyModel::CODE_USDT_TRC20;
protected $amount = 0;
protected $before_total_amount;
protected $after_total_amount;
protected $source_params;
protected $remark;
protected $target_uid;
protected $target_post_order_id;
protected $sign;
protected $callback_time;
protected $delay_payment_time;
protected $created_at;
protected $updated_at;
public function getId()
{
return $this->id;
}
public function setId($id): void
{
$this->id = $id;
}
public function getType()
{
return $this->type;
}
public function setType($type): void
{
$this->type = $type;
}
public function getStatus(): int
{
return $this->status;
}
public function setStatus(int $status): void
{
$this->status = $status;
}
public function getWalletId()
{
return $this->wallet_id;
}
public function setWalletId($wallet_id): void
{
$this->wallet_id = $wallet_id;
}
public function getUid()
{
return $this->uid;
}
public function setUid($uid): void
{
$this->uid = $uid;
}
public function getCurrencyCode(): string
{
return $this->currency_code;
}
public function setCurrencyCode(string $currency_code): void
{
$this->currency_code = $currency_code;
}
public function getAmount(): int
{
return $this->amount;
}
public function setAmount(int $amount): void
{
$this->amount = $amount;
}
public function getBeforeTotalAmount()
{
return $this->before_total_amount;
}
public function setBeforeTotalAmount($before_total_amount): void
{
$this->before_total_amount = $before_total_amount;
}
public function getAfterTotalAmount()
{
return $this->after_total_amount;
}
public function setAfterTotalAmount($after_total_amount): void
{
$this->after_total_amount = $after_total_amount;
}
public function getSourceParams()
{
return $this->source_params;
}
public function setSourceParams($source_params): void
{
$this->source_params = $source_params;
}
public function getRemark()
{
return $this->remark;
}
public function setRemark($remark): void
{
$this->remark = $remark;
}
public function getTargetUid()
{
return $this->target_uid;
}
public function setTargetUid($target_uid): void
{
$this->target_uid = $target_uid;
}
public function getTargetPostOrderId()
{
return $this->target_post_order_id;
}
public function setTargetPostOrderId($target_post_order_id): void
{
$this->target_post_order_id = $target_post_order_id;
}
public function getSign()
{
return $this->sign;
}
public function setSign($sign): void
{
$this->sign = $sign;
}
public function getCallbackTime()
{
return $this->callback_time;
}
public function setCallbackTime($callback_time): void
{
$this->callback_time = $callback_time;
}
public function getDelayPaymentTime()
{
return $this->delay_payment_time;
}
public function setDelayPaymentTime($delay_payment_time): void
{
$this->delay_payment_time = $delay_payment_time;
}
public function getCreatedAt()
{
return $this->created_at;
}
public function setCreatedAt($created_at): void
{
$this->created_at = $created_at;
}
public function getUpdatedAt()
{
return $this->updated_at;
}
public function setUpdatedAt($updated_at): void
{
$this->updated_at = $updated_at;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace App\Bean\Model\Base;
use EasySwoole\Spl\SplBean;
abstract class BaseBean extends SplBean
{
function toArrayNotNull(): array
{
return $this->toArray(self::FILTER_NOT_NULL);
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace App\Bean\Model\Base;
use App\Bean\Model\Base\BaseBean;
class TmplBean extends BaseBean
{
}

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 App\Tools\Tools;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Symfony\Contracts\Service;
class WalletPlatformBalanceTransactionModel extends WalletBaseModel 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{ try{
Db::beginTransaction(); Db::beginTransaction();
$resModel = $this->findItemByWhere(['from_user_transaction_id' => $user_transaction_id, 'type' => self::TYPE_USER_WITHDRAW]); $resModel = $this->findItemByWhere(['from_user_transaction_id' => $user_transaction_id, 'type' => self::TYPE_USER_WITHDRAW]);
if (!$resModel) return false; if (!$resModel) throw new ModelException('user transaction not found');
if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) return false; if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) throw new ModelException('status error');
$resModel->status = self::STATUS_SUCCESS; $updateItems = [
if(!empty($remark)) $resModel->remark = $remark; 'id' => $resModel->id,
$resModel->updated_at = Times::getNowDateTime(); 'status' => self::STATUS_SUCCESS,
$res = $resModel->save(); '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'); 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 (!$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'); if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) throw new ModelException('status error');
//更改账变状态 //更改账变状态
$resModel->status = self::STATUS_FAIL; $updateItems = [
if(!empty($remark)) $resModel->remark = $remark; 'id' => $resModel->id,
$res = $resModel->save(); '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'); 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->type != self::TYPE_WITHDRAW) throw new ModelException('type error');
if ($resWalletPlatformUserTransactionModel->status != self::STATUS_CHAIN_WAITING) throw new ModelException('status error'); if ($resWalletPlatformUserTransactionModel->status != self::STATUS_CHAIN_WAITING) throw new ModelException('status error');
//用户账变处理 //用户账变处理
$resWalletPlatformUserTransactionModel->status = $status; $updateItems = [
$resWalletPlatformUserTransactionModel->save(); '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(); $oWalletPlatformBalanceTransactionModel = new WalletPlatformBalanceTransactionModel();
if ($status == self::STATUS_SUCCESS) { //成功 if ($status == self::STATUS_SUCCESS) { //成功
$oWalletPlatformBalanceTransactionModel->withdrawSuccByUserTransactionId($id); $oWalletPlatformBalanceTransactionModel->withdrawSuccByUserTransactionId($id,$resWalletAddrTransactionModel->entered_amount);
} else { //失败 } else { //失败
$oWalletPlatformBalanceTransactionModel->withdrawErrByUserTransactionId($id); $oWalletPlatformBalanceTransactionModel->withdrawErrByUserTransactionId($id);
} }

View File

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

View File

@ -19,6 +19,12 @@ class WalletCurrencyModel extends WalletBaseModel
'created_at', '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'; const CODE_USDT_TRC20 = 'USDT_TRC20';
function findByCode($code,$columns = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null function findByCode($code,$columns = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null

25
app/Tools/Logs.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App\Tools;
use Illuminate\Support\Facades\Log;
class Logs
{
static function ErrLog($title, \Exception $e, $params = []): void
{
Log::error(self::getTitle($title,'success:'), ['message' => $e->getMessage(), 'trace' => $e->getTrace(), 'params' => $params]);
}
static function SuccLog($title, $params = []): void
{
Log::error(self::getTitle($title,'error:'), $params);
}
static function getTitle($title,$prefix=''): string
{
return $title.' '.$prefix;
}
}

View File

@ -1,12 +1,19 @@
<?php <?php
namespace App\Tools; namespace App\Tools;
use Illuminate\Support\Carbon;
class Times class Times
{ {
//获取当前时间 //获取当前时间
public static function getNowDateTime($format = 'Y-m-d H:i:s'): string public static function getNowDateTime(string $format = 'Y-m-d H:i:s'): string
{ {
return date($format); return date($format);
} }
public static function getNowDateTimeAddDays(int $days, string $format = 'Y-m-d'): string
{
return Carbon::now()->addDays($days)->format($format);
}
} }