Wallet.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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\ConfigService;
  7. use App\Models\Config;
  8. use App\Models\Recharge;
  9. use App\Models\Wallet as WalletModel;
  10. use App\Models\Withdraw;
  11. use App\Models\User;
  12. use App\Models\Bank;
  13. use App\Models\Address;
  14. use App\Models\RechargeChannel;
  15. use App\Models\PaymentOrder;
  16. use App\Services\BalanceLogService;
  17. use App\Services\PaymentOrderService;
  18. use App\Services\QianBaoWithdrawService;
  19. use App\Services\WithdrawService;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Validation\ValidationException;
  22. use Exception;
  23. /**
  24. * 充值提现接口
  25. */
  26. class Wallet extends BaseController
  27. {
  28. //获取三斤充值通道(微信、支付宝、扫码充值)
  29. public function getChannel()
  30. {
  31. $member_id = request()->user->member_id;
  32. if (empty(request()->user->recharge_channel_group_id)) {
  33. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  34. } else {
  35. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  36. }
  37. $list = RechargeChannel::getFormatChannel(1, $recharge_channel_group_id);
  38. return $this->success([
  39. 'list' => $list,
  40. ]);
  41. }
  42. /**
  43. * 创建代收订单(自动扫码充值)
  44. */
  45. public function createPay()
  46. {
  47. try {
  48. $params = request()->validate([
  49. 'amount' => ['required', 'numeric', 'min:0.01'],
  50. 'payment_type' => ['required', 'string'],
  51. ]);
  52. if ($params['payment_type'] == 'rgcz') {
  53. return $this->error('参数有误');
  54. }
  55. $member_id = request()->user->member_id;
  56. $user = User::where('member_id', $member_id)->first();
  57. //校验是否支持此充值方式
  58. $check = RechargeChannel::checkRechargeChannel($params['payment_type'], $user->recharge_channel_group_id);
  59. if ($check === false) {
  60. throw new Exception(lang("不支持此充值方式").$user->recharge_channel_group_id);
  61. }
  62. $res = PaymentOrderService::createPay($member_id, $params['amount'], $params['payment_type']);
  63. if ($res['code'] == 0) {
  64. return $this->success($res);
  65. }
  66. return $this->error($res['text']);
  67. } catch (ValidationException $e) {
  68. return $this->error($e->validator->errors()->first());
  69. } catch (\Exception $e) {
  70. return $this->error($e->getMessage());
  71. }
  72. }
  73. /**
  74. * 获取充值二维码(USDT充值)
  75. */
  76. public function scan()
  77. {
  78. try {
  79. $member_id = request()->user->member_id;
  80. $params = request()->validate([
  81. 'type' => ['required', 'string'],
  82. ]);
  83. if (strtolower($params['type']) === "trc20") {
  84. $address = Config::where('field', 'receiving_address')->first()->val;
  85. } elseif (strtolower($params['type']) === "erc20") {
  86. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  87. } else {
  88. return $this->error(lang('充值类型错误'));
  89. }
  90. $receivingType = ConfigService::getVal("receiving_type");
  91. //自动充值
  92. if ($receivingType == 1) {
  93. $res = WalletService::getRechargeImageAddress($member_id);
  94. $address = $res['address'];
  95. $qrCode = $res['full_path'];
  96. } else {
  97. //手动充值
  98. $res = WalletService::getPlatformImageAddress($address);
  99. $res['net'] = $params['type'];
  100. $qrCode = $res['full_path'];
  101. }
  102. return $this->success([
  103. 'qrcode' => $qrCode,
  104. 'address' => $address,
  105. // 'photo' => InputFile::create($qrCode),
  106. ]);
  107. } catch (ValidationException $e) {
  108. return $this->error($e->validator->errors()->first());
  109. } catch (\Exception $e) {
  110. return $this->error($e->getMessage());
  111. }
  112. }
  113. /**
  114. * 提交充值凭证(USDT手动充值)
  115. */
  116. public function recharge()
  117. {
  118. try {
  119. $params = request()->validate([
  120. 'net' => ['required', 'string'],
  121. 'amount' => ['required', 'numeric', 'min:0.01'],
  122. 'toAddress' => ['required', 'string'],
  123. 'image' => ['required', 'url'],
  124. ]);
  125. $member_id = request()->user->member_id;
  126. $user = User::where('member_id', $member_id)->first();
  127. //校验是否支持此充值方式
  128. $check = RechargeChannel::checkRechargeChannel('usdt', $user->recharge_channel_group_id);
  129. if ($check === false) {
  130. throw new Exception(lang("不支持此充值方式"));
  131. }
  132. $recharge = new Recharge();
  133. $recharge->member_id = $member_id;
  134. $recharge->net = $params['net'];
  135. $recharge->coin = "USDT";
  136. $recharge->amount = $params['amount'];
  137. $recharge->to_address = $params['toAddress'];
  138. $recharge->status = 0;
  139. $recharge->type = 2;
  140. $recharge->image = Util::replacePartInUrl($params['image']);
  141. $recharge->save();
  142. return $this->success($recharge,'提交成功');
  143. } catch (ValidationException $e) {
  144. return $this->error($e->validator->errors()->first());
  145. } catch (Exception $e) {
  146. return $this->error($e->getMessage());
  147. }
  148. }
  149. //支付宝、微信、银行卡手动充值
  150. public function paymentOrder()
  151. {
  152. try {
  153. $member_id = request()->user->member_id;
  154. $params = request()->validate([
  155. 'amount' => ['required', 'numeric', 'min:0.01'],
  156. 'payment_type' => ['required', 'integer'],
  157. ]);
  158. $member_id = request()->user->member_id;
  159. $user = User::where('member_id', $member_id)->first();
  160. //校验是否支持此充值方式
  161. $check = RechargeChannel::checkRechargeChannel('rgcz', $user->recharge_channel_group_id);
  162. if ($check === false) {
  163. throw new Exception(lang("不支持此充值方式"));
  164. }
  165. $data = [];
  166. $data['type'] = PaymentOrderService::TYPE_SELF_PAY;
  167. $data['order_no'] = PaymentOrderService::createOrderNo('rgcz_', $member_id);
  168. $data['member_id'] = $member_id;
  169. $data['fee'] = 0;
  170. $data['amount'] = number_format($params['amount'], 2, '.', '');
  171. $data['payment_type'] = $params['payment_type'];
  172. $data['channel'] = 'rgcz';
  173. $data['status'] = PaymentOrderService::STATUS_STAY;
  174. $data['remark'] = '';
  175. // 创建待处理状态的提现记录
  176. $info = PaymentOrder::create($data);
  177. return $this->success($info,'提交成功,请等待人工回复');
  178. } catch (ValidationException $e) {
  179. return $this->error($e->validator->errors()->first());
  180. } catch (Exception $e) {
  181. return $this->error($e->getMessage());
  182. }
  183. }
  184. //支付宝、微信、银行卡手动充值提交图片
  185. public function submitImage()
  186. {
  187. try {
  188. $member_id = request()->user->member_id;
  189. $params = request()->validate([
  190. 'id' => ['required', 'integer'],
  191. 'image' => ['required', 'url'],
  192. ]);
  193. // 创建待处理状态的提现记录
  194. $info = PaymentOrder::where('id', $params['id'])->where('member_id', $member_id)->first();
  195. if (!$info) {
  196. return $this->error(lang('找不到此记录'));
  197. }
  198. if ($info->status != PaymentOrderService::STATUS_USER) {
  199. return $this->error(lang('待处理中,请稍后'));
  200. }
  201. $info->image = $params['image'];
  202. $info->status = PaymentOrderService::STATUS_AUDIT;
  203. $info->save();
  204. return $this->success($info,'提交成功,请等待人工审核');
  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. * 获取提现通道
  213. */
  214. public function withdrawChannel()
  215. {
  216. $member_id = request()->user->member_id;
  217. if (empty(request()->user->recharge_channel_group_id)) {
  218. $recharge_channel_group_id = User::where('member_id', $member_id)->value('recharge_channel_group_id');
  219. } else {
  220. $recharge_channel_group_id = request()->user->recharge_channel_group_id;
  221. }
  222. $list = RechargeChannel::getFormatChannel(2, $recharge_channel_group_id);
  223. return $this->success($list);
  224. }
  225. //USDT提现
  226. public function withdraw()
  227. {
  228. try {
  229. $member_id = request()->user->member_id;
  230. $params = request()->validate([
  231. 'amount' => ['required', 'numeric', 'min:0.01'],
  232. 'address' => ['required', 'string'],
  233. 'safe_word' => ['required'],
  234. ]);
  235. $user = User::where('member_id', $member_id)->first();
  236. if (empty($user->payment_password)) throw new Exception(lang("请先设置资金密码"));
  237. //校验资金密码
  238. if (!password_verify($params['safe_word'], $user->payment_password)) {
  239. throw new Exception(lang('资金密码错误'));
  240. }
  241. //校验是否支持此提现方式
  242. $check = RechargeChannel::checkWithdrawChannel('usdt', $user->recharge_channel_group_id);
  243. if ($check === false) {
  244. throw new Exception(lang("不支持此提现方式"));
  245. }
  246. $serviceCharge = (new WithdrawService())->serviceCharge;
  247. $amount = $params['amount'];
  248. $address = $params['address'];
  249. $real = bcsub($amount, $serviceCharge, 10);
  250. $real = floatval($real);
  251. if ($amount <= $serviceCharge) {
  252. throw new Exception(lang("提现不能少于") . "{$serviceCharge} USDT");
  253. }
  254. $wallet = WalletModel::where('member_id', $member_id)->first();
  255. $temp = floatval($wallet->available_balance);
  256. // 汇率
  257. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  258. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  259. $rate = bcadd($rate, $exchange_rate_difference, 2);
  260. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  261. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  262. if ($amount > $rate_usdt_amount) {
  263. throw new Exception(lang("余额不足") . "{$serviceCharge} USDT");
  264. }
  265. $wallet = WalletModel::where('member_id', $member_id)->first();
  266. $changeAmount = bcmul(($amount * -1), $rate, 2);
  267. $beforeBalance = $wallet->available_balance;
  268. $afterBalance = bcsub($wallet->available_balance, $rate_rmb_amount, 2);
  269. $wallet->available_balance = $afterBalance;
  270. $wallet->save();
  271. $withdraw = Withdraw::create([
  272. 'member_id' => $member_id,
  273. 'amount' => $amount,
  274. 'service_charge' => $serviceCharge,
  275. 'to_account' => $real,
  276. 'address' => $address,
  277. 'exchange_rate' => $rate,
  278. 'status' => 0,
  279. 'after_balance' => $afterBalance
  280. ]);
  281. BalanceLogService::addLog($member_id, $changeAmount, $beforeBalance, $afterBalance, '提现', $withdraw->id, '');
  282. return $this->success($withdraw,'提交成功');
  283. } catch (ValidationException $e) {
  284. return $this->error($e->validator->errors()->first());
  285. } catch (\Exception $e) {
  286. return $this->error($e->getMessage());
  287. }
  288. }
  289. /**
  290. * 提现(手动到账): DF001 支付宝转卡; DF002 支付宝转支付宝; DF005数字人民币; rgtx(人工提现,手动打款)
  291. */
  292. public function payout() {
  293. try {
  294. $params = request()->validate([
  295. 'amount' => ['required', 'numeric', 'min:0.01'],
  296. 'channel' => ['required', 'string', 'in:DF001,DF002,DF005,rgtx'],
  297. 'bank_name' => ['required', 'string'],
  298. 'account' => ['required', 'string'],
  299. 'card_no' => ['required', 'string'],
  300. 'safe_word' => ['required'],
  301. ]);
  302. $member_id = request()->user->member_id;
  303. $user = User::where('member_id', $member_id)->first();
  304. if (empty($user->payment_password)) throw new Exception(lang("请先设置资金密码"));
  305. //校验资金密码
  306. if (!password_verify($params['safe_word'], $user->payment_password)) {
  307. throw new Exception(lang('资金密码错误'));
  308. }
  309. //校验是否支持此提现方式
  310. $check = RechargeChannel::checkWithdrawChannel($params['channel'], $user->recharge_channel_group_id);
  311. if ($check === false) {
  312. throw new Exception(lang("不支持此提现方式"));
  313. }
  314. if ($params['channel'] == 'rgtx') {
  315. DB::beginTransaction();
  316. $amount = $params['amount'];
  317. try {
  318. $wallet = WalletService::findOne(['member_id' => $member_id]);
  319. if (!$wallet) throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  320. $balance = $wallet->available_balance;
  321. if (bccomp($balance, $amount, 2) < 0) {
  322. throw new Exception("您的钱包余额不足!", HttpStatus::CUSTOM_ERROR);
  323. }
  324. $available_balance = bcsub($balance, $amount, 10);
  325. // 先预扣款(锁定资金)
  326. $wallet->available_balance = $available_balance;
  327. if (!$wallet->save()) throw new Exception('钱包更新失败!', HttpStatus::CUSTOM_ERROR);
  328. $rate = RechargeChannel::where('type', 'rgtx')->value('rate');
  329. $rate = $rate ?? 1;
  330. $data = [];
  331. $data['type'] = PaymentOrderService::TYPE_SELF_PAYOUT;
  332. $data['order_no'] = PaymentOrderService::createOrderNo('rgtx' . $data['type'] . '_', $member_id);
  333. $data['member_id'] = $member_id;
  334. $data['fee'] = $amount * $rate;
  335. $data['amount'] = number_format($amount, 2, '.', '');
  336. $data['channel'] = $params['channel'];
  337. $data['bank_name'] = $params['bank_name'];
  338. $data['account'] = $params['account'];
  339. $data['card_no'] = $params['card_no'];
  340. $data['status'] = PaymentOrderService::STATUS_STAY;
  341. // 创建待处理状态的提现记录
  342. $info = PaymentOrder::create($data);
  343. // 记录余额变动日志
  344. BalanceLogService::addLog($member_id, $amount * -1, $balance, $available_balance, '人工提现', $info->id, '钱宝提现费率:'.($rate * 100) . '%');
  345. $balance = bcadd($available_balance, 0, 2);
  346. DB::commit();
  347. } catch (Exception $e) {
  348. DB::rollBack();
  349. if ($e->getCode() === HttpStatus::CUSTOM_ERROR) {
  350. return $this->error($e->getMessage());
  351. }
  352. }
  353. return $this->success($info,'提交成功');
  354. } else {
  355. $res = QianBaoWithdrawService::createOrder($member_id, $params['amount'], $params['channel'], $params['bank_name'], $params['account'], $params['card_no']);
  356. if ($res['code'] == 0) {
  357. return $this->success($res,'提交成功');
  358. }
  359. return $this->error($res['text']);
  360. }
  361. } catch (ValidationException $e) {
  362. return $this->error($e->validator->errors()->first());
  363. } catch (\Exception $e) {
  364. return $this->error($e->getMessage());
  365. }
  366. }
  367. /**
  368. * 提现(自动到账): DF001 支付宝转卡/DF002 支付宝转支付宝
  369. */
  370. public function autoPayout()
  371. {
  372. try {
  373. $params = request()->validate([
  374. 'amount' => ['required', 'numeric', 'min:0.01'],
  375. 'channel' => ['required', 'string'],
  376. 'bank_name' => ['required', 'string'],
  377. 'account' => ['required', 'string'],
  378. 'card_no' => ['required', 'string'],
  379. ]);
  380. $member_id = request()->user->member_id;
  381. $res = PaymentOrderService::autoCreatePayout($member_id, $params['amount'], $params['channel'], $params['bank_name'], $params['account'], $params['card_no']);
  382. if (empty($res['text'])) {
  383. return $this->success($res,'提交成功');
  384. }
  385. return $this->error($res['text']);
  386. } catch (ValidationException $e) {
  387. return $this->error($e->validator->errors()->first());
  388. } catch (Exception $e) {
  389. return $this->error($e->getMessage());
  390. }
  391. }
  392. public function addBank()
  393. {
  394. try {
  395. $params = request()->validate([
  396. 'id' => 'nullable|integer',
  397. 'channel' => 'required',
  398. 'account' => 'required',
  399. 'card_no' => 'required',
  400. 'bank_name' => 'required',
  401. 'alias' => 'nullable',
  402. ]);
  403. $member_id = request()->user->member_id;
  404. if (!empty($params['id'])) {
  405. $info = Bank::where('id', $params['id'])->where('member_id', $member_id)->first();
  406. if (empty($info)) throw new Exception(lang('找不到此记录'));
  407. $info->channel = $params['channel'];
  408. $info->account = $params['account'];
  409. $info->card_no = $params['card_no'];
  410. $info->bank_name = $params['bank_name'];
  411. $info->alias = $params['alias'] ?? '';
  412. $info->save();
  413. } else {
  414. $count = Bank::where('member_id', $member_id)->where('channel', $params['channel'])->count();
  415. if ($count >= 5) throw new Exception(lang('已达添加上限'));
  416. Bank::create([
  417. 'member_id' => $member_id,
  418. 'channel' => $params['channel'],
  419. 'account' => $params['account'],
  420. 'card_no' => $params['card_no'],
  421. 'bank_name' => $params['bank_name'],
  422. 'alias' => $params['alias'] ?? '',
  423. ]);
  424. }
  425. return $this->success([],'提交成功');
  426. } catch (ValidationException $e) {
  427. return $this->error($e->validator->errors()->first());
  428. } catch (\Exception $e) {
  429. return $this->error($e->getMessage());
  430. }
  431. }
  432. public function delBank()
  433. {
  434. try {
  435. $params = request()->validate([
  436. 'id' => 'required|integer',
  437. ]);
  438. $member_id = request()->user->member_id;
  439. $info = Bank::where('id', $params['id'])->where('member_id', $member_id)->first();
  440. if (empty($info)) throw new Exception(lang('找不到此记录'));
  441. $info->delete();
  442. return $this->success([],'删除成功');
  443. } catch (ValidationException $e) {
  444. return $this->error($e->validator->errors()->first());
  445. } catch (\Exception $e) {
  446. return $this->error($e->getMessage());
  447. }
  448. }
  449. public function bankList()
  450. {
  451. try {
  452. $params = request()->validate([
  453. 'channel' => 'nullable',
  454. ]);
  455. $member_id = request()->user->member_id;
  456. $where = !empty($params['channel']) ? ['channel' => $params['channel']] : [];
  457. $list = Bank::where('member_id', $member_id)->where($where)->get()->toArray();
  458. return $this->success([
  459. 'list' => $list,
  460. ]);
  461. } catch (\Exception $e) {
  462. return $this->error($e->getMessage());
  463. }
  464. }
  465. public function addAddress()
  466. {
  467. try {
  468. $params = request()->validate([
  469. 'id' => 'nullable|integer',
  470. 'address' => 'required',
  471. 'alias' => 'nullable',
  472. ]);
  473. $member_id = request()->user->member_id;
  474. if (!empty($params['id'])) {
  475. $info = Address::where('id', $params['id'])->where('member_id', $member_id)->first();
  476. if (empty($info)) throw new Exception(lang('找不到此记录'));
  477. $info->address = $params['address'];
  478. $info->alias = $params['alias'] ?? '';
  479. $info->save();
  480. } else {
  481. $count = Address::where('member_id', $member_id)->where('address', $params['address'])->count();
  482. if ($count >= 5) throw new Exception(lang('已达添加上限'));
  483. Address::create([
  484. 'member_id' => $member_id,
  485. 'address' => $params['address'],
  486. 'alias' => $params['alias'] ?? '',
  487. ]);
  488. }
  489. return $this->success([],'提交成功');
  490. } catch (ValidationException $e) {
  491. return $this->error($e->validator->errors()->first());
  492. } catch (\Exception $e) {
  493. return $this->error($e->getMessage());
  494. }
  495. }
  496. public function delAddress()
  497. {
  498. try {
  499. $params = request()->validate([
  500. 'id' => 'required|integer',
  501. ]);
  502. $member_id = request()->user->member_id;
  503. $info = Address::where('id', $params['id'])->where('member_id', $member_id)->first();
  504. if (empty($info)) throw new Exception(lang('找不到此记录'));
  505. $info->delete();
  506. return $this->success([],'删除成功');
  507. } catch (ValidationException $e) {
  508. return $this->error($e->validator->errors()->first());
  509. } catch (\Exception $e) {
  510. return $this->error($e->getMessage());
  511. }
  512. }
  513. public function address()
  514. {
  515. try {
  516. $member_id = request()->user->member_id;
  517. $list = Address::where('member_id', $member_id)->get()->toArray();
  518. return $this->success([
  519. 'list' => $list,
  520. ]);
  521. } catch (\Exception $e) {
  522. return $this->error($e->getMessage());
  523. }
  524. }
  525. }