Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Classes\Billing;
- use App\Exceptions\ChargeFailedException;
- use App\Exceptions\CouldNotGetTaxException;
- use App\Http\Requests\CheckoutRequest;
- use App\Models\Orders\Order;
- use App\Models\Product\Product;
- use App\Models\ShippingMethod;
- use Gloudemans\Shoppingcart\Cart;
- use Illuminate\Http\Request;
- class ShoppingCart {
- /**
- * The cart instance.
- *
- * @var Cart
- */
- protected $cart;
- /**
- * The TaxJar instance.
- *
- * @var \App\Classes\Billing\TaxJar
- */
- protected $taxjar;
- /**
- * The Stripe instance.
- *
- * @var \App\Classes\Billing\Stripe
- */
- protected $stripe;
- /**
- * The shipping cost of the order.
- *
- * @var
- */
- protected $shipping;
- /**
- * The tax of the order.
- *
- * @var
- */
- protected $tax;
- /**
- * The total cost of the order.
- *
- * @var
- */
- protected $total;
- /**
- * ShoppingCart constructor.
- *
- * @param Cart $cart
- * @param \App\Classes\Billing\TaxJar $taxjar
- * @param \App\Classes\Billing\Stripe $stripe
- */
- public function __construct(Cart $cart, TaxJar $taxjar, Stripe $stripe) {
- $this->cart = $cart;
- $this->taxjar = $taxjar;
- $this->stripe = $stripe;
- }
- /**
- * Calculate the the tax, shipping and total for the cart content.
- *
- * @param Request $request
- * @throws CouldNotGetTaxException
- */
- public function calculateOrderDetails(Request $request) {
- try {
- $tax = $this->taxjar->calculateTax($this, $request);
- } catch (CouldNotGetTaxException $exception) {
- throw $exception;
- }
- $this->shipping = ShippingMethod::find($request->shipping_method)->price;
- $this->tax = formatAsCents($tax->amount_to_collect);
- $this->total = (formatAsCents($this->subtotal()) + $this->shipping() + $this->tax());
- }
- /**
- * Place the customer's order and charge them.
- *
- * @param CheckoutRequest $request
- * @return static
- * @throws ChargeFailedException
- * @throws CouldNotGetTaxException
- */
- public function checkout(CheckoutRequest $request) {
- try {
- $this->calculateOrderDetails($request);
- } catch (CouldNotGetTaxException $exception) {
- throw $exception;
- }
- $order = Order::buildFromRequest($request, $this);
- try {
- $this->stripe->createCharge($order, $request->stripeToken);
- } catch (ChargeFailedException $exception) {
- throw $exception;
- }
- $order->saveOrder($this);
- $this->destroy();
- return $order;
- }
- /**
- * Add a product to the shopping cart.
- *
- * @param Product $product
- * @param $quantity
- * @param $options
- * @return \Gloudemans\Shoppingcart\CartItem
- */
- public function add(Product $product, $quantity, $options) {
- return $this->cart->add($product, $quantity, $options);
- }
- /**
- * Remove an item from the shopping cart.
- *
- * @param $rowId
- * @return Product
- */
- public function remove($rowId) {
- $product = $this->cart->get($rowId)->model;
- $this->cart->remove($rowId);
- return $product;
- }
- /**
- * Get the content of the cart.
- *
- * @return \Illuminate\Support\Collection
- */
- public function content() {
- return $this->cart->content();
- }
- /**
- * Get the subtotal (total minus shipping and tax) of the cart.
- *
- * @return float
- */
- public function subtotal() {
- return $this->cart->subtotal();
- }
- /**
- * Returns the shipping cost for the order.
- *
- * @return mixed
- */
- public function shipping() {
- return $this->shipping;
- }
- /**
- * Returns the total tax for the order.
- *
- * @return mixed
- */
- public function tax() {
- return $this->tax;
- }
- /**
- * Returns the order total.
- *
- * @return mixed
- */
- public function total() {
- return $this->total;
- }
- /**
- * Destroy the contents of the shopping cart.
- *
- */
- public function destroy() {
- $this->cart->destroy();
- }
- /**
- * Returns the number of items in the cart.
- *
- * @return float|int
- */
- public function count() {
- return $this->cart->count();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement