QianBaoWithdrawService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\Wallet;
  7. use Illuminate\Support\Facades\Cache;
  8. use Telegram\Bot\Api;
  9. class QianBaoWithdrawService
  10. {
  11. /**
  12. * @param Api $telegram
  13. * @param $data
  14. * @param $chatId
  15. * @param $firstName
  16. * @param $messageId
  17. * @throws \Telegram\Bot\Exceptions\TelegramSDKException
  18. */
  19. static function init(Api $telegram, $data, $chatId, $firstName, $messageId)
  20. {
  21. //点击钱宝提现按钮
  22. if ($data === "withdraw@@qb_apply") {
  23. $res = QianBaoWithdrawService::qbApply($chatId, $messageId);
  24. $telegram->editMessageText($res);
  25. }
  26. //选择银行卡号
  27. $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
  28. if (preg_match($pattern, $data)) {
  29. $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  30. $res = QianBaoWithdrawService::chooseBank($chatId, $id);
  31. $telegram->deleteMessage([
  32. 'chat_id' => $chatId,
  33. 'message_id' => $messageId,
  34. ]);
  35. $telegram->sendMessage($res);
  36. }
  37. //银行卡管理
  38. if ($data === 'withdraw@@banks') {
  39. $res = QianBaoWithdrawService::banks($chatId, $messageId);
  40. $telegram->editMessageText($res);
  41. }
  42. //添加银行卡
  43. if ($data === "withdrawAddress@@bank_add") {
  44. $res = QianBaoWithdrawService::addBank($chatId, $messageId);
  45. $telegram->editMessageText($res);
  46. }
  47. $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  48. if (preg_match($pattern, $data)) {
  49. $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  50. $res = static::chooseChannel($chatId, $messageId, $channel);
  51. $telegram->editMessageText($res);
  52. }
  53. }
  54. //1.钱宝提现
  55. static function qbApply($chatId, $messageId)
  56. {
  57. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  58. if ($three_payment_switch != 1) {
  59. $res = WalletService::getBalance($chatId);
  60. $res['message_id'] = $messageId;
  61. return $res;
  62. }
  63. $wallet = Wallet::where('member_id', $chatId)->first();
  64. $temp = floatval($wallet->available_balance);
  65. $text = "请发送提现金额\n";
  66. $text .= "💰 当前余额{$temp} RMB\n";
  67. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  68. return [
  69. 'chat_id' => $chatId,
  70. 'text' => $text,
  71. 'message_id' => $messageId,
  72. ];
  73. }
  74. //2.输入钱宝提现金额
  75. static function inputQbAmount($chatId, $amount, $messageId)
  76. {
  77. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  78. return [
  79. 'chat_id' => $chatId,
  80. 'text' => "金额输入不正确,请发送提现数字",
  81. 'reply_to_message_id' => $messageId
  82. ];
  83. }
  84. $amount = floatval($amount);
  85. $wallet = Wallet::where('member_id', $chatId)->first();
  86. $temp = floatval($wallet->available_balance);
  87. if ($amount > $temp) {
  88. return [
  89. 'chat_id' => $chatId,
  90. 'text' => "⚠️可用余额不足,请重试",
  91. 'reply_to_message_id' => $messageId
  92. ];
  93. }
  94. if ($amount < 100) {
  95. return [
  96. 'chat_id' => $chatId,
  97. 'text' => "⚠️提现不能少于100 RMB,请重试",
  98. 'reply_to_message_id' => $messageId
  99. ];
  100. }
  101. if ($amount > 49999) {
  102. return [
  103. 'chat_id' => $chatId,
  104. 'text' => "⚠️最多提现 49999 RMB,请重试",
  105. 'reply_to_message_id' => $messageId
  106. ];
  107. }
  108. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  109. $list = Bank::where('member_id', $chatId)->get();
  110. $keyboard = [];
  111. foreach ($list as $item) {
  112. $keyboard[] = [['text' => "$item->card_no($item->bank_name)", 'callback_data' => "withdrawAddress@@choose_qb_{$item->id}"]];
  113. }
  114. $keyboard[] = [
  115. ['text' => '🏠 银行卡管理', 'callback_data' => "withdraw@@banks"],
  116. ['text' => '❌取消', 'callback_data' => "message@@close"]
  117. ];
  118. $text = "请直接选择下面的地址\n";
  119. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  120. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  121. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_QB_ADDRESS);
  122. return [
  123. 'chat_id' => $chatId,
  124. 'text' => $text,
  125. 'reply_to_message_id' => $messageId,
  126. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  127. ];
  128. }
  129. //3.选择银行卡号
  130. static function chooseBank($chatId, $id)
  131. {
  132. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  133. if (!$amount) return WalletService::getBalance($chatId);
  134. $bank = Bank::where('id', $id)->first();
  135. // PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  136. $text = "提交成功\n";
  137. $text .= "结果将在稍后通知您,请留意通知!!!";
  138. return [
  139. 'chat_id' => $chatId,
  140. 'text' => $text,
  141. ];
  142. }
  143. //银行卡管理
  144. static function banks($chatId, $messageId)
  145. {
  146. $text = "🏠 银行卡管理\n";
  147. $text .= "--------------------------\n";
  148. $list = Bank::where('member_id', $chatId)
  149. ->get();
  150. $keyboard = [];
  151. foreach ($list as $item) {
  152. $keyboard[] = [['text' => "$item->card_no($item->bank_name)", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  153. }
  154. if (count($list) < 5) {
  155. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  156. }
  157. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "withdraw@@home"]];
  158. return [
  159. 'chat_id' => $chatId,
  160. 'text' => $text,
  161. 'message_id' => $messageId,
  162. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  163. ];
  164. }
  165. static function addBank($chatId, $messageId)
  166. {
  167. $text = "请选择 提现通道\n";
  168. $keyboard = [
  169. [
  170. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  171. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  172. ],
  173. [
  174. ['text' => '❌取消', 'callback_data' => "message@@close"]
  175. ]
  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. static function chooseChannel($chatId, $messageId, $channel)
  185. {
  186. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  187. switch ($channel) {
  188. case "DF002"://支付宝
  189. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  190. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  191. return [
  192. 'chat_id' => $chatId,
  193. 'text' => "请输入支付宝账号",
  194. 'message_id' => $messageId,
  195. ];
  196. break;
  197. case "DF001"://银行卡
  198. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  199. return [
  200. 'chat_id' => $chatId,
  201. 'text' => "请输入银行名称",
  202. 'message_id' => $messageId,
  203. ];
  204. break;
  205. default:
  206. return [
  207. 'chat_id' => $chatId,
  208. 'text' => "选择通道错误",
  209. 'message_id' => $messageId,
  210. ];
  211. break;
  212. }
  213. }
  214. static function inputBankName($chatId, $bankName, $messageId)
  215. {
  216. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  217. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  218. return [
  219. 'chat_id' => $chatId,
  220. 'text' => "请输入银行卡号",
  221. 'message_id' => $messageId,
  222. ];
  223. }
  224. static function inputCardNo($chatId, $cardNo, $messageId)
  225. {
  226. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  227. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  228. return [
  229. 'chat_id' => $chatId,
  230. 'text' => "请输入姓名",
  231. 'message_id' => $messageId,
  232. ];
  233. }
  234. static function inputAccount($chatId, $account, $messageId)
  235. {
  236. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  237. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  238. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  239. Bank::create([
  240. 'member_id' => $chatId,
  241. 'account' => $account,
  242. 'channel' => $channel,
  243. 'card_no' => $cardNo,
  244. 'bank_name' => $bankName
  245. ]);
  246. return static::banks($chatId, $messageId);
  247. }
  248. }