SanJinRechargeService.php 18 KB

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