SanJinRechargeService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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::getChannel();
  52. if (!isset($channel[$k])) {
  53. // 处理无效的通道
  54. $text = lang("无效的支付通道!");
  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 = lang('充值金额').":(".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 = lang("请输入充值的金额")." \n";
  97. // $text .= "充值方式:".$channel[$k];
  98. if($rechargeText){
  99. $text .= $rechargeText;
  100. $text .= lang("🔔 提示:请选择下方充值的金额");
  101. }else{
  102. $text .= lang("充值金额").":{$min} - {$max} \n";
  103. $text .= lang("🔔 提示:请务必核对(充值最大金额和最小金额,账号信息一致),若不一致无法到账。");
  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. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  117. {
  118. switch ($stepStatus) {
  119. case StepStatus::INPUT_RECHARGE_SJ_MONEY://输入提现金额
  120. $res = SanJinRechargeService::inputSjAmount($chatId, $text, $messageId);
  121. return $res;
  122. break;
  123. }
  124. return null;
  125. }
  126. //三斤账单
  127. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  128. {
  129. $list = PaymentOrder::where('member_id', $chatId)
  130. // ->where('type', 1)
  131. ->orderByDesc('created_at')
  132. ->forPage($page, $limit)
  133. ->get();
  134. $count = PaymentOrder::where('member_id', $chatId)
  135. // ->where('type', 1)
  136. ->count();
  137. $text = "👤 {$firstName}({$chatId}) ".lang('第三方订单记录')."\n\n";
  138. foreach ($list as $item) {
  139. $amount = floatval($item->amount);
  140. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  141. $text .= "-------------------------------------\n";
  142. $text .= lang('订单类型').":".($item->type == 1?lang("充值"):lang("提现"))." \n";
  143. $text .= lang('订单金额').":{$amount} \n";
  144. $text .= lang('订单号').":{$item->order_no}\n";
  145. if($item->type == 2){
  146. $text .= lang("银行").":{$item->bank_name}\n";
  147. $text .= lang("姓名").":{$item->account}\n";
  148. $text .= lang('卡号').":{$item->card_no}\n";
  149. }else{
  150. $text .= lang("充值类型").":".lang($item->bank_name)."\n";
  151. }
  152. if($item->type == 1){
  153. if(time() - strtotime($item->created_at) > 600 && $item->status == 1){
  154. // 超过10分钟未处理,视为超时
  155. $status = [lang('已超时'), lang('处理中'), lang('成功'), lang('失败')];
  156. }else{
  157. $status = [lang('待处理'), lang('处理中'), lang('成功'), lang('失败')];
  158. }
  159. }else{
  160. $status = [lang('待处理'), lang('处理中'), lang('成功'), lang('失败')];
  161. }
  162. $text .= lang('状态').":{$status[$item->status]}\n";
  163. if ($item->remark) {
  164. $text .= lang('说明').":{$item->remark}\n";
  165. }
  166. $text .= lang('日期').":{$item->created_at}\n";
  167. }
  168. if ($page > 1) {
  169. $keyboard[] = [
  170. ['text' => lang("👆上一页"), 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  171. ];
  172. }
  173. $allPage = ceil($count / $limit);
  174. if ($allPage > $page) {
  175. if ($page > 1) {
  176. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  177. } else {
  178. $keyboard[] = [
  179. ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  180. ];
  181. }
  182. }
  183. $keyboard[] = [
  184. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  185. ];
  186. return [
  187. 'chat_id' => $chatId,
  188. 'text' => $text,
  189. 'message_id' => $messageId,
  190. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  191. ];
  192. }
  193. //1.三斤充值
  194. public static function qbApply($chatId, $messageId = null)
  195. {
  196. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  197. $text = lang("请选择支付的通道")." \n";
  198. $keyboard = [];
  199. $channel = SanJinService::getChannel();
  200. $keyboard[] = [
  201. ['text' => 'USDT', 'callback_data' => "topup@@topup"],
  202. ];
  203. if ($three_payment_switch == 1) {
  204. foreach($channel as $k => $v){
  205. $keyboard[] = [
  206. ['text' => lang($v), 'callback_data' => "topup_channel@@" .$k]
  207. ];
  208. }
  209. }
  210. $keyboard[] = [
  211. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  212. ];
  213. // $wallet = Wallet::where('member_id', $chatId)->first();
  214. // $temp = floatval($wallet->available_balance);
  215. // $text = "请发送提现金额\n";
  216. // $text .= "💰 当前余额{$temp} RMB\n";
  217. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  218. return [
  219. 'chat_id' => $chatId,
  220. 'text' => $text,
  221. 'message_id' => $messageId,
  222. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  223. ];
  224. }
  225. //2.输入三斤提现金额
  226. private static function inputSjAmount($chatId, $amount, $messageId)
  227. {
  228. // 支付通道
  229. $paymentType = Cache::get($chatId.'_sj_payment_type');
  230. if(empty($paymentType)){
  231. // 处理无效的通道
  232. $text = lang("无效的支付通道!");
  233. $res = [
  234. 'chat_id' => $chatId,
  235. 'text' => $text
  236. ];
  237. return $res;
  238. }
  239. // 验证 $k 是否有效
  240. $channel = SanJinService::getChannel();
  241. if (!isset($channel[$paymentType])) {
  242. // 处理无效的通道
  243. $text = lang("无效的支付通道!");
  244. $res = [
  245. 'chat_id' => $chatId,
  246. 'text' => $text
  247. ];
  248. return $res;
  249. return;
  250. }
  251. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  252. return [
  253. 'chat_id' => $chatId,
  254. 'text' => lang("金额输入不正确,请发送预充值数字"),
  255. 'reply_to_message_id' => $messageId
  256. ];
  257. }
  258. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  259. if(isset($res['image'])){
  260. Cache::forget(get_step_key($chatId));
  261. }
  262. return $res;
  263. }
  264. }