Wallet.php 18 KB

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