Wallet.php 21 KB

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