SanJinRechargeService.php 17 KB

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