TopUpService.php 8.8 KB

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