/**
  * @param $order
  * @return Order
  */
 public function fromApi($order)
 {
     $billingAddress = new Address(['id' => $order->billing_address->id, 'address1' => $order->billing_address->address1, 'address2' => $order->billing_address->address2, 'address3' => $order->billing_address->address3, 'city' => $order->billing_address->city, 'state' => $order->billing_address->state, 'postcode' => $order->billing_address->postcode, 'countryId' => $order->billing_address->country_id]);
     $shippingAddress = new Address(['id' => $order->shipping_address->id, 'address1' => $order->shipping_address->address1, 'address2' => $order->shipping_address->address2, 'address3' => $order->shipping_address->address3, 'city' => $order->shipping_address->city, 'state' => $order->shipping_address->state, 'postcode' => $order->shipping_address->postcode, 'countryId' => $order->shipping_address->country_id]);
     // customer
     $customer = new Customer($order->customer_id, Name::fromFullName($order->customer_id ? $order->customer->fullname : $order->guest_full_name));
     $customer->setJobRole($order->customer_id ? $order->customer->jobrole : $order->guest_jobrole);
     $customer->setPrimaryEmail($order->customer_id ? $order->customer->primary_email : $order->guest_email);
     $customer->setCompanyName($order->customer_id ? $order->customer->organisation_name : $order->guest_company_name);
     $customer->setTaxNumber($order->customer_id ? $order->customer->tax_number : $order->guest_tax_number);
     // order items
     $items = [];
     foreach ($order->items as $item) {
         $items[] = new OrderItem($item->id, $item->product_variation_id, $item->tax_rate, $item->price, $item->quantity, $item->product_name, $item->product_sku, $item->is_shipping, $item->discount_amount);
     }
     $vrOrder = new Order($customer, $billingAddress, $shippingAddress, $items, null, $order->id, $order->created);
     // add the payments
     foreach ($order->payments as $payment) {
         $vrOrder->addPayment(new Payment($payment->payment_amount, $payment->created, $payment->payment_type, $payment->transaction_data, $payment->id));
     }
     // shipments
     $shipmentFactory = new ShipmentFactory();
     foreach ($order->shipments as $shipment) {
         $vrOrder->addShipment($shipmentFactory->fromApi($shipment, $vrOrder));
     }
     return $vrOrder;
 }
 /**
  * @param null   $id
  * @param string $fullName
  * @param string $primaryEmail
  * @return Customer
  */
 public function make($id = null, $fullName = '', $primaryEmail = '')
 {
     $name = Name::fromFullName($fullName);
     return new Customer($id, $name, $primaryEmail);
 }
 /**
  * @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);
 }