SanJinRechargeService.php 17 KB

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