public function establishFocus($payment_plan, $processor = 'none', $silent = false, $bias = null) { if (!is_object($payment_plan)) { $planid = $payment_plan; $payment_plan = new SubscriptionPlan(); $payment_plan->load($planid); if (empty($payment_plan->id)) { return false; } } if (is_object($this->focusSubscription)) { if ($this->focusSubscription->plan == $payment_plan->id) { return 'existing'; } } $plan_params = $payment_plan->params; if (!isset($plan_params['make_primary'])) { $plan_params['make_primary'] = 1; } if ($plan_params['make_primary'] && $this->hasSubscription) { if ($this->objSubscription->primary) { $this->focusSubscription = $this->objSubscription; return 'existing'; } else { $existing_record = $this->objSubscription->getSubscriptionID($this->userid); if ($existing_record) { $this->objSubscription = new Subscription(); $this->objSubscription->load($existing_record); $this->focusSubscription = $this->objSubscription; return 'existing'; } } } // If we are not dealing with a primary (or an otherwise unclear situation), // we need to figure out how to prepare the switch $existing_record = 0; $existing_status = false; // Check whether a record exists if ($this->hasSubscription) { $existing_record = $this->focusSubscription->getSubscriptionID($this->userid, $payment_plan->id, $plan_params['make_primary'], false, $bias); if (!empty($existing_record)) { $db = JFactory::getDBO(); $query = 'SELECT `status`' . ' FROM #__acctexp_subscr' . ' WHERE `id` = \'' . (int) $existing_record . '\''; $db->setQuery($query); $existing_status = $db->loadResult(); } } $return = false; // To be failsafe, a new subscription may have to be added in here if (empty($this->hasSubscription) || !$plan_params['make_primary'] || $plan_params['update_existing']) { if (!empty($existing_record) && ($existing_status == 'Trial' || $existing_status == 'Pending' || $plan_params['update_existing'] || $plan_params['make_primary'])) { // Update existing non-primary subscription if ($this->focusSubscription->id !== $existing_record) { $this->focusSubscription = new Subscription(); $this->focusSubscription->load($existing_record); } $return = 'existing'; } else { if (!empty($this->hasSubscription)) { $existing_parent = $this->focusSubscription->getSubscriptionID($this->userid, $plan_params['standard_parent'], null); } else { $existing_parent = false; } // Create a root new subscription if (empty($this->hasSubscription) && !$plan_params['make_primary'] && !empty($plan_params['standard_parent']) && empty($existing_parent)) { $this->objSubscription = new Subscription(); $this->objSubscription->load(0); if ($this->objSubscription->createNew($this->userid, 'none', 1, 1, $plan_params['standard_parent'])) { $this->objSubscription->applyUsage($plan_params['standard_parent'], 'none', $silent, 0); } } elseif (!$plan_params['make_primary'] && !empty($plan_params['standard_parent']) && $existing_parent) { $this->objSubscription = new Subscription(); $this->objSubscription->load($existing_parent); if ($this->objSubscription->isExpired()) { $this->objSubscription->applyUsage($plan_params['standard_parent'], 'none', $silent, 0); } } // Create new subscription $this->focusSubscription = new Subscription(); $this->focusSubscription->load(0); $this->focusSubscription->createNew($this->userid, $processor, 1, $plan_params['make_primary'], $payment_plan->id); $this->hasSubscription = 1; if ($plan_params['make_primary']) { $this->objSubscription = clone $this->focusSubscription; } $return = 'new'; } } if (empty($this->objSubscription) && !empty($this->focusSubscription)) { $this->objSubscription = clone $this->focusSubscription; } $this->temporaryRFIX(); return $return; }
public function beat() { $this->processors = array(); $this->proc_prepare = array(); $this->result = array('fail_expired' => 0, 'fallback' => 0, 'skipped' => 0, 'expired' => 0, 'pre_expired' => 0, 'pre_exp_actions' => 0); // Some cleanup $this->deleteTempTokens(); // Receive maximum pre expiration time $pre_expiration = microIntegrationHandler::getMaxPreExpirationTime(); $subscription_list = $this->getSubscribers($pre_expiration); // Efficient way to check for expired users without checking on each one if (empty($subscription_list)) { return $this->endBeat(); } foreach ($subscription_list as $sid => $sub_id) { $subscription = new Subscription(); $subscription->load($sub_id); if (!aecUserHelper::UserExists($subscription->userid)) { unset($subscription_list[$sid]); continue; } // Check whether this user really is expired // If this check fails, the following subscriptions might still be pre-expiration events if ($subscription->isExpired()) { // If we don't have any validation response, expire $validate = $this->processorValidation($subscription, $subscription_list); if ($validate === false) { // There was some kind of fatal error, return. return false; } elseif ($validate !== true) { $expire = $subscription->expire(); if ($expire) { $this->result['expired']++; } elseif ($expire === false) { $this->result['fail_expired']++; } elseif (is_null($expire)) { $this->result['fallback']++; } else { $this->result['skipped']++; } } unset($subscription_list[$sid]); } elseif (!$subscription->recurring) { break; } } // Only go for pre expiration action if we have at least one user for it if (empty($pre_expiration) || empty($subscription_list)) { return $this->endBeat(); } // Get all the MIs which have a pre expiration check $mi_pexp = microIntegrationHandler::getPreExpIntegrations(); // Find plans which have the MIs assigned $expmi_plans = microIntegrationHandler::getPlansbyMI($mi_pexp); // Filter out the users which dont have the correct plan $query = 'SELECT `id`, `userid`' . ' FROM #__acctexp_subscr' . ' WHERE `id` IN (' . implode(',', $subscription_list) . ')' . ' AND `plan` IN (' . implode(',', $expmi_plans) . ')'; $this->_db->setQuery($query); $sub_list = $this->_db->loadObjectList(); if (!empty($sub_list)) { foreach ($sub_list as $sl) { $metaUser = new metaUser($sl->userid); $metaUser->moveFocus($sl->id); $res = $metaUser->focusSubscription->triggerPreExpiration($metaUser, $mi_pexp); if ($res) { $this->result['pre_exp_actions'] += $res; $this->result['pre_expired']++; } } } return $this->endBeat(); }