public function action() { /** @var Order $order */ $order = $this->orderService->find((int) $this->wp->getQueryParameter('pay')); if ($order->getKey() !== $_GET['key']) { $this->messages->addError(__('Invalid security key. Unable to process order.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT)); } if (isset($_POST['action']) && $_POST['action'] == 'purchase') { try { if ($this->options->get('advanced.pages.terms') > 0 && (!isset($_POST['terms']) || $_POST['terms'] != 'on')) { throw new Exception(__('You need to accept terms & conditions!', 'jigoshop')); } if (!isset($_POST['payment_method'])) { throw new Exception(__('Please select one of available payment methods.', 'jigoshop')); } $payment = $this->paymentService->get($_POST['payment_method']); $order->setPaymentMethod($payment); if (!$payment->isEnabled()) { throw new Exception(__('Selected payment method is not available. Please select another one.', 'jigoshop')); } $this->orderService->save($order); $url = $payment->process($order); // Redirect to thank you page if (empty($url)) { $url = $this->wp->getPermalink($this->wp->applyFilters('jigoshop\\checkout\\redirect_page_id', $this->options->getPageId(Pages::THANK_YOU))); $url = $this->wp->getHelpers()->addQueryArg(array('order' => $order->getId(), 'key' => $order->getKey()), $url); } $this->wp->wpRedirect($url); exit; } catch (Exception $e) { $this->messages->addError($e->getMessage()); } } }
/** * Marks old, but still in "Processing" status orders as completed. * * @internal */ public function completeProcessingOrders() { if ($this->options->get('advanced.automatic_complete')) { $orders = $this->orderService->findOldProcessing(); foreach ($orders as $order) { $this->emailService->suppressNextEmail(); /** @var $order \Jigoshop\Entity\Order */ $order->setStatus(Status::COMPLETED, __('Completed due to order being in processing state for a month or longer.', 'jigoshop')); $this->orderService->save($order); } } }
/** * Change order status. */ public function ajaxChangeStatus() { try { $status = trim($_POST['status']); if (empty($status)) { throw new \Exception('Empty status'); } $orderId = (int) $_POST['orderId']; if ($orderId < 1) { throw new \Exception('Bad order id'); } /** @var Entity $order */ $order = $this->orderService->find($orderId); if ($this->isAvailbleChange($order->getStatus(), $status)) { $order->setStatus($status); $this->orderService->save($order); ob_start(); OrderHelper::renderStatus($order); $html = ob_get_clean(); echo json_encode(array('success' => true, 'html' => $html)); } else { throw new \Exception('Not possible'); } } catch (\Exception $e) { echo json_encode(array('status' => false, 'error' => $e->getMessage())); } exit; }
/** * Ajax action for changing postcode. */ public function ajaxChangePostcode() { try { $post = $this->wp->getPost((int) $_POST['order']); $this->wp->updateGlobalPost($post); /** @var \Jigoshop\Entity\Order $order */ $order = $this->orderService->findForPost($post); if ($order->getId() === null) { throw new Exception(__('Order not found.', 'jigoshop')); } switch ($_POST['type']) { case 'shipping': $address = $order->getCustomer()->getShippingAddress(); break; case 'billing': default: $address = $order->getCustomer()->getBillingAddress(); } if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($_POST['value'], $address->getCountry())) { throw new Exception(__('Invalid postcode.', 'jigoshop')); } $address->setPostcode($_POST['value']); $order = $this->rebuildOrder($order); $this->orderService->save($order); $result = $this->getAjaxResponse($order); } catch (Exception $e) { $result = array('success' => false, 'error' => $e->getMessage()); } echo json_encode($result); exit; }
/** * @param Order $order Order to process payment for. * * @return string URL to redirect to. * @throws Exception On any payment error. */ public function process($order) { $order->setStatus(Order\Status::ON_HOLD, __('Waiting for cheque to arrive.', 'jigoshop')); $this->orderService->save($order); return ''; }
/** * Saves entity to database. * * @param $object EntityInterface Entity to save. */ public function save(EntityInterface $object) { $this->queries = array(); $this->objects[$object->getId()] = $object; $this->service->save($object); }
/** * @param Order $order Order to process payment for. * * @return string URL to redirect to. * @throws Exception On any payment error. */ public function process($order) { $order->setStatus(Order\Status::PROCESSING, __('Payment on delivery.', 'jigoshop')); $this->orderService->save($order); return ''; }
public function action() { if (isset($_REQUEST['action'])) { switch ($_REQUEST['action']) { case 'cancel_order': if ($this->wp->getHelpers()->verifyNonce($_REQUEST['nonce'], 'cancel_order')) { /** @var Order $order */ $order = $this->orderService->find((int) $_REQUEST['id']); if ($order->getKey() != $_REQUEST['key']) { $this->messages->addError(__('Invalid order key.', 'jigoshop')); return; } if ($order->getStatus() != Status::PENDING) { $this->messages->addError(__('Unable to cancel order.', 'jigoshop')); return; } $order->setStatus(Status::CANCELLED); $cart = $this->cartService->createFromOrder($this->cartService->getCartIdForCurrentUser(), $order); $this->orderService->save($order); $this->cartService->save($cart); $this->messages->addNotice(__('The order has been cancelled', 'jigoshop')); } break; case 'update-shipping': $customer = $this->customerService->getCurrent(); $this->updateCustomer($customer); break; case 'checkout': try { $cart = $this->cartService->getCurrent(); // Update quantities $this->updateQuantities($cart); // Update customer (if needed) if ($this->options->get('shipping.calculator')) { $customer = $this->customerService->getCurrent(); $this->updateCustomer($customer); } if (isset($_POST['jigoshop_order']['shipping_method'])) { // Select shipping method $method = $this->shippingService->get($_POST['jigoshop_order']['shipping_method']); $cart->setShippingMethod($method); } if ($cart->getShippingMethod() && !$cart->getShippingMethod()->isEnabled()) { $cart->removeShippingMethod(); $this->messages->addWarning(__('Previous shipping method is unavailable. Please select different one.', 'jigoshop')); } if ($this->options->get('shopping.validate_zip')) { $address = $cart->getCustomer()->getShippingAddress(); if ($address->getPostcode() && !Validation::isPostcode($address->getPostcode(), $address->getCountry())) { throw new Exception(__('Postcode is not valid!', 'jigoshop')); } } do_action('jigoshop\\cart\\before_checkout', $cart); $this->cartService->save($cart); $this->messages->preserveMessages(); $this->wp->redirectTo($this->options->getPageId(Pages::CHECKOUT)); } catch (Exception $e) { $this->messages->addError(sprintf(__('Error occurred while updating cart: %s', 'jigoshop'), $e->getMessage())); } break; case 'update-cart': if (isset($_POST['cart']) && is_array($_POST['cart'])) { try { $cart = $this->cartService->getCurrent(); $this->updateQuantities($cart); $this->cartService->save($cart); $this->messages->addNotice(__('Successfully updated the cart.', 'jigoshop')); } catch (Exception $e) { $this->messages->addError(sprintf(__('Error occurred while updating cart: %s', 'jigoshop'), $e->getMessage())); } } } } if (isset($_GET['action']) && isset($_GET['item']) && $_GET['action'] === 'remove-item' && is_numeric($_GET['item'])) { $cart = $this->cartService->getCurrent(); $cart->removeItem((int) $_GET['item']); $this->cartService->save($cart); $this->messages->addNotice(__('Successfully removed item from cart.', 'jigoshop'), false); } }
/** * Executes actions associated with selected page. */ public function action() { $cart = $this->cartService->getCurrent(); if ($cart->isEmpty()) { $this->messages->addWarning(__('Your cart is empty, please add products before proceeding.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::SHOP)); } if (!$this->isAllowedToEnterCheckout()) { $this->messages->addError(__('You need to log in before processing to checkout.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::CART)); } if (isset($_POST['action']) && $_POST['action'] == 'purchase') { try { $allowRegistration = $this->options->get('shopping.allow_registration'); if ($allowRegistration && !$this->wp->isUserLoggedIn()) { $this->createUserAccount(); } if (!$this->isAllowedToCheckout($cart)) { if ($allowRegistration) { throw new Exception(__('You need either to log in or create account to purchase.', 'jigoshop')); } throw new Exception(__('You need to log in before purchasing.', 'jigoshop')); } if ($this->options->get('advanced.pages.terms') > 0 && (!isset($_POST['terms']) || $_POST['terms'] != 'on')) { throw new Exception(__('You need to accept terms & conditions!', 'jigoshop')); } $this->cartService->validate($cart); $this->customerService->save($cart->getCustomer()); if (!Country::isAllowed($cart->getCustomer()->getBillingAddress()->getCountry())) { $locations = array_map(function ($location) { return Country::getName($location); }, $this->options->get('shopping.selling_locations')); throw new Exception(sprintf(__('This location is not supported, we sell only to %s.'), join(', ', $locations))); } $shipping = $cart->getShippingMethod(); if ($this->isShippingRequired($cart) && (!$shipping || !$shipping->isEnabled())) { throw new Exception(__('Shipping is required for this order. Please select shipping method.', 'jigoshop')); } $payment = $cart->getPaymentMethod(); $isPaymentRequired = $this->isPaymentRequired($cart); $this->wp->doAction('jigoshop\\checkout\\payment', $payment); if ($isPaymentRequired && (!$payment || !$payment->isEnabled())) { throw new Exception(__('Payment is required for this order. Please select payment method.', 'jigoshop')); } $order = $this->orderService->createFromCart($cart); /** @var Order $order */ $order = $this->wp->applyFilters('jigoshop\\checkout\\order', $order); $this->orderService->save($order); $this->cartService->remove($cart); $url = ''; if ($isPaymentRequired) { $url = $payment->process($order); } else { $order->setStatus(\Jigoshop\Helper\Order::getStatusAfterCompletePayment($order)); $this->orderService->save($order); } // Redirect to thank you page if (empty($url)) { $url = $this->wp->getPermalink($this->wp->applyFilters('jigoshop\\checkout\\redirect_page_id', $this->options->getPageId(Pages::THANK_YOU))); $url = $this->wp->getHelpers()->addQueryArg(array('order' => $order->getId(), 'key' => $order->getKey()), $url); } $this->wp->wpRedirect($url); exit; } catch (Exception $e) { $this->messages->addError($e->getMessage()); } } }
/** * @param Order $order Order to process payment for. * * @return string URL to redirect to. * @throws Exception On any payment error. */ public function process($order) { $order->setStatus(Order\Status::ON_HOLD, __('Waiting for the confirmation of the bank transfer.', 'jigoshop')); $this->orderService->save($order); return ''; }