WalletController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. use App\Models\Wallet;
  6. use App\Models\Shop;
  7. use App\Models\Recharge;
  8. use App\Models\BusinessSetting;
  9. use App\Models\AffiliateLog;
  10. use App\Models\SellerWithdrawRequest;
  11. use App\Models\SellerPackage;
  12. use Auth;
  13. use Session;
  14. use function array_column;
  15. use function back;
  16. use function date;
  17. use function dd;
  18. use function explode;
  19. use function flash;
  20. use function redirect;
  21. use function strtotime;
  22. use function translate;
  23. class WalletController extends Controller
  24. {
  25. public function index() {
  26. $wallets = Wallet::where('user_id', Auth::user()->id)->latest()->paginate(9);
  27. $user = Auth::user();
  28. $userId = $user->id;
  29. $seller_withdraw_requests = SellerWithdrawRequest::where('user_id', $userId)->latest()->paginate(9)
  30. ->appends([ 'opage' => \request()->opage, 'rpage' => \request()->rpage ]);
  31. return view('frontend.user.wallet.index', compact('wallets', 'seller_withdraw_requests'));
  32. }
  33. public function do_money_withdraw_request( Request $request ) {
  34. $user = Auth::user();
  35. /*
  36. if( $request->w_type == 2 )//银行
  37. {
  38. if( empty($user->bank_acc_name) || empty($user->bank_name) || $user->bank_payment_status == 0 || empty($user->bank_acc_no) || empty($user->bank_routing_no))
  39. {
  40. flash(translate('You Should Fill Bank Info'))->error();
  41. return back();
  42. }
  43. }
  44. if( $request->w_type == 3 )//USDT
  45. {
  46. if( empty($user->usdt_address) || empty($user->usdt_type) || $user->usdt_payment_status == 0 )
  47. {
  48. flash(translate('You Should Fill USDT Info'))->error();
  49. return back();
  50. }
  51. }
  52. */
  53. if ( $request->amount > $user->balance )
  54. {
  55. flash(translate('You do not have enough balance to send withdraw request'))->error();
  56. return back();
  57. }
  58. $exits = SellerWithdrawRequest::where('status', '0')->where('type', 1)->where('user_id', $user->id)->count();
  59. if ( $exits !== 0 )
  60. {
  61. #flash(translate('withdraw exited'))->error();
  62. # return back();
  63. }
  64. $seller_withdraw_request = new SellerWithdrawRequest;
  65. $seller_withdraw_request->user_id = $user->id;
  66. $seller_withdraw_request->amount = $request->amount;
  67. $seller_withdraw_request->message = $request->message;
  68. $seller_withdraw_request->status = '0';
  69. $seller_withdraw_request->viewed = '0';
  70. $seller_withdraw_request->w_type = $request->w_type;
  71. $seller_withdraw_request->t_type = 2; //客户
  72. if ( $seller_withdraw_request->save() )
  73. {//扣除余额
  74. $userModel = User::find($user->id);
  75. $userModel->balance = $user->balance - $request->amount;
  76. $userModel->save();
  77. flash(translate('Request has been sent successfully'))->success();
  78. return redirect()->route('wallet.index');
  79. }
  80. else
  81. {
  82. flash(translate('Something went wrong'))->error();
  83. return back();
  84. }
  85. }
  86. public function recharge( Request $request ) {
  87. // $data['payment_method'] = $request->payment_option;
  88. // $request->session()->put('payment_type', 'wallet_payment');
  89. // $request->session()->put('payment_data', $data);
  90. // $request->session()->put('payment_type', 'wallet_payment');
  91. // $request->session()->put('payment_data', $data);
  92. // $decorator = __NAMESPACE__ . '\\Payment\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', $request->payment_option))) . "Controller";
  93. // if ( class_exists($decorator) )
  94. // {
  95. // return ( new $decorator )->pay($request);
  96. // }
  97. $data['money'] = $request->amount;
  98. if($data['money']>1000000 || $data['money']<20){
  99. flash(translate('The top-up amount must not be less than $20'))->error();
  100. return back();
  101. }
  102. $userid=Auth::user()->id;
  103. $orderId='D'.date('YmdHis').rand(10000000,99999999).'-'.$userid;//生成订单号
  104. $address = new Recharge;
  105. $address->userid = $userid;
  106. $address->coinname = 'USDT';
  107. $address->money = $data['money'];
  108. $address->merchant_ref = $orderId;
  109. $address->createtime = time();
  110. $address->save();
  111. $requests=[
  112. 'merchant_no'=>'1270087',//商户号
  113. 'timestamp'=>time(),
  114. 'sign_type'=>'MD5'
  115. ];
  116. $params=[
  117. 'merchant_ref'=>$orderId,
  118. 'product'=>'TRC20Buy',
  119. 'amount'=>sprintf("%.6f",$data['money'])
  120. ];
  121. $params['extra']=[
  122. 'fiat_currency'=>'USD'
  123. ];
  124. $requests['params']=json_encode($params);
  125. $key='63110f813b2619cf47f83c40b36e7314';
  126. $sign=md5($requests['merchant_no'].$requests['params'].$requests['sign_type'].$requests['timestamp'].$key);//生成签名
  127. $requests['sign']=$sign;
  128. $url="https://api.ap-pay.top/api/gateway/pay";
  129. $header = array("Content-Type:application/x-www-form-urlencoded");
  130. $result = $this->curlPost($url, $requests, 5, $header);
  131. $res=json_decode($result,true);
  132. if($res['code']==200){
  133. $params=json_decode($res['params'],true);
  134. header("location:".$params['payurl']);
  135. }
  136. }
  137. public function curlPost($url, $post_data = array(), $timeout = 5, $header = "", $data_type = "") {
  138. $header = empty($header) ? '' : $header;
  139. //支持json数据数据提交
  140. if($data_type == 'json'){
  141. $post_string = json_encode($post_data);
  142. }elseif($data_type == 'array') {
  143. $post_string = $post_data;
  144. }elseif(is_array($post_data)){
  145. $post_string = http_build_query($post_data, '', '&');
  146. }
  147. $ch = curl_init(); // 启动一个CURL会话
  148. curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
  150. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  151. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  152. //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  153. //curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  154. curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
  155. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
  156. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
  157. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  158. //curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  160. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
  161. $result = curl_exec($ch);
  162. // 打印请求的header信息
  163. //$a = curl_getinfo($ch);
  164. //var_dump($a);
  165. curl_close($ch);
  166. return $result;
  167. }
  168. public function wallet_payment_done( $payment_data, $payment_details ) {
  169. $user = Auth::user();
  170. $user->balance = $user->balance + $payment_data['amount'];
  171. $user->save();
  172. $wallet = new Wallet;
  173. $wallet->user_id = $user->id;
  174. $wallet->amount = $payment_data['amount'];
  175. $wallet->payment_method = $payment_data['payment_method'];
  176. $wallet->payment_details = $payment_details;
  177. $wallet->save();
  178. Session::forget('payment_data');
  179. Session::forget('payment_type');
  180. flash(translate('Payment completed'))->success();
  181. return redirect()->route('wallet.index');
  182. }
  183. public function offline_recharge( Request $request ) {
  184. if (Auth::user()->user_type=='admin'){//如果是管理员充值
  185. $user = $user = User::where('id', $request->user_id)->first();
  186. if (!$user) {
  187. flash(translate('Please select one customer'))->warning();
  188. return redirect()->route('admin.customers');
  189. }
  190. $wallet = new Wallet;
  191. $wallet->user_id = $user->id;
  192. $wallet->amount = $request->amount;
  193. $wallet->payment_method = $request->payment_option;
  194. $wallet->payment_details = '';
  195. $wallet->approval = 1;//直接状态为已审核
  196. $wallet->offline_payment = 1;
  197. $wallet->reciept = '';
  198. $wallet->operator_id = Auth::user()->id;
  199. $wallet->type = $request->type ?? 1;
  200. $wallet->save();
  201. $user->balance = $user->balance + $wallet->amount;
  202. $user->save();
  203. flash(translate('Recharge has been done'))->success();
  204. return redirect()->route('customers.index');
  205. }
  206. $wallet = new Wallet;
  207. $wallet->user_id = Auth::user()->id;
  208. $wallet->amount = $request->amount;
  209. $wallet->payment_method = $request->payment_option;
  210. $wallet->payment_details = $request->trx_id;
  211. $wallet->approval = 0;
  212. $wallet->offline_payment = 1;
  213. $wallet->reciept = $request->photo;
  214. $wallet->type = $request->type ?? 1;
  215. $wallet->save();
  216. flash(translate('Offline Recharge has been done. Please wait for response.'))->success();
  217. return redirect()->route('wallet.index');
  218. }
  219. public function offline_recharge_request( Request $request ) {
  220. $name = $request->name ?? '';
  221. $operator = $request->operator ?? '';
  222. $date = $request->date ?? '';
  223. $wallets = Wallet::where('offline_payment', 1);
  224. if ($name){
  225. $users = User::where('name', 'like',"%".$name."%")->get()->toArray();
  226. $ids = array_column($users, 'id');
  227. $wallets = $wallets->whereIn('user_id', $ids);
  228. }
  229. if ($date) {
  230. $wallets = $wallets->whereDate('created_at', '>=', date('Y-m-d', strtotime(explode(" to ", $date)[0])))->whereDate('created_at', '<=', date('Y-m-d', strtotime(explode(" to ", $date)[1])));
  231. }
  232. if ($operator){
  233. if ($operator == 'admin'){
  234. $users = User::where('user_type', 'admin')->get()->toArray();
  235. $ids = array_column($users, 'id');
  236. $wallets = $wallets->whereIn('operator_id', $ids);
  237. }else{
  238. $users = User::where('name', 'like',"%".$operator."%")->get()->toArray();
  239. $ids = array_column($users, 'id');
  240. $wallets = $wallets->whereIn('operator_id', $ids);
  241. }
  242. }
  243. if ( Auth::user()->user_type == 'salesman' )
  244. {
  245. $userIds = User::where('pid', Auth::user()->id)->get()->toArray();
  246. $wallets = Wallet::where('offline_payment', 1)->whereIn('user_id', array_column($userIds, 'id'))->latest()->paginate(10);
  247. return view('manual_payment_methods.wallet_request_salesman', compact('wallets'));
  248. }
  249. $wallets = $wallets->latest()->paginate(10);
  250. //dump($wallets->toArray());dump($name);dump($operator);dump($date);die;
  251. return view('manual_payment_methods.wallet_request', compact('wallets', 'name', 'operator', 'date'));
  252. }
  253. public function offline_recharge_request_by_seller( Request $request ) {
  254. // dd($request->user_id);
  255. $wallets = Wallet::where('offline_payment', 1)->where('user_id', $request->user_id)->get();
  256. return view('manual_payment_methods.wallet_request_by_seller', compact('wallets'));
  257. }
  258. public function updateApproved( Request $request ) {
  259. $wallet = Wallet::findOrFail($request->id);
  260. $wallet->approval = $request->status;
  261. if ( $request->status == 1 )
  262. {
  263. if ( $wallet->type == 2 )
  264. {
  265. #print_r( $wallet->user->shop->toArray());
  266. #$wallet->user->shop->bzj_money+
  267. $shopid = $wallet->user->shop->id;
  268. $shop = Shop::findOrFail($shopid);
  269. $shop->bzj_money = $wallet->user->shop->bzj_money + $wallet->amount;
  270. $shop->save();
  271. }
  272. else
  273. {
  274. //确认用户充值申请
  275. $user = $wallet->user;
  276. $user->balance = $user->balance + $wallet->amount;
  277. $user->total_recharge = $user->total_recharge + $wallet->amount;
  278. $user->save();
  279. if($user->invit_1 != null){ //一级分佣
  280. $invit_1=BusinessSetting::where('type', 'commission_recharge_level_1')->first()->value;
  281. $invit_1=sprintf('%.2f', ($wallet->amount)*$invit_1/100);
  282. User::where('id', $user->invit_1)->increment('balance', $invit_1);
  283. //写入日志
  284. $affiliate_log = new AffiliateLog;
  285. $affiliate_log->user_id = $user->id;
  286. $affiliate_log->referred_by_user = $user->invit_1;
  287. $affiliate_log->amount = $invit_1;
  288. $affiliate_log->order_id = $wallet->payment_details;
  289. $affiliate_log->note ='User:'.$user->name.'('.$user->email.') be recharged successfully $'. $wallet->amount.',First level rebate reward:$'.$invit_1.',Level 1 referrer balance added';
  290. $affiliate_log->affiliate_type = 'recharge_rebate';
  291. $affiliate_log->save();
  292. if($user->invit_2 != null){ //二级分佣
  293. $invit_2=BusinessSetting::where('type', 'commission_recharge_level_2')->first()->value;
  294. $invit_2=sprintf('%.2f', ($wallet->amount)*$invit_2/100);
  295. User::where('id', $user->invit_2)->increment('balance', $invit_2);
  296. //写入日志
  297. $affiliate_log = new AffiliateLog;
  298. $affiliate_log->user_id = $user->id;
  299. $affiliate_log->referred_by_user = $user->invit_2;
  300. $affiliate_log->amount = $invit_2;
  301. $affiliate_log->order_id = $wallet->payment_details;
  302. $affiliate_log->note = 'User:'.$user->name.'('.$user->email.') be recharged successfully $'. $wallet->amount.',Second level rebate reward:$'.$invit_2.',The secondary referrer balance has been added';
  303. $affiliate_log->affiliate_type = 'recharge_rebate';
  304. $affiliate_log->save();
  305. if($user->invit_3 != null){ //三级分佣
  306. $invit_3=BusinessSetting::where('type', 'commission_recharge_level_3')->first()->value;
  307. $invit_3=sprintf('%.2f', ($wallet->amount)*$invit_3/100);
  308. User::where('id', $user->invit_3)->increment('balance', $invit_3);
  309. //写入日志
  310. $affiliate_log = new AffiliateLog;
  311. $affiliate_log->user_id = $user->id;
  312. $affiliate_log->referred_by_user = $user->invit_3;
  313. $affiliate_log->amount = $invit_3;
  314. $affiliate_log->order_id = $wallet->payment_details;
  315. $affiliate_log->note = 'User:'.$user->name.'('.$user->email.') be recharged successfully $'. $wallet->amount.',Three level rebate reward:$'.$invit_3.',The third level referrer balance has been added';
  316. $affiliate_log->affiliate_type = 'recharge_rebate';
  317. $affiliate_log->save();
  318. }
  319. }
  320. }
  321. $wallet->operator_id = Auth::user()->id;
  322. }
  323. }
  324. else
  325. {
  326. if ( $wallet->type == 2 )
  327. {
  328. #print_r( $wallet->user->shop->toArray());
  329. #$wallet->user->shop->bzj_money+
  330. $shopid = $wallet->user->shop->id;
  331. $shop = Shop::findOrFail($shopid);
  332. $shop->bzj_money = $wallet->user->shop->bzj_money - $wallet->amount;
  333. $shop->bzj_money = $shop->bzj_money < 0 ? 0 : $shop->bzj_money;
  334. $shop->save();
  335. }
  336. else
  337. {
  338. //取消用户充值申请
  339. $user = $wallet->user;
  340. $user->balance = $user->balance - $wallet->amount;
  341. $user->total_recharge = $user->total_recharge - $wallet->amount;
  342. $user->save();
  343. if($user->invit_1 != null){ //一级分佣
  344. $invit_1=BusinessSetting::where('type', 'commission_recharge_level_1')->first()->value;
  345. $invit_1=sprintf('%.2f', ($wallet->amount)*$invit_1/100);
  346. User::where('id', $user->invit_1)->decrement('balance', $invit_1);
  347. //写入日志
  348. $affiliate_log = new AffiliateLog;
  349. $affiliate_log->user_id = $user->id;
  350. $affiliate_log->referred_by_user = $user->invit_1;
  351. $affiliate_log->amount = $invit_1;
  352. $affiliate_log->order_id = $wallet->payment_details;
  353. $affiliate_log->note ='User:'.$user->name.'('.$user->email.') Recharge cancellation $'. $wallet->amount.',First level rebate reward:$'.$invit_1.',First-level rebate cancels recovery';
  354. $affiliate_log->affiliate_type = 'recharge_cancellation';
  355. $affiliate_log->save();
  356. if($user->invit_2 != null){ //二级分佣
  357. $invit_2=BusinessSetting::where('type', 'commission_recharge_level_2')->first()->value;
  358. $invit_2=sprintf('%.2f', ($wallet->amount)*$invit_2/100);
  359. User::where('id', $user->invit_2)->decrement('balance', $invit_2);
  360. //写入日志
  361. $affiliate_log = new AffiliateLog;
  362. $affiliate_log->user_id = $user->id;
  363. $affiliate_log->referred_by_user = $user->invit_2;
  364. $affiliate_log->amount = $invit_2;
  365. $affiliate_log->order_id = $wallet->payment_details;
  366. $affiliate_log->note = 'User:'.$user->name.'('.$user->email.') Recharge cancellation $'. $wallet->amount.',Second level rebate reward:$'.$invit_2.',Secondary rebate cancels recovery';
  367. $affiliate_log->affiliate_type = 'recharge_cancellation';
  368. $affiliate_log->save();
  369. if($user->invit_3 != null){ //三级分佣
  370. $invit_3=BusinessSetting::where('type', 'commission_recharge_level_3')->first()->value;
  371. $invit_3=sprintf('%.2f', ($wallet->amount)*$invit_3/100);
  372. User::where('id', $user->invit_3)->decrement('balance', $invit_3);
  373. //写入日志
  374. $affiliate_log = new AffiliateLog;
  375. $affiliate_log->user_id = $user->id;
  376. $affiliate_log->referred_by_user = $user->invit_3;
  377. $affiliate_log->amount = $invit_3;
  378. $affiliate_log->order_id = $wallet->payment_details;
  379. $affiliate_log->note = 'User:'.$user->name.'('.$user->email.') Recharge cancellation $'. $wallet->amount.',Three level rebate reward:$'.$invit_3.',The third level rebate is cancelled';
  380. $affiliate_log->affiliate_type = 'recharge_cancellation';
  381. $affiliate_log->save();
  382. }
  383. }
  384. }
  385. $wallet->operator_id = Auth::user()->id;
  386. }
  387. }
  388. $total_recharge = $user->total_recharge;
  389. error_log('=='.$total_recharge, 3, '/www/wwwroot/www.easybuyjp.shop/log.txt');
  390. if($user->user_type == 'seller'){
  391. $seller_packages = SellerPackage::all();
  392. $package_id = 3;
  393. foreach ($seller_packages as $seller_package) {
  394. if($total_recharge >= $seller_package->amount){
  395. error_log('=='.$seller_package->id, 3, '/www/wwwroot/www.easybuyjp.shop/log.txt');
  396. $package_id = $seller_package->id;
  397. }
  398. }
  399. error_log('=='.$package_id, 3, '/www/wwwroot/www.easybuyjp.shop/log.txt');
  400. $seller = $wallet->user->shop;
  401. $seller->seller_package_id = $package_id;
  402. $seller->product_upload_limit = 100000;
  403. $duration = 360;
  404. $seller->package_invalid_at = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $duration . 'days'));
  405. $seller->save();
  406. }
  407. if ( $wallet->save() )
  408. {
  409. return 1;
  410. }
  411. return 0;
  412. }
  413. public function updateApproved345( Request $request ) {
  414. $wallet = Wallet::findOrFail($request->id);
  415. $wallet->approval = $request->status;
  416. if ( $request->status == 1 )
  417. {
  418. $user = $wallet->user;
  419. $user->balance = $user->balance + $wallet->amount;
  420. $user->save();
  421. }
  422. else
  423. {
  424. $user = $wallet->user;
  425. $user->balance = $user->balance - $wallet->amount;
  426. $user->save();
  427. }
  428. if ( $wallet->save() )
  429. {
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. }