Wallet.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Constants\Util;
  4. use App\Constants\HttpStatus;
  5. use App\Services\WalletService;
  6. use App\Services\UsdtRechargeConfigService;
  7. use App\Services\ConfigService;
  8. use App\Models\Config;
  9. use App\Models\Recharge;
  10. use App\Models\Wallet as WalletModel;
  11. use App\Models\Withdraw;
  12. use App\Models\User;
  13. use App\Support\FirstDepositChoice;
  14. use App\Models\Bank;
  15. use App\Models\Address;
  16. use App\Models\RechargeChannel;
  17. use App\Models\PaymentOrder;
  18. use App\Services\BalanceLogService;
  19. use App\Services\WithdrawFundsService;
  20. use App\Services\WithdrawalName;
  21. use App\Services\WithdrawalDestination;
  22. use App\Services\PaymentOrderService;
  23. use App\Services\Payment\JdPayService;
  24. use App\Services\Payment\NoPayService;
  25. use App\Services\Payment\ZimuPayService;
  26. use App\Services\QianBaoWithdrawService;
  27. use App\Services\WithdrawService;
  28. use App\Services\LogService;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Validation\ValidationException;
  31. use Exception;
  32. /**
  33. * 充值提现接口
  34. */
  35. class Wallet extends BaseController
  36. {
  37. //获取三斤充值通道(微信、支付宝、扫码充值)
  38. public function getChannel()
  39. {
  40. try {
  41. $params = request()->validate(['currency_type' => ['nullable', 'string', 'in:crypto,fiat']],
  42. ['currency_type.in' => '充值渠道分类仅支持crypto或fiat']);
  43. } catch (ValidationException $e) {
  44. return $this->error($e->validator->errors()->first(), [], HttpStatus::CUSTOM_ERROR);
  45. }
  46. $member_id = request()->user->member_id;
  47. if (empty(request()->user->recharge_channel_group_id)) {
  48. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  49. } else {
  50. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  51. }
  52. $list = RechargeChannel::getFormatChannel(1, $recharge_channel_group_id);
  53. if (!JdPayService::canUserRecharge(request()->user->id)) {
  54. $list = array_values(array_filter($list, function ($item) {
  55. return !JdPayService::isChannel($item['value'] ?? '');
  56. }));
  57. }
  58. if (!NoPayService::canUserRecharge(request()->user->id)) {
  59. $list = array_values(array_filter($list, function ($item) {
  60. return !NoPayService::isRechargeChannel($item['value'] ?? '');
  61. }));
  62. }
  63. if (!ZimuPayService::canUserRecharge(request()->user->id)) {
  64. $list = array_values(array_filter($list, function ($item) {
  65. return !ZimuPayService::isRechargeChannel($item['value'] ?? '');
  66. }));
  67. }
  68. $currencyType = $params['currency_type'] ?? null;
  69. if ($currencyType !== null && $currencyType !== '') {
  70. $list = array_values(array_filter($list, static function ($item) use ($currencyType) {
  71. return (($item['value'] ?? '') === 'usdt') === ($currencyType === 'crypto');
  72. }));
  73. }
  74. $list = \App\Services\RechargeBonusService::decorate($list, $this->lang, (int)request()->user->from === 1);
  75. return $this->success([
  76. 'list' => $list,
  77. ]);
  78. }
  79. /**
  80. * 创建代收订单(自动扫码充值)
  81. */
  82. public function createPay()
  83. {
  84. try {
  85. $params = request()->validate([
  86. 'amount' => ['required', 'numeric', 'min:0.01'],
  87. 'payment_type' => ['required_without:channel_id', 'nullable', 'string'],
  88. 'channel_id' => ['nullable', 'integer', 'min:1'],
  89. ]);
  90. $joinFirstDeposit = FirstDepositChoice::parse(request()->input('join_first_deposit'));
  91. $channelId = !empty($params['channel_id']) ? (int)$params['channel_id'] : null;
  92. if ($channelId) {
  93. $channelType = RechargeChannel::query()->where('data_type', 1)->whereKey($channelId)->value('type');
  94. if ($channelType === null) {
  95. throw new Exception(lang("充值通道不存在"));
  96. }
  97. if (!empty($params['payment_type']) && $params['payment_type'] !== $channelType) {
  98. throw new Exception(lang("充值通道与充值方式不匹配"));
  99. }
  100. $params['payment_type'] = $channelType;
  101. }
  102. if ($params['payment_type'] == 'rgcz') {
  103. return $this->error('参数有误');
  104. }
  105. $member_id = request()->user->member_id;
  106. $user = User::where('member_id', $member_id)->first();
  107. if (JdPayService::isChannel($params['payment_type']) && !JdPayService::canUserRecharge($user->id)) {
  108. throw new Exception(lang("不支持此充值方式"));
  109. }
  110. if (NoPayService::isRechargeChannel($params['payment_type']) && !NoPayService::canUserRecharge($user->id)) {
  111. throw new Exception(lang("不支持此充值方式"));
  112. }
  113. if (ZimuPayService::isRechargeChannel($params['payment_type']) && !ZimuPayService::canUserRecharge($user->id)) {
  114. throw new Exception(lang("不支持此充值方式"));
  115. }
  116. //校验是否支持此充值方式
  117. $check = RechargeChannel::checkRechargeChannel($params['payment_type'], $user->recharge_channel_group_id);
  118. if ($check === false) {
  119. throw new Exception(lang("不支持此充值方式").$user->recharge_channel_group_id);
  120. }
  121. // Confirm before contacting the provider. The wallet lock is released before the external request.
  122. FirstDepositChoice::record($user, $joinFirstDeposit, 'online_pay', request()->input('first_deposit_offer'));
  123. $res = PaymentOrderService::createPay($member_id, $params['amount'], $params['payment_type'], $channelId);
  124. if ($res['code'] == 0) return $this->success($res);
  125. return $this->error($res['text']);
  126. } catch (ValidationException $e) {
  127. return $this->error($e->validator->errors()->first());
  128. } catch (\Exception $e) {
  129. return $this->error($e->getMessage());
  130. }
  131. }
  132. /**
  133. * 获取充值二维码(USDT充值)
  134. */
  135. public function scan()
  136. {
  137. try {
  138. $member_id = request()->user->member_id;
  139. $params = request()->validate([
  140. 'type' => ['required', 'string'],
  141. ]);
  142. if (strtolower($params['type']) === "trc20") {
  143. $address = Config::where('field', 'receiving_address')->first()->val;
  144. } elseif (strtolower($params['type']) === "erc20") {
  145. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  146. } else {
  147. return $this->error(lang('充值类型错误'));
  148. }
  149. $receivingType = ConfigService::getVal("receiving_type");
  150. //自动充值
  151. if ($receivingType == 1) {
  152. $res = WalletService::getRechargeImageAddress($member_id);
  153. $address = $res['address'];
  154. $qrCode = $res['full_path'];
  155. } else {
  156. //手动充值
  157. $res = WalletService::getPlatformImageAddress($address);
  158. $res['net'] = $params['type'];
  159. $qrCode = $res['full_path'];
  160. }
  161. return $this->success([
  162. 'qrcode' => $qrCode,
  163. 'address' => $address,
  164. ] + (new UsdtRechargeConfigService())->get());
  165. } catch (ValidationException $e) {
  166. return $this->error($e->validator->errors()->first());
  167. } catch (\Exception $e) {
  168. return $this->error($e->getMessage());
  169. }
  170. }
  171. /**
  172. * 提交充值凭证(USDT手动充值)
  173. */
  174. public function recharge()
  175. {
  176. try {
  177. $params = request()->validate([
  178. 'net' => ['required', 'string'],
  179. 'amount' => ['required', 'numeric', 'min:0.01'],
  180. 'toAddress' => ['required', 'string'],
  181. 'image' => ['required', 'url'],
  182. ]);
  183. $joinFirstDeposit = FirstDepositChoice::parse(request()->input('join_first_deposit'));
  184. $member_id = request()->user->member_id;
  185. $user = User::where('member_id', $member_id)->first();
  186. //校验是否支持此充值方式
  187. $check = RechargeChannel::checkRechargeChannel('usdt', $user->recharge_channel_group_id);
  188. if ($check === false) {
  189. throw new Exception(lang("不支持此充值方式"));
  190. }
  191. $recharge = new Recharge();
  192. $recharge->member_id = $member_id;
  193. $recharge->net = $params['net'];
  194. $recharge->coin = "USDT";
  195. $recharge->amount = $params['amount'];
  196. $recharge->to_address = $params['toAddress'];
  197. $recharge->status = 0;
  198. $recharge->type = 2;
  199. $recharge->image = Util::replacePartInUrl($params['image']);
  200. DB::transaction(function () use ($recharge, $user, $joinFirstDeposit) {
  201. FirstDepositChoice::record($user, $joinFirstDeposit, 'usdt_recharge', request()->input('first_deposit_offer'));
  202. $recharge->save();
  203. });
  204. return $this->success($recharge,'提交成功');
  205. } catch (ValidationException $e) {
  206. return $this->error($e->validator->errors()->first());
  207. } catch (Exception $e) {
  208. return $this->error($e->getMessage());
  209. }
  210. }
  211. //支付宝、微信、银行卡手动充值
  212. public function paymentOrder()
  213. {
  214. try {
  215. $member_id = request()->user->member_id;
  216. $params = request()->validate([
  217. 'amount' => ['required', 'numeric', 'min:0.01'],
  218. 'payment_type' => ['required', 'integer'],
  219. ]);
  220. $joinFirstDeposit = FirstDepositChoice::parse(request()->input('join_first_deposit'));
  221. $member_id = request()->user->member_id;
  222. $user = User::where('member_id', $member_id)->first();
  223. //校验是否支持此充值方式
  224. $check = RechargeChannel::checkRechargeChannel('rgcz', $user->recharge_channel_group_id);
  225. if ($check === false) {
  226. throw new Exception(lang("不支持此充值方式"));
  227. }
  228. $data = [];
  229. $data['type'] = PaymentOrderService::TYPE_SELF_PAY;
  230. $data['order_no'] = PaymentOrderService::createOrderNo('rgcz_', $member_id);
  231. $data['member_id'] = $member_id;
  232. $data += PaymentOrderService::feeFields($params['amount'], 0);
  233. $data['amount'] = number_format($params['amount'], 2, '.', '');
  234. $data['payment_type'] = $params['payment_type'];
  235. $data['channel'] = 'rgcz';
  236. $data['status'] = PaymentOrderService::STATUS_STAY;
  237. $data['remark'] = '';
  238. // 创建待处理状态的提现记录
  239. $info = DB::transaction(function () use ($data, $user, $joinFirstDeposit) {
  240. FirstDepositChoice::record($user, $joinFirstDeposit, 'manual_pay', request()->input('first_deposit_offer'));
  241. return PaymentOrder::create($data);
  242. });
  243. return $this->success($info,'提交成功,请等待人工回复');
  244. } catch (ValidationException $e) {
  245. return $this->error($e->validator->errors()->first());
  246. } catch (Exception $e) {
  247. return $this->error($e->getMessage());
  248. }
  249. }
  250. //支付宝、微信、银行卡手动充值提交图片
  251. public function submitImage()
  252. {
  253. try {
  254. $member_id = request()->user->member_id;
  255. $params = request()->validate([
  256. 'id' => ['required', 'integer'],
  257. 'image' => ['required', 'url'],
  258. ]);
  259. // 创建待处理状态的提现记录
  260. $info = PaymentOrder::where('id', $params['id'])->where('member_id', $member_id)->first();
  261. if (!$info) {
  262. return $this->error(lang('找不到此记录'));
  263. }
  264. if ($info->status != PaymentOrderService::STATUS_USER) {
  265. return $this->error(lang('待处理中,请稍后'));
  266. }
  267. $info->image = $params['image'];
  268. $info->status = PaymentOrderService::STATUS_AUDIT;
  269. $info->save();
  270. return $this->success($info,'提交成功,请等待人工审核');
  271. } catch (ValidationException $e) {
  272. return $this->error($e->validator->errors()->first());
  273. } catch (Exception $e) {
  274. return $this->error($e->getMessage());
  275. }
  276. }
  277. /**
  278. * 获取提现通道
  279. */
  280. public function withdrawChannel()
  281. {
  282. $member_id = request()->user->member_id;
  283. if (empty(request()->user->recharge_channel_group_id)) {
  284. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  285. } else {
  286. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  287. }
  288. $list = WithdrawalDestination::describeChannels(RechargeChannel::getFormatChannel(2, $recharge_channel_group_id));
  289. return $this->success($list);
  290. }
  291. //USDT提现
  292. public function withdraw()
  293. {
  294. $error_code = -1;
  295. try {
  296. $member_id = request()->user->member_id;
  297. $params = request()->validate([
  298. 'amount' => ['required', 'numeric', 'min:0.01'],
  299. 'address' => ['required', 'string'],
  300. 'safe_word' => ['required'],
  301. ]);
  302. $user = User::where('member_id', $member_id)->first();
  303. if ($user->from == 2) {
  304. throw new Exception(lang("游客不支持提现"));
  305. }
  306. if (empty($user->payment_password)) {
  307. $error_code = 10011;
  308. throw new Exception(lang("请先设置资金密码"));
  309. }
  310. //校验资金密码
  311. if (!password_verify($params['safe_word'], $user->payment_password)) {
  312. throw new Exception(lang('资金密码错误'));
  313. }
  314. WithdrawalName::required($user);
  315. //校验是否支持此提现方式
  316. $check = RechargeChannel::checkWithdrawChannel('usdt', $user->recharge_channel_group_id);
  317. if ($check === false) {
  318. throw new Exception(lang("不支持此提现方式"));
  319. }
  320. $amount = $params['amount'];
  321. $address = $params['address'];
  322. //校验提现金额的限制
  323. if ($amount < $check->min || $amount > $check->max) {
  324. throw new Exception(lang("提现金额超出限制"));
  325. }
  326. $serviceCharge = (new WithdrawService())->serviceCharge;
  327. $real = bcsub($amount, $serviceCharge, 10);
  328. $real = floatval($real);
  329. if ($amount <= $serviceCharge) {
  330. throw new Exception(lang("提现不能少于") . "{$serviceCharge} USDT");
  331. }
  332. $wallet = WalletModel::where('member_id', $member_id)->first();
  333. $temp = floatval($wallet->available_balance);
  334. // 汇率
  335. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  336. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  337. $rate = bcadd($rate, $exchange_rate_difference, 2);
  338. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  339. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  340. if ($amount > $rate_usdt_amount) {
  341. throw new Exception(lang("余额不足") . "{$serviceCharge} USDT");
  342. }
  343. // 校验打码量 → 可用余额转入冻结余额 → 创建提现单 → 重置打码周期
  344. [$withdraw] = WithdrawFundsService::hold($member_id, $rate_rmb_amount, '提现', '', WithdrawFundsService::WITHDRAWS,
  345. fn ($before, $after) => Withdraw::create([
  346. 'member_id' => $member_id,
  347. 'amount' => $amount,
  348. 'service_charge' => $serviceCharge,
  349. 'to_account' => $real,
  350. 'address' => $address,
  351. 'exchange_rate' => $rate,
  352. 'status' => 0,
  353. 'after_balance' => $after,
  354. ]));
  355. return $this->success($withdraw,'提交成功');
  356. } catch (ValidationException $e) {
  357. return $this->error($e->validator->errors()->first());
  358. } catch (\Exception $e) {
  359. return $this->error($e->getMessage(), [], $error_code);
  360. }
  361. }
  362. /**
  363. * 提现(手动到账): DF001 支付宝转卡; DF002 支付宝转支付宝; DF005数字人民币; JDpay JD钱包; NOwithdraw NO钱包; ZIMUwithdraw 808币钱包地址提现;ZIMUcash 808人民币代付; rgtx(人工提现,手动打款)
  364. */
  365. public function payout() {
  366. $error_code = -1;
  367. try {
  368. $params = request()->validate([
  369. 'amount' => ['required', 'numeric', 'min:0.01'],
  370. 'channel' => ['required', 'string', 'in:' . WithdrawalName::CHANNELS],
  371. 'bank_name' => ['nullable', 'string', 'max:255'],
  372. 'bank_branch' => ['nullable', 'string', 'max:255'],
  373. 'card_no' => ['nullable', 'string', 'max:255'],
  374. 'payout_method' => ['nullable', 'string', 'in:' . implode(',', array_keys(WithdrawalDestination::MANUAL_METHODS))],
  375. 'qr_image' => ['nullable', 'string', 'max:255'],
  376. 'safe_word' => ['required'],
  377. ]);
  378. $member_id = request()->user->member_id;
  379. $user = User::where('member_id', $member_id)->first();
  380. if ($user->from == 2) {
  381. throw new Exception(lang("游客不支持提现"));
  382. }
  383. if (empty($user->payment_password)) {
  384. $error_code = 10011;
  385. throw new Exception(lang("请先设置资金密码"));
  386. }
  387. //校验资金密码
  388. if (!password_verify($params['safe_word'], $user->payment_password)) {
  389. throw new Exception(lang('资金密码错误'));
  390. }
  391. //校验是否支持此提现方式
  392. $check = RechargeChannel::checkWithdrawChannel($params['channel'], $user->recharge_channel_group_id);
  393. if ($check === false) {
  394. throw new Exception(lang("不支持此提现方式"));
  395. }
  396. $channel = $params['channel'];
  397. $destination = WithdrawalDestination::resolve($channel, $params);
  398. $amount = $params['amount'];
  399. //校验提现金额的限制
  400. if ($amount < $check->min || $amount > $check->max) {
  401. throw new Exception(lang("提现金额超出限制"));
  402. }
  403. if ($channel === 'rgtx') {
  404. try {
  405. $data = [];
  406. $data['type'] = PaymentOrderService::TYPE_SELF_PAYOUT;
  407. $data['order_no'] = PaymentOrderService::createOrderNo('rgtx' . $data['type'] . '_', $member_id);
  408. $data['member_id'] = $member_id;
  409. // 人工提现暂不记三方手续费(全额出款,不扣客户)。
  410. $data += PaymentOrderService::feeFields($amount, 0);
  411. $data['amount'] = number_format($amount, 2, '.', '');
  412. $data['channel'] = $params['channel'];
  413. $data['bank_name'] = $destination['bank_name'];
  414. $data['bank_branch'] = $destination['bank_branch'] ?? null;
  415. $data['account'] = WithdrawalName::forPayout($user, $channel);
  416. $data['card_no'] = $destination['card_no'];
  417. $data['qr_image'] = $destination['qr_image'] ?? null;
  418. $data['status'] = PaymentOrderService::STATUS_STAY;
  419. // 校验打码量 → 冻结资金 → 创建待处理的提现记录 → 重置打码周期
  420. [$info] = WithdrawFundsService::hold($member_id, $amount, '人工提现', '',
  421. WithdrawFundsService::PAYMENT_ORDERS, function () use ($data, $member_id) {
  422. $pending = PaymentOrder::where('type', PaymentOrderService::TYPE_SELF_PAYOUT)
  423. ->where('member_id', $member_id)->whereIn('status', [0, 1, 4, 5])
  424. ->lockForUpdate()->first(['id']);
  425. if ($pending) {
  426. throw new Exception(lang('您已有1笔提现订单正常处理中!'), HttpStatus::CUSTOM_ERROR);
  427. }
  428. return PaymentOrder::create($data);
  429. });
  430. } catch (Exception $e) {
  431. if ($e->getCode() === HttpStatus::CUSTOM_ERROR) {
  432. return $this->error($e->getMessage());
  433. }
  434. LogService::error($e);
  435. return $this->error('系统繁忙,请稍后重试!');
  436. }
  437. return $this->success(array_replace($info->toArray(), WithdrawalDestination::manualResponse(
  438. $destination['bank_name'], $destination['card_no'], $destination['qr_image'] ?? null
  439. )), '提交成功');
  440. } else {
  441. $res = QianBaoWithdrawService::createOrder(
  442. $member_id, $params['amount'], $channel, $destination['bank_name'], $destination['card_no'],
  443. $destination['bank_branch']
  444. );
  445. if ($res['code'] == 0) {
  446. return $this->success($res,'提交成功');
  447. }
  448. return $this->error($res['text']);
  449. }
  450. } catch (ValidationException $e) {
  451. return $this->error($e->validator->errors()->first());
  452. } catch (\Exception $e) {
  453. return $this->error($e->getMessage(), [], $error_code);
  454. }
  455. }
  456. /**
  457. * 提现(自动到账): DF001 支付宝转卡/DF002 支付宝转支付宝
  458. */
  459. public function autoPayout()
  460. {
  461. try {
  462. $params = request()->validate([
  463. 'amount' => ['required', 'numeric', 'min:0.01'],
  464. 'channel' => ['required', 'string'],
  465. 'bank_name' => ['nullable', 'string', 'max:255'],
  466. 'bank_branch' => ['nullable', 'string', 'max:255'],
  467. 'card_no' => ['required', 'string', 'max:255'],
  468. ]);
  469. $member_id = request()->user->member_id;
  470. $destination = WithdrawalDestination::resolve($params['channel'], $params);
  471. $res = PaymentOrderService::autoCreatePayout(
  472. $member_id, $params['amount'], $params['channel'], $destination['bank_name'], $destination['card_no'],
  473. $destination['bank_branch'] ?? null
  474. );
  475. if (empty($res['text'])) {
  476. return $this->success($res,'提交成功');
  477. }
  478. return $this->error($res['text']);
  479. } catch (ValidationException $e) {
  480. return $this->error($e->validator->errors()->first());
  481. } catch (Exception $e) {
  482. return $this->error($e->getMessage());
  483. }
  484. }
  485. public function addBank()
  486. {
  487. try {
  488. $params = request()->validate([
  489. 'id' => 'nullable|integer',
  490. 'channel' => 'required|string|in:' . WithdrawalName::CHANNELS,
  491. 'account' => 'nullable|string',
  492. 'card_no' => 'nullable|string|max:255',
  493. 'bank_name' => 'nullable|string|max:255',
  494. 'bank_branch' => 'nullable|string|max:255',
  495. 'payout_method' => 'nullable|string|in:' . implode(',', array_keys(WithdrawalDestination::MANUAL_METHODS)),
  496. 'qr_image' => 'nullable|string|max:255',
  497. ]);
  498. $member_id = request()->user->member_id;
  499. $destination = WithdrawalDestination::resolve($params['channel'], $params);
  500. $realName = DB::transaction(function () use ($member_id, $params, $destination) {
  501. $user = User::where('member_id', $member_id)->lockForUpdate()->firstOrFail();
  502. $accounts = Bank::where('member_id', $member_id);
  503. if (!empty($params['id'])) {
  504. $info = $accounts->where('id', $params['id'])->lockForUpdate()->first();
  505. if (!$info) throw new Exception(lang('找不到此记录'));
  506. } else {
  507. if ($accounts->where('channel', $params['channel'])->count() >= 5) {
  508. throw new Exception(lang('已达添加上限'));
  509. }
  510. $info = new Bank();
  511. }
  512. $name = WithdrawalName::bind($user, $params['account'] ?? null);
  513. if (!$info->fill([
  514. 'member_id' => $member_id,
  515. 'channel' => $params['channel'],
  516. 'account' => WithdrawalName::usesOrderName($params['channel']) ? $name : '',
  517. 'card_no' => $destination['card_no'],
  518. 'bank_name' => $destination['bank_name'],
  519. 'bank_branch' => $destination['bank_branch'] ?? null,
  520. 'qr_image' => $destination['qr_image'] ?? null,
  521. ])->save()) {
  522. throw new Exception(lang('系统繁忙,请稍后重试!'));
  523. }
  524. return $user->real_name;
  525. });
  526. return $this->success(['real_name' => $realName], '提交成功');
  527. } catch (ValidationException $e) {
  528. return $this->error($e->validator->errors()->first());
  529. } catch (\Exception $e) {
  530. return $this->error($e->getMessage());
  531. }
  532. }
  533. public function delBank()
  534. {
  535. try {
  536. $params = request()->validate([
  537. 'id' => 'required|integer',
  538. ]);
  539. $member_id = request()->user->member_id;
  540. $info = Bank::where('id', $params['id'])->where('member_id', $member_id)->first();
  541. if (empty($info)) throw new Exception(lang('找不到此记录'));
  542. $info->delete();
  543. return $this->success([],'删除成功');
  544. } catch (ValidationException $e) {
  545. return $this->error($e->validator->errors()->first());
  546. } catch (\Exception $e) {
  547. return $this->error($e->getMessage());
  548. }
  549. }
  550. public function bankList()
  551. {
  552. try {
  553. $params = request()->validate([
  554. 'channel' => 'nullable',
  555. ]);
  556. $member_id = request()->user->member_id;
  557. $realName = request()->user->real_name;
  558. $where = !empty($params['channel']) ? ['channel' => $params['channel']] : [];
  559. $list = Bank::where('member_id', $member_id)->where($where)->get()->toArray();
  560. foreach ($list as &$account) {
  561. if (WithdrawalName::usesOrderName($account['channel'])) {
  562. $account['account'] = $realName ?? '';
  563. }
  564. if ($account['channel'] === 'rgtx') {
  565. $account = array_replace($account, WithdrawalDestination::manualResponse(
  566. $account['bank_name'], $account['card_no'], $account['qr_image'] ?? null
  567. ));
  568. }
  569. }
  570. return $this->success([
  571. 'list' => $list,
  572. 'real_name' => $realName,
  573. ]);
  574. } catch (\Exception $e) {
  575. return $this->error($e->getMessage());
  576. }
  577. }
  578. public function addAddress()
  579. {
  580. try {
  581. $params = request()->validate([
  582. 'id' => 'nullable|integer',
  583. 'address' => 'required|string',
  584. 'account' => 'nullable|string',
  585. ]);
  586. $member_id = request()->user->member_id;
  587. $name = DB::transaction(function () use ($member_id, $params) {
  588. $user = User::where('member_id', $member_id)->lockForUpdate()->firstOrFail();
  589. $addresses = Address::where('member_id', $member_id);
  590. if (!empty($params['id'])) {
  591. $info = $addresses->where('id', $params['id'])->lockForUpdate()->first();
  592. if (!$info) throw new Exception(lang('找不到此记录'));
  593. } else {
  594. if ($addresses->where('address', $params['address'])->count() >= 5) {
  595. throw new Exception(lang('已达添加上限'));
  596. }
  597. $info = new Address();
  598. }
  599. $name = WithdrawalName::bind($user, $params['account'] ?? null);
  600. if (!$info->fill([
  601. 'member_id' => $member_id,
  602. 'address' => $params['address'],
  603. ])->save()) {
  604. throw new Exception(lang('系统繁忙,请稍后重试!'));
  605. }
  606. return $name;
  607. });
  608. return $this->success(['real_name' => $name], '提交成功');
  609. } catch (ValidationException $e) {
  610. return $this->error($e->validator->errors()->first());
  611. } catch (\Exception $e) {
  612. return $this->error($e->getMessage());
  613. }
  614. }
  615. public function delAddress()
  616. {
  617. try {
  618. $params = request()->validate([
  619. 'id' => 'required|integer',
  620. ]);
  621. $member_id = request()->user->member_id;
  622. $info = Address::where('id', $params['id'])->where('member_id', $member_id)->first();
  623. if (empty($info)) throw new Exception(lang('找不到此记录'));
  624. $info->delete();
  625. return $this->success([],'删除成功');
  626. } catch (ValidationException $e) {
  627. return $this->error($e->validator->errors()->first());
  628. } catch (\Exception $e) {
  629. return $this->error($e->getMessage());
  630. }
  631. }
  632. public function address()
  633. {
  634. try {
  635. $member_id = request()->user->member_id;
  636. $list = Address::where('member_id', $member_id)->get()->toArray();
  637. return $this->success([
  638. 'list' => $list,
  639. 'real_name' => request()->user->real_name,
  640. ]);
  641. } catch (\Exception $e) {
  642. return $this->error($e->getMessage());
  643. }
  644. }
  645. }