SanJinRechargeService.php 16 KB

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