Example #1
0
 /**
  * Place order (for orders with zero balances)
  *
  * @return     void
  */
 public function placeTask()
 {
     // Get the current active trancsaction
     $cart = new CartModelCurrentCart();
     $transaction = $cart->liftTransaction();
     //print_r($transaction); die;
     if (!$transaction) {
         $redirect_url = Route::url('index.php?option=' . 'com_cart');
         App::redirect($redirect_url);
     }
     // get security token (Parameter 0)
     $token = Request::getVar('p0');
     if (!$token || !$cart->verifyToken($token)) {
         die('Error processing your order. Bad security token.');
     }
     // Check if the order total is 0
     if ($transaction->info->tiTotal != 0) {
         die('Cannot process transaction. Order total is not zero.');
     }
     // Check if the transaction's status is pending
     if ($transaction->info->tStatus != 'pending') {
         die('Cannot process transaction. Transaction status is invalid.');
     }
     //print_r($transaction); die;
     if ($this->completeOrder($transaction)) {
         // Get the transaction ID variable name to pull from URL
         $params = Component::params(Request::getVar('option'));
         // Get payment provider
         $paymentGatewayProivder = $params->get('paymentProvider');
         include_once JPATH_COMPONENT . DS . 'lib' . DS . 'payment' . DS . 'PaymentDispatcher.php';
         $verificationVar = PaymentDispatcher::getTransactionIdVerificationVarName($paymentGatewayProivder);
         // redirect to thank you page
         $redirect_url = Route::url('index.php?option=' . 'com_cart') . '/order/complete/' . '?' . $verificationVar . '=' . $token . '-' . $transaction->info->tId;
         App::redirect($redirect_url);
     }
 }
Example #2
0
 /**
  * Confirm step of the checkout. Should be a pass-through page for JS-enabled browsers, requires a form submission to the payment gateway
  *
  * @return     void
  */
 public function confirmTask()
 {
     require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'models' . DS . 'CurrentCart.php';
     $cart = new CartModelCurrentCart();
     $transaction = $cart->liftTransaction();
     if (!$transaction) {
         $cart->redirect('home');
     }
     // Get security token
     $transaction->token = $cart->getToken();
     // Check if there are any steps missing. Redirect if needed
     $nextStep = $cart->getNextCheckoutStep();
     if ($nextStep != 'summary') {
         $cart->redirect($nextStep);
     }
     // Final step here before payment
     CartModelCart::updateTransactionStatus('awaiting payment', $transaction->info->tId);
     // Generate payment code
     $params = Component::params(Request::getVar('option'));
     $paymentGatewayProivder = $params->get('paymentProvider');
     include_once JPATH_COMPONENT . DS . 'lib' . DS . 'payment' . DS . 'PaymentDispatcher.php';
     $paymentDispatcher = new PaymentDispatcher($paymentGatewayProivder);
     $pay = $paymentDispatcher->getPaymentProvider();
     $pay->setTransactionDetails($transaction);
     $error = false;
     try {
         $paymentCode = $pay->getPaymentCode();
         $this->view->paymentCode = $paymentCode;
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
     if (!empty($error)) {
         $this->view->setError($error);
     }
     $this->view->display();
 }
Example #3
0
 public function addTask()
 {
     $response = new stdClass();
     $response->status = 'ok';
     include_once JPATH_COMPONENT . DS . 'models' . DS . 'cart.php';
     $cart = new CartModelCurrentCart();
     // update cart
     $updateCartRequest = Request::getVar('updateCart', false, 'post');
     $pIds = Request::getVar('pId', false, 'post');
     //print_r($pIds); die;
     // If pIds are posted, convert them to SKUs
     if (!empty($pIds)) {
         $skus = array();
         include_once JPATH_BASE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         foreach ($pIds as $pId => $qty) {
             $product_skus = $warehouse->getProductSkus($pId);
             // must be only one sku to work
             if (sizeof($product_skus) != 1) {
                 // each pId must map to one SKU, otherwise ignored
                 continue;
             }
             $skus[$product_skus[0]] = $qty;
         }
     } else {
         $skus = Request::getVar('skus', false, 'post');
     }
     //print_r($skus); die;
     // Initialize errors array
     $errors = array();
     if ($updateCartRequest && $skus) {
         // Turn off syncing to prevent redundant session update queries
         $cart->setSync(false);
         foreach ($skus as $sId => $qty) {
             try {
                 $cart->update($sId, $qty);
             } catch (Exception $e) {
                 $errors[] = $e->getMessage();
             }
         }
     }
     // add coupon if needed
     $addCouponRequest = Request::getVar('addCouponCode', false, 'post');
     $couponCode = Request::getVar('couponCode', false, 'post');
     if ($addCouponRequest && $couponCode) {
         // Sync cart before pontial coupons applying
         $cart->getCartInfo(true);
         // Initialize errors array
         $errors = array();
         // Add coupon
         try {
             $cart->addCoupon($couponCode);
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
         }
     }
     if (!empty($errors)) {
         $response->status = 'error';
         $response->errors = $errors;
     }
     echo htmlspecialchars(json_encode($response), ENT_NOQUOTES);
     die;
 }
Example #4
0
 /**
  * Display product
  *
  * @param		$pId
  * @return     	void
  */
 public function displayTask()
 {
     $pId = $this->warehouse->productExists(Request::getVar('product', ''));
     if (!$pId) {
         App::abort(404, Lang::txt('COM_STOREFRONT_PRODUCT_NOT_FOUND'));
     }
     $this->view->pId = $pId;
     $this->view->css();
     $this->view->js('product_display.js');
     // A flag whether the item is available for purchase (for any reason, used by the auditors)
     $productAvailable = true;
     $pageMessages = array();
     // Get the cart
     require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'models' . DS . 'CurrentCart.php';
     $cart = new CartModelCurrentCart();
     // POST add to cart request
     $addToCartRequest = Request::getVar('addToCart', false, 'post');
     $options = Request::getVar('og', false, 'post');
     $qty = Request::getInt('qty', 1, 'post');
     if ($addToCartRequest) {
         // Initialize errors array
         $errors = array();
         // Check if passed options/productID map to a SKU
         try {
             $sku = $this->warehouse->mapSku($pId, $options);
             $cart->add($sku, $qty);
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
             $pageMessages[] = array($e->getMessage(), 'error');
         }
         if (!empty($errors)) {
             $this->view->setError($errors);
         } else {
             // prevent resubmitting by refresh
             // If not an ajax call, redirect to cart
             $redirect_url = Route::url('index.php?option=' . 'com_cart');
             App::redirect($redirect_url);
         }
     }
     // Get the product info
     $product = $this->warehouse->getProductInfo($pId);
     $this->view->product = $product;
     // Run the auditor
     require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'helpers' . DS . 'Audit.php';
     $auditor = Audit::getAuditor($product, $cart->getCartInfo()->crtId);
     $auditorResponse = $auditor->audit();
     //print_r($auditor); die;
     if (!empty($auditorResponse) && $auditorResponse->status != 'ok') {
         if ($auditorResponse->status == 'error') {
             // Product is not available for purchase
             $productAvailable = false;
             foreach ($auditorResponse->notices as $notice) {
                 $pageMessages[] = array($notice, 'warning');
             }
         }
     }
     // Get option groups with options and SKUs
     $data = $this->warehouse->getProductOptions($pId);
     if ($data) {
         //throw new Exception(Lang::txt('COM_STOREFRONT_PRODUCT_ERROR'), 404);
         $this->view->options = $data->options;
     }
     //print_r($data); die;
     // Find a price range for the product
     $priceRange = array('high' => 0, 'low' => false);
     /*
     	Find if there is a need to display a product quantity dropdown on the initial view load. It will be only displayed for single SKU that allows multiple items.
     	For multiple SKUs it will be generated by JS (no drop-down for non-JS users, sorry)
     */
     $qtyDropDownMaxVal = 0;
     $inStock = true;
     if (!$data || !count($data->skus)) {
         $inStock = false;
     }
     $this->view->inStock = $inStock;
     if ($data && count($data->skus) == 1) {
         // Set the max value for the dropdown QTY
         // TODO: add it to the SKU table to set on the per SKU level
         $qtyDropDownMaxValLimit = 20;
         // Get the first and the only value
         $sku = array_shift(array_values($data->skus));
         // If no inventory tracking, there is no limit on how many can be purchased
         $qtyDropDownMaxVal = $qtyDropDownMaxValLimit;
         if ($sku['info']->sTrackInventory) {
             $qtyDropDownMaxVal = $sku['info']->sInventory;
         }
         if ($qtyDropDownMaxVal < 1) {
             $qtyDropDownMaxVal = 1;
         } elseif ($qtyDropDownMaxVal > $qtyDropDownMaxValLimit) {
             $qtyDropDownMaxVal = $qtyDropDownMaxValLimit;
         }
         // If the SKU doesn't allow multiple items, set the dropdown to 1
         if (!$sku['info']->sAllowMultiple) {
             $qtyDropDownMaxVal = 1;
         }
     }
     $this->view->qtyDropDown = $qtyDropDownMaxVal;
     if ($data) {
         foreach ($data->skus as $sId => $info) {
             $info = $info['info'];
             if ($info->sPrice > $priceRange['high']) {
                 $priceRange['high'] = $info->sPrice;
             }
             if (!$priceRange['low'] || $priceRange['low'] > $info->sPrice) {
                 $priceRange['low'] = $info->sPrice;
             }
         }
     }
     $this->view->price = $priceRange;
     // Add custom page JS
     if ($data && (count($data->options) > 1 || count($data->skus) > 1)) {
         $js = $this->getDisplayJs($data->options, $data->skus);
         Document::addScriptDeclaration($js);
     }
     // Get images (if any), gets all images from /site/storefront/products/$pId
     $allowedImgExt = array('jpg', 'gif', 'png');
     $productImg = array();
     $imgWebPath = DS . 'site' . DS . 'storefront' . DS . 'products' . DS . $pId;
     $imgPath = PATH_APP . $imgWebPath;
     if (file_exists($imgPath)) {
         $files = scandir($imgPath);
         foreach ($files as $file) {
             if (in_array(pathinfo($file, PATHINFO_EXTENSION), $allowedImgExt)) {
                 if (substr($file, 0, 7) == 'default') {
                     // Let the default image to be the first one
                     array_unshift($productImg, $imgWebPath . DS . $file);
                 } else {
                     $productImg[] = $imgWebPath . DS . $file;
                 }
             }
         }
     } else {
         $productImg[] = DS . 'site' . DS . 'storefront' . DS . 'products' . DS . 'noimage.png';
     }
     $this->view->productImg = $productImg;
     $this->view->productAvailable = $productAvailable;
     //build pathway
     $this->_buildPathway($product->pName);
     // Set notifications
     $this->view->notifications = $pageMessages;
     $this->view->display();
 }
Example #5
0
 /**
  * Display default page
  *
  * @return     void
  */
 public function homeTask()
 {
     $cart = new CartModelCurrentCart();
     // Initialize errors array
     $errors = array();
     // Update cart if needed
     $updateCartRequest = Request::getVar('updateCart', false, 'post');
     // If pIds are posted, convert them to SKUs
     $pIds = Request::getVar('pId', false, 'post');
     //print_r($pIds); die;
     $skus = Request::getVar('skus', false, 'post');
     if ($updateCartRequest && ($pIds || $skus)) {
         if (!empty($pIds)) {
             $skus = array();
             include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
             $warehouse = new StorefrontModelWarehouse();
             foreach ($pIds as $pId => $qty) {
                 $product_skus = $warehouse->getProductSkus($pId);
                 // each pId must map to one SKU, otherwise ignored, since there is no way which SKU is being added
                 // Must be only one sku...
                 if (sizeof($product_skus) != 1) {
                     continue;
                 }
                 $skus[$product_skus[0]] = $qty;
             }
         } else {
             if (!is_array($skus)) {
                 $skus = array($skus => 1);
             }
         }
         //print_r($skus); die;
         // Turn off syncing to prevent redundant session update queries
         $cart->setSync(false);
         foreach ($skus as $sId => $qty) {
             try {
                 $cart->update($sId, $qty);
             } catch (Exception $e) {
                 $cart->setMessage($e->getMessage(), 'error');
             }
         }
         // set flag to redirect
         $redirect = true;
         if ($cart->hasMessages()) {
             $redirect = false;
         }
     } else {
         $allPost = Request::request();
         foreach ($allPost as $var => $val) {
             if ($val == 'delete') {
                 $toDelete = explode('_', $var);
                 //print_r($toDelete);	die;
                 if ($toDelete[0] == 'delete') {
                     $sId = $toDelete[1];
                     // Delete the requested item by setting its QTY to zero
                     $redirect = true;
                     try {
                         $cart->update($sId, 0);
                     } catch (Exception $e) {
                         $cart->setMessage($e->getMessage(), 'error');
                         $redirect = false;
                     }
                 }
             }
         }
     }
     // Add coupon if needed
     $addCouponRequest = Request::getVar('addCouponCode', false, 'post');
     $couponCode = Request::getVar('couponCode', false, 'post');
     if ($addCouponRequest && $couponCode) {
         // Sync cart before pontial coupons applying
         $cart->getCartInfo(true);
         // Add coupon
         try {
             $cart->addCoupon($couponCode);
         } catch (Exception $e) {
             $cart->setMessage($e->getMessage(), 'error');
         }
         // set flag to redirect
         $redirect = true;
         if ($cart->hasMessages()) {
             $redirect = false;
         }
     }
     // Check for express add to cart
     if (!empty($redirect) && $redirect) {
         // If this is an express checkout (go to the confirm page right away) there shouldn't be any items in the cart
         // Since redirect is set, there are no errors
         $expressCheckout = Request::getVar('expressCheckout', false, 'post');
         // make sure the cart is empty
         if ($expressCheckout && !empty($skus) && $cart->isEmpty()) {
             // Get the latest synced cart info, it will also enable cart syncing that was turned off before
             $cart->getCartInfo(true);
             // Redirect directly to checkout, skip the cart page
             $redirect_url = Route::url('index.php?option=' . 'com_cart') . DS . 'checkout';
             App::redirect($redirect_url);
         }
         // prevent resubmitting form by refresh
         // redirect to cart
         $redirect_url = Route::url('index.php?option=' . 'com_cart');
         App::redirect($redirect_url);
     }
     // Get the latest synced cart info, it will also enable cart syncing that was turned off before
     $cartInfo = $cart->getCartInfo(true);
     $this->view->cartInfo = $cartInfo;
     // Handle coupons
     $couponPerks = $cart->getCouponPerks();
     //print_r($couponPerks); die;
     $this->view->couponPerks = $couponPerks;
     // Handle memberships
     $membershipInfo = $cart->getMembershipInfo();
     //print_r($membershipInfo); die;
     $this->view->membershipInfo = $membershipInfo;
     // At this point the cart is lifted and may have some issues/errors (say, after merging), get them
     if ($cart->hasMessages()) {
         $cartMessages = $cart->getMessages();
         $this->view->notifications = $cartMessages;
     }
     $this->view->display();
 }