Wallet.php 19 KB

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