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 .= "支付金额:{$item->amo}\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. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  234. return $res;
  235. }
  236. //3.选择银行卡号
  237. private static function chooseBank($chatId, $id)
  238. {
  239. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  240. if (!$amount) return WalletService::getBalance($chatId);
  241. $bank = Bank::where('id', $id)->first();
  242. $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  243. return $result;
  244. // $text = "提交成功\n";
  245. // $text .= "结果将在稍后通知您,请留意通知!!!";
  246. // return [
  247. // 'chat_id' => $chatId,
  248. // 'text' => $text,
  249. // ];
  250. }
  251. //银行卡管理
  252. private static function banks($chatId, $messageId)
  253. {
  254. $text = "💳️ 银行卡管理\n";
  255. $text .= "--------------------------\n";
  256. $list = Bank::where('member_id', $chatId)
  257. ->get();
  258. $keyboard = [];
  259. foreach ($list as $item) {
  260. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  261. }
  262. if (count($list) < 5) {
  263. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  264. }
  265. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  266. return [
  267. 'chat_id' => $chatId,
  268. 'text' => $text,
  269. 'message_id' => $messageId,
  270. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  271. ];
  272. }
  273. //银行卡详情
  274. private static function bankDetails($chatId, $messageId, $id)
  275. {
  276. $text = "*银行卡管理*\n\n";
  277. $bank = Bank::where('id', $id)
  278. ->where('member_id', $chatId)->first();
  279. switch ($bank->channel) {
  280. case "DF001":
  281. $text .= "姓名:{$bank->account}\n";
  282. $text .= "银行:{$bank->bank_name}\n";
  283. $text .= "卡号:{$bank->card_no}\n";
  284. break;
  285. case "DF002":
  286. $text .= "姓名:{$bank->account}\n";
  287. $text .= "银行:{$bank->bank_name}\n";
  288. $text .= "账号:{$bank->card_no}\n";
  289. break;
  290. default:
  291. $text .= "姓名:{$bank->account}\n";
  292. $text .= "银行:{$bank->bank_name}\n";
  293. $text .= "卡号:{$bank->card_no}\n";
  294. break;
  295. }
  296. $keyboard = [
  297. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  298. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  299. ];
  300. return [
  301. 'chat_id' => $chatId,
  302. 'parse_mode' => 'MarkdownV2',
  303. 'text' => $text,
  304. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  305. 'message_id' => $messageId
  306. ];
  307. }
  308. //删除银行卡
  309. private static function bankDelete($chatId, $messageId, $id)
  310. {
  311. Bank::where('id', $id)
  312. ->where('member_id', $chatId)->delete();
  313. return static::banks($chatId, $messageId);
  314. }
  315. //添加银行卡
  316. private static function addBank($chatId, $messageId)
  317. {
  318. $text = "请选择 提现通道\n";
  319. $keyboard = [
  320. [
  321. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  322. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  323. ],
  324. [
  325. ['text' => '❌取消', 'callback_data' => "message@@close"]
  326. ]
  327. ];
  328. return [
  329. 'chat_id' => $chatId,
  330. 'text' => $text,
  331. 'message_id' => $messageId,
  332. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  333. ];
  334. }
  335. //选择通道
  336. private static function chooseChannel($chatId, $messageId, $channel)
  337. {
  338. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  339. switch ($channel) {
  340. case "DF002"://支付宝
  341. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  342. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  343. return [
  344. 'chat_id' => $chatId,
  345. 'text' => "请输入支付宝账号",
  346. 'message_id' => $messageId,
  347. ];
  348. break;
  349. case "DF001"://银行卡
  350. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  351. return [
  352. 'chat_id' => $chatId,
  353. 'text' => "请输入银行名称",
  354. 'message_id' => $messageId,
  355. ];
  356. break;
  357. default:
  358. return [
  359. 'chat_id' => $chatId,
  360. 'text' => "选择通道错误",
  361. 'message_id' => $messageId,
  362. ];
  363. break;
  364. }
  365. }
  366. //输入银行名称
  367. private static function inputBankName($chatId, $bankName, $messageId)
  368. {
  369. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  370. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  371. return [
  372. 'chat_id' => $chatId,
  373. 'text' => "请输入银行卡号",
  374. 'message_id' => $messageId,
  375. ];
  376. }
  377. //输入卡号
  378. private static function inputCardNo($chatId, $cardNo, $messageId)
  379. {
  380. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  381. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  382. return [
  383. 'chat_id' => $chatId,
  384. 'text' => "输入的银行卡号有误,请重新输入",
  385. 'reply_to_message_id' => $messageId,
  386. ];
  387. }
  388. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  389. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  390. return [
  391. 'chat_id' => $chatId,
  392. 'text' => "请输入姓名",
  393. 'message_id' => $messageId,
  394. ];
  395. }
  396. //输入姓名,并保存到数据库
  397. private static function inputAccount($chatId, $account, $messageId)
  398. {
  399. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  400. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  401. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  402. Bank::create([
  403. 'member_id' => $chatId,
  404. 'account' => $account,
  405. 'channel' => $channel,
  406. 'card_no' => $cardNo,
  407. 'bank_name' => $bankName
  408. ]);
  409. return static::banks($chatId, $messageId);
  410. }
  411. }