RechargeService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. use App\Models\Config;
  14. /**
  15. * 用户充值记录
  16. */
  17. class RechargeService extends BaseService
  18. {
  19. public static string $MODEL = Recharge::class;
  20. /**
  21. * @description: 模型
  22. * @return {string}
  23. */
  24. public static function model(): string
  25. {
  26. return Recharge::class;
  27. }
  28. /**
  29. * @description: 枚举
  30. * @return {*}
  31. */
  32. public static function enum(): string
  33. {
  34. return '';
  35. }
  36. /**
  37. * @description: 获取查询条件
  38. * @param {array} $search 查询内容
  39. * @return {array}
  40. */
  41. public static function getWhere(array $search = []): array
  42. {
  43. $where = [];
  44. if (isset($search['coin']) && !empty($search['coin'])) {
  45. $where[] = ['coin', '=', $search['coin']];
  46. }
  47. if (isset($search['net']) && !empty($search['net'])) {
  48. $where[] = ['net', '=', $search['net']];
  49. }
  50. if (isset($search['to_address']) && !empty($search['to_address'])) {
  51. $where[] = ['to_address', '=', $search['to_address']];
  52. }
  53. if (isset($search['id']) && !empty($search['id'])) {
  54. $where[] = ['id', '=', $search['id']];
  55. }
  56. if (isset($search['member_id']) && !empty($search['member_id'])) {
  57. $where[] = ['member_id', '=', $search['member_id']];
  58. }
  59. if (isset($search['txid']) && !empty($search['txid'])) {
  60. $where[] = ['txid', '=', $search['txid']];
  61. }
  62. if (isset($search['status']) && $search['status'] != '') {
  63. $where[] = ['status', '=', $search['status']];
  64. }
  65. if (isset($search['type']) && $search['type'] != '') {
  66. $where[] = ['type', '=', $search['type']];
  67. }
  68. return $where;
  69. }
  70. /**
  71. * @description: 查询单条数据
  72. * @param array $search
  73. * @return \App\Models\Coin|null
  74. */
  75. public static function findOne(array $search): ?Recharge
  76. {
  77. return static::$MODEL::where(self::getWhere($search))->first();
  78. }
  79. /**
  80. * @description: 查询所有数据
  81. * @param array $search
  82. * @return \Illuminate\Database\Eloquent\Collection
  83. */
  84. public static function findAll(array $search = [])
  85. {
  86. return static::$MODEL::where(self::getWhere($search))->get();
  87. }
  88. /**
  89. * @description: 分页查询
  90. * @param array $search
  91. * @return array
  92. */
  93. public static function paginate(array $search = [])
  94. {
  95. $limit = isset($search['limit']) ? $search['limit'] : 15;
  96. $paginator = static::$MODEL::where(self::getWhere($search))
  97. ->with(['member'])
  98. ->orderBy("created_at", 'desc')
  99. ->paginate($limit);
  100. $list = $paginator->items();
  101. $totalAmount = 0;
  102. $totalSuccess = 0;
  103. $totalFail = 0;
  104. foreach ($list as $item) {
  105. $item['amount'] = floatval($item['amount']);
  106. $totalAmount += $item['amount'];
  107. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  108. if ($item['status'] == 2) $totalFail += $item['amount'];
  109. }
  110. return [
  111. 'total' => $paginator->total(),
  112. 'total_amount' => $totalAmount,
  113. 'total_success' => $totalSuccess,
  114. 'total_fail' => $totalFail,
  115. 'data' => $list];
  116. }
  117. /**
  118. * @description: 同步会员的USDT充值记录
  119. * @param {*} $memberId
  120. * @return {*}
  121. */
  122. public static function syncUsdtRechargeRecords($memberId)
  123. {
  124. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  125. $data = TronHelper::getTrc20UsdtRecharges($walletInfo->address);
  126. foreach ($data as $k => $v) {
  127. $v['member_id'] = $memberId;
  128. $v['net'] = $walletInfo->net;
  129. $V['type'] = static::$MODEL::TYPE_AUTO;
  130. $v['created_at'] = now();
  131. $v['updated_at'] = now();
  132. $data[$k] = $v;
  133. }
  134. $m = new (static::$MODEL);
  135. $result = $m->insertOrIgnore($data);
  136. return $result;
  137. }
  138. /**
  139. * @description: 充值确认
  140. * @param {*} $txid
  141. * @return {*}
  142. */
  143. public static function handleRechargeConfirmation($txid)
  144. {
  145. $info = self::findOne(['txid' => $txid]); // 获取充值的信息
  146. // 汇率
  147. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  148. // 待处理进行充值
  149. if ($info['status'] == static::$MODEL::STATUS_STAY) {
  150. $result = TronHelper::getTransactionConfirmations($txid);
  151. if ($result['success']) {
  152. $data = [];
  153. $data['block_height'] = $result['block_number'];
  154. $data['confirmations'] = $result['latest_block'] - $result['block_number'];
  155. $data['exchange_rate'] = $rate;
  156. if ($data['confirmations'] >= TronHelper::CONFIRMED_NUMBER) {
  157. $data['status'] = static::$MODEL::STATUS_SUCCESS;
  158. $where = self::getWhere(['txid' => $txid, 'status' => static::$MODEL::STATUS_STAY]);
  159. $recharge = static::$MODEL::where($where)->update($data);
  160. $amount = floatval($info->amount);
  161. $rate_amount = bcmul($amount, $rate, 10); // 汇率转换后分数
  162. // 更新成功,变动可用余额
  163. if ($recharge) {
  164. $balanceData = WalletService::updateBalance($info->member_id, $rate_amount);
  165. BalanceLogService::addLog($info->member_id, $rate_amount, $balanceData['before_balance'], $balanceData['after_balance'], '充值', $info->id, '');
  166. CollectService::createCollect($info->to_address, $info->coin, $info->net);
  167. $amount = floatval($info->amount);
  168. $text = "充值结果通知\n";
  169. $text .= "充值数量:{$amount} USDT\n";
  170. $text .= "充值地址:{$info->to_address}\n";
  171. $text .= "汇率:1 USDT = {$rate} RMB\n";
  172. $text .= "折合金额:" . number_format($rate_amount, 2) . " RMB\n";
  173. $text .= "状态:成功\n";
  174. TopUpService::notifyTransferSuccess($info->member_id, $text);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * @description: 同步待处理的充值记录
  182. * @return {*}
  183. */
  184. public static function syncRechargeStay()
  185. {
  186. $list = self::findAll(['status' => static::$MODEL::STATUS_STAY, 'type' => static::$MODEL::TYPE_AUTO]);
  187. foreach ($list as $k => $v) {
  188. self::handleRechargeConfirmation($v->txid);
  189. }
  190. }
  191. }