TopUpService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Services;
  3. //充值服务
  4. use App\Constants\StepStatus;
  5. use App\Models\BalanceLog;
  6. use App\Models\Config;
  7. use App\Models\Recharge;
  8. use App\Models\Wallet;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Storage;
  11. use Telegram\Bot\Api;
  12. use Telegram\Bot\FileUpload\InputFile;
  13. use Telegram\Bot\Exceptions\TelegramSDKException;
  14. class TopUpService
  15. {
  16. /**
  17. * 到账通知
  18. * @param $memberId string 要通知的TgID
  19. * @param $text string 通知内容
  20. * @return bool 是否发送成功
  21. */
  22. public static function notifyTransferSuccess($memberId, $text)
  23. {
  24. try {
  25. $telegram = new Api(config('services.telegram.token'));
  26. $telegram->sendMessage(['chat_id' => $memberId, 'text' => $text]);
  27. } catch (TelegramSDKException $e) {
  28. return false;
  29. }
  30. return true;
  31. }
  32. public function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
  33. {
  34. RechargeService::syncUsdtRechargeRecords($chatId);
  35. $list = BalanceLog::where('member_id', $chatId)
  36. // ->where('change_type', '充值')
  37. ->whereIn('change_type', ['充值','提现','人工充值','人工扣款'])
  38. ->orderBy('created_at', 'desc')
  39. ->forPage($page, $limit)
  40. ->get();
  41. $count = BalanceLog::where('member_id', $chatId)
  42. // ->where('change_type', '充值')
  43. ->count();
  44. $text = "👤 {$firstName}({$chatId}) 钱包充值记录\n\n";
  45. foreach ($list as $item) {
  46. $amount = floatval($item->amount);
  47. $amount = $amount < 0 ? ("➖ " . ($amount * -1)) : "➕ $amount";
  48. $balance = floatval($item->after_balance);
  49. $text .= "-------------------------------------\n";
  50. $text .= "{$amount} \n余额:{$balance} \n";
  51. $text .= "类别:{$item->change_type}\n";
  52. if ($item->remark) {
  53. $text .= "说明:{$item->remark}\n";
  54. }
  55. $text .= "日期:{$item->created_at}\n";
  56. }
  57. if ($page > 1) {
  58. $keyboard[] = [
  59. ['text' => "👆上一页", 'callback_data' => "topUpBillNextPage@@" . ($page - 1)]
  60. ];
  61. }
  62. $allPage = ceil($count / $limit);
  63. if ($allPage > $page) {
  64. if ($page > 1) {
  65. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "topUpBillNextPage@@" . ($page + 1)];
  66. } else {
  67. $keyboard[] = [
  68. ['text' => "👇下一页", 'callback_data' => "topUpBillNextPage@@" . ($page + 1)]
  69. ];
  70. }
  71. }
  72. $keyboard[] = [
  73. ['text' => "返回", 'callback_data' => "topUp@@home"]
  74. ];
  75. return [
  76. 'chat_id' => $chatId,
  77. 'text' => $text,
  78. 'message_id' => $messageId,
  79. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  80. ];
  81. }
  82. /**
  83. * 充值图片
  84. * @param $chatId
  85. * @param $photo
  86. * @throws TelegramSDKException
  87. */
  88. public static function photo($chatId, $photo)
  89. {
  90. $telegram = $telegram = new Api(config('services.telegram.token'));
  91. $file = $telegram->getFile(['file_id' => $photo['file_id']]);
  92. $filePath = $file->getFilePath();
  93. $token = config('services.telegram.token');
  94. $file_url = "https://api.telegram.org/file/bot{$token}/{$filePath}";
  95. $file_info = pathinfo($file_url);
  96. $save_path = storage_path("app/public/images/{$chatId}/");
  97. if (!is_dir($save_path)) {
  98. mkdir($save_path, 0777, true);
  99. }
  100. $fileName = uniqid();
  101. $fileName = "{$fileName}.{$file_info['extension']}";
  102. $save_path .= $fileName;
  103. file_put_contents($save_path, file_get_contents($file_url));
  104. $path = Storage::url("images/{$chatId}/" . $fileName);
  105. $amount = Cache::get("{$chatId}_TOP_UP_AMOUNT");
  106. $toAddress = Cache::get("{$chatId}_TOP_UP_ADDRESS");
  107. $net = Cache::get("{$chatId}_TOP_UP_TYPE");
  108. $recharge = new Recharge();
  109. $recharge->member_id = $chatId;
  110. $recharge->net = $net;
  111. $recharge->coin = "USDT";
  112. $recharge->amount = $amount;
  113. $recharge->to_address = $toAddress;
  114. $recharge->status = 0;
  115. $recharge->type = 2;
  116. $recharge->image = $path;
  117. $recharge->save();
  118. Cache::delete(get_step_key($chatId));
  119. Cache::delete("{$chatId}_TOP_UP_AMOUNT");
  120. Cache::delete("{$chatId}_TOP_UP_ADDRESS");
  121. Cache::delete("{$chatId}_TOP_UP_TYPE");
  122. return [
  123. 'chat_id' => $chatId,
  124. 'text' => '已提交充值凭证,请等待系统审核完成'
  125. ];
  126. }
  127. //输入充值金额
  128. public static function inputAmount($chatId, $amount, $messageId)
  129. {
  130. if (!preg_match('/^\d+(\.\d+)?$/', $amount)) {
  131. return [
  132. 'chat_id' => $chatId,
  133. 'text' => "金额输入不正确,请发送充值数字",
  134. 'reply_to_message_id' => $messageId
  135. ];
  136. }
  137. Cache::put("{$chatId}_TOP_UP_AMOUNT", $amount);
  138. Cache::put(get_step_key($chatId), StepStatus::INPUT_TOP_UP_IMAGE);
  139. return [
  140. 'chat_id' => $chatId,
  141. 'text' => "请发送您的充值凭证截图,方便系统验证",
  142. ];
  143. }
  144. //用户点击我已付款 (手动)
  145. public static function pay2($chatId)
  146. {
  147. Cache::put(get_step_key($chatId), StepStatus::INPUT_TOP_UP_MONEY);
  148. return [
  149. 'chat_id' => $chatId,
  150. 'text' => '请输入充值金额'
  151. ];
  152. }
  153. //用户点击我已付款
  154. public function done($chatId)
  155. {
  156. RechargeService::syncUsdtRechargeRecords($chatId);
  157. $keyboard = [
  158. [['text' => "🔙返回", 'callback_data' => "topUp@@home"]]
  159. ];
  160. return [
  161. 'chat_id' => $chatId,
  162. 'text' => '请耐心等待,充值成功后 Bot 会通知您!',
  163. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  164. ];
  165. }
  166. public static function chooseAddress($chatId, $messageId)
  167. {
  168. $keyboard = [
  169. [
  170. ['text' => 'TRC20', 'callback_data' => "topUp@@TRC20"],
  171. ['text' => 'ERC20', 'callback_data' => 'topUp@@ERC20']
  172. ],
  173. [
  174. ['text' => "🔙返回", 'callback_data' => "topUp@@home"],
  175. ]
  176. ];
  177. $text = '请选择网络类型';
  178. return [
  179. 'chat_id' => $chatId,
  180. 'text' => $text,
  181. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  182. 'message_id' => $messageId,
  183. ];
  184. }
  185. //获取充值二维码
  186. public function scan($chatId, $messageId, $type)
  187. {
  188. // $receivingType = Config::where('field', 'receiving_type')->first()->val;
  189. $receivingType = 2;
  190. //自动
  191. if ($receivingType == 1) {
  192. $res = WalletService::getRechargeImageAddress($chatId);
  193. $address = $res['address'];
  194. $qrCode = $res['full_path'];
  195. } //
  196. //手动
  197. else {
  198. if ($type === "TRC20") {
  199. $address = Config::where('field', 'receiving_address')->first()->val;
  200. } else if ($type === "ERC20") {
  201. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  202. }
  203. $res = WalletService::getPlatformImageAddress($address);
  204. $res['net'] = $type;
  205. $qrCode = $res['full_path'];
  206. Cache::put("{$chatId}_TOP_UP_ADDRESS", $address);
  207. Cache::put("{$chatId}_TOP_UP_TYPE", $type);
  208. }
  209. $replyInfo = KeyboardService::findOne(['button' => '充值提示文字']);
  210. $caption = "\n";
  211. if ($replyInfo) {
  212. $caption .= "{$replyInfo->reply}\n\n";
  213. } else {
  214. $caption .= "支持货币({$res['net']}):{$res['coin']}\n";
  215. $caption .= "专属收款地址:\n";
  216. $caption .= "{$address}\n";
  217. $caption .= "\n";
  218. $caption .= "⚠️提示:\n";
  219. $caption .= "- 如果上面地址和二维码不一致,请不要付款!\n";
  220. $caption .= "- 对上述地址充值后, 请点击我已付款!\n";
  221. $caption .= "- 请耐心等待, 充值成功后 Bot 会通知您!\n";
  222. }
  223. $keyboard = [
  224. [
  225. ['text' => '📋 复制地址', 'copy_text' => ['text' => $address]],
  226. ['text' => '✅ 我已付款', 'callback_data' => 'topUp@@pay']
  227. ],
  228. [
  229. ['text' => "🔙返回", 'callback_data' => "topUp@@home1"],
  230. ]
  231. ];
  232. //手动
  233. if ($receivingType != 1) {
  234. $keyboard[0][1] = ['text' => '✅ 我已付款', 'callback_data' => 'topUp@@pay2'];
  235. }
  236. return [
  237. 'chat_id' => $chatId,
  238. 'photo' => InputFile::create($qrCode),
  239. 'caption' => $caption,
  240. 'protect_content' => true,
  241. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  242. 'message_id' => $messageId
  243. ];
  244. }
  245. public function index($chatId, $firstName, $messageId = null)
  246. {
  247. $wallet = Wallet::where('member_id', $chatId)->first();
  248. $text = "👤 {$firstName}($chatId)\n";
  249. $text .= "💰钱包余额\n";
  250. $temp = floatval($wallet->available_balance);
  251. $text .= "USDT:{$temp}\n";
  252. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  253. $keyboard = [
  254. [
  255. ['text' => '➕充值', 'callback_data' => "topup@@topup"],
  256. ['text' => '🧾账单', 'callback_data' => "topup@@bill"],
  257. ],
  258. [
  259. ['text' => '👩 客服帮助', 'url' => "https://t.me/{$serviceAccount}"],
  260. ['text' => '❌取消', 'callback_data' => "message@@close"],
  261. ]
  262. ];
  263. return [
  264. 'chat_id' => $chatId,
  265. 'text' => $text,
  266. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  267. 'message_id' => $messageId
  268. ];
  269. }
  270. }