SanJinRechargeService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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('失败'), 7 => lang('已超时')];
  155. $text .= lang('状态').":".($status[$item->status] ?? '')."\n";
  156. if ($item->remark) {
  157. $text .= lang('说明').":{$item->remark}\n";
  158. }
  159. $text .= lang('日期').":{$item->created_at}\n";
  160. }
  161. if ($page > 1) {
  162. $keyboard[] = [
  163. ['text' => lang("👆上一页"), 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  164. ];
  165. }
  166. $allPage = ceil($count / $limit);
  167. if ($allPage > $page) {
  168. if ($page > 1) {
  169. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  170. } else {
  171. $keyboard[] = [
  172. ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  173. ];
  174. }
  175. }
  176. $keyboard[] = [
  177. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  178. ];
  179. return [
  180. 'chat_id' => $chatId,
  181. 'text' => $text,
  182. 'message_id' => $messageId,
  183. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  184. ];
  185. }
  186. //1.三斤充值
  187. public static function qbApply($chatId, $messageId = null)
  188. {
  189. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  190. $text = lang("请选择支付的通道")." \n";
  191. $keyboard = [];
  192. $groupId = (int)(User::query()->where('member_id', $chatId)->value('recharge_channel_group_id') ?: 1);
  193. $channel = SanJinService::getChannel('', $groupId);
  194. $keyboard[] = [
  195. ['text' => 'USDT', 'callback_data' => "topup@@topup"],
  196. ];
  197. if ($three_payment_switch == 1) {
  198. foreach($channel as $k => $v){
  199. $keyboard[] = [
  200. ['text' => lang($v), 'callback_data' => "topup_channel@@" .$k]
  201. ];
  202. }
  203. }
  204. $keyboard[] = [
  205. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  206. ];
  207. // $wallet = Wallet::where('member_id', $chatId)->first();
  208. // $temp = floatval($wallet->available_balance);
  209. // $text = "请发送提现金额\n";
  210. // $text .= "💰 当前余额{$temp} RMB\n";
  211. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  212. return [
  213. 'chat_id' => $chatId,
  214. 'text' => $text,
  215. 'message_id' => $messageId,
  216. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  217. ];
  218. }
  219. //2.输入三斤提现金额
  220. private static function inputSjAmount($chatId, $amount, $messageId)
  221. {
  222. // 支付通道
  223. $paymentType = Cache::get($chatId.'_sj_payment_type');
  224. if(empty($paymentType)){
  225. // 处理无效的通道
  226. $text = lang("无效的支付通道!");
  227. $res = [
  228. 'chat_id' => $chatId,
  229. 'text' => $text
  230. ];
  231. return $res;
  232. }
  233. // 验证 $k 是否有效
  234. $groupId = (int)(User::query()->where('member_id', $chatId)->value('recharge_channel_group_id') ?: 1);
  235. $channel = SanJinService::getChannel('', $groupId);
  236. if (!isset($channel[$paymentType])) {
  237. // 处理无效的通道
  238. $text = lang("无效的支付通道!");
  239. $res = [
  240. 'chat_id' => $chatId,
  241. 'text' => $text
  242. ];
  243. return $res;
  244. return;
  245. }
  246. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  247. return [
  248. 'chat_id' => $chatId,
  249. 'text' => lang("金额输入不正确,请发送预充值数字"),
  250. 'reply_to_message_id' => $messageId
  251. ];
  252. }
  253. $res = PaymentOrderService::createPay($chatId,$amount,$paymentType);
  254. if(isset($res['image'])){
  255. Cache::forget(get_step_key($chatId));
  256. }
  257. return $res;
  258. }
  259. public static function getWhere(array $search = []): array
  260. {
  261. return [];
  262. }
  263. }