Esempio n. 1
0
 public function action()
 {
     if (isset($_POST['action']) && $_POST['action'] == 'change_password') {
         $errors = array();
         $user = $this->wp->wpGetCurrentUser();
         /** @noinspection PhpUndefinedFieldInspection */
         if (!$this->wp->wpCheckPassword($_POST['password'], $user->user_pass, $user->ID)) {
             $errors[] = __('Current password is invalid.', 'jigoshop');
         }
         if (empty($_POST['new-password'])) {
             $errors[] = __('Please enter new password.', 'jigoshop');
         } else {
             if ($_POST['new-password'] != $_POST['new-password-2']) {
                 $errors[] = __('Passwords do not match.', 'jigoshop');
             }
         }
         if (!empty($errors)) {
             $this->messages->addError(join('<br/>', $errors), false);
         } else {
             $this->wp->wpUpdateUser(array('ID' => $user->ID, 'user_pass' => $_POST['new-password']));
             $this->messages->addNotice(__('Password changed.', 'jigoshop'));
             $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT));
         }
     }
 }
Esempio n. 2
0
 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 &amp; 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());
         }
     }
 }
Esempio n. 3
0
 public function action()
 {
     if (isset($_POST['action']) && $_POST['action'] == 'save_address') {
         $customer = $this->customerService->getCurrent();
         switch ($this->wp->getQueryParameter('edit-address')) {
             case 'shipping':
                 $address = $customer->getShippingAddress();
                 break;
             case 'billing':
             default:
                 $address = $customer->getBillingAddress();
                 break;
         }
         $errors = array();
         if ($address instanceof CompanyAddress) {
             $address->setCompany(trim(htmlspecialchars(strip_tags($_POST['address']['company']))));
             $address->setVatNumber(trim(htmlspecialchars(strip_tags($_POST['address']['euvatno']))));
         }
         $address->setPhone(trim(htmlspecialchars(strip_tags($_POST['address']['phone']))));
         $address->setFirstName(trim(htmlspecialchars(strip_tags($_POST['address']['first_name']))));
         $address->setLastName(trim(htmlspecialchars(strip_tags($_POST['address']['last_name']))));
         $address->setAddress(trim(htmlspecialchars(strip_tags($_POST['address']['address']))));
         $address->setCity(trim(htmlspecialchars(strip_tags($_POST['address']['city']))));
         $postcode = trim(htmlspecialchars(strip_tags($_POST['address']['postcode'])));
         if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($postcode, $address->getCountry())) {
             $errors[] = __('Postcode is not valid!', 'jigoshop');
         } else {
             $address->setPostcode($postcode);
         }
         $country = trim(htmlspecialchars(strip_tags($_POST['address']['country'])));
         if (!Country::exists($country)) {
             $errors[] = sprintf(__('Country "%s" does not exists.', 'jigoshop'), $country);
         } else {
             $address->setCountry($country);
         }
         $state = trim(htmlspecialchars(strip_tags($_POST['address']['state'])));
         if (Country::hasStates($address->getCountry()) && !Country::hasState($address->getCountry(), $state)) {
             $errors[] = sprintf(__('Country "%s" does not have state "%s".', 'jigoshop'), Country::getName($address->getCountry()), $state);
         } else {
             $address->setState($state);
         }
         $email = trim(htmlspecialchars(strip_tags($_POST['address']['email'])));
         if (!Validation::isEmail($email)) {
             $errors[] = __('Invalid email address', 'jigoshop');
         } else {
             $address->setEmail($email);
         }
         if (!empty($errors)) {
             $this->messages->addError(join('<br/>', $errors), false);
         } else {
             $this->customerService->save($customer);
             $this->messages->addNotice(__('Address saved.', 'jigoshop'));
             $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT));
         }
     }
 }
Esempio n. 4
0
 public function render()
 {
     $content = $this->wp->getPostField('post_content', $this->options->getPageId(Pages::THANK_YOU));
     /** @var Order $order */
     $order = $this->orderService->find((int) $_REQUEST['order']);
     if ($order->getKey() != $_REQUEST['key']) {
         $this->messages->addError(__('Invalid security key. The order was processed.', 'jigoshop'));
         $this->wp->redirectTo($this->options->getPageId(Pages::SHOP));
     }
     return Render::get('shop/checkout/thanks', array('content' => $content, 'messages' => $this->messages, 'order' => $order, 'showWithTax' => $this->options->get('tax.price_tax') == 'with_tax', 'shopUrl' => $this->wp->getPermalink($this->options->getPageId(Pages::SHOP)), 'cancelUrl' => \Jigoshop\Helper\Order::getCancelLink($order), 'getTaxLabel' => function ($taxClass) use($order) {
         return Tax::getLabel($taxClass, $order);
     }));
 }
Esempio n. 5
0
 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);
     }
 }
Esempio n. 6
0
 /**
  * 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 &amp; 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());
         }
     }
 }
Esempio n. 7
0
 public function processResponse()
 {
     if (isset($_GET['file'])) {
         try {
             $data = explode('.', $_GET['file']);
             if (count($data) != 3) {
                 throw new Exception(__('Invalid download key. Unable to download file.', 'jigoshop'));
             }
             list($key, $id, $itemKey) = $data;
             $order = $this->orderService->find((int) $id);
             /** @var $order Order */
             if ($order->getKey() !== $key) {
                 throw new Exception(__('Invalid security key. Unable to download file.', 'jigoshop'));
             }
             if (!in_array($order->getStatus(), array(Order\Status::COMPLETED, Order\Status::PROCESSING))) {
                 throw new Exception(__('Invalid order.', 'jigoshop'));
             }
             $item = $order->getItem($itemKey);
             if ($item === null) {
                 throw new Exception(__('Product not found.', 'jigoshop'));
             }
             if ($item->getType() !== Downloadable::TYPE) {
                 throw new Exception(__('Invalid file to download.', 'jigoshop'));
             }
             $downloads = $item->getMeta('downloads')->getValue();
             if (!empty($downloads) && $downloads == 0) {
                 throw new Exception(__('Sorry, you have reached your download limit for this file.', 'jigoshop'));
             }
             if ($this->options->get('shopping.login_for_downloads')) {
                 if (!$this->wp->isUserLoggedIn()) {
                     throw new Exception(__('You have to log in before you can download a file.', 'jigoshop'));
                 } else {
                     if ($order->getCustomer()->getId() != $this->wp->getCurrentUserId()) {
                         throw new Exception(__('This is not your download link.', 'jigoshop'));
                     }
                 }
             }
             $file = $item->getMeta('file')->getValue();
             if (!$file) {
                 throw new Exception(__('File not found.', 'jigoshop'));
             }
             if (!empty($downloads)) {
                 $item->getMeta('downloads')->setValue($downloads - 1);
                 $this->orderService->saveItemMeta($item, $item->getMeta('downloads'));
             }
             if (!$this->wp->isMultisite()) {
                 $site_url = $this->wp->siteUrl();
                 $site_url = str_replace('https:', 'http:', $site_url);
                 $file = str_replace($this->wp->getHelpers()->trailingslashit($site_url), ABSPATH, $file);
             } else {
                 $network_url = $this->wp->networkAdminUrl();
                 $network_url = str_replace('https:', 'http:', $network_url);
                 $upload_dir = $this->wp->wpUploadDir();
                 // Try to replace network url
                 $file = str_replace($this->wp->getHelpers()->trailingslashit($network_url), ABSPATH, $file);
                 // Now try to replace upload URL
                 $file = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file);
             }
             $file = $this->wp->applyFilters('jigoshop\\downloadable\\file_path', $file, $itemKey, $order);
             // See if its local or remote
             if (strstr($file, 'http:') || strstr($file, 'https:') || strstr($file, 'ftp:')) {
                 $isRemote = true;
             } else {
                 $isRemote = false;
                 $file = realpath($file);
             }
             // Download the file
             $extension = strtolower(substr(strrchr($file, '.'), 1));
             switch ($extension) {
                 case 'pdf':
                     $type = 'application/pdf';
                     break;
                 case 'exe':
                     $type = 'application/octet-stream';
                     break;
                 case 'zip':
                     $type = 'application/zip';
                     break;
                 case 'doc':
                     $type = 'application/msword';
                     break;
                 case 'xls':
                     $type = 'application/vnd.ms-excel';
                     break;
                 case 'ppt':
                     $type = 'application/vnd.ms-powerpoint';
                     break;
                 case 'gif':
                     $type = 'image/gif';
                     break;
                 case 'png':
                     $type = 'image/png';
                     break;
                 case 'jpe':
                 case 'jpeg':
                 case 'jpg':
                     $type = 'image/jpg';
                     break;
                 default:
                     $type = 'application/force-download';
             }
             $this->wp->doAction('jigoshop\\downloadable\\before_download', $file, $order);
             @session_write_close();
             @set_time_limit(0);
             @ob_end_clean();
             // required for IE, otherwise Content-Disposition may be ignored
             if (ini_get('zlib.output_compression')) {
                 ini_set('zlib.output_compression', 'Off');
             }
             header('Pragma: no-cache');
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Robots: none');
             header('Content-Type: ' . $type);
             header('Content-Description: File Transfer');
             header('Content-Transfer-Encoding: binary');
             if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
                 // workaround for IE filename bug with multiple periods / multiple dots in filename
                 header('Content-Disposition: attachment; filename="' . preg_replace('/\\./', '%2e', basename($file), substr_count(basename($file), '.') - 1) . '";');
             } else {
                 header('Content-Disposition: attachment; filename="' . basename($file) . '";');
             }
             if ($isRemote) {
                 header('Location: ' . $file);
             } else {
                 if (file_exists($file)) {
                     header('Content-Length: ' . filesize($file));
                     readfile($file);
                 } else {
                     throw new Exception(__('File not found.', 'jigoshop'));
                 }
             }
         } catch (Exception $e) {
             $this->messages->addError($e->getMessage());
             $this->wp->redirectTo($this->options->getPageId(Pages::SHOP));
         }
         exit;
     }
 }