Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Classes\Billing;
  4.  
  5. use App\Exceptions\ChargeFailedException;
  6. use App\Exceptions\CouldNotGetTaxException;
  7. use App\Http\Requests\CheckoutRequest;
  8. use App\Models\Orders\Order;
  9. use App\Models\Product\Product;
  10. use App\Models\ShippingMethod;
  11. use Gloudemans\Shoppingcart\Cart;
  12. use Illuminate\Http\Request;
  13.  
  14. class ShoppingCart {
  15.  
  16.     /**
  17.      * The cart instance.
  18.      *
  19.      * @var Cart
  20.      */
  21.     protected $cart;
  22.  
  23.     /**
  24.      * The TaxJar instance.
  25.      *
  26.      * @var \App\Classes\Billing\TaxJar
  27.      */
  28.     protected $taxjar;
  29.  
  30.     /**
  31.      * The Stripe instance.
  32.      *
  33.      * @var \App\Classes\Billing\Stripe
  34.      */
  35.     protected $stripe;
  36.  
  37.     /**
  38.      * The shipping cost of the order.
  39.      *
  40.      * @var
  41.      */
  42.     protected $shipping;
  43.  
  44.     /**
  45.      * The tax of the order.
  46.      *
  47.      * @var
  48.      */
  49.     protected $tax;
  50.  
  51.     /**
  52.      * The total cost of the order.
  53.      *
  54.      * @var
  55.      */
  56.     protected $total;
  57.  
  58.     /**
  59.      * ShoppingCart constructor.
  60.      *
  61.      * @param Cart $cart
  62.      * @param \App\Classes\Billing\TaxJar $taxjar
  63.      * @param \App\Classes\Billing\Stripe $stripe
  64.      */
  65.     public function __construct(Cart $cart, TaxJar $taxjar, Stripe $stripe) {
  66.  
  67.         $this->cart = $cart;
  68.         $this->taxjar = $taxjar;
  69.         $this->stripe = $stripe;
  70.  
  71.     }
  72.  
  73.     /**
  74.      * Calculate the the tax, shipping and total for the cart content.
  75.      *
  76.      * @param Request $request
  77.      * @throws CouldNotGetTaxException
  78.      */
  79.     public function calculateOrderDetails(Request $request) {
  80.  
  81.         try {
  82.  
  83.             $tax = $this->taxjar->calculateTax($this, $request);
  84.  
  85.         } catch (CouldNotGetTaxException $exception) {
  86.  
  87.             throw $exception;
  88.  
  89.         }
  90.  
  91.         $this->shipping = ShippingMethod::find($request->shipping_method)->price;
  92.         $this->tax = formatAsCents($tax->amount_to_collect);
  93.         $this->total = (formatAsCents($this->subtotal()) + $this->shipping() + $this->tax());
  94.  
  95.     }
  96.  
  97.     /**
  98.      * Place the customer's order and charge them.
  99.      *
  100.      * @param CheckoutRequest $request
  101.      * @return static
  102.      * @throws ChargeFailedException
  103.      * @throws CouldNotGetTaxException
  104.      */
  105.     public function checkout(CheckoutRequest $request) {
  106.  
  107.         try {
  108.  
  109.             $this->calculateOrderDetails($request);
  110.  
  111.         } catch (CouldNotGetTaxException $exception) {
  112.  
  113.             throw $exception;
  114.  
  115.         }
  116.  
  117.         $order = Order::buildFromRequest($request, $this);
  118.  
  119.         try {
  120.  
  121.             $this->stripe->createCharge($order, $request->stripeToken);
  122.  
  123.         } catch (ChargeFailedException $exception) {
  124.  
  125.             throw $exception;
  126.  
  127.         }
  128.  
  129.         $order->saveOrder($this);
  130.  
  131.         $this->destroy();
  132.  
  133.         return $order;
  134.     }
  135.  
  136.     /**
  137.      * Add a product to the shopping cart.
  138.      *
  139.      * @param Product $product
  140.      * @param $quantity
  141.      * @param $options
  142.      * @return \Gloudemans\Shoppingcart\CartItem
  143.      */
  144.     public function add(Product $product, $quantity, $options) {
  145.         return $this->cart->add($product, $quantity, $options);
  146.     }
  147.  
  148.     /**
  149.      * Remove an item from the shopping cart.
  150.      *
  151.      * @param $rowId
  152.      * @return Product
  153.      */
  154.     public function remove($rowId) {
  155.  
  156.         $product = $this->cart->get($rowId)->model;
  157.  
  158.         $this->cart->remove($rowId);
  159.  
  160.         return $product;
  161.     }
  162.  
  163.     /**
  164.      * Get the content of the cart.
  165.      *
  166.      * @return \Illuminate\Support\Collection
  167.      */
  168.     public function content() {
  169.         return $this->cart->content();
  170.     }
  171.  
  172.     /**
  173.      * Get the subtotal (total minus shipping and tax) of the cart.
  174.      *
  175.      * @return float
  176.      */
  177.     public function subtotal() {
  178.         return $this->cart->subtotal();
  179.     }
  180.  
  181.     /**
  182.      * Returns the shipping cost for the order.
  183.      *
  184.      * @return mixed
  185.      */
  186.     public function shipping() {
  187.         return $this->shipping;
  188.     }
  189.  
  190.     /**
  191.      * Returns the total tax for the order.
  192.      *
  193.      * @return mixed
  194.      */
  195.     public function tax() {
  196.         return $this->tax;
  197.     }
  198.  
  199.     /**
  200.      * Returns the order total.
  201.      *
  202.      * @return mixed
  203.      */
  204.     public function total() {
  205.         return $this->total;
  206.     }
  207.  
  208.     /**
  209.      * Destroy the contents of the shopping cart.
  210.      *
  211.      */
  212.     public function destroy() {
  213.         $this->cart->destroy();
  214.     }
  215.  
  216.     /**
  217.      * Returns the number of items in the cart.
  218.      *
  219.      * @return float|int
  220.      */
  221.     public function count() {
  222.         return $this->cart->count();
  223.     }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement