SanJinRechargeService.php 17 KB

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