SanJinRechargeService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. $keyboard[] = [
  171. ['text' => "返回", 'callback_data' => "topUp@@home"]
  172. ];
  173. // $wallet = Wallet::where('member_id', $chatId)->first();
  174. // $temp = floatval($wallet->available_balance);
  175. // $text = "请发送提现金额\n";
  176. // $text .= "💰 当前余额{$temp} RMB\n";
  177. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  178. return [
  179. 'chat_id' => $chatId,
  180. 'text' => $text,
  181. 'message_id' => $messageId,
  182. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  183. ];
  184. }
  185. //2.输入三斤提现金额
  186. private static function inputQbAmount($chatId, $amount, $messageId)
  187. {
  188. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  189. return [
  190. 'chat_id' => $chatId,
  191. 'text' => "金额输入不正确,请发送提现数字",
  192. 'reply_to_message_id' => $messageId
  193. ];
  194. }
  195. $amount = floatval($amount);
  196. $wallet = Wallet::where('member_id', $chatId)->first();
  197. $temp = floatval($wallet->available_balance);
  198. if ($amount > $temp) {
  199. return [
  200. 'chat_id' => $chatId,
  201. 'text' => "⚠️可用余额不足,请重试",
  202. 'reply_to_message_id' => $messageId
  203. ];
  204. }
  205. if ($amount < 100) {
  206. return [
  207. 'chat_id' => $chatId,
  208. 'text' => "⚠️提现不能少于100 RMB,请重试",
  209. 'reply_to_message_id' => $messageId
  210. ];
  211. }
  212. if ($amount > 49999) {
  213. return [
  214. 'chat_id' => $chatId,
  215. 'text' => "⚠️最多提现 49999 RMB,请重试",
  216. 'reply_to_message_id' => $messageId
  217. ];
  218. }
  219. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  220. $list = Bank::where('member_id', $chatId)->get();
  221. $keyboard = [];
  222. foreach ($list as $item) {
  223. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@choose_qb_{$item->id}"]];
  224. }
  225. $keyboard[] = [
  226. ['text' => '🏠 银行卡管理', 'callback_data' => "withdraw@@banks"],
  227. ['text' => '❌取消', 'callback_data' => "message@@close"]
  228. ];
  229. $text = "请直接选择下面的地址\n";
  230. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  231. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  232. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_QB_ADDRESS);
  233. return [
  234. 'chat_id' => $chatId,
  235. 'text' => $text,
  236. 'reply_to_message_id' => $messageId,
  237. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  238. ];
  239. }
  240. //3.选择银行卡号
  241. private static function chooseBank($chatId, $id)
  242. {
  243. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  244. if (!$amount) return WalletService::getBalance($chatId);
  245. $bank = Bank::where('id', $id)->first();
  246. $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  247. return $result;
  248. // $text = "提交成功\n";
  249. // $text .= "结果将在稍后通知您,请留意通知!!!";
  250. // return [
  251. // 'chat_id' => $chatId,
  252. // 'text' => $text,
  253. // ];
  254. }
  255. //银行卡管理
  256. private static function banks($chatId, $messageId)
  257. {
  258. $text = "💳️ 银行卡管理\n";
  259. $text .= "--------------------------\n";
  260. $list = Bank::where('member_id', $chatId)
  261. ->get();
  262. $keyboard = [];
  263. foreach ($list as $item) {
  264. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  265. }
  266. if (count($list) < 5) {
  267. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  268. }
  269. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  270. return [
  271. 'chat_id' => $chatId,
  272. 'text' => $text,
  273. 'message_id' => $messageId,
  274. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  275. ];
  276. }
  277. //银行卡详情
  278. private static function bankDetails($chatId, $messageId, $id)
  279. {
  280. $text = "*银行卡管理*\n\n";
  281. $bank = Bank::where('id', $id)
  282. ->where('member_id', $chatId)->first();
  283. switch ($bank->channel) {
  284. case "DF001":
  285. $text .= "姓名:{$bank->account}\n";
  286. $text .= "银行:{$bank->bank_name}\n";
  287. $text .= "卡号:{$bank->card_no}\n";
  288. break;
  289. case "DF002":
  290. $text .= "姓名:{$bank->account}\n";
  291. $text .= "银行:{$bank->bank_name}\n";
  292. $text .= "账号:{$bank->card_no}\n";
  293. break;
  294. default:
  295. $text .= "姓名:{$bank->account}\n";
  296. $text .= "银行:{$bank->bank_name}\n";
  297. $text .= "卡号:{$bank->card_no}\n";
  298. break;
  299. }
  300. $keyboard = [
  301. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  302. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  303. ];
  304. return [
  305. 'chat_id' => $chatId,
  306. 'parse_mode' => 'MarkdownV2',
  307. 'text' => $text,
  308. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  309. 'message_id' => $messageId
  310. ];
  311. }
  312. //删除银行卡
  313. private static function bankDelete($chatId, $messageId, $id)
  314. {
  315. Bank::where('id', $id)
  316. ->where('member_id', $chatId)->delete();
  317. return static::banks($chatId, $messageId);
  318. }
  319. //添加银行卡
  320. private static function addBank($chatId, $messageId)
  321. {
  322. $text = "请选择 提现通道\n";
  323. $keyboard = [
  324. [
  325. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  326. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  327. ],
  328. [
  329. ['text' => '❌取消', 'callback_data' => "message@@close"]
  330. ]
  331. ];
  332. return [
  333. 'chat_id' => $chatId,
  334. 'text' => $text,
  335. 'message_id' => $messageId,
  336. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  337. ];
  338. }
  339. //选择通道
  340. private static function chooseChannel($chatId, $messageId, $channel)
  341. {
  342. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  343. switch ($channel) {
  344. case "DF002"://支付宝
  345. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  346. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  347. return [
  348. 'chat_id' => $chatId,
  349. 'text' => "请输入支付宝账号",
  350. 'message_id' => $messageId,
  351. ];
  352. break;
  353. case "DF001"://银行卡
  354. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  355. return [
  356. 'chat_id' => $chatId,
  357. 'text' => "请输入银行名称",
  358. 'message_id' => $messageId,
  359. ];
  360. break;
  361. default:
  362. return [
  363. 'chat_id' => $chatId,
  364. 'text' => "选择通道错误",
  365. 'message_id' => $messageId,
  366. ];
  367. break;
  368. }
  369. }
  370. //输入银行名称
  371. private static function inputBankName($chatId, $bankName, $messageId)
  372. {
  373. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  374. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  375. return [
  376. 'chat_id' => $chatId,
  377. 'text' => "请输入银行卡号",
  378. 'message_id' => $messageId,
  379. ];
  380. }
  381. //输入卡号
  382. private static function inputCardNo($chatId, $cardNo, $messageId)
  383. {
  384. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  385. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  386. return [
  387. 'chat_id' => $chatId,
  388. 'text' => "输入的银行卡号有误,请重新输入",
  389. 'reply_to_message_id' => $messageId,
  390. ];
  391. }
  392. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  393. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  394. return [
  395. 'chat_id' => $chatId,
  396. 'text' => "请输入姓名",
  397. 'message_id' => $messageId,
  398. ];
  399. }
  400. //输入姓名,并保存到数据库
  401. private static function inputAccount($chatId, $account, $messageId)
  402. {
  403. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  404. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  405. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  406. Bank::create([
  407. 'member_id' => $chatId,
  408. 'account' => $account,
  409. 'channel' => $channel,
  410. 'card_no' => $cardNo,
  411. 'bank_name' => $bankName
  412. ]);
  413. return static::banks($chatId, $messageId);
  414. }
  415. }