芝麻web文件管理V1.00
编辑当前文件:/home/qrafawbu/myvyralapp.com/bck/app/Http/Controllers/User/SudoVirtualCardController.php
api = $cardApi; } public function index() { $page_title = "Virtual Card"; $myCards = SudoVirtualCard::where('user_id',auth()->user()->id)->get(); $cardCharge = TransactionSetting::where('slug','virtual_card')->where('status',1)->first(); $transactions = Transaction::auth()->virtualCard()->latest()->take(5)->get(); $cardApi = $this->api; return view('user.sections.virtual-card-sudo.index',compact( 'page_title','myCards','cardApi', 'transactions','cardCharge' )); } public function cardDetails($card_id) { $page_title = "Card Details"; $myCard = SudoVirtualCard::where('card_id',$card_id)->first(); $cardToken = getCardToken($this->api->config->sudo_api_key,$this->api->config->sudo_url,$myCard->card_id); if($cardToken['statusCode'] == 200){ $cardToken = $cardToken['data']['token']; }else{ $cardToken = ''; } $api_mode = $this->api->config->sudo_mode; $api_vault_id = $this->api->config->sudo_vault_id; return view('user.sections.virtual-card-sudo.details',compact('page_title','myCard','cardToken','api_mode','api_vault_id')); } public function makeDefaultOrRemove(Request $request) { $validated = Validator::make($request->all(),[ 'target' => "required|numeric", ])->validate(); $user = auth()->user(); $targetCard = SudoVirtualCard::where('id',$validated['target'])->where('user_id',$user->id)->first(); $withOutTargetCards = SudoVirtualCard::where('id','!=',$validated['target'])->where('user_id',$user->id)->get(); try{ $targetCard->update([ 'is_default' => $targetCard->is_default ? 0 : 1, ]); if(isset( $withOutTargetCards)){ foreach( $withOutTargetCards as $card){ $card->is_default = false; $card->save(); } } }catch(Exception $e) { return back()->with(['error' => ['Something went wrong! Please try again']]); } return back()->with(['success' => ['Status Updated Successfully!']]); } public function cardTransaction($card_id) { $page_title = "Virtual Card Transaction "; $user = auth()->user(); $card = SudoVirtualCard::where('user_id',$user->id)->where('card_id', $card_id)->first(); $card_truns = getCardTransactions($this->api->config->sudo_api_key,$this->api->config->sudo_url,$card->card_id); return view('user.sections.virtual-card-sudo.trx',compact('page_title','card','card_truns')); } public function cardBlockUnBlock(Request $request) { $validator = Validator::make($request->all(),[ 'status' => 'required|boolean', 'data_target' => 'required|string', ]); if ($validator->stopOnFirstFailure()->fails()) { $error = ['error' => $validator->errors()]; return Response::error($error,null,400); } $validated = $validator->safe()->all(); if($request->status == 1 ){ $card = SudoVirtualCard::where('id',$request->data_target)->where('status',1)->first(); $status = 'inactive'; if(!$card){ $error = ['error' => ['Something is wrong in your card']]; return Response::error($error,null,404); } $result = cardUpdate($this->api->config->sudo_api_key,$this->api->config->sudo_url,$card->card_id,$status); if(isset($result['statusCode'])){ if($result['statusCode'] == 200){ $card->status = false; $card->save(); $success = ['success' => [' Card block successfully']]; return Response::success($success,null,200); }elseif($result['statusCode'] != 200){ $success = ['error' => [$result['message']??"Something is wrong"]]; return Response::success($success,null,200); } } }else{ $card = SudoVirtualCard::where('id',$request->data_target)->where('status',0)->first(); $status = 'active'; if(!$card){ $error = ['error' => ['Something is wrong in your card']]; return Response::error($error,null,404); } $result = cardUpdate($this->api->config->sudo_api_key,$this->api->config->sudo_url,$card->card_id,$status); if(isset($result['statusCode'])){ if($result['statusCode'] == 200){ $card->status = true; $card->save(); $success = ['success' => [' Card unblock successfully']]; return Response::success($success,null,200); }elseif($result['statusCode'] != 200){ $success = ['error' => [$result['message']??"Something is wrong"]]; return Response::success($success,null,200); } } } } public function cardBuy(Request $request) { $request->validate([ 'card_amount' => 'required|numeric|gt:0', ]); $basic_setting = BasicSettings::first(); $user = auth()->user(); if($basic_setting->kyc_verification){ if( $user->kyc_verified == 0){ return redirect()->route('user.profile.index')->with(['error' => ['Please submit kyc information']]); }elseif($user->kyc_verified == 2){ return redirect()->route('user.profile.index')->with(['error' => ['Please wait before admin approved your kyc information']]); }elseif($user->kyc_verified == 3){ return redirect()->route('user.profile.index')->with(['error' => ['Admin rejected your kyc information, Please re-submit again']]); } } $amount = $request->card_amount; $wallet = UserWallet::where('user_id',$user->id)->first(); if(!$wallet){ return back()->with(['error' => ['Wallet not found']]); } $cardCharge = TransactionSetting::where('slug','virtual_card')->where('status',1)->first(); $baseCurrency = Currency::default(); $rate = $baseCurrency->rate; if(!$baseCurrency){ return back()->with(['error' => ['Default currency not setup yet']]); } $minLimit = $cardCharge->min_limit * $rate; $maxLimit = $cardCharge->max_limit * $rate; if($amount < $minLimit || $amount > $maxLimit) { return back()->with(['error' => ['Please follow the transaction limit']]); } //charge calculations $fixedCharge = $cardCharge->fixed_charge * $rate; $percent_charge = ($amount / 100) * $cardCharge->percent_charge; $total_charge = $fixedCharge + $percent_charge; $payable = $total_charge + $amount; if($payable > $wallet->balance ){ return back()->with(['error' => ['Sorry, insufficient balance']]); } $currency = $baseCurrency->code; $funding_sources = get_funding_source( $this->api->config->sudo_api_key,$this->api->config->sudo_url); if(isset( $funding_sources['statusCode'])){ if($funding_sources['statusCode'] == 403){ return back()->with(['error' => [$funding_sources['message']]]); }elseif($funding_sources['statusCode'] == 404){ return back()->with(['error' => [$funding_sources['message']]]); } } $supported_currency = ['USD','NGN']; if( !in_array($currency,$supported_currency??[])){ return back()->with(['error' => [$currency." Currency doesn't supported for creating virtual card, Please contact"]]); } $bankCode = $funding_sources['data'][0]['_id']??''; $sudo_accounts = get_sudo_accounts( $this->api->config->sudo_api_key,$this->api->config->sudo_url); $filteredArray = array_filter($sudo_accounts, function($item) use ($currency) { return $item['currency'] === $currency; }); $matchingElements = array_values($filteredArray); $debitAccountId= $matchingElements[0]['_id']??""; if( $debitAccountId == ""){ //create debit account if( $user->sudo_account == null){ //create account $store_account = create_sudo_account($this->api->config->sudo_api_key,$this->api->config->sudo_url, $currency); if( isset($store_account['error'])){ return back()->with(['error' => ["Have't any debit account this currency, Please contact with owner"]]); } $user->sudo_account = (object)$store_account['data']; $user->save(); } }else{ $user->sudo_account = (object)$matchingElements[0]; $user->save(); } $debitAccountId = $user->sudo_account->_id??''; $issuerCountry = ''; if(get_default_currency_code() == "NGN"){ $issuerCountry = "NGA"; }elseif(get_default_currency_code() === "USD"){ $issuerCountry = "USA"; } //check sudo customer have or not if( $user->sudo_customer == null){ //create customer $store_customer = create_sudo_customer($this->api->config->sudo_api_key,$this->api->config->sudo_url,$user); if( isset($store_customer['error'])){ return back()->with(['error' => ["Customer doesn't created properly,Contact with owner"]]); } $user->sudo_customer = (object)$store_customer['data']; $user->save(); $customerId = $user->sudo_customer->_id; }else{ $customerId = $user->sudo_customer->_id; } //create card now $created_card = create_virtual_card($this->api->config->sudo_api_key,$this->api->config->sudo_url, $customerId, $currency,$bankCode, $debitAccountId, $issuerCountry ); if(isset($created_card['statusCode'])){ if($created_card['statusCode'] == 400){ return back()->with(['error' => [$created_card['message']]]); } } if($created_card['statusCode'] = 200){ $card_info = (object)$created_card['data']; $v_card = new SudoVirtualCard(); $v_card->user_id = $user->id; $v_card->name = $user->fullname; $v_card->card_id = $card_info->_id; $v_card->business_id = $card_info->business; $v_card->customer = $card_info->customer; $v_card->account = $card_info->account; $v_card->fundingSource = $card_info->fundingSource; $v_card->type = $card_info->type; $v_card->brand = $card_info->brand; $v_card->currency = $card_info->currency; $v_card->amount = $card_info->balance; $v_card->charge = $total_charge; $v_card->maskedPan = $card_info->maskedPan; $v_card->last4 = $card_info->last4; $v_card->expiryMonth = $card_info->expiryMonth; $v_card->expiryYear = $card_info->expiryYear; $v_card->status = true; $v_card->isDeleted = $card_info->isDeleted; $v_card->billingAddress = $card_info->billingAddress; $v_card->save(); $trx_id = 'CB'.getTrxNum(); try{ $sender = $this->insertCadrBuy( $trx_id,$user,$wallet,$amount, $v_card ,$payable); $this->insertBuyCardCharge( $fixedCharge,$percent_charge, $total_charge,$user,$sender,$v_card->maskedPan); if( $basic_setting->email_notification == true){ $notifyDataSender = [ 'trx_id' => $trx_id, 'title' => "Virtual Card (Buy Card)", 'request_amount' => getAmount($amount,4).' '.get_default_currency_code(), 'payable' => getAmount($payable,4).' ' .get_default_currency_code(), 'charges' => getAmount( $total_charge, 2).' ' .get_default_currency_code(), 'card_amount' => getAmount( $v_card->amount, 2).' ' .get_default_currency_code(), 'card_pan' => $v_card->maskedPan, 'status' => "Success", ]; $user->notify(new CreateMail($user,(object)$notifyDataSender)); } return redirect()->route("user.sudo.virtual.card.index")->with(['success' => ['Card Successfully Buy']]); }catch(Exception $e){ return back()->with(['error' => ["The email cannot be sent as the recipient's email address is invalid."]]); } } } //card buy helper public function insertCadrBuy( $trx_id,$user,$wallet,$amount, $v_card ,$payable) { $trx_id = $trx_id; $authWallet = $wallet; $afterCharge = ($authWallet->balance - $payable); $details =[ 'card_info' => $v_card??'' ]; DB::beginTransaction(); try{ $id = DB::table("transactions")->insertGetId([ 'user_id' => $user->id, 'user_wallet_id' => $authWallet->id, 'payment_gateway_currency_id' => null, 'type' => PaymentGatewayConst::VIRTUALCARD, 'trx_id' => $trx_id, 'request_amount' => $amount, 'payable' => $payable, 'available_balance' => $afterCharge, 'remark' => ucwords(remove_speacial_char(PaymentGatewayConst::CARDBUY," ")), 'details' => json_encode($details), 'attribute' =>PaymentGatewayConst::RECEIVED, 'status' => true, 'created_at' => now(), ]); $this->updateSenderWalletBalance($authWallet,$afterCharge); DB::commit(); }catch(Exception $e) { DB::rollBack(); throw new Exception($e->getMessage()); } return $id; } public function insertBuyCardCharge($fixedCharge,$percent_charge, $total_charge,$user,$id,$masked_card) { DB::beginTransaction(); try{ DB::table('transaction_charges')->insert([ 'transaction_id' => $id, 'percent_charge' => $percent_charge, 'fixed_charge' =>$fixedCharge, 'total_charge' =>$total_charge, 'created_at' => now(), ]); DB::commit(); //notification $notification_content = [ 'title' =>"Buy Card ", 'message' => "Buy card successful ".$masked_card, 'image' => get_image($user->image,'user-profile'), ]; UserNotification::create([ 'type' => NotificationConst::CARD_BUY, 'user_id' => $user->id, 'message' => $notification_content, ]); //Push Notifications event(new UserNotificationEvent($notification_content,$user)); send_push_notification(["user-".$user->id],[ 'title' => $notification_content['title'], 'body' => $notification_content['message'], 'icon' => $notification_content['image'], ]); //admin notification $notification_content['title'] = 'Buy Card Successful '.$masked_card.' Successful ('.$user->username.')'; AdminNotification::create([ 'type' => NotificationConst::CARD_BUY, 'admin_id' => 1, 'message' => $notification_content, ]); DB::commit(); }catch(Exception $e) { DB::rollBack(); throw new Exception($e->getMessage()); } } //update user balance public function updateSenderWalletBalance($authWalle,$afterCharge) { $authWalle->update([ 'balance' => $afterCharge, ]); } }