/**
  * Displays the cart view page.
  *
  * Show the products in the cart with a form to adjust cart contents or go to
  * checkout.
  */
 public function listing()
 {
     // Load the array of shopping cart items.
     $cart = $this->cartManager->get();
     $items = $cart->getContents();
     // Display the empty cart page if there are no items in the cart.
     if (empty($items)) {
         $build = ['#theme' => 'uc_cart_empty'];
         \Drupal::service('renderer')->addCacheableDependency($build, $cart);
         return $build;
     }
     return $this->formBuilder()->getForm('Drupal\\uc_cart\\Form\\CartForm', $cart);
 }
 /**
  * Finalizes 2Checkout transaction.
  *
  * @param int $cart_id
  *   The cart identifier.
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request of the page.
  */
 public function complete($cart_id = 0, Request $request)
 {
     $cart_config = $this->config('uc_cart.settings');
     $module_config = $this->config('uc_2checkout.settings');
     \Drupal::logger('uc_2checkout')->notice('Receiving new order notification for order @order_id.', ['@order_id' => SafeMarkup::checkPlain($request->request->get('merchant_order_id'))]);
     $order = Order::load($request->request->get('merchant_order_id'));
     if (!$order || $order->getStateId() != 'in_checkout') {
         return $this->t('An error has occurred during payment.  Please contact us to ensure your order has submitted.');
     }
     $key = $request->request->get('key');
     $order_number = $module_config->get('demo') ? 1 : $request->request->get('order_number');
     $valid = md5($module_config->get('secret_word') . $request->request->get('sid') . $order_number . $request->request->get('total'));
     if (Unicode::strtolower($key) != Unicode::strtolower($valid)) {
         uc_order_comment_save($order->id(), 0, $this->t('Attempted unverified 2Checkout completion for this order.'), 'admin');
         throw new AccessDeniedHttpException();
     }
     if ($request->request->get('demo') == 'Y' xor $module_config->get('demo')) {
         \Drupal::logger('uc_2checkout')->error('The 2Checkout payment for order <a href=":order_url">@order_id</a> demo flag was set to %flag, but the module is set to %mode mode.', array(':order_url' => Url::fromRoute('entity.uc_order.canonical', ['uc_order' => $order->id()])->toString(), '@order_id' => $order->id(), '%flag' => $request->request->get('demo') == 'Y' ? 'Y' : 'N', '%mode' => $module_config->get('demo') ? 'Y' : 'N'));
         if (!$module_config->get('demo')) {
             throw new AccessDeniedHttpException();
         }
     }
     //@todo: Check if this is the right way to save the order
     $order->billing_street1 = $request->request->get('street_address');
     $order->billing_street2 = $request->request->get('street_address2');
     $order->billing_city = $request->request->get('city');
     $order->billing_postal_code = $request->request->get('zip');
     $order->billing_phone = $request->request->get('phone');
     $order->billing_zone = $request->request->get('state');
     $order->billing_country = $request->request->get('country');
     $order->save();
     if (Unicode::strtolower($request->request->get('email')) !== Unicode::strtolower($order->getEmail())) {
         uc_order_comment_save($order->id(), 0, $this->t('Customer used a different e-mail address during payment: @email', ['@email' => SafeMarkup::checkPlain($request->request->get('email'))]), 'admin');
     }
     if ($request->request->get('credit_card_processes') == 'Y' && is_numeric($request->request->get('total'))) {
         $comment = $this->t('Paid by @type, 2Checkout.com order #@order.', ['@type' => $request->request->get('pay_method') == 'CC' ? $this->t('credit card') : $this->t('echeck'), '@order' => SafeMarkup::checkPlain($request->request->get('order_number'))]);
         uc_payment_enter($order->id(), '2Checkout', $request->request->get('total'), 0, NULL, $comment);
     } else {
         drupal_set_message($this->t('Your order will be processed as soon as your payment clears at 2Checkout.com.'));
         uc_order_comment_save($order->id(), 0, $this->t('@type payment is pending approval at 2Checkout.com.', ['@type' => $request->request->get('pay_method') == 'CC' ? $this->t('Credit card') : $this->t('eCheck')]), 'admin');
     }
     // Empty that cart...
     $cart = $this->cartManager->get($cart_id);
     $cart->emptyCart();
     // Add a comment to let sales team know this came in through the site.
     uc_order_comment_save($order->id(), 0, $this->t('Order created through website.'), 'admin');
     $build = $cart->completeSale($order, $cart_config->get('new_customer_login'));
     return $build;
 }
 /**
  * Completes the sale and finishes checkout.
  */
 public function complete()
 {
     if (!$this->session->has('cart_order') || !$this->session->has('uc_checkout_complete_' . $this->session->get('cart_order'))) {
         return $this->redirect('uc_cart.cart');
     }
     $order = $this->loadOrder();
     if (empty($order)) {
         // Display messages to customers and the administrator if the order was lost.
         drupal_set_message($this->t("We're sorry.  An error occurred while processing your order that prevents us from completing it at this time. Please contact us and we will resolve the issue as soon as possible."), 'error');
         $this->logger('uc_cart')->error('An empty order made it to checkout! Cart order ID: @cart_order', ['@cart_order' => $this->session->get('cart_order')]);
         return $this->redirect('uc_cart.cart');
     }
     $cart_config = $this->config('uc_cart.settings');
     $build = $this->cartManager->completeSale($order, $cart_config->get('new_customer_login'));
     $this->session->remove('uc_checkout_complete_' . $this->session->get('cart_order'));
     $this->session->remove('cart_order');
     // Add a comment to let sales team know this came in through the site.
     uc_order_comment_save($order->id(), 0, $this->t('Order created through website.'), 'admin');
     return $build;
 }