|
|
@@ -1,272 +0,0 @@
|
|
|
-<?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\AppGame\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\AppGame\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('创建提现订单出错');
|
|
|
- }
|
|
|
- }
|
|
|
-}
|