/** * 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; }
/** * Displays the cart checkout page built of checkout panes from enabled modules. */ public function checkout() { $cart_config = $this->config('uc_cart.settings'); $items = $this->cartManager->get()->getContents(); if (count($items) == 0 || !$cart_config->get('checkout_enabled')) { return $this->redirect('uc_cart.cart'); } // Send anonymous users to login page when anonymous checkout is disabled. if ($this->currentUser()->isAnonymous() && !$cart_config->get('checkout_anonymous')) { drupal_set_message($this->t('You must login before you can proceed to checkout.')); if ($this->config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { drupal_set_message($this->t('If you do not have an account yet, you should <a href=":url">register now</a>.', [':url' => Url::fromRoute('user.register', [], ['query' => drupal_get_destination()])->toString()])); } return $this->redirect('user.page', [], ['query' => drupal_get_destination()]); } // Load an order from the session, if available. if ($this->session->has('cart_order')) { $order = $this->loadOrder(); if ($order) { // Don't use an existing order if it has changed status or owner, or if // there has been no activity for 10 minutes (to prevent identity theft). if ($order->getStateId() != 'in_checkout' || $this->currentUser()->isAuthenticated() && $this->currentUser()->id() != $order->getOwnerId() || $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { if ($order->getStateId() == 'in_checkout' && $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { // Mark expired orders as abandoned. $order->setStatusId('abandoned')->save(); } unset($order); } } else { // Ghost session. $this->session->remove('cart_order'); drupal_set_message($this->t('Your session has expired or is no longer valid. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } } // Determine whether the form is being submitted or built for the first time. if (isset($_POST['form_id']) && $_POST['form_id'] == 'uc_cart_checkout_form') { // If this is a form submission, make sure the cart order is still valid. if (!isset($order)) { drupal_set_message($this->t('Your session has expired or is no longer valid. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } elseif ($this->session->has('uc_cart_order_rebuild')) { drupal_set_message($this->t('Your shopping cart contents have changed. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } } else { // Prepare the cart order. $rebuild = FALSE; if (!isset($order)) { // Create a new order if necessary. $order = Order::create(array('uid' => $this->currentUser()->id())); $order->save(); $this->session->set('cart_order', $order->id()); $rebuild = TRUE; } elseif ($this->session->has('uc_cart_order_rebuild')) { // Or, if the cart has changed, then remove old products and line items. $result = \Drupal::entityQuery('uc_order_product')->condition('order_id', $order->id())->execute(); if (!empty($result)) { $storage = $this->entityTypeManager()->getStorage('uc_order_product'); $entities = $storage->loadMultiple(array_keys($result)); $storage->delete($entities); } uc_order_delete_line_item($order->id(), TRUE); $rebuild = TRUE; } if ($rebuild) { // Copy the cart contents to the cart order. $order->products = array(); foreach ($items as $item) { $order->products[] = $item->toOrderProduct(); } $this->session->remove('uc_cart_order_rebuild'); } elseif (!uc_order_product_revive($order->products)) { drupal_set_message($this->t('Some of the products in this order are no longer available.'), 'error'); return $this->redirect('uc_cart.cart'); } } $min = $cart_config->get('minimum_subtotal'); if ($min > 0 && $order->getSubtotal() < $min) { drupal_set_message($this->t('The minimum order subtotal for checkout is @min.', ['@min' => uc_currency_format($min)]), 'error'); return $this->redirect('uc_cart.cart'); } // Trigger the "Customer starts checkout" hook and event. $this->moduleHandler()->invokeAll('uc_cart_checkout_start', array($order)); // rules_invoke_event('uc_cart_checkout_start', $order); return $this->formBuilder()->getForm('Drupal\\uc_cart\\Form\\CheckoutForm', $order); }