TopUpService.php 10 KB

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