SanJinRechargeService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 .= "订单类型:".($item->type == 1?"充值":"提现")." \n";
  183. $text .= "订单金额:{$amount} \n";
  184. $text .= "订单号:{$item->order_no}\n";
  185. if($item->type == 2){
  186. $text .= "银行:{$item->bank_name}\n";
  187. $text .= "姓名:{$item->account}\n";
  188. $text .= "卡号:{$item->card_no}\n";
  189. }else{
  190. $text .= "充值类型:{$item->bank_name}\n";
  191. }
  192. $status = ['待处理', '处理中', '成功', '失败'];
  193. $text .= "状态:{$status[$item->status]}\n";
  194. if ($item->remark) {
  195. $text .= "说明:{$item->remark}\n";
  196. }
  197. $text .= "日期:{$item->created_at}\n";
  198. }
  199. if ($page > 1) {
  200. $keyboard[] = [
  201. ['text' => "👆上一页", 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  202. ];
  203. }
  204. $allPage = ceil($count / $limit);
  205. if ($allPage > $page) {
  206. if ($page > 1) {
  207. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  208. } else {
  209. $keyboard[] = [
  210. ['text' => "👇下一页", 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  211. ];
  212. }
  213. }
  214. $keyboard[] = [
  215. ['text' => "返回", 'callback_data' => "topUp@@home"]
  216. ];
  217. return [
  218. 'chat_id' => $chatId,
  219. 'text' => $text,
  220. 'message_id' => $messageId,
  221. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  222. ];
  223. }
  224. //1.三斤充值
  225. private static function qbApply($chatId, $messageId)
  226. {
  227. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  228. $text = "请选择支付的通道 \n";
  229. $keyboard = [];
  230. $channel = SanJinService::$CHANNEL;
  231. $keyboard[] = [
  232. ['text' => 'USDT', 'callback_data' => "topup@@topup"],
  233. ];
  234. if ($three_payment_switch == 1) {
  235. foreach($channel as $k => $v){
  236. $keyboard[] = [
  237. ['text' => $v, 'callback_data' => "topup_channel@@" .$k]
  238. ];
  239. }
  240. }
  241. $keyboard[] = [
  242. ['text' => "返回", 'callback_data' => "topUp@@home"]
  243. ];
  244. // $wallet = Wallet::where('member_id', $chatId)->first();
  245. // $temp = floatval($wallet->available_balance);
  246. // $text = "请发送提现金额\n";
  247. // $text .= "💰 当前余额{$temp} RMB\n";
  248. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  249. return [
  250. 'chat_id' => $chatId,
  251. 'text' => $text,
  252. 'message_id' => $messageId,
  253. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  254. ];
  255. }
  256. //2.输入三斤提现金额
  257. private static function inputSjAmount($chatId, $amount, $messageId)
  258. {
  259. // 支付通道
  260. $paymentType = Cache::get($chatId.'_sj_payment_type');
  261. if(empty($paymentType)){
  262. // 处理无效的通道
  263. $text = "无效的支付通道!";
  264. $res = [
  265. 'chat_id' => $chatId,
  266. 'text' => $text
  267. ];
  268. return $res;
  269. }
  270. // 验证 $k 是否有效
  271. $channel = SanJinService::$CHANNEL;
  272. if (!isset($channel[$paymentType])) {
  273. // 处理无效的通道
  274. $text = "无效的支付通道!";
  275. $res = [
  276. 'chat_id' => $chatId,
  277. 'text' => $text
  278. ];
  279. return $res;
  280. return;
  281. }
  282. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  283. return [
  284. 'chat_id' => $chatId,
  285. 'text' => "金额输入不正确,请发送预充值数字",
  286. 'reply_to_message_id' => $messageId
  287. ];
  288. }
  289. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  290. if(isset($res['image'])){
  291. Cache::forget(get_step_key($chatId));
  292. }
  293. return $res;
  294. }
  295. //3.选择银行卡号
  296. private static function chooseBank($chatId, $id)
  297. {
  298. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  299. if (!$amount) return WalletService::getBalance($chatId);
  300. $bank = Bank::where('id', $id)->first();
  301. $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  302. return $result;
  303. // $text = "提交成功\n";
  304. // $text .= "结果将在稍后通知您,请留意通知!!!";
  305. // return [
  306. // 'chat_id' => $chatId,
  307. // 'text' => $text,
  308. // ];
  309. }
  310. //银行卡管理
  311. private static function banks($chatId, $messageId)
  312. {
  313. $text = "💳️ 银行卡管理\n";
  314. $text .= "--------------------------\n";
  315. $list = Bank::where('member_id', $chatId)
  316. ->get();
  317. $keyboard = [];
  318. foreach ($list as $item) {
  319. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  320. }
  321. if (count($list) < 5) {
  322. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  323. }
  324. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  325. return [
  326. 'chat_id' => $chatId,
  327. 'text' => $text,
  328. 'message_id' => $messageId,
  329. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  330. ];
  331. }
  332. //银行卡详情
  333. private static function bankDetails($chatId, $messageId, $id)
  334. {
  335. $text = "*银行卡管理*\n\n";
  336. $bank = Bank::where('id', $id)
  337. ->where('member_id', $chatId)->first();
  338. switch ($bank->channel) {
  339. case "DF001":
  340. $text .= "姓名:{$bank->account}\n";
  341. $text .= "银行:{$bank->bank_name}\n";
  342. $text .= "卡号:{$bank->card_no}\n";
  343. break;
  344. case "DF002":
  345. $text .= "姓名:{$bank->account}\n";
  346. $text .= "银行:{$bank->bank_name}\n";
  347. $text .= "账号:{$bank->card_no}\n";
  348. break;
  349. default:
  350. $text .= "姓名:{$bank->account}\n";
  351. $text .= "银行:{$bank->bank_name}\n";
  352. $text .= "卡号:{$bank->card_no}\n";
  353. break;
  354. }
  355. $keyboard = [
  356. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  357. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  358. ];
  359. return [
  360. 'chat_id' => $chatId,
  361. 'parse_mode' => 'MarkdownV2',
  362. 'text' => $text,
  363. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  364. 'message_id' => $messageId
  365. ];
  366. }
  367. //删除银行卡
  368. private static function bankDelete($chatId, $messageId, $id)
  369. {
  370. Bank::where('id', $id)
  371. ->where('member_id', $chatId)->delete();
  372. return static::banks($chatId, $messageId);
  373. }
  374. //添加银行卡
  375. private static function addBank($chatId, $messageId)
  376. {
  377. $text = "请选择 提现通道\n";
  378. $keyboard = [
  379. [
  380. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  381. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  382. ],
  383. [
  384. ['text' => '❌取消', 'callback_data' => "message@@close"]
  385. ]
  386. ];
  387. return [
  388. 'chat_id' => $chatId,
  389. 'text' => $text,
  390. 'message_id' => $messageId,
  391. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  392. ];
  393. }
  394. //选择通道
  395. private static function chooseChannel($chatId, $messageId, $channel)
  396. {
  397. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  398. switch ($channel) {
  399. case "DF002"://支付宝
  400. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  401. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  402. return [
  403. 'chat_id' => $chatId,
  404. 'text' => "请输入支付宝账号",
  405. 'message_id' => $messageId,
  406. ];
  407. break;
  408. case "DF001"://银行卡
  409. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  410. return [
  411. 'chat_id' => $chatId,
  412. 'text' => "请输入银行名称",
  413. 'message_id' => $messageId,
  414. ];
  415. break;
  416. default:
  417. return [
  418. 'chat_id' => $chatId,
  419. 'text' => "选择通道错误",
  420. 'message_id' => $messageId,
  421. ];
  422. break;
  423. }
  424. }
  425. //输入银行名称
  426. private static function inputBankName($chatId, $bankName, $messageId)
  427. {
  428. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  429. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  430. return [
  431. 'chat_id' => $chatId,
  432. 'text' => "请输入银行卡号",
  433. 'message_id' => $messageId,
  434. ];
  435. }
  436. //输入卡号
  437. private static function inputCardNo($chatId, $cardNo, $messageId)
  438. {
  439. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  440. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  441. return [
  442. 'chat_id' => $chatId,
  443. 'text' => "输入的银行卡号有误,请重新输入",
  444. 'reply_to_message_id' => $messageId,
  445. ];
  446. }
  447. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  448. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  449. return [
  450. 'chat_id' => $chatId,
  451. 'text' => "请输入姓名",
  452. 'message_id' => $messageId,
  453. ];
  454. }
  455. //输入姓名,并保存到数据库
  456. private static function inputAccount($chatId, $account, $messageId)
  457. {
  458. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  459. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  460. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  461. Bank::create([
  462. 'member_id' => $chatId,
  463. 'account' => $account,
  464. 'channel' => $channel,
  465. 'card_no' => $cardNo,
  466. 'bank_name' => $bankName
  467. ]);
  468. return static::banks($chatId, $messageId);
  469. }
  470. }