SanJinRechargeService.php 10 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 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. $status = [lang('待处理'), lang('处理中'), lang('成功'), lang('失败')];
  153. if($item->type == 1 && $item->created_at < date('Y-m-d H:i:s',time() - 600) && $item->status == 1){
  154. $text .= lang('状态').":".lang("已超时")."\n";
  155. }else{
  156. $text .= lang('状态').":{$status[$item->status]}\n";
  157. }
  158. if ($item->remark) {
  159. $text .= lang('说明').":{$item->remark}\n";
  160. }
  161. $text .= lang('日期').":{$item->created_at}\n";
  162. }
  163. if ($page > 1) {
  164. $keyboard[] = [
  165. ['text' => lang("👆上一页"), 'callback_data' => "topup@@sj_bill_" . ($page - 1)]
  166. ];
  167. }
  168. $allPage = ceil($count / $limit);
  169. if ($allPage > $page) {
  170. if ($page > 1) {
  171. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)];
  172. } else {
  173. $keyboard[] = [
  174. ['text' => lang("👇下一页"), 'callback_data' => "topup@@sj_bill_" . ($page + 1)]
  175. ];
  176. }
  177. }
  178. $keyboard[] = [
  179. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  180. ];
  181. return [
  182. 'chat_id' => $chatId,
  183. 'text' => $text,
  184. 'message_id' => $messageId,
  185. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  186. ];
  187. }
  188. //1.三斤充值
  189. public static function qbApply($chatId, $messageId = null)
  190. {
  191. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  192. $text = lang("请选择支付的通道")." \n";
  193. $keyboard = [];
  194. $channel = SanJinService::getChannel();
  195. $keyboard[] = [
  196. ['text' => 'USDT', 'callback_data' => "topup@@topup"],
  197. ];
  198. if ($three_payment_switch == 1) {
  199. foreach($channel as $k => $v){
  200. $keyboard[] = [
  201. ['text' => lang($v), 'callback_data' => "topup_channel@@" .$k]
  202. ];
  203. }
  204. }
  205. $keyboard[] = [
  206. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  207. ];
  208. // $wallet = Wallet::where('member_id', $chatId)->first();
  209. // $temp = floatval($wallet->available_balance);
  210. // $text = "请发送提现金额\n";
  211. // $text .= "💰 当前余额{$temp} RMB\n";
  212. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  213. return [
  214. 'chat_id' => $chatId,
  215. 'text' => $text,
  216. 'message_id' => $messageId,
  217. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  218. ];
  219. }
  220. //2.输入三斤提现金额
  221. private static function inputSjAmount($chatId, $amount, $messageId)
  222. {
  223. // 支付通道
  224. $paymentType = Cache::get($chatId.'_sj_payment_type');
  225. if(empty($paymentType)){
  226. // 处理无效的通道
  227. $text = lang("无效的支付通道!");
  228. $res = [
  229. 'chat_id' => $chatId,
  230. 'text' => $text
  231. ];
  232. return $res;
  233. }
  234. // 验证 $k 是否有效
  235. $channel = SanJinService::getChannel();
  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. }