SanJinRechargeService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 App\Models\User;
  9. use Illuminate\Support\Facades\Cache;
  10. use Telegram\Bot\Api;
  11. use App\Services\Payment\SanJinService;
  12. use App\Services\BaseService;
  13. class SanJinRechargeService extends BaseService
  14. {
  15. /**
  16. * @param Api $telegram
  17. * @param $data
  18. * @param $chatId
  19. * @param $firstName
  20. * @param $messageId
  21. * @throws \Telegram\Bot\Exceptions\TelegramSDKException
  22. */
  23. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId)
  24. {
  25. //点击三斤充值按钮
  26. if ($data === "topup@@sj_apply") {
  27. $res = SanJinRechargeService::qbApply($chatId, $messageId);
  28. $telegram->editMessageText($res);
  29. }
  30. //三斤账单
  31. $pattern = "/^topup@@sj_bill_\d+$/";
  32. if (preg_match($pattern, $data)) {
  33. $page = preg_replace('/^topup@@sj_bill_/', '', $data);
  34. if (empty($page) || $page < 1) $page = 1;
  35. $page = intval($page);
  36. $res = SanJinRechargeService::bill($chatId, $firstName, $messageId, $page);
  37. $telegram->editMessageText($res);
  38. }
  39. //选择充值
  40. $pattern = "/^topup@@sj_amount_\d+$/";
  41. if (preg_match($pattern, $data)) {
  42. $amount = preg_replace('/^topup@@sj_amount_/', '', $data);
  43. $res = SanJinRechargeService::inputSjAmount($chatId, $amount, $messageId);
  44. self::sendMessage($chatId,$res['text'],$res['keyboard']??[],$res['image']);
  45. // $telegram->editMessageText($res);
  46. }
  47. // 支付通道
  48. $pattern = '/^topup_channel@@(.+)$/';
  49. if (preg_match($pattern, $data, $matches)) {
  50. $k = $matches[1];
  51. // 验证 $k 是否有效
  52. $groupId = (int)(User::query()->where('member_id', $chatId)->value('recharge_channel_group_id') ?: 1);
  53. $channel = SanJinService::getChannel('', $groupId);
  54. if (!isset($channel[$k])) {
  55. // 处理无效的通道
  56. $text = lang("无效的支付通道!");
  57. $res = [
  58. 'chat_id' => $chatId,
  59. 'text' => $text
  60. ];
  61. $telegram->sendMessage($res);
  62. return;
  63. }
  64. Cache::put($chatId.'_sj_payment_type', $k);
  65. Cache::put(get_step_key($chatId), StepStatus::INPUT_RECHARGE_SJ_MONEY);
  66. $paymentType = $k;
  67. $product = SanJinService::product($groupId);
  68. $max = 0;
  69. $min = 0;
  70. $rate = 0;
  71. $keyboard = [];
  72. $rechargeText = '';
  73. foreach ($product as $k => $v) {
  74. if ($v['type'] == $paymentType) {
  75. if($v['type'] == 'zfbge'){
  76. $rechargeText = lang('充值金额').":(".implode(',',$v['fixed']).") \n";
  77. foreach($v['fixed'] as $num){
  78. $keyboard[] = [
  79. ['text' => "{$num}", 'callback_data' => "topup@@sj_amount_".$num ]
  80. ];
  81. }
  82. }else{
  83. if ($min == 0) {
  84. $min = $v['min'];
  85. }
  86. if ($max == 0) {
  87. $max = $v['max'];
  88. }
  89. if ($min > $v['min']) {
  90. $min = $v['min'];
  91. }
  92. if ($max < $v['max']) {
  93. $max = $v['max'];
  94. }
  95. }
  96. }
  97. }
  98. $text = lang("请输入充值的金额")." \n";
  99. // $text .= "充值方式:".$channel[$k];
  100. if($rechargeText){
  101. $text .= $rechargeText;
  102. $text .= lang("🔔 提示:请选择下方充值的金额");
  103. }else{
  104. $text .= lang("充值金额").":{$min} - {$max} \n";
  105. $text .= lang("🔔 提示:请务必核对(充值最大金额和最小金额,账号信息一致),若不一致无法到账。");
  106. }
  107. $res = [
  108. 'chat_id' => $chatId,
  109. 'text' => $text,
  110. 'message_id' => $messageId
  111. ];
  112. if($keyboard){
  113. $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  114. }
  115. $telegram->editMessageText($res);
  116. }
  117. }
  118. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  119. {
  120. switch ($stepStatus) {
  121. case StepStatus::INPUT_RECHARGE_SJ_MONEY://输入提现金额
  122. $res = SanJinRechargeService::inputSjAmount($chatId, $text, $messageId);
  123. return $res;
  124. break;
  125. }
  126. return null;
  127. }
  128. //三斤账单
  129. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  130. {
  131. $list = PaymentOrder::where('member_id', $chatId)
  132. // ->where('type', 1)
  133. ->orderByDesc('created_at')
  134. ->forPage($page, $limit)
  135. ->get();
  136. $count = PaymentOrder::where('member_id', $chatId)
  137. // ->where('type', 1)
  138. ->count();
  139. $text = "👤 {$firstName}({$chatId}) ".lang('第三方订单记录')."\n\n";
  140. foreach ($list as $item) {
  141. $amount = floatval($item->amount);
  142. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  143. $text .= "-------------------------------------\n";
  144. $text .= lang('订单类型').":".($item->type == 1?lang("充值"):lang("提现"))." \n";
  145. $text .= lang('订单金额').":{$amount} \n";
  146. $text .= lang('订单号').":{$item->order_no}\n";
  147. if($item->type == 2){
  148. $text .= lang("银行").":{$item->bank_name}\n";
  149. $text .= lang("姓名").":{$item->account}\n";
  150. $text .= lang('卡号').":{$item->card_no}\n";
  151. }else{
  152. $text .= lang("充值类型").":".lang($item->bank_name)."\n";
  153. }
  154. $status = [lang('待处理'), lang('处理中'), lang('成功'), lang('失败')];
  155. if($item->type == 1 && $item->created_at < date('Y-m-d H:i:s',time() - 600) && $item->status == 1){
  156. $text .= lang('状态').":".lang("已超时")."\n";
  157. }else{
  158. $text .= lang('状态').":{$status[$item->status]}\n";
  159. }
  160. if ($item->remark) {
  161. $text .= lang('说明').":{$item->remark}\n";
  162. }
  163. $text .= lang('日期').":{$item->created_at}\n";
  164. }
  165. if ($page > 1) {
  166. $keyboard[] = [
  167. ['text' => lang("👆上一页"), 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  168. ];
  169. }
  170. $allPage = ceil($count / $limit);
  171. if ($allPage > $page) {
  172. if ($page > 1) {
  173. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  174. } else {
  175. $keyboard[] = [
  176. ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  177. ];
  178. }
  179. }
  180. $keyboard[] = [
  181. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  182. ];
  183. return [
  184. 'chat_id' => $chatId,
  185. 'text' => $text,
  186. 'message_id' => $messageId,
  187. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  188. ];
  189. }
  190. //1.三斤充值
  191. public static function qbApply($chatId, $messageId = null)
  192. {
  193. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  194. $text = lang("请选择支付的通道")." \n";
  195. $keyboard = [];
  196. $groupId = (int)(User::query()->where('member_id', $chatId)->value('recharge_channel_group_id') ?: 1);
  197. $channel = SanJinService::getChannel('', $groupId);
  198. $keyboard[] = [
  199. ['text' => 'USDT', 'callback_data' => "topup@@topup"],
  200. ];
  201. if ($three_payment_switch == 1) {
  202. foreach($channel as $k => $v){
  203. $keyboard[] = [
  204. ['text' => lang($v), 'callback_data' => "topup_channel@@" .$k]
  205. ];
  206. }
  207. }
  208. $keyboard[] = [
  209. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  210. ];
  211. // $wallet = Wallet::where('member_id', $chatId)->first();
  212. // $temp = floatval($wallet->available_balance);
  213. // $text = "请发送提现金额\n";
  214. // $text .= "💰 当前余额{$temp} RMB\n";
  215. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  216. return [
  217. 'chat_id' => $chatId,
  218. 'text' => $text,
  219. 'message_id' => $messageId,
  220. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  221. ];
  222. }
  223. //2.输入三斤提现金额
  224. private static function inputSjAmount($chatId, $amount, $messageId)
  225. {
  226. // 支付通道
  227. $paymentType = Cache::get($chatId.'_sj_payment_type');
  228. if(empty($paymentType)){
  229. // 处理无效的通道
  230. $text = lang("无效的支付通道!");
  231. $res = [
  232. 'chat_id' => $chatId,
  233. 'text' => $text
  234. ];
  235. return $res;
  236. }
  237. // 验证 $k 是否有效
  238. $groupId = (int)(User::query()->where('member_id', $chatId)->value('recharge_channel_group_id') ?: 1);
  239. $channel = SanJinService::getChannel('', $groupId);
  240. if (!isset($channel[$paymentType])) {
  241. // 处理无效的通道
  242. $text = lang("无效的支付通道!");
  243. $res = [
  244. 'chat_id' => $chatId,
  245. 'text' => $text
  246. ];
  247. return $res;
  248. return;
  249. }
  250. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  251. return [
  252. 'chat_id' => $chatId,
  253. 'text' => lang("金额输入不正确,请发送预充值数字"),
  254. 'reply_to_message_id' => $messageId
  255. ];
  256. }
  257. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  258. if(isset($res['image'])){
  259. Cache::forget(get_step_key($chatId));
  260. }
  261. return $res;
  262. }
  263. public static function getWhere(array $search = []): array
  264. {
  265. return [];
  266. }
  267. }