SanJinRechargeService.php 10 KB

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