RechargeService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Recharge;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\WalletService;
  9. use App\Services\CollectService;
  10. use App\Services\UserService;
  11. use App\Services\BalanceLogService;
  12. use App\Helpers\TronHelper;
  13. /**
  14. * 用户充值记录
  15. */
  16. class RechargeService extends BaseService
  17. {
  18. /**
  19. * @description: 模型
  20. * @return {string}
  21. */
  22. public static function model(): string
  23. {
  24. return Recharge::class;
  25. }
  26. /**
  27. * @description: 枚举
  28. * @return {*}
  29. */
  30. public static function enum(): string
  31. {
  32. return '';
  33. }
  34. /**
  35. * @description: 获取查询条件
  36. * @param {array} $search 查询内容
  37. * @return {array}
  38. */
  39. public static function getWhere(array $search = []): array
  40. {
  41. $where = [];
  42. if (isset($search['coin']) && !empty($search['coin'])) {
  43. $where[] = ['coin', '=', $search['coin']];
  44. }
  45. if (isset($search['net']) && !empty($search['net'])) {
  46. $where[] = ['net', '=', $search['net']];
  47. }
  48. if (isset($search['to_address']) && !empty($search['to_address'])) {
  49. $where[] = ['to_address', '=', $search['to_address']];
  50. }
  51. if (isset($search['id']) && !empty($search['id'])) {
  52. $where[] = ['id', '=', $search['id']];
  53. }
  54. if (isset($search['member_id']) && !empty($search['member_id'])) {
  55. $where[] = ['member_id', '=', $search['member_id']];
  56. }
  57. if (isset($search['txid']) && !empty($search['txid'])) {
  58. $where[] = ['txid', '=', $search['txid']];
  59. }
  60. if (isset($search['status']) && $search['status'] != '') {
  61. $where[] = ['status', '=', $search['status']];
  62. }
  63. if (isset($search['type']) && $search['type'] != '') {
  64. $where[] = ['type', '=', $search['type']];
  65. }
  66. return $where;
  67. }
  68. /**
  69. * @description: 查询单条数据
  70. * @param array $search
  71. * @return \App\Models\Coin|null
  72. */
  73. public static function findOne(array $search): ?Recharge
  74. {
  75. return self::model()::where(self::getWhere($search))->first();
  76. }
  77. /**
  78. * @description: 查询所有数据
  79. * @param array $search
  80. * @return \Illuminate\Database\Eloquent\Collection
  81. */
  82. public static function findAll(array $search = [])
  83. {
  84. return self::model()::where(self::getWhere($search))->get();
  85. }
  86. /**
  87. * @description: 分页查询
  88. * @param array $search
  89. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  90. */
  91. public static function paginate(array $search = [])
  92. {
  93. $limit = isset($search['limit']) ? $search['limit'] : 15;
  94. $paginator = self::model()::where(self::getWhere($search))
  95. ->orderBy("created_at", 'desc')
  96. ->paginate($limit);
  97. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  98. }
  99. /**
  100. * @description: 同步会员的USDT充值记录
  101. * @param {*} $memberId
  102. * @return {*}
  103. */
  104. public static function syncUsdtRechargeRecords($memberId)
  105. {
  106. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  107. $data = TronHelper::getTrc20UsdtRecharges($walletInfo->address);
  108. foreach ($data as $k => $v) {
  109. $v['member_id'] = $memberId;
  110. $v['net'] = $walletInfo->net;
  111. $V['type'] = self::model()::TYPE_AUTO;
  112. $v['created_at'] = now();
  113. $v['updated_at'] = now();
  114. $data[$k] = $v;
  115. }
  116. $m = new (self::model());
  117. $result = $m->insertOrIgnore($data);
  118. return $result;
  119. }
  120. /**
  121. * @description: 充值确认
  122. * @param {*} $txid
  123. * @return {*}
  124. */
  125. public static function handleRechargeConfirmation($txid)
  126. {
  127. $info = self::findOne(['txid' => $txid]); // 获取充值的信息
  128. // 待处理进行充值
  129. if ($info['status'] == self::model()::STATUS_STAY) {
  130. $result = TronHelper::getTransactionConfirmations($txid);
  131. if ($result['success']) {
  132. $data = [];
  133. $data['block_height'] = $result['block_number'];
  134. $data['confirmations'] = $result['latest_block'] - $result['block_number'];
  135. if ($data['confirmations'] >= TronHelper::CONFIRMED_NUMBER) {
  136. $data['status'] = self::model()::STATUS_SUCCESS;
  137. $where = self::getWhere(['txid' => $txid, 'status' => self::model()::STATUS_STAY]);
  138. $recharge = self::model()::where($where)->update($data);
  139. // 更新成功,变动可用余额
  140. if ($recharge) {
  141. $balanceData = WalletService::updateBalance($info->member_id, $info->amount);
  142. BalanceLogService::addLog($info->member_id, $info->amount, $balanceData['before_balance'], $balanceData['after_balance'], '充值', $info->id, '');
  143. CollectService::createCollect($info->to_address, $info->coin, $info->net);
  144. $amount = floatval($info->amount);
  145. $text = " ➕ 充币到帐 +{$amount} USDT\n";
  146. $text .= "链上哈希:{$txid}\n";
  147. $text .= "当前余额:{$balanceData['after_balance']}\n";
  148. TopUpService::notifyTransferSuccess($info->member_id, $text);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. /**
  155. * @description: 同步待处理的充值记录
  156. * @return {*}
  157. */
  158. public static function syncRechargeStay()
  159. {
  160. $list = self::findAll(['status' => self::model()::STATUS_STAY ,'type' => self::model()::TYPE_AUTO]);
  161. foreach ($list as $k => $v) {
  162. self::handleRechargeConfirmation($v->txid);
  163. }
  164. }
  165. }