cartRepo = $cartRepository; $this->courierRepo = $courierRepository; $this->addressRepo = $addressRepository; $this->customerRepo = $customerRepository; $this->productRepo = $productRepository; $this->orderRepo = $orderRepository; $this->payPal = new PayPalExpressCheckoutRepository; $this->shippingRepo = $shipping; } /** * Display a listing of the resource. * * @param Request $request * * @return \Illuminate\Http\Response */ public function index(Request $request) { $products = $this->cartRepo->getCartItems(); $customer = $request->user(); $rates = null; $shipment_object_id = null; if (env('ACTIVATE_SHIPPING') == 1) { $shipment = $this->createShippingProcess($customer, $products); if (!is_null($shipment)) { $shipment_object_id = $shipment->object_id; $rates = $shipment->rates; } } // Get payment gateways $paymentGateways = collect(explode(',', config('payees.name')))->transform(function ($name) { return config($name); })->all(); $billingAddress = $customer->addresses()->first(); return view('front.checkout', [ 'customer' => $customer, 'billingAddress' => $billingAddress, 'addresses' => $customer->addresses()->get(), 'products' => $this->cartRepo->getCartItems(), 'subtotal' => $this->cartRepo->getSubTotal(), 'tax' => $this->cartRepo->getTax(), 'total' => $this->cartRepo->getTotal(2), 'payments' => $paymentGateways, 'cartItems' => $this->cartRepo->getCartItemsTransformed(), 'shipment_object_id' => $shipment_object_id, 'rates' => $rates ]); } /** * Checkout the items * * @param CartCheckoutRequest $request * * @return \Illuminate\Http\RedirectResponse * @throws \App\Shop\Addresses\Exceptions\AddressNotFoundException * @throws \App\Shop\Customers\Exceptions\CustomerPaymentChargingErrorException * @codeCoverageIgnore */ public function store(CartCheckoutRequest $request) { $shippingFee = 0; switch ($request->input('payment')) { case 'paypal': return $this->payPal->process($shippingFee, $request); break; case 'stripe': $details = [ 'description' => 'Stripe payment', 'metadata' => $this->cartRepo->getCartItems()->all() ]; $customer = $this->customerRepo->findCustomerById(auth()->id()); $customerRepo = new CustomerRepository($customer); $customerRepo->charge($this->cartRepo->getTotal(2, $shippingFee), $details); break; default: } } /** * Execute the PayPal payment * * @param PayPalCheckoutExecutionRequest $request * @return \Illuminate\Http\RedirectResponse */ public function executePayPalPayment(PayPalCheckoutExecutionRequest $request) { try { $this->payPal->execute($request); $this->cartRepo->clearCart(); return redirect()->route('checkout.success'); } catch (PayPalConnectionException $e) { throw new PaypalRequestError($e->getData()); } catch (Exception $e) { throw new PaypalRequestError($e->getMessage()); } } /** * @param StripeExecutionRequest $request * @return \Stripe\Charge */ public function charge(StripeExecutionRequest $request) { try { $customer = $this->customerRepo->findCustomerById(auth()->id()); $stripeRepo = new StripeRepository($customer); $stripeRepo->execute( $request->all(), Cart::total(), Cart::tax() ); return redirect()->route('checkout.success')->with('message', 'Stripe payment successful!'); } catch (StripeChargingErrorException $e) { Log::info($e->getMessage()); return redirect()->route('checkout.index')->with('error', 'There is a problem processing your request.'); } } /** * Cancel page * * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function cancel(Request $request) { return view('front.checkout-cancel', ['data' => $request->all()]); } /** * Success page * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function success() { return view('front.checkout-success'); } /** * @param Customer $customer * @param Collection $products * * @return mixed */ private function createShippingProcess(Customer $customer, Collection $products) { $customerRepo = new CustomerRepository($customer); if ($customerRepo->findAddresses()->count() > 0 && $products->count() > 0) { $this->shippingRepo->setPickupAddress(); $deliveryAddress = $customerRepo->findAddresses()->first(); $this->shippingRepo->setDeliveryAddress($deliveryAddress); $this->shippingRepo->readyParcel($this->cartRepo->getCartItems()); return $this->shippingRepo->readyShipment(); } } }