| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace App\Module\Transaction\Services;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Fund\Services\FundService;
- use App\Module\System\Models\ReceiveAddress;
- use App\Module\System\Services\OrderNo;
- use App\Module\Transaction\Enums\ACCOUNT_TYPE;
- use App\Module\Transaction\Enums\RECHARGE_STATUS;
- use App\Module\Transaction\Enums\STATUS;
- use App\Module\Transaction\Models\Transaction;
- use App\Module\Transaction\Models\TransactionRecharge;
- use App\Module\Transaction\Models\TransactionTransfer;
- use App\Module\Transaction\Models\TransactionWithdrawal;
- use App\Module\Ulogic\Services\UserAddressService;
- use Illuminate\Support\Facades\DB;
- use UCore\Helper\Logger;
- /**
- * 交易单
- */
- class TransactionService
- {
- /**
- * @param $userId
- * @param $coinType
- * @param $type
- * @param $amount
- * @param $addressId
- * @return bool|int
- */
- public function createRecharge($userId, $coinType, $type, $amount, $addressId)
- {
- // 系统账户id
- $adminUserId = 1;
- $fromAddress = UserAddressService::getAddressById($addressId)->address;
- $toAddress = ReceiveAddress::getAddress()->address;
- DB::beginTransaction();
- try {
- // 插入交易主表
- $transactionData = [
- 'transaction_no' => OrderNo::generate(),
- 'user_id' => $userId,
- 'from_user_id' => $userId,
- 'to_user_id' => $adminUserId,
- 'coin_type' => $coinType,
- 'type' => $type,
- 'amount' => $amount,
- 'status' => STATUS::CALL_CREATED->value,
- ];
- $transactionId = Transaction::insert($transactionData);
- // 插入附表
- $rechargeData = [
- 'transaction_id' => $transactionId,
- 'from_address' => $fromAddress,
- 'to_address' => $toAddress,
- 'status' => RECHARGE_STATUS::WAIT_TX->value
- ];
- $re = TransactionRecharge::insert($rechargeData);
- DB::commit();
- return $re->id;
- } catch (\Exception $exception) {
- DB::rollBack();
- Logger::error('充值订单创建失败:'.$exception->getMessage());
- return false;
- }
- }
- /**
- * @param $userId
- * @param $transactionId
- * @return \Illuminate\Database\Eloquent\Collection
- * 获取交易单详情
- */
- public function getDetail($userId, $transactionId)
- {
- return Transaction::getData($userId, $transactionId);
- }
- /**
- * @param $transactionId
- * @return null
- * 获取充值单详情
- */
- public function getRechargeDetail($transactionId)
- {
- return TransactionRecharge::getDetail($transactionId);
- }
- /**
- * @param $transactionId
- * @return null
- * 获取转赠单详情
- */
- public static function getTransferDetail($transactionId)
- {
- return TransactionTransfer::getDetail($transactionId);
- }
- /**
- * @param $transactionId
- * @return null
- * 获取提现单详情
- */
- public static function getWithdrawalDetail($transactionId)
- {
- return TransactionWithdrawal::getDetail($transactionId);
- }
- /**
- * @param $id
- * @param $hash
- * @return int
- * 充值单增加hash
- */
- public function addHash($id, $hash)
- {
- return TransactionRecharge::addHash($id, $hash);
- }
- /**
- * @param $userId
- * @param $coinType
- * @param $type
- * @param $amount
- * @param $addressId
- * @return int|void
- * 创建转赠订单
- */
- public function createTransfer($userId, $coinType, $type, $amount, $addressId)
- {
- // 矿工费
- $minerAmount = \App\Module\App\Service\FundService::getMiner($coinType, $amount);
- $toUserAddressData = UserAddressService::getAddressById($addressId);
- // 交易主表数据
- $transactionData = [
- 'transaction_no' => OrderNo::generate(),
- 'user_id' => $userId,
- 'from_user_id' => $userId,
- 'to_user_id' => $toUserAddressData->user_id,
- 'coin_type' => $coinType,
- 'type' => $type,
- 'amount' => $amount,
- 'status' => STATUS::CALL_CREATED->value,
- ];
- DB::beginTransaction();
- try {
- // 交易主表数据
- $transactionId = Transaction::insert($transactionData);
- // 交易附表数据
- $transferData = [
- 'transaction_id' => $transactionId,
- 'from_address' => UserAddressService::getDataByUserId($userId)->address,
- 'to_address' => $toUserAddressData->address,
- 'miner_amount' => $minerAmount
- ];
- TransactionTransfer::insert($transferData);
- // 资金操作(冻结转出方资金,增加日志)
- $fund = [];
- $toUserId = 0;
- $minerUserId = 0;
- // uraus转赠
- if ($coinType == ACCOUNT_TYPE::URAUS->value) {
- $fund = new FundService($userId, FUND_TYPE::FUND1->value);
- $toUserId = FUND_TYPE::FUND2;
- $minerUserId = FUND_TYPE::FUND2;
- }
- // usdt转赠
- if ($coinType == ACCOUNT_TYPE::USDT->value) {
- $fund = new FundService($userId, FUND_TYPE::USD->value);
- $toUserId = FUND_TYPE::USD2;
- $minerUserId = FUND_TYPE::USD2;
- }
- // bnb转赠
- if ($coinType == ACCOUNT_TYPE::BNB->value) {
- $fund = new FundService($userId, FUND_TYPE::BNB->value);
- $toUserId = FUND_TYPE::BBB2;
- $minerUserId = FUND_TYPE::USD2;
- }
- $fund->circulation($toUserId, $amount, $transactionId, 'TRANSFER', 'TRANSFER');
- // 矿工费
- $fund->circulation($minerUserId, $minerAmount, $transactionId, 'TRANSFER-MINER', 'TRANSFER-MINER');
- DB::commit();
- return $transactionId;
- } catch (\Exception $exception) {
- DB::rollBack();
- Logger::error('创建转赠订单出错:'.$exception->getMessage());
- return 0;
- }
- }
- /**
- * @param $userId
- * @param $coinType
- * @param $type
- * @param $amount
- * @param $toAddress
- * @return int
- * 创建提现订单
- */
- public function createWithdrawal($userId, $coinType, $type, $amount, $toAddress)
- {
- // 矿工费
- $minerAmount = \App\Module\App\Service\FundService::getMiner($coinType, $amount);
- // 交易主表数据
- $transactionData = [
- 'transaction_no' => OrderNo::generate(),
- 'user_id' => $userId,
- 'from_user_id' => $userId,
- 'to_user_id' => 0,
- 'coin_type' => $coinType,
- 'type' => $type,
- 'amount' => $amount,
- 'status' => STATUS::CALL_CREATED->value,
- ];
- DB::beginTransaction();
- try {
- // 交易主表数据
- $transactionId = Transaction::insert($transactionData);
- // 交易附表数据
- $transferData = [
- 'transaction_id' => $transactionId,
- 'from_address' => UserAddressService::getDataByUserId($userId)->address,
- 'to_address' => $toAddress,
- 'miner_amount' => $minerAmount
- ];
- TransactionWithdrawal::insert($transferData);
- // 资金操作(冻结转出方资金,增加日志)
- $fund = [];
- $toUserId = 0;
- $minerUserId = 0;
- // uraus提现
- if ($coinType == ACCOUNT_TYPE::URAUS->value) {
- $fund = new FundService($userId, FUND_TYPE::FUND1->value);
- $toUserId = FUND_TYPE::FUND2;
- $minerUserId = FUND_TYPE::FUND2;
- }
- // usdt提现
- if ($coinType == ACCOUNT_TYPE::USDT->value) {
- $fund = new FundService($userId, FUND_TYPE::USD->value);
- $toUserId = FUND_TYPE::USD2;
- $minerUserId = FUND_TYPE::USD2;
- }
- // bnb提现
- if ($coinType == ACCOUNT_TYPE::BNB->value) {
- $fund = new FundService($userId, FUND_TYPE::BNB->value);
- $minerFund = new FundService($userId, FUND_TYPE::USD->value);
- $toUserId = FUND_TYPE::BBB2;
- $minerUserId = FUND_TYPE::USD2;
- }
- $fund->circulation($toUserId, $amount, $transactionId, 'WITHDRAWAL', 'WITHDRAWAL');
- // 矿工费
- $minerFund->circulation($minerUserId, $minerAmount, $transactionId, 'WITHDRAWAL-MINER', 'WITHDRAWAL-MINER');
- Db::commit();
- return $transactionId;
- }catch (\Exception $exception){
- DB::rollBack();
- Logger::error('创建提现订单出错:'.$exception->getMessage());
- throw new \Exception('创建提现订单出错');
- }
- }
- }
|