SanJinRechargeService.php 17 KB

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