| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace App\Http\Controllers\api;
- use App\Services\WalletService;
- use App\Services\ConfigService;
- use App\Models\Config;
- use App\Models\Recharge;
- use App\Models\Wallet as WalletModel;
- use App\Models\Withdraw;
- use App\Models\User;
- use App\Services\BalanceLogService;
- use App\Services\Payment\SanJinService;
- use App\Services\PaymentOrderService;
- use App\Services\QianBaoWithdrawService;
- use App\Services\Payment\QianBaoService;
- use App\Services\WithdrawService;
- use Illuminate\Validation\ValidationException;
- use Exception;
- /**
- * 充值提现接口
- */
- class Wallet extends BaseController
- {
- //获取三斤充值通道(微信、支付宝、扫码充值)
- public function getChannel()
- {
- $data = SanJinService::getChannel();
- $product = SanJinService::$PRODUCT;
- $list = [];
- foreach($data as $k => $v) {
- foreach($product as $pv) {
- if ($k == $pv['type']) {
- $config = $pv;
- }
- }
- $list[] = [
- 'label' => lang($v),
- 'value' => $k,
- 'config' => $config ?? [],
- ];
- }
- return $this->success([
- 'list' => $list,
- ]);
- }
- /**
- * 创建代收订单
- */
- public function createPay()
- {
- try {
- $params = request()->validate([
- 'amount' => ['required', 'numeric', 'min:0.01'],
- 'payment_type' => ['required', 'string'],
- ]);
- $member_id = request()->user->member_id;
- $res = PaymentOrderService::createPay($member_id, $params['amount'], $params['payment_type']);
- if ($res['code'] == 0) {
- return $this->success($res);
- }
- return $this->error($res['text']);
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 获取充值二维码(USDT充值)
- */
- public function scan()
- {
- try {
- $member_id = request()->user->member_id;
- $params = request()->validate([
- 'type' => ['required', 'string'],
- ]);
- $receivingType = ConfigService::getVal("receiving_type");
- //自动充值
- if ($receivingType == 1) {
- $res = WalletService::getRechargeImageAddress($member_id);
- $address = $res['address'];
- $qrCode = $res['full_path'];
- } else {
- //手动充值
- if ($params['type'] === "TRC20") {
- $address = Config::where('field', 'receiving_address')->first()->val;
- } elseif ($params['type'] === "ERC20") {
- $address = Config::where('field', 'receiving_address_erc20')->first()->val;
- } else {
- return $this->error(lang('充值类型错误'));
- }
- $res = WalletService::getPlatformImageAddress($address);
- $res['net'] = $params['type'];
- $qrCode = $res['full_path'];
- }
-
- return $this->success([
- 'qrcode' => $qrCode,
- // 'photo' => InputFile::create($qrCode),
- ]);
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 提交充值凭证
- */
- public function recharge()
- {
- try {
- $params = request()->validate([
- 'net' => ['required', 'string'],
- 'amount' => ['required', 'numeric', 'min:0.01'],
- 'toAddress' => ['required', 'string'],
- 'image' => ['required', 'url'],
- ]);
- $member_id = '';
- $recharge = new Recharge();
- $recharge->member_id = $member_id;
- $recharge->net = $params['net'];
- $recharge->coin = "USDT";
- $recharge->amount = $params['amount'];
- $recharge->to_address = $params['toAddress'];
- $recharge->status = 0;
- $recharge->type = 2;
- $recharge->image = $params['image'];
- $recharge->save();
- return $this->success($recharge,'提交成功');
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 获取提现通道
- */
- public function withdrawChannel()
- {
- $list = QianBaoService::withdrawChannel();
- $data[] = ['label' => 'USDT', 'value' => 'USDT'];
- foreach ($list as $key => $item) {
- $data[] = ['label' => $item, 'value' => $key];
- }
- return $this->success($data);
- }
- public function withdraw()
- {
- try {
- $member_id = request()->user->member_id;
- $params = request()->validate([
- 'amount' => ['required', 'numeric', 'min:0.01'],
- 'address' => ['required', 'string'],
- 'safe_word' => ['required'],
- ]);
- $user = User::where('member_id', $member_id)->first();
- if (empty($user->payment_password)) throw new Exception(lang("请先设置资金密码"));
- //校验资金密码
- if (!password_verify($params['safe_word'], $user->payment_password)) {
- throw new Exception(lang('资金密码错误'));
- }
-
- $serviceCharge = (new WithdrawService())->serviceCharge;
- $amount = $params['amount'];
- $address = $params['address'];
- $real = bcsub($amount, $serviceCharge, 10);
- $real = floatval($real);
- if ($amount <= $serviceCharge) {
- throw new Exception(lang("提现不能少于") . "{$serviceCharge} USDT");
- }
- $wallet = WalletModel::where('member_id', $member_id)->first();
- $temp = floatval($wallet->available_balance);
- // 汇率
- $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
- $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
- $rate = bcadd($rate, $exchange_rate_difference, 2);
- $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
- $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
- if ($amount > $rate_usdt_amount) {
- throw new Exception(lang("余额不足") . "{$serviceCharge} USDT");
- }
- $wallet = WalletModel::where('member_id', $member_id)->first();
- $changeAmount = bcmul(($amount * -1), $rate, 2);
- $beforeBalance = $wallet->available_balance;
- $afterBalance = bcsub($wallet->available_balance, $rate_rmb_amount, 2);
- $wallet->available_balance = $afterBalance;
- $wallet->save();
- $withdraw = Withdraw::create([
- 'member_id' => $member_id,
- 'amount' => $amount,
- 'service_charge' => $serviceCharge,
- 'to_account' => $real,
- 'address' => $address,
- 'exchange_rate' => $rate,
- 'status' => 0,
- 'after_balance' => $afterBalance
- ]);
- BalanceLogService::addLog($member_id, $changeAmount, $beforeBalance, $afterBalance, '提现', $withdraw->id, '');
- return $this->success($withdraw,'提交成功');
-
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
-
- /**
- * 提现(手动到账): DF001 支付宝转卡; DF002 支付宝转支付宝; DF005数字人民币
- */
- public function payout() {
- try {
- $params = request()->validate([
- 'amount' => ['required', 'numeric', 'min:0.01'],
- 'channel' => ['required', 'string', 'in:DF001,DF002,DF005'],
- 'bank_name' => ['required', 'string'],
- 'account' => ['required', 'string'],
- 'card_no' => ['required', 'string'],
- 'safe_word' => ['required'],
- ]);
- $member_id = request()->user->member_id;
- $user = User::where('member_id', $member_id)->first();
- if (empty($user->payment_password)) throw new Exception(lang("请先设置资金密码"));
- //校验资金密码
- if (!password_verify($params['safe_word'], $user->payment_password)) {
- throw new Exception(lang('资金密码错误'));
- }
- $res = QianBaoWithdrawService::createOrder($member_id, $params['amount'], $params['channel'], $params['bank_name'], $params['account'], $params['card_no']);
- if ($res['code'] == 0) {
- return $this->success($res,'提交成功');
- }
- return $this->error($res['text']);
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 提现(自动到账): DF001 支付宝转卡/DF002 支付宝转支付宝
- */
- public function autoPayout()
- {
- try {
- $params = request()->validate([
- 'amount' => ['required', 'numeric', 'min:0.01'],
- 'channel' => ['required', 'string'],
- 'bank_name' => ['required', 'string'],
- 'account' => ['required', 'string'],
- 'card_no' => ['required', 'string'],
- ]);
- $member_id = request()->user->member_id;
- $res = PaymentOrderService::autoCreatePayout($member_id, $params['amount'], $params['channel'], $params['bank_name'], $params['account'], $params['card_no']);
- if (empty($res['text'])) {
- return $this->success($res,'提交成功');
- }
- return $this->error($res['text']);
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|