public function handle()
 {
     require_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Memberships.php';
     $ms = new \Components\Storefront\Models\Memberships();
     /* NEW
     		$subscription = StorefrontModelMemberships::getSubscriptionObject($this->type, $this->pId, $this->uId);
     		// Get the expiration for the current subscription (if any)
     		$currentExpiration = $subscription->getExpiration();
     		*/
     // Get current registration
     $membership = $ms->getMembershipInfo($this->crtId, $this->item['info']->pId);
     $expiration = $membership['crtmExpires'];
     /* Add the user to the corresponding user access group (pull access group ID from the meta) */
     try {
         // Get user ID for the cart
         require_once dirname(dirname(dirname(__DIR__))) . DS . 'models' . DS . 'Cart.php';
         $userId = \Components\Cart\Models\Cart::getCartUser($this->crtId);
         // Get the user group ID to set the user to (from meta)
         require_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Product.php';
         $userGId = \Components\Storefront\Models\Product::getMeta($this->item['info']->pId, 'userGroupId');
         $add = \JUserHelper::addUserToGroup($userId, $userGId);
         if ($add instanceof \Exception) {
             mail(Config::get('mailfrom'), 'Error adding to the group', $add->getMessage() . ' Cart #' . $this->crtId);
         }
         $table = \JTable::getInstance('User', 'JTable', array());
         $table->load($userId);
         // Trigger the onAftereStoreUser event
         Event::trigger('onUserAfterSave', array($table->getProperties(), false, true, null));
     } catch (Exception $e) {
         // Error
         return false;
     }
 }
 /**
  * Main handler. Does all the checks
  *
  * @param 	void
  * @return 	void
  */
 public function audit()
 {
     /* Membership may have a limit on when it can be extended */
     /* If no user, some checks may be skipped... */
     // Get user
     $jUser = User::getRoot();
     if (!$jUser->get('guest')) {
         // Check if there is a limitation on when the subscription can be extended
         $subscriptionMaxLen = Product::getMeta($this->pId, 'subscriptionMaxLen');
         if ($subscriptionMaxLen) {
             /* Check if the current user has the existing subscription and how much is left on it
             		 i.e. figure out if he may extend his current subscription */
             /*
              *  This is not working very well for multiple SKUs with multiple subscriptionMaxLen's
              *  at this point code doesn't know what SKU will be added,
              *  so for one SKU subscriptionMaxLen should
              *  be set to time less than actual membership length, ie if membership is sold for 1 year and
              *  cannot be renewed more than 6 month before it expires the subscriptionMaxLen must be set to 6 MONTH
              *  if it cannot be renewed more than 3 month before it expires the subscriptionMaxLen must be set to 3 MONTH
              *
              *  so subscriptionMaxLen = XX is actually "let renew XX time before expiration"
              */
             // Get the proper product type subscription object reference
             $subscription = Memberships::getSubscriptionObject($this->type, $this->pId, $this->uId);
             // Get the expiration for the current subscription (if any)
             $currentExpiration = $subscription->getExpiration();
             if ($currentExpiration && $currentExpiration['crtmActive']) {
                 // Do the check
                 $currentExpirationTime = $currentExpiration['crtmExpires'];
                 // See if current expiration is later than max allowed time from now (max allowed time + now)
                 if (strtotime('+' . $subscriptionMaxLen) < strtotime($currentExpirationTime)) {
                     // Expiration is not allowed -- the current expiration is too far in the future
                     $this->setResponseStatus('error');
                     $this->setResponseNotice('You already have an active subscription to this item. Subscription extension is not available at this time.');
                     $this->setResponseError(': you already have an active subscription. Subscription extension is not available at this time.');
                 }
             }
         }
     }
     return $this->getResponse();
 }
 /**
  * Main handler. Does all the checks
  *
  * @param 	void
  * @return 	void
  */
 public function audit()
 {
     /* If no user, some checks may be skipped... */
     // Get user
     $jUser = User::getRoot();
     // User specific checks
     if (!$jUser->get('guest')) {
         if ($sId = $this->getSku()) {
             // Check if the current user reached the max count of downloads for this SKU
             $sku = new Sku($sId);
             $skuDownloadLimit = $sku->getMeta('downloadLimit');
             if ($skuDownloadLimit > 0) {
                 // Get SKU download count
                 $skuDownloadCount = CartDownload::countUserSkuDownloads($this->sId, $this->uId);
                 // Check if the limit is reached
                 if ($skuDownloadCount >= $skuDownloadLimit) {
                     $this->setResponseStatus('error');
                     $this->setResponseNotice('You have reached the maximum number of allowed downloads for this product.');
                     $this->setResponseError(': you have reached the maximum number of allowed downloads for this product.');
                 }
             }
             return $this->getResponse();
         }
     }
     // Check SKU-related stuff if this is a SKU
     if ($sId = $this->getSku()) {
         // Check if SKU is reached the download max count
         $sku = new Sku($sId);
         $skuDownloadLimit = $sku->getMeta('globalDownloadLimit');
         if ($skuDownloadLimit > 0) {
             // Get SKU download count
             $skuDownloadCount = CartDownload::countSkuDownloads($this->sId);
             // Check if the limit is reached
             if ($skuDownloadCount >= $skuDownloadLimit) {
                 $this->setResponseStatus('error');
                 $this->setResponseNotice('This product has reached the maximum number of allowed downloads and cannot be downloaded.');
                 $this->setResponseError(': this product has reached the maximum number of allowed downloads and cannot be downloaded.');
             }
         }
         return $this->getResponse();
     }
     // Get product download limit
     $productDownloadLimit = Product::getMeta($this->pId, 'globalDownloadLimit');
     // Get product downloads count
     if ($productDownloadLimit > 0) {
         $productDownloadCount = CartDownload::countProductDownloads($this->pId);
         // Check if the limit is reached
         if ($productDownloadCount >= $productDownloadLimit) {
             $this->setResponseStatus('error');
             $this->setResponseNotice('This product has reached the maximum number of allowed downloads and cannot be downloaded.');
             $this->setResponseError(': this product has reached the maximum number of allowed downloads and cannot be downloaded.');
         }
     }
     return $this->getResponse();
 }