/**
  * @return Customer
  * @throws CustomerNotFoundException
  */
 public function find()
 {
     try {
         $data = ['token' => Session::get('token')];
         $apiCustomer = VendirunApi::makeRequest('customer/find', $data)->getData();
         $customerFactory = new CustomerFactory();
         $customer = $customerFactory->make($apiCustomer->id, $apiCustomer->fullname, $apiCustomer->primary_email);
         $customer->setCompanyName($apiCustomer->organisation_name);
         $customer->setJobRole($apiCustomer->jobrole);
         foreach ($apiCustomer->addresses as $address) {
             $customer->addAddress(new Address(['id' => $address->id, 'address1' => $address->address1, 'address2' => $address->address2, 'address3' => $address->address3, 'city' => $address->city, 'state' => $address->state, 'postcode' => $address->postcode, 'countryId' => $address->country_id]));
         }
         return $customer;
     } catch (FailResponseException $e) {
         Session::remove('token');
         throw new CustomerNotFoundException('Customer is not logged in');
     }
 }
 /**
  * Process Contact form
  * @param IlRequest $request
  * @param CustomerRepository $customerRepository
  * @return mixed
  */
 public function processContactForm(IlRequest $request, CustomerRepository $customerRepository)
 {
     $this->validate($request, ['email' => 'required']);
     // we will always make a new customer on contact forms, in case the form values are wildly different from the logged in user's details
     $customerFactory = new CustomerFactory();
     $customer = $customerFactory->make(null, Request::get('fullname', null), Request::get('email'));
     $customer->setPrimaryTelephone(Request::get('telephone', null));
     $customerRepository->save($customer);
     if (Request::has('propertyId')) {
         VendirunApi::makeRequest('property/addToFavourite', ['customer_id' => $customer->getId(), 'property_id' => Request::get('propertyId')]);
         Cache::forget('favourites-' . Session::get('token'));
     }
     $note = '';
     if (Request::has('property')) {
         $note .= "<strong>Property Name: " . Request::get('property') . '</strong><br>';
     }
     if (Request::has('product')) {
         $note .= "<strong>Product Name: " . Request::get('product') . '</strong><br>';
     }
     if (Request::has('message')) {
         $note .= nl2br(Request::get('message', ''));
     }
     if ($note) {
         VendirunApi::makeRequest('customer/addNote', ['customer_id' => $customer->getId(), 'note' => $note]);
     }
     $formId = Request::get('formId', 'Website Form');
     VendirunApi::makeRequest('customer/registerFormCompletion', ['customer_id' => $customer->getId(), 'new_customer' => 1, 'data' => json_encode(Request::all()), 'form_id' => $formId]);
     switch ($formId) {
         case 'Newsletter Signup':
             Session::flash('vendirun-alert-success', trans('vendirun::forms.newsletterThanks'));
             break;
         default:
             Session::flash('vendirun-alert-success', trans('vendirun::forms.contactThanks'));
     }
     return Redirect::back();
 }
 /**
  * @param OrderRequest $request
  * @param CartRepository $cartRepository
  * @param OrderRepository $orderRepository
  * @param CustomerRepository $customerRepository
  * @param CartValuesTransformer $cartValuesTransformer
  * @param CartItemValuesTransformer $cartItemValuesTransformer
  * @return mixed
  */
 public function process(OrderRequest $request, CartRepository $cartRepository, OrderRepository $orderRepository, CustomerRepository $customerRepository, CartValuesTransformer $cartValuesTransformer, CartItemValuesTransformer $cartItemValuesTransformer)
 {
     if (Request::has('recalculateShipping')) {
         return $this->recalculateShipping($customerRepository);
     }
     if (Request::has('orderId')) {
         return $this->processExistingOrder($orderRepository, Request::get('orderId'));
     }
     // construct the customer
     $customerFactory = new CustomerFactory();
     try {
         $customer = $customerRepository->find();
         $customer->setName(Name::fromFullName(Request::get('fullName')));
         $customer->setPrimaryEmail(Request::get('emailAddress'));
     } catch (CustomerNotFoundException $e) {
         $customer = $customerFactory->make(null, Request::get('fullName'), Request::get('emailAddress'));
     }
     // get country ID from the shipping address
     $shippingAddressId = $request->get('shippingaddressId');
     $shippingAddress = $customer->getAddressFromAddressId($shippingAddressId);
     $countryId = $shippingAddress ? $shippingAddress->getCountryId() : $request->get('shippingcountryId');
     // construct the cart
     $cart = $cartRepository->find($countryId, Request::get('shippingType', ''));
     // if cart is empty go back to main checkout page
     if ($cart->count() == 0) {
         return Redirect::back()->with('paymentError', 'No items in your cart')->withInput();
     }
     // convert cart to order
     $orderFactory = new OrderFactory();
     $order = $orderFactory->fromCart($cart, $customer, $cartValuesTransformer, $cartItemValuesTransformer, Request::all());
     // persist the order
     if (!($order = $orderRepository->save($order))) {
         return Redirect::back()->with('paymentError', 'Payment Has NOT Been Taken - unable to create order, please try again');
     }
     Session::set('orderOneTimeToken', $order->getOneTimeToken());
     return $order->getTotalPrice() > 0 ? $this->takePayment($orderRepository, $order) : $this->noPaymentNecessary($order);
 }