Wallet.php 18 KB

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