PATH:
home
/
lab2454c
/
netxzero.com
/
carbon-credit
/
app
/
Http
/
Controllers
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Project; use App\Models\CartItem; use App\Models\Country; use App\Models\Order; use Stripe\Stripe; use Stripe\Customer; use Stripe\Charge; class CartController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } public function estimates($item='') { $url = "https://api-prod-no-cert.cloverly.com/2021-10/estimates/carbon"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "Authorization: ".env('CLOVERLY_API_KEY'), "Content-Type: application/json", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $data = '{ "weight": { "value": '.$item->carbon_quantity.', "units": "kg" }, "project_match":{ "project_id":"'.$item->project_id.'" } }'; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //for debug only! curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); curl_close($curl); $totalEstimate = json_decode($response, true); return $totalEstimate; //dd($totalEstimate['cost']['in_requested_currency']['total_cost']); } public function cart() { $item = auth()->user()->cartItem; if ($item) { $data['totalEstimate'] = $this->estimates($item); } $data['cartItem'] = $item; return view('frontend.ecommerce.cart', $data); } public function addToCart(Request $request) { $values = $request->validate([ "carbon_quantity" => "required|numeric|min:1", "project_id" => "required|string|exists:projects,project_id", ]); $project = Project::where('project_id', $request->project_id)->first(); if ($values['carbon_quantity'] > $project->available_carbon_in_kg) { $notify[] = ['error', 'Your quantity exceeds stock on hand.The maximum quantity is '.$project->available_carbon_in_kg]; return redirect()->back()->withNotify($notify); } //dd($request); if (!auth()->user()->cartItem) { $cartItem = new CartItem(); $cartItem->fill($values); $cartItem->user()->associate(auth()->user()); $cartItem->save(); $notify[] = ['success', 'Item is added to cart']; return redirect()->route('cart')->withNotify($notify); } else { $cartItem = auth()->user()->cartItem; //dd($cartItem); if ($cartItem->project_id !== $values['project_id']) { $notify[] = ['warning', 'You can only add projects from a single provider in one order. Please complete your order or clear the cart.']; return redirect()->back()->withNotify($notify); } $cartItem->carbon_quantity += $values['carbon_quantity']; $cartItem->save(); $notify[] = ['success', 'CartItem is updated']; return redirect()->route('cart')->withNotify($notify); } } public function updateCart(Request $request) { //dd($request); $changed = false; $values = $request->validate([ "carbon_quantity" => "required|numeric|min:1", ]); $cartItem = auth()->user()->cartItem; $project = Project::where('project_id', $cartItem->project_id)->first(); if ($values['carbon_quantity'] > $project->available_carbon_in_kg) { $notify[] = ['error', 'Your quantity exceeds stock on hand.The maximum quantity is '.$project->available_carbon_in_kg]; return redirect()->back()->withNotify($notify); } $cartItem->carbon_quantity = $values['carbon_quantity']; if ($cartItem->isDirty()) { $cartItem->save(); $changed = true; } if (! $changed) { $notify[] = ['warning', 'No changes done to save']; return redirect()->route('cart')->withNotify($notify); } $notify[] = ['success', 'Item quantity is updated!!']; return redirect()->route('cart')->withNotify($notify); } public function removeFromCart(Request $request) { $cartItem = auth()->user()->cartItem; if($cartItem) { $cartItem->delete(); $notify[] = ['success', 'Item is Removed from Cart']; return redirect()->route('cart')->withNotify($notify); } } public function checkout() { $item = auth()->user()->cartItem; if (auth()->user()->CartCarbonQuantity() <= 0) { $notify[] = ['warning', 'Please Add Carbon to cart before checkout']; return redirect()->route('home')->withNotify($notify); } if ($item) { $data['totalEstimate'] = $this->estimates($item); } if ($data['totalEstimate']['cost']['in_requested_currency']['total_cost'] < 0.5) { $notify[] = ['error', 'Amount must be at least $0.50 usd']; return redirect()->back()->withNotify($notify); } $data['cartItem'] = $item; $data['countries'] = Country::all(); return view('frontend.ecommerce.checkout', $data); } public function submitCheckout(Request $request) { //dd($request->all()); $values = $request->validate([ "first_name" => 'required|alpha|max:100|min:3', "last_name" => 'required|alpha|max:100|min:3', "company_name" => 'nullable|string|max:100', 'country' => 'required|string|max:100', 'address' => 'required|string|max:500', 'city' => 'required|string|max:100|min:2', 'zipcode' => 'required|numeric', 'phone' => 'required|string|min:10|max:30|regex:/^([0-9\s\-\+\(\)]*)$/', 'email' => 'required|string|email|max:100', 'payment_method' => 'required|string|max:100', 'card_holder_name' => 'required|string|max:100', 'stripeToken' => 'required|string|max:200', ]); $user = auth()->user(); $item = $user->cartItem; if ($item) { $totalEstimate = $this->estimates($item); $carbonCost = $totalEstimate['cost']['in_requested_currency']['carbon_cost']; $transactionCost = $totalEstimate['cost']['in_requested_currency']['transaction_cost']; $totalPrice = $totalEstimate['cost']['in_requested_currency']['total_cost']; } $purchaseData = $this->purchases($item); if ($purchaseData['transaction_state'] == 'purchased') { try { Stripe::setApiKey(env('STRIPE_SECRET')); $customer = Customer::create(array( 'email' => $request->email, 'source' => $values['stripeToken'] )); $charge = Charge::create(array( 'customer' => $customer->id, 'amount' => $totalPrice * 100, 'currency' => 'usd', "description" => "Payment for Carbon Offset by " .$user->fullname, )); if ($charge->status == 'succeeded') { //dd('payment Done'); $order = new Order(); $order->fill($values); $order->carbon_quantity = $item->carbon_quantity; $order->carbon_cost = $carbonCost; $order->transaction_cost = $transactionCost; $order->total_cost = $totalPrice; $order->project_id = $item->project_id; $order->transaction_id = $charge->id; $order->status = 1; $order->customer()->associate($user); $order->receipt_url = $purchaseData['receipt_url']; $order->cloverly_transaction_id = $purchaseData['transaction_id']; $order->save(); $item->delete(); $notify[] = ['success', 'Order placed successsfully !']; return redirect()->route('dashboard')->withNotify($notify); } else{ $notify[] = ['error', 'Payment not succesful. Some Error Occured!']; return redirect()->route('cart')->withNotify($notify); } } catch (\Stripe\Exception\CardException $e) { $notify[] = ['error', $e->getError()->message]; return redirect()->back()->withNotify($notify); }catch (\Stripe\Exception\RateLimitException $e) { // Too many requests made to the API too quickly $notify[] = ['error', $e->getError()->message]; return redirect()->back()->withNotify($notify); } catch (\Stripe\Exception\InvalidRequestException $e) { // Invalid parameters were supplied to Stripe's API $notify[] = ['error', $e->getError()->message]; return redirect()->route('cart')->withNotify($notify); } catch (\Stripe\Exception\AuthenticationException $e) { // Authentication with Stripe's API failed // (maybe you changed API keys recently) $notify[] = ['error', $e->getError()->message]; return redirect()->back()->withNotify($notify); } catch (\Stripe\Exception\ApiConnectionException $e) { // Network communication with Stripe failed $notify[] = ['error', $e->getError()->message]; return redirect()->back()->withNotify($notify); } catch (\Stripe\Exception\ApiErrorException $e) { // Display a very generic error to the user $notify[] = ['error', $e->getError()->message]; return redirect()->route('cart')->withNotify($notify); } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $notify[] = ['error', $e->getError()->message]; return redirect()->back()->withNotify($notify); } } else{ $notify[] = ['error', 'Some Internal Error Occured!']; return redirect()->back()->withNotify($notify); } } public function purchases($item='') { $url = "https://api-prod-no-cert.cloverly.com/2021-10/purchases/carbon"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "Authorization: ".env('CLOVERLY_API_KEY'), "Content-Type: application/json", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $data = '{ "weight": { "value": '.$item->carbon_quantity.', "units": "kg" }, "project_match":{ "project_id":"'.$item->project_id.'" } }'; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //for debug only! curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); curl_close($curl); $purchaseData = json_decode($response, true); return $purchaseData; } }
[-] CartController.php
[edit]
[+]
..
[-] Controller.php
[edit]
[+]
Admin
[+]
Auth
[-] SiteController.php
[edit]
[-] HomeController.php
[edit]
[-] CronController.php
[edit]