Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Cart;
  6. use App\Product;
  7. use App\Order;
  8. use Illuminate\Http\Request;
  9.  
  10. use App\Http\Requests;
  11. use Session;
  12. use Auth;
  13. use Stripe\Charge;
  14. use Stripe\Stripe;
  15.  
  16. class ProductController extends Controller
  17. {
  18.     public function getIndex()
  19.     {
  20.         $products = Product::all();
  21.         return view('shop.index', ['products' => $products]);
  22.     }
  23.  
  24.     public function getAddToCart(Request $request, $id)
  25.     {
  26.         $product = Product::find($id);
  27.         $oldCart = Session::has('cart') ? Session::get('cart') : null;
  28.         $cart = new Cart($oldCart);
  29.         $cart->add($product, $product->id);
  30.  
  31.         $request->session()->put('cart', $cart);
  32.         return redirect()->route('product.index');
  33.     }
  34.  
  35.     public function getReduceByOne($id) {
  36.         $oldCart = Session::has('cart') ? Session::get('cart') : null;
  37.         $cart = new Cart($oldCart);
  38.         $cart->reduceByOne($id);
  39.  
  40.         if (count($cart->items) > 0) {
  41.             Session::put('cart', $cart);
  42.         } else {
  43.             Session::forget('cart');
  44.         }
  45.         return redirect()->route('product.shoppingCart');
  46.     }
  47.  
  48.     public function getRemoveItem($id) {
  49.         $oldCart = Session::has('cart') ? Session::get('cart') : null;
  50.         $cart = new Cart($oldCart);
  51.         $cart->removeItem($id);
  52.  
  53.         if (count($cart->items) > 0) {
  54.             Session::put('cart', $cart);
  55.         } else {
  56.             Session::forget('cart');
  57.         }
  58.  
  59.         return redirect()->route('product.shoppingCart');
  60.     }
  61.  
  62.     public function getCart()
  63.     {
  64.         if (!Session::has('cart')) {
  65.             return view('shop.shopping-cart');
  66.         }
  67.         $oldCart = Session::get('cart');
  68.         $cart = new Cart($oldCart);
  69.         return view('shop.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
  70.     }
  71.  
  72.     public function getCheckout()
  73.     {
  74.         if (!Session::has('cart')) {
  75.             return view('shop.shopping-cart');
  76.         }
  77.         $oldCart = Session::get('cart');
  78.         $cart = new Cart($oldCart);
  79.         $total = $cart->totalPrice;
  80.         return view('shop.checkout', ['total' => $total]);
  81.     }
  82.  
  83.     public function postCheckout(Request $request)
  84.     {
  85.         if (!Session::has('cart')) {
  86.             return redirect()->route('shop.shoppingCart');
  87.         }
  88.         $oldCart = Session::get('cart');
  89.         $cart = new Cart($oldCart);
  90.  
  91.         Stripe::setApiKey('sk_test_4ztAJTLefgRHtwXKew5QxxTl'); //
  92.         try {//sk_test_fwmVPdJfpkmwlQRedXec5IxR
  93.             $charge = Charge::create(array(
  94.                 "amount" => $cart->totalPrice * 100,
  95.                 "currency" => "usd",
  96.                 "source" => $request->input('stripeToken'), // obtained with Stripe.js
  97.                 "description" => "Test Charge"
  98.             ));
  99.             $order = new Order();
  100.             $order->cart = serialize($cart);
  101.             $order->address = $request->input('address');
  102.             $order->name = $request->input('name');
  103.             $order->payment_id = $charge->id;
  104.            
  105.             Auth::user()->orders()->save($order);
  106.         } catch (\Exception $e) {
  107.             return redirect()->route('checkout')->with('error', $e->getMessage());
  108.         }
  109.  
  110.         Session::forget('cart');
  111.         return redirect()->route('product.index')->with('success', 'Successfully purchased products!');
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement