SanJinRechargeService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Bank;
  5. use App\Models\Config;
  6. use App\Models\PaymentOrder;
  7. use App\Models\Wallet;
  8. use Illuminate\Support\Facades\Cache;
  9. use Telegram\Bot\Api;
  10. class SanJinRechargeService
  11. {
  12. /**
  13. * @param Api $telegram
  14. * @param $data
  15. * @param $chatId
  16. * @param $firstName
  17. * @param $messageId
  18. * @throws \Telegram\Bot\Exceptions\TelegramSDKException
  19. */
  20. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId)
  21. {
  22. // //点击三斤提现按钮
  23. // if ($data === "withdraw@@qb_apply") {
  24. // $res = SanJinRechargeService::qbApply($chatId, $messageId);
  25. // $telegram->editMessageText($res);
  26. // }
  27. //三斤账单
  28. $pattern = "/^topup@@sj_bill_\d+$/";
  29. if (preg_match($pattern, $data)) {
  30. $page = preg_replace('/^topup@@sj_bill_/', '', $data);
  31. if (empty($page) || $page < 1) $page = 1;
  32. $page = intval($page);
  33. $res = SanJinRechargeService::bill($chatId, $firstName, $messageId, $page);
  34. $telegram->editMessageText($res);
  35. }
  36. // //选择银行卡号
  37. // $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
  38. // if (preg_match($pattern, $data)) {
  39. // $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  40. // $res = SanJinRechargeService::chooseBank($chatId, $id);
  41. // $telegram->deleteMessage([
  42. // 'chat_id' => $chatId,
  43. // 'message_id' => $messageId,
  44. // ]);
  45. // $telegram->sendMessage($res);
  46. // }
  47. // //银行卡管理
  48. // if ($data === 'withdraw@@banks') {
  49. // $res = SanJinRechargeService::banks($chatId, $messageId);
  50. // $telegram->editMessageText($res);
  51. // }
  52. // //银行卡详情
  53. // $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  54. // if (preg_match($pattern, $data)) {
  55. // $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  56. // $res = static::bankDetails($chatId, $messageId, $id);
  57. // $telegram->editMessageText($res);
  58. // }
  59. // $pattern = "/^withdraw@@bank_del_\d+$/";
  60. // if (preg_match($pattern, $data)) {
  61. // $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  62. // $res = static::bankDelete($chatId, $messageId, $id);
  63. // $telegram->editMessageText($res);
  64. // }
  65. // //添加银行卡
  66. // if ($data === "withdrawAddress@@bank_add") {
  67. // $res = SanJinRechargeService::addBank($chatId, $messageId);
  68. // $telegram->editMessageText($res);
  69. // }
  70. // $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  71. // if (preg_match($pattern, $data)) {
  72. // $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  73. // $res = static::chooseChannel($chatId, $messageId, $channel);
  74. // $telegram->editMessageText($res);
  75. // }
  76. }
  77. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  78. {
  79. switch ($stepStatus) {
  80. case StepStatus::INPUT_WITHDRAW_QB_MONEY://输入提现金额
  81. $res = SanJinRechargeService::inputQbAmount($chatId, $text, $messageId);
  82. return $res;
  83. break;
  84. case StepStatus::QB_INPUT_BANK_NAME://输入银行名称
  85. $res = SanJinRechargeService::inputBankName($chatId, $text, $messageId);
  86. return $res;
  87. case StepStatus::QB_INPUT_CARD_NO://输入银行卡号/支付宝账号
  88. $res = SanJinRechargeService::inputCardNo($chatId, $text, $messageId);
  89. return $res;
  90. break;
  91. case StepStatus::QB_INPUT_ACCOUNT://输入姓名
  92. $res = SanJinRechargeService::inputAccount($chatId, $text, $messageId);
  93. return $res;
  94. break;
  95. }
  96. return null;
  97. }
  98. //三斤账单
  99. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  100. {
  101. $list = PaymentOrder::where('member_id', $chatId)
  102. ->where('type', 1)
  103. ->orderByDesc('created_at')
  104. ->forPage($page, $limit)
  105. ->get();
  106. $count = PaymentOrder::where('member_id', $chatId)
  107. ->where('type', 1)
  108. ->count();
  109. $text = "👤 {$firstName}({$chatId}) 三斤充值记录\n\n";
  110. foreach ($list as $item) {
  111. $amount = floatval($item->amount);
  112. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  113. $text .= "-------------------------------------\n";
  114. $text .= "{$amount} \n";
  115. $text .= "订单号:{$item->order_no}\n";
  116. $text .= "支付金额:{$item->amo}\n";
  117. // $text .= "银行:{$item->bank_name}\n";
  118. // $text .= "姓名:{$item->account}\n";
  119. // $text .= "卡号:{$item->card_no}\n";
  120. $status = ['待处理', '处理中', '成功', '失败'];
  121. $text .= "状态:{$status[$item->status]}\n";
  122. if ($item->remark) {
  123. $text .= "说明:{$item->remark}\n";
  124. }
  125. $text .= "日期:{$item->created_at}\n";
  126. }
  127. if ($page > 1) {
  128. $keyboard[] = [
  129. ['text' => "👆上一页", 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  130. ];
  131. }
  132. $allPage = ceil($count / $limit);
  133. if ($allPage > $page) {
  134. if ($page > 1) {
  135. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  136. } else {
  137. $keyboard[] = [
  138. ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  139. ];
  140. }
  141. }
  142. $keyboard[] = [
  143. ['text' => "返回", 'callback_data' => "topUp@@home"]
  144. ];
  145. return [
  146. 'chat_id' => $chatId,
  147. 'text' => $text,
  148. 'message_id' => $messageId,
  149. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  150. ];
  151. }
  152. //1.三斤提现
  153. private static function qbApply($chatId, $messageId)
  154. {
  155. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  156. if ($three_payment_switch != 1) {
  157. $res = WalletService::getBalance($chatId);
  158. $res['message_id'] = $messageId;
  159. return $res;
  160. }
  161. $wallet = Wallet::where('member_id', $chatId)->first();
  162. $temp = floatval($wallet->available_balance);
  163. $text = "请发送提现金额\n";
  164. $text .= "💰 当前余额{$temp} RMB\n";
  165. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  166. return [
  167. 'chat_id' => $chatId,
  168. 'text' => $text,
  169. 'message_id' => $messageId,
  170. ];
  171. }
  172. //2.输入三斤提现金额
  173. private static function inputQbAmount($chatId, $amount, $messageId)
  174. {
  175. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  176. return [
  177. 'chat_id' => $chatId,
  178. 'text' => "金额输入不正确,请发送提现数字",
  179. 'reply_to_message_id' => $messageId
  180. ];
  181. }
  182. $amount = floatval($amount);
  183. $wallet = Wallet::where('member_id', $chatId)->first();
  184. $temp = floatval($wallet->available_balance);
  185. if ($amount > $temp) {
  186. return [
  187. 'chat_id' => $chatId,
  188. 'text' => "⚠️可用余额不足,请重试",
  189. 'reply_to_message_id' => $messageId
  190. ];
  191. }
  192. if ($amount < 100) {
  193. return [
  194. 'chat_id' => $chatId,
  195. 'text' => "⚠️提现不能少于100 RMB,请重试",
  196. 'reply_to_message_id' => $messageId
  197. ];
  198. }
  199. if ($amount > 49999) {
  200. return [
  201. 'chat_id' => $chatId,
  202. 'text' => "⚠️最多提现 49999 RMB,请重试",
  203. 'reply_to_message_id' => $messageId
  204. ];
  205. }
  206. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  207. $list = Bank::where('member_id', $chatId)->get();
  208. $keyboard = [];
  209. foreach ($list as $item) {
  210. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@choose_qb_{$item->id}"]];
  211. }
  212. $keyboard[] = [
  213. ['text' => '🏠 银行卡管理', 'callback_data' => "withdraw@@banks"],
  214. ['text' => '❌取消', 'callback_data' => "message@@close"]
  215. ];
  216. $text = "请直接选择下面的地址\n";
  217. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  218. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  219. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_QB_ADDRESS);
  220. return [
  221. 'chat_id' => $chatId,
  222. 'text' => $text,
  223. 'reply_to_message_id' => $messageId,
  224. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  225. ];
  226. }
  227. //3.选择银行卡号
  228. private static function chooseBank($chatId, $id)
  229. {
  230. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  231. if (!$amount) return WalletService::getBalance($chatId);
  232. $bank = Bank::where('id', $id)->first();
  233. $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  234. return $result;
  235. // $text = "提交成功\n";
  236. // $text .= "结果将在稍后通知您,请留意通知!!!";
  237. // return [
  238. // 'chat_id' => $chatId,
  239. // 'text' => $text,
  240. // ];
  241. }
  242. //银行卡管理
  243. private static function banks($chatId, $messageId)
  244. {
  245. $text = "💳️ 银行卡管理\n";
  246. $text .= "--------------------------\n";
  247. $list = Bank::where('member_id', $chatId)
  248. ->get();
  249. $keyboard = [];
  250. foreach ($list as $item) {
  251. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  252. }
  253. if (count($list) < 5) {
  254. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  255. }
  256. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  257. return [
  258. 'chat_id' => $chatId,
  259. 'text' => $text,
  260. 'message_id' => $messageId,
  261. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  262. ];
  263. }
  264. //银行卡详情
  265. private static function bankDetails($chatId, $messageId, $id)
  266. {
  267. $text = "*银行卡管理*\n\n";
  268. $bank = Bank::where('id', $id)
  269. ->where('member_id', $chatId)->first();
  270. switch ($bank->channel) {
  271. case "DF001":
  272. $text .= "姓名:{$bank->account}\n";
  273. $text .= "银行:{$bank->bank_name}\n";
  274. $text .= "卡号:{$bank->card_no}\n";
  275. break;
  276. case "DF002":
  277. $text .= "姓名:{$bank->account}\n";
  278. $text .= "银行:{$bank->bank_name}\n";
  279. $text .= "账号:{$bank->card_no}\n";
  280. break;
  281. default:
  282. $text .= "姓名:{$bank->account}\n";
  283. $text .= "银行:{$bank->bank_name}\n";
  284. $text .= "卡号:{$bank->card_no}\n";
  285. break;
  286. }
  287. $keyboard = [
  288. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  289. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  290. ];
  291. return [
  292. 'chat_id' => $chatId,
  293. 'parse_mode' => 'MarkdownV2',
  294. 'text' => $text,
  295. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  296. 'message_id' => $messageId
  297. ];
  298. }
  299. //删除银行卡
  300. private static function bankDelete($chatId, $messageId, $id)
  301. {
  302. Bank::where('id', $id)
  303. ->where('member_id', $chatId)->delete();
  304. return static::banks($chatId, $messageId);
  305. }
  306. //添加银行卡
  307. private static function addBank($chatId, $messageId)
  308. {
  309. $text = "请选择 提现通道\n";
  310. $keyboard = [
  311. [
  312. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  313. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  314. ],
  315. [
  316. ['text' => '❌取消', 'callback_data' => "message@@close"]
  317. ]
  318. ];
  319. return [
  320. 'chat_id' => $chatId,
  321. 'text' => $text,
  322. 'message_id' => $messageId,
  323. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  324. ];
  325. }
  326. //选择通道
  327. private static function chooseChannel($chatId, $messageId, $channel)
  328. {
  329. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  330. switch ($channel) {
  331. case "DF002"://支付宝
  332. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  333. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  334. return [
  335. 'chat_id' => $chatId,
  336. 'text' => "请输入支付宝账号",
  337. 'message_id' => $messageId,
  338. ];
  339. break;
  340. case "DF001"://银行卡
  341. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  342. return [
  343. 'chat_id' => $chatId,
  344. 'text' => "请输入银行名称",
  345. 'message_id' => $messageId,
  346. ];
  347. break;
  348. default:
  349. return [
  350. 'chat_id' => $chatId,
  351. 'text' => "选择通道错误",
  352. 'message_id' => $messageId,
  353. ];
  354. break;
  355. }
  356. }
  357. //输入银行名称
  358. private static function inputBankName($chatId, $bankName, $messageId)
  359. {
  360. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  361. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  362. return [
  363. 'chat_id' => $chatId,
  364. 'text' => "请输入银行卡号",
  365. 'message_id' => $messageId,
  366. ];
  367. }
  368. //输入卡号
  369. private static function inputCardNo($chatId, $cardNo, $messageId)
  370. {
  371. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  372. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  373. return [
  374. 'chat_id' => $chatId,
  375. 'text' => "输入的银行卡号有误,请重新输入",
  376. 'reply_to_message_id' => $messageId,
  377. ];
  378. }
  379. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  380. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  381. return [
  382. 'chat_id' => $chatId,
  383. 'text' => "请输入姓名",
  384. 'message_id' => $messageId,
  385. ];
  386. }
  387. //输入姓名,并保存到数据库
  388. private static function inputAccount($chatId, $account, $messageId)
  389. {
  390. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  391. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  392. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  393. Bank::create([
  394. 'member_id' => $chatId,
  395. 'account' => $account,
  396. 'channel' => $channel,
  397. 'card_no' => $cardNo,
  398. 'bank_name' => $bankName
  399. ]);
  400. return static::banks($chatId, $messageId);
  401. }
  402. }