QianBaoWithdrawService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. return [
  191. 'chat_id' => $chatId,
  192. 'text' => "请输入支付宝账号",
  193. 'message_id' => $messageId,
  194. ];
  195. break;
  196. case "DF001"://银行卡
  197. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  198. return [
  199. 'chat_id' => $chatId,
  200. 'text' => "请输入银行名称",
  201. 'message_id' => $messageId,
  202. ];
  203. break;
  204. default:
  205. return [
  206. 'chat_id' => $chatId,
  207. 'text' => "选择通道错误",
  208. 'message_id' => $messageId,
  209. ];
  210. break;
  211. }
  212. }
  213. static function inputBankName($chatId, $bankName, $messageId)
  214. {
  215. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  216. return [
  217. 'chat_id' => $chatId,
  218. 'text' => "请输入银行卡号",
  219. 'message_id' => $messageId,
  220. ];
  221. }
  222. }