SanJinRechargeService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. use App\Services\BaseService;
  12. class SanJinRechargeService extends BaseService
  13. {
  14. /**
  15. * @param Api $telegram
  16. * @param $data
  17. * @param $chatId
  18. * @param $firstName
  19. * @param $messageId
  20. * @throws \Telegram\Bot\Exceptions\TelegramSDKException
  21. */
  22. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId)
  23. {
  24. //点击三斤充值按钮
  25. if ($data === "topup@@sj_apply") {
  26. $res = SanJinRechargeService::qbApply($chatId, $messageId);
  27. $telegram->editMessageText($res);
  28. }
  29. //三斤账单
  30. $pattern = "/^topup@@sj_bill_\d+$/";
  31. if (preg_match($pattern, $data)) {
  32. $page = preg_replace('/^topup@@sj_bill_/', '', $data);
  33. if (empty($page) || $page < 1) $page = 1;
  34. $page = intval($page);
  35. $res = SanJinRechargeService::bill($chatId, $firstName, $messageId, $page);
  36. $telegram->editMessageText($res);
  37. }
  38. //选择充值
  39. $pattern = "/^topup@@sj_amount_\d+$/";
  40. if (preg_match($pattern, $data)) {
  41. $amount = preg_replace('/^topup@@sj_amount_/', '', $data);
  42. $res = SanJinRechargeService::inputSjAmount($chatId, $amount, $messageId);
  43. self::sendMessage($chatId,$res['text'],$res['keyboard']??[],$res['image']);
  44. // $telegram->editMessageText($res);
  45. }
  46. // 支付通道
  47. $pattern = '/^topup_channel@@(.+)$/';
  48. if (preg_match($pattern, $data, $matches)) {
  49. $k = $matches[1];
  50. // 验证 $k 是否有效
  51. $channel = SanJinService::$CHANNEL;
  52. if (!isset($channel[$k])) {
  53. // 处理无效的通道
  54. $text = "无效的支付通道!";
  55. $res = [
  56. 'chat_id' => $chatId,
  57. 'text' => $text
  58. ];
  59. $telegram->sendMessage($res);
  60. return;
  61. }
  62. Cache::put($chatId.'_sj_payment_type', $k);
  63. Cache::put(get_step_key($chatId), StepStatus::INPUT_RECHARGE_SJ_MONEY);
  64. $paymentType = $k;
  65. $product = SanJinService::$PRODUCT;
  66. $max = 0;
  67. $min = 0;
  68. $rate = 0;
  69. $keyboard = [];
  70. $rechargeText = '';
  71. foreach ($product as $k => $v) {
  72. if ($v['type'] == $paymentType) {
  73. if($v['type'] == 'zfbge'){
  74. $rechargeText = "充值金额:(".implode(',',$v['fixed']).") \n";
  75. foreach($v['fixed'] as $num){
  76. $keyboard[] = [
  77. ['text' => "{$num}", 'callback_data' => "topup@@sj_amount_".$num ]
  78. ];
  79. }
  80. }else{
  81. if ($min == 0) {
  82. $min = $v['min'];
  83. }
  84. if ($max == 0) {
  85. $max = $v['max'];
  86. }
  87. if ($min > $v['min']) {
  88. $min = $v['min'];
  89. }
  90. if ($max < $v['max']) {
  91. $max = $v['max'];
  92. }
  93. }
  94. }
  95. }
  96. $text = "请输入充值的金额 \n";
  97. // $text .= "充值方式:".$channel[$k];
  98. if($rechargeText){
  99. $text .= $rechargeText;
  100. $text .= "🔔 提示:请选择下方充值的金额";
  101. }else{
  102. $text .= "充值金额:{$min} - {$max} \n";
  103. $text .= "🔔 提示:请务必核对(充值最大金额和最小金额,账号信息一致),若不一致无法到账。";
  104. }
  105. $res = [
  106. 'chat_id' => $chatId,
  107. 'text' => $text,
  108. 'message_id' => $messageId
  109. ];
  110. if($keyboard){
  111. $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  112. }
  113. $telegram->editMessageText($res);
  114. }
  115. // //选择银行卡号
  116. // $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
  117. // if (preg_match($pattern, $data)) {
  118. // $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  119. // $res = SanJinRechargeService::chooseBank($chatId, $id);
  120. // $telegram->deleteMessage([
  121. // 'chat_id' => $chatId,
  122. // 'message_id' => $messageId,
  123. // ]);
  124. // $telegram->sendMessage($res);
  125. // }
  126. // //银行卡管理
  127. // if ($data === 'withdraw@@banks') {
  128. // $res = SanJinRechargeService::banks($chatId, $messageId);
  129. // $telegram->editMessageText($res);
  130. // }
  131. // //银行卡详情
  132. // $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  133. // if (preg_match($pattern, $data)) {
  134. // $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  135. // $res = static::bankDetails($chatId, $messageId, $id);
  136. // $telegram->editMessageText($res);
  137. // }
  138. // $pattern = "/^withdraw@@bank_del_\d+$/";
  139. // if (preg_match($pattern, $data)) {
  140. // $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  141. // $res = static::bankDelete($chatId, $messageId, $id);
  142. // $telegram->editMessageText($res);
  143. // }
  144. // //添加银行卡
  145. // if ($data === "withdrawAddress@@bank_add") {
  146. // $res = SanJinRechargeService::addBank($chatId, $messageId);
  147. // $telegram->editMessageText($res);
  148. // }
  149. // $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  150. // if (preg_match($pattern, $data)) {
  151. // $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  152. // $res = static::chooseChannel($chatId, $messageId, $channel);
  153. // $telegram->editMessageText($res);
  154. // }
  155. }
  156. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  157. {
  158. switch ($stepStatus) {
  159. case StepStatus::INPUT_RECHARGE_SJ_MONEY://输入提现金额
  160. $res = SanJinRechargeService::inputSjAmount($chatId, $text, $messageId);
  161. return $res;
  162. break;
  163. }
  164. return null;
  165. }
  166. //三斤账单
  167. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  168. {
  169. $list = PaymentOrder::where('member_id', $chatId)
  170. ->where('type', 1)
  171. ->orderByDesc('created_at')
  172. ->forPage($page, $limit)
  173. ->get();
  174. $count = PaymentOrder::where('member_id', $chatId)
  175. ->where('type', 1)
  176. ->count();
  177. $text = "👤 {$firstName}({$chatId}) 三斤充值记录\n\n";
  178. foreach ($list as $item) {
  179. $amount = floatval($item->amount);
  180. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  181. $text .= "-------------------------------------\n";
  182. $text .= "{$amount} \n";
  183. $text .= "订单号:{$item->order_no}\n";
  184. $text .= "支付平台:{$item->bank_name}\n";
  185. // $text .= "银行:{$item->bank_name}\n";
  186. // $text .= "姓名:{$item->account}\n";
  187. // $text .= "卡号:{$item->card_no}\n";
  188. $status = ['待处理', '处理中', '成功', '失败'];
  189. $text .= "状态:{$status[$item->status]}\n";
  190. if ($item->remark) {
  191. $text .= "说明:{$item->remark}\n";
  192. }
  193. $text .= "日期:{$item->created_at}\n";
  194. }
  195. if ($page > 1) {
  196. $keyboard[] = [
  197. ['text' => "👆上一页", 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  198. ];
  199. }
  200. $allPage = ceil($count / $limit);
  201. if ($allPage > $page) {
  202. if ($page > 1) {
  203. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  204. } else {
  205. $keyboard[] = [
  206. ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  207. ];
  208. }
  209. }
  210. $keyboard[] = [
  211. ['text' => "返回", 'callback_data' => "topUp@@home"]
  212. ];
  213. return [
  214. 'chat_id' => $chatId,
  215. 'text' => $text,
  216. 'message_id' => $messageId,
  217. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  218. ];
  219. }
  220. //1.三斤充值
  221. private static function qbApply($chatId, $messageId)
  222. {
  223. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  224. if ($three_payment_switch != 1) {
  225. $res = WalletService::getBalance($chatId);
  226. $res['message_id'] = $messageId;
  227. return $res;
  228. }
  229. $text = "请选择支付的通道 \n";
  230. $keyboard = [];
  231. $channel = SanJinService::$CHANNEL;
  232. $keyboard[] = [
  233. ['text' => 'USDT', 'callback_data' => "topup@@topup"],
  234. ];
  235. foreach($channel as $k => $v){
  236. $keyboard[] = [
  237. ['text' => $v, 'callback_data' => "topup_channel@@" .$k]
  238. ];
  239. }
  240. $keyboard[] = [
  241. ['text' => "返回", 'callback_data' => "topUp@@home"]
  242. ];
  243. // $wallet = Wallet::where('member_id', $chatId)->first();
  244. // $temp = floatval($wallet->available_balance);
  245. // $text = "请发送提现金额\n";
  246. // $text .= "💰 当前余额{$temp} RMB\n";
  247. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  248. return [
  249. 'chat_id' => $chatId,
  250. 'text' => $text,
  251. 'message_id' => $messageId,
  252. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  253. ];
  254. }
  255. //2.输入三斤提现金额
  256. private static function inputSjAmount($chatId, $amount, $messageId)
  257. {
  258. // 支付通道
  259. $paymentType = Cache::get($chatId.'_sj_payment_type');
  260. if(empty($paymentType)){
  261. // 处理无效的通道
  262. $text = "无效的支付通道!";
  263. $res = [
  264. 'chat_id' => $chatId,
  265. 'text' => $text
  266. ];
  267. return $res;
  268. }
  269. // 验证 $k 是否有效
  270. $channel = SanJinService::$CHANNEL;
  271. if (!isset($channel[$paymentType])) {
  272. // 处理无效的通道
  273. $text = "无效的支付通道!";
  274. $res = [
  275. 'chat_id' => $chatId,
  276. 'text' => $text
  277. ];
  278. return $res;
  279. return;
  280. }
  281. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  282. return [
  283. 'chat_id' => $chatId,
  284. 'text' => "金额输入不正确,请发送预充值数字",
  285. 'reply_to_message_id' => $messageId
  286. ];
  287. }
  288. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  289. if(isset($res['image'])){
  290. Cache::forget(get_step_key($chatId));
  291. }
  292. return $res;
  293. }
  294. //3.选择银行卡号
  295. private static function chooseBank($chatId, $id)
  296. {
  297. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  298. if (!$amount) return WalletService::getBalance($chatId);
  299. $bank = Bank::where('id', $id)->first();
  300. $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  301. return $result;
  302. // $text = "提交成功\n";
  303. // $text .= "结果将在稍后通知您,请留意通知!!!";
  304. // return [
  305. // 'chat_id' => $chatId,
  306. // 'text' => $text,
  307. // ];
  308. }
  309. //银行卡管理
  310. private static function banks($chatId, $messageId)
  311. {
  312. $text = "💳️ 银行卡管理\n";
  313. $text .= "--------------------------\n";
  314. $list = Bank::where('member_id', $chatId)
  315. ->get();
  316. $keyboard = [];
  317. foreach ($list as $item) {
  318. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  319. }
  320. if (count($list) < 5) {
  321. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  322. }
  323. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  324. return [
  325. 'chat_id' => $chatId,
  326. 'text' => $text,
  327. 'message_id' => $messageId,
  328. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  329. ];
  330. }
  331. //银行卡详情
  332. private static function bankDetails($chatId, $messageId, $id)
  333. {
  334. $text = "*银行卡管理*\n\n";
  335. $bank = Bank::where('id', $id)
  336. ->where('member_id', $chatId)->first();
  337. switch ($bank->channel) {
  338. case "DF001":
  339. $text .= "姓名:{$bank->account}\n";
  340. $text .= "银行:{$bank->bank_name}\n";
  341. $text .= "卡号:{$bank->card_no}\n";
  342. break;
  343. case "DF002":
  344. $text .= "姓名:{$bank->account}\n";
  345. $text .= "银行:{$bank->bank_name}\n";
  346. $text .= "账号:{$bank->card_no}\n";
  347. break;
  348. default:
  349. $text .= "姓名:{$bank->account}\n";
  350. $text .= "银行:{$bank->bank_name}\n";
  351. $text .= "卡号:{$bank->card_no}\n";
  352. break;
  353. }
  354. $keyboard = [
  355. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  356. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  357. ];
  358. return [
  359. 'chat_id' => $chatId,
  360. 'parse_mode' => 'MarkdownV2',
  361. 'text' => $text,
  362. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  363. 'message_id' => $messageId
  364. ];
  365. }
  366. //删除银行卡
  367. private static function bankDelete($chatId, $messageId, $id)
  368. {
  369. Bank::where('id', $id)
  370. ->where('member_id', $chatId)->delete();
  371. return static::banks($chatId, $messageId);
  372. }
  373. //添加银行卡
  374. private static function addBank($chatId, $messageId)
  375. {
  376. $text = "请选择 提现通道\n";
  377. $keyboard = [
  378. [
  379. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  380. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  381. ],
  382. [
  383. ['text' => '❌取消', 'callback_data' => "message@@close"]
  384. ]
  385. ];
  386. return [
  387. 'chat_id' => $chatId,
  388. 'text' => $text,
  389. 'message_id' => $messageId,
  390. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  391. ];
  392. }
  393. //选择通道
  394. private static function chooseChannel($chatId, $messageId, $channel)
  395. {
  396. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  397. switch ($channel) {
  398. case "DF002"://支付宝
  399. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  400. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  401. return [
  402. 'chat_id' => $chatId,
  403. 'text' => "请输入支付宝账号",
  404. 'message_id' => $messageId,
  405. ];
  406. break;
  407. case "DF001"://银行卡
  408. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  409. return [
  410. 'chat_id' => $chatId,
  411. 'text' => "请输入银行名称",
  412. 'message_id' => $messageId,
  413. ];
  414. break;
  415. default:
  416. return [
  417. 'chat_id' => $chatId,
  418. 'text' => "选择通道错误",
  419. 'message_id' => $messageId,
  420. ];
  421. break;
  422. }
  423. }
  424. //输入银行名称
  425. private static function inputBankName($chatId, $bankName, $messageId)
  426. {
  427. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  428. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  429. return [
  430. 'chat_id' => $chatId,
  431. 'text' => "请输入银行卡号",
  432. 'message_id' => $messageId,
  433. ];
  434. }
  435. //输入卡号
  436. private static function inputCardNo($chatId, $cardNo, $messageId)
  437. {
  438. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  439. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  440. return [
  441. 'chat_id' => $chatId,
  442. 'text' => "输入的银行卡号有误,请重新输入",
  443. 'reply_to_message_id' => $messageId,
  444. ];
  445. }
  446. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  447. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  448. return [
  449. 'chat_id' => $chatId,
  450. 'text' => "请输入姓名",
  451. 'message_id' => $messageId,
  452. ];
  453. }
  454. //输入姓名,并保存到数据库
  455. private static function inputAccount($chatId, $account, $messageId)
  456. {
  457. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  458. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  459. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  460. Bank::create([
  461. 'member_id' => $chatId,
  462. 'account' => $account,
  463. 'channel' => $channel,
  464. 'card_no' => $cardNo,
  465. 'bank_name' => $bankName
  466. ]);
  467. return static::banks($chatId, $messageId);
  468. }
  469. }