Wallet.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\Services\BalanceLogService;
  11. use App\Services\Payment\SanJinService;
  12. use App\Services\PaymentOrderService;
  13. use App\Services\QianBaoWithdrawService;
  14. use App\Services\Payment\QianBaoService;
  15. use App\Services\WithdrawService;
  16. use Illuminate\Validation\ValidationException;
  17. use Exception;
  18. /**
  19. * 充值提现接口
  20. */
  21. class Wallet extends BaseController
  22. {
  23. //获取三斤充值通道(微信、支付宝、扫码充值)
  24. public function getChannel()
  25. {
  26. $data = SanJinService::getChannel();
  27. $product = SanJinService::$PRODUCT;
  28. $list = [];
  29. foreach($data as $k => $v) {
  30. foreach($product as $pv) {
  31. if ($k == $pv['type']) {
  32. $config = $pv;
  33. }
  34. }
  35. $list[] = [
  36. 'label' => lang($v),
  37. 'value' => $k,
  38. 'config' => $config ?? [],
  39. ];
  40. }
  41. return $this->success([
  42. 'list' => $list,
  43. ]);
  44. }
  45. /**
  46. * 创建代收订单
  47. */
  48. public function createPay()
  49. {
  50. try {
  51. $params = request()->validate([
  52. 'amount' => ['required', 'numeric', 'min:0.01'],
  53. 'payment_type' => ['required', 'string'],
  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. $receivingType = ConfigService::getVal("receiving_type");
  78. //自动充值
  79. if ($receivingType == 1) {
  80. $res = WalletService::getRechargeImageAddress($member_id);
  81. $address = $res['address'];
  82. $qrCode = $res['full_path'];
  83. } else {
  84. //手动充值
  85. if ($params['type'] === "TRC20") {
  86. $address = Config::where('field', 'receiving_address')->first()->val;
  87. } elseif ($params['type'] === "ERC20") {
  88. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  89. } else {
  90. return $this->error(lang('充值类型错误'));
  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. // '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. * 提交充值凭证
  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 = '';
  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 = $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. * 获取提现通道
  138. */
  139. public function withdrawChannel()
  140. {
  141. $list = QianBaoService::withdrawChannel();
  142. $data[] = ['label' => 'USDT', 'value' => 'USDT'];
  143. foreach ($list as $key => $item) {
  144. $data[] = ['label' => $item, 'value' => $key];
  145. }
  146. return $this->success($data);
  147. }
  148. public function withdraw()
  149. {
  150. try {
  151. $member_id = request()->user->member_id;
  152. $params = request()->validate([
  153. 'amount' => ['required', 'numeric', 'min:0.01'],
  154. 'address' => ['required', 'string'],
  155. 'safe_word' => ['required'],
  156. ]);
  157. $user = User::where('member_id', $member_id)->first();
  158. if (empty($user->payment_password)) throw new Exception(lang("请先设置资金密码"));
  159. //校验资金密码
  160. if (!password_verify($params['safe_word'], $user->payment_password)) {
  161. throw new Exception(lang('资金密码错误'));
  162. }
  163. $serviceCharge = (new WithdrawService())->serviceCharge;
  164. $amount = $params['amount'];
  165. $address = $params['address'];
  166. $real = bcsub($amount, $serviceCharge, 10);
  167. $real = floatval($real);
  168. if ($amount <= $serviceCharge) {
  169. throw new Exception(lang("提现不能少于") . "{$serviceCharge} USDT");
  170. }
  171. $wallet = WalletModel::where('member_id', $member_id)->first();
  172. $temp = floatval($wallet->available_balance);
  173. // 汇率
  174. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  175. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  176. $rate = bcadd($rate, $exchange_rate_difference, 2);
  177. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  178. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  179. if ($amount > $rate_usdt_amount) {
  180. throw new Exception(lang("余额不足") . "{$serviceCharge} USDT");
  181. }
  182. $wallet = WalletModel::where('member_id', $member_id)->first();
  183. $changeAmount = bcmul(($amount * -1), $rate, 2);
  184. $beforeBalance = $wallet->available_balance;
  185. $afterBalance = bcsub($wallet->available_balance, $rate_rmb_amount, 2);
  186. $wallet->available_balance = $afterBalance;
  187. $wallet->save();
  188. $withdraw = Withdraw::create([
  189. 'member_id' => $member_id,
  190. 'amount' => $amount,
  191. 'service_charge' => $serviceCharge,
  192. 'to_account' => $real,
  193. 'address' => $address,
  194. 'exchange_rate' => $rate,
  195. 'status' => 0,
  196. 'after_balance' => $afterBalance
  197. ]);
  198. BalanceLogService::addLog($member_id, $changeAmount, $beforeBalance, $afterBalance, '提现', $withdraw->id, '');
  199. return $this->success($withdraw,'提交成功');
  200. } catch (ValidationException $e) {
  201. return $this->error($e->validator->errors()->first());
  202. } catch (\Exception $e) {
  203. return $this->error($e->getMessage());
  204. }
  205. }
  206. /**
  207. * 提现(手动到账): DF001 支付宝转卡; DF002 支付宝转支付宝; DF005数字人民币
  208. */
  209. public function payout() {
  210. try {
  211. $params = request()->validate([
  212. 'amount' => ['required', 'numeric', 'min:0.01'],
  213. 'channel' => ['required', 'string', 'in:DF001,DF002,DF005'],
  214. 'bank_name' => ['required', 'string'],
  215. 'account' => ['required', 'string'],
  216. 'card_no' => ['required', 'string'],
  217. 'safe_word' => ['required'],
  218. ]);
  219. $member_id = request()->user->member_id;
  220. $user = User::where('member_id', $member_id)->first();
  221. if (empty($user->payment_password)) throw new Exception(lang("请先设置资金密码"));
  222. //校验资金密码
  223. if (!password_verify($params['safe_word'], $user->payment_password)) {
  224. throw new Exception(lang('资金密码错误'));
  225. }
  226. $res = QianBaoWithdrawService::createOrder($member_id, $params['amount'], $params['channel'], $params['bank_name'], $params['account'], $params['card_no']);
  227. if ($res['code'] == 0) {
  228. return $this->success($res,'提交成功');
  229. }
  230. return $this->error($res['text']);
  231. } catch (ValidationException $e) {
  232. return $this->error($e->validator->errors()->first());
  233. } catch (\Exception $e) {
  234. return $this->error($e->getMessage());
  235. }
  236. }
  237. /**
  238. * 提现(自动到账): DF001 支付宝转卡/DF002 支付宝转支付宝
  239. */
  240. public function autoPayout()
  241. {
  242. try {
  243. $params = request()->validate([
  244. 'amount' => ['required', 'numeric', 'min:0.01'],
  245. 'channel' => ['required', 'string'],
  246. 'bank_name' => ['required', 'string'],
  247. 'account' => ['required', 'string'],
  248. 'card_no' => ['required', 'string'],
  249. ]);
  250. $member_id = request()->user->member_id;
  251. $res = PaymentOrderService::autoCreatePayout($member_id, $params['amount'], $params['channel'], $params['bank_name'], $params['account'], $params['card_no']);
  252. if (empty($res['text'])) {
  253. return $this->success($res,'提交成功');
  254. }
  255. return $this->error($res['text']);
  256. } catch (ValidationException $e) {
  257. return $this->error($e->validator->errors()->first());
  258. } catch (Exception $e) {
  259. return $this->error($e->getMessage());
  260. }
  261. }
  262. }