SanJinRechargeService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. Cache::put($chatId.'_sj_payment_type', $k);
  54. Cache::put(get_step_key($chatId), StepStatus::INPUT_RECHARGE_SJ_MONEY);
  55. $text = "请输入预充值金额!";
  56. $res = [
  57. 'chat_id' => $chatId,
  58. 'text' => $text,
  59. 'message_id' => $messageId
  60. ];
  61. $telegram->editMessageText($res);
  62. }
  63. // //选择银行卡号
  64. // $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
  65. // if (preg_match($pattern, $data)) {
  66. // $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  67. // $res = SanJinRechargeService::chooseBank($chatId, $id);
  68. // $telegram->deleteMessage([
  69. // 'chat_id' => $chatId,
  70. // 'message_id' => $messageId,
  71. // ]);
  72. // $telegram->sendMessage($res);
  73. // }
  74. // //银行卡管理
  75. // if ($data === 'withdraw@@banks') {
  76. // $res = SanJinRechargeService::banks($chatId, $messageId);
  77. // $telegram->editMessageText($res);
  78. // }
  79. // //银行卡详情
  80. // $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  81. // if (preg_match($pattern, $data)) {
  82. // $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  83. // $res = static::bankDetails($chatId, $messageId, $id);
  84. // $telegram->editMessageText($res);
  85. // }
  86. // $pattern = "/^withdraw@@bank_del_\d+$/";
  87. // if (preg_match($pattern, $data)) {
  88. // $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  89. // $res = static::bankDelete($chatId, $messageId, $id);
  90. // $telegram->editMessageText($res);
  91. // }
  92. // //添加银行卡
  93. // if ($data === "withdrawAddress@@bank_add") {
  94. // $res = SanJinRechargeService::addBank($chatId, $messageId);
  95. // $telegram->editMessageText($res);
  96. // }
  97. // $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  98. // if (preg_match($pattern, $data)) {
  99. // $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  100. // $res = static::chooseChannel($chatId, $messageId, $channel);
  101. // $telegram->editMessageText($res);
  102. // }
  103. }
  104. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  105. {
  106. switch ($stepStatus) {
  107. case StepStatus::INPUT_RECHARGE_SJ_MONEY://输入提现金额
  108. $res = SanJinRechargeService::inputSjAmount($chatId, $text, $messageId);
  109. return $res;
  110. break;
  111. }
  112. return null;
  113. }
  114. //三斤账单
  115. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  116. {
  117. $list = PaymentOrder::where('member_id', $chatId)
  118. ->where('type', 1)
  119. ->orderByDesc('created_at')
  120. ->forPage($page, $limit)
  121. ->get();
  122. $count = PaymentOrder::where('member_id', $chatId)
  123. ->where('type', 1)
  124. ->count();
  125. $text = "👤 {$firstName}({$chatId}) 三斤充值记录\n\n";
  126. foreach ($list as $item) {
  127. $amount = floatval($item->amount);
  128. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  129. $text .= "-------------------------------------\n";
  130. $text .= "{$amount} \n";
  131. $text .= "订单号:{$item->order_no}\n";
  132. $text .= "请使用浏览器扫码,进行支付 \n";
  133. // $text .= "银行:{$item->bank_name}\n";
  134. // $text .= "姓名:{$item->account}\n";
  135. // $text .= "卡号:{$item->card_no}\n";
  136. $status = ['待处理', '处理中', '成功', '失败'];
  137. $text .= "状态:{$status[$item->status]}\n";
  138. if ($item->remark) {
  139. $text .= "说明:{$item->remark}\n";
  140. }
  141. $text .= "日期:{$item->created_at}\n";
  142. }
  143. if ($page > 1) {
  144. $keyboard[] = [
  145. ['text' => "👆上一页", 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  146. ];
  147. }
  148. $allPage = ceil($count / $limit);
  149. if ($allPage > $page) {
  150. if ($page > 1) {
  151. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  152. } else {
  153. $keyboard[] = [
  154. ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  155. ];
  156. }
  157. }
  158. $keyboard[] = [
  159. ['text' => "返回", 'callback_data' => "topUp@@home"]
  160. ];
  161. return [
  162. 'chat_id' => $chatId,
  163. 'text' => $text,
  164. 'message_id' => $messageId,
  165. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  166. ];
  167. }
  168. //1.三斤充值
  169. private static function qbApply($chatId, $messageId)
  170. {
  171. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  172. if ($three_payment_switch != 1) {
  173. $res = WalletService::getBalance($chatId);
  174. $res['message_id'] = $messageId;
  175. return $res;
  176. }
  177. $text = "请选择支付的通道 \n";
  178. $keyboard = [];
  179. $channel = SanJinService::$CHANNEL;
  180. foreach($channel as $k => $v){
  181. $keyboard[] = [
  182. ['text' => $v, 'callback_data' => "topup_channel@@" .$k]
  183. ];
  184. }
  185. $keyboard[] = [
  186. ['text' => "返回", 'callback_data' => "topUp@@home"]
  187. ];
  188. // $wallet = Wallet::where('member_id', $chatId)->first();
  189. // $temp = floatval($wallet->available_balance);
  190. // $text = "请发送提现金额\n";
  191. // $text .= "💰 当前余额{$temp} RMB\n";
  192. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  193. return [
  194. 'chat_id' => $chatId,
  195. 'text' => $text,
  196. 'message_id' => $messageId,
  197. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  198. ];
  199. }
  200. //2.输入三斤提现金额
  201. private static function inputSjAmount($chatId, $amount, $messageId)
  202. {
  203. // 支付通道
  204. $paymentType = Cache::get($chatId.'_sj_payment_type');
  205. if(empty($paymentType)){
  206. // 处理无效的通道
  207. $text = "无效的支付通道!";
  208. $res = [
  209. 'chat_id' => $chatId,
  210. 'text' => $text
  211. ];
  212. return $res;
  213. }
  214. // 验证 $k 是否有效
  215. $channel = SanJinService::$CHANNEL;
  216. if (!isset($channel[$paymentType])) {
  217. // 处理无效的通道
  218. $text = "无效的支付通道!";
  219. $res = [
  220. 'chat_id' => $chatId,
  221. 'text' => $text
  222. ];
  223. return $res;
  224. return;
  225. }
  226. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  227. return [
  228. 'chat_id' => $chatId,
  229. 'text' => "金额输入不正确,请发送预充值数字",
  230. 'reply_to_message_id' => $messageId
  231. ];
  232. }
  233. Cache::forget(get_step_key($chatId));
  234. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  235. return $res;
  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. }