/** * This is not the best example to follow * Please see the category plugin for a better example */ public function post() { require_once JPATH_SITE . '/components/com_acctexp/acctexp.class.php'; $db =& JFactory::getDBO(); $app = JFactory::getApplication(); $userid = JRequest::getInt('user_id', 0); $planid = JRequest::getInt('plan_id', 0); //$new_expiry = JRequest::getString('date',0); $obj = new stdClass(); //validate plan $plans = SubscriptionPlanHandler::getPlanList(); $muser = metaUserDB::getIDbyUserid($userid); //$pplan = metaUserDB::getPreviousPlan($muser); //$uplan = metaUserDB::getUsedPlans($muser); //print_r($new_expiry);die; if (!$userid) { $obj->success = 0; $obj->code = 21; $obj->message = "invalid user id"; } elseif (!$plans[array_search($planid, $plans)]) { $obj->success = 0; $obj->code = 22; $obj->message = "invalid plan id"; } else { $plan = new SubscriptionPlan($db); $plan->load($planid); //print_r($plan->params['full_period']);die; //check user is metauser /*if ( is_a( $user, 'metaUser' ) ) { $metaUser = $user; } elseif( is_a( $user, 'Subscription' ) ) { $metaUser = new metaUser( $user->userid ); $metaUser->focusSubscription = $user; }*/ $metaUser = new metaUser($userid); $renew = $metaUser->is_renewing(); //$metaUser->focusSubscription->lifetime; $metaUser->focusSubscription->plan = $planid; $metaUser->focusSubscription->status = 'Active'; $metaUser->temporaryRFIX(); //$metaUser->focusSubscription->lifetime = 1; //set expiration $now = (int) gmdate('U'); //$current = strtotime($new_expiry); //$metaUser->focusSubscription->expiration = $new_expiry; //$metaUser->objSubscription->expiration = $new_expiry; $reply = $metaUser->focusSubscription->storeload(); if ($reply && $planid) { $history = new logHistory($db); $obj->success = 1; $obj->message = "User added to plan"; } } $this->plugin->setResponse($obj); }
/** * This is not the best example to follow * Please see the category plugin for a better example */ public function post() { require_once JPATH_SITE . '/components/com_acctexp/acctexp.class.php'; $db =& JFactory::getDBO(); $app = JFactory::getApplication(); $userid = JRequest::getInt('user_id', 0); $planid = JRequest::getInt('plan_id', 0); $new_expiry = JRequest::getString('date', 0); //convert date in format $new_expiry = date("Y-m-d h:i:s", strtotime($new_expiry)); $obj = new stdClass(); //validate plan $plans = SubscriptionPlanHandler::getPlanList(); $muser = metaUserDB::getIDbyUserid($userid); $plnuser = SubscriptionPlanHandler::getPlanUserlist($planid); if (!$userid) { $obj->success = 0; $obj->code = 21; $obj->message = "invalid user id"; } elseif (!$plans[array_search($planid, $plans)]) { $obj->success = 0; $obj->code = 22; $obj->message = "invalid plan id"; } elseif ($userid == $plnuser[array_search($userid, $plnuser)]) { $plan = new SubscriptionPlan($db); $plan->load($planid); $metaUser = new metaUser($userid); $renew = $metaUser->is_renewing(); $lifetime = $metaUser->focusSubscription->lifetime; $metaUser->focusSubscription->plan = $planid; $metaUser->focusSubscription->status = 'Active'; $metaUser->temporaryRFIX(); $metaUser->focusSubscription->lifetime = 0; //set expiration //$now = (int) gmdate('U'); $metaUser->focusSubscription->expiration = $new_expiry; //$metaUser->objSubscription->expiration = $new_expiry; $reply = $metaUser->focusSubscription->storeload(); if ($reply && $planid) { $obj->success = 1; $obj->message = "Expiry updated"; } } else { $obj->success = 0; $obj->code = 31; $obj->message = "Plan not assigned to user"; } $this->plugin->setResponse($obj); }
public function applyPlan($user, $processor = 'none', $silent = 0, $multiplicator = 1, $invoice = null, $tempparams = null) { global $aecConfig; $forcelifetime = false; if (is_string($multiplicator)) { if (strcmp($multiplicator, 'lifetime') === 0) { $forcelifetime = true; } } elseif (is_int($multiplicator) && $multiplicator < 1) { $multiplicator = 1; } if (empty($user)) { return false; } if (is_object($user)) { if (is_a($user, 'metaUser')) { $metaUser = $user; } elseif (is_a($user, 'Subscription')) { $metaUser = new metaUser($user->userid); $metaUser->focusSubscription = $user; } } else { $metaUser = new metaUser($user); } if (!isset($this->params['make_primary'])) { $this->params['make_primary'] = 1; } $fstatus = $metaUser->establishFocus($this, $processor, false); // TODO: Figure out why $status returns 'existing' - even on a completely fresh subscr if ($fstatus != 'existing') { $is_pending = $metaUser->focusSubscription->isPending(); $is_trial = $metaUser->focusSubscription->isTrial(); } else { $is_pending = false; $is_trial = $metaUser->focusSubscription->isTrial(); } $comparison = $this->doPlanComparison($metaUser->focusSubscription); $renew = $metaUser->is_renewing(); $lifetime = $metaUser->focusSubscription->lifetime; if ($comparison['total_comparison'] === false || $is_pending) { // If user is using global trial period he still can use the trial period of a plan if ($this->params['trial_period'] > 0 && !$is_trial) { $trial = true; $value = $this->params['trial_period']; $perunit = $this->params['trial_periodunit']; $this->params['lifetime'] = 0; // We are entering the trial period. The lifetime will come at the renew. $amount = $this->params['trial_amount']; } else { $trial = false; $value = $this->params['full_period']; $perunit = $this->params['full_periodunit']; $amount = $this->params['full_amount']; } } elseif (!$is_pending) { $trial = false; $value = $this->params['full_period']; $perunit = $this->params['full_periodunit']; $amount = $this->params['full_amount']; } else { return false; } if ($this->params['lifetime'] || $forcelifetime) { $metaUser->focusSubscription->expiration = '9999-12-31 00:00:00'; $metaUser->focusSubscription->lifetime = 1; } else { $metaUser->focusSubscription->lifetime = 0; $value *= $multiplicator; if ($comparison['comparison'] == 2 && !$lifetime) { $metaUser->focusSubscription->setExpiration($perunit, $value, 1); } else { $metaUser->focusSubscription->setExpiration($perunit, $value, 0); } } if ($is_pending) { // Is new = set signup date $metaUser->focusSubscription->signup_date = date('Y-m-d H:i:s', (int) gmdate('U')); if ($this->params['trial_period'] > 0 && !$is_trial) { $status = 'Trial'; } else { if ($this->params['full_period'] || $this->params['lifetime']) { if (!isset($this->params['make_active'])) { $status = 'Active'; } else { $status = $this->params['make_active'] ? 'Active' : 'Pending'; } } else { // This should not happen $status = 'Pending'; } } } else { // Renew subscription - Do NOT set signup_date if (!isset($this->params['make_active'])) { $status = $trial ? 'Trial' : 'Active'; } else { $status = $this->params['make_active'] ? $trial ? 'Trial' : 'Active' : 'Pending'; } } $metaUser->focusSubscription->status = $status; $metaUser->focusSubscription->plan = $this->id; $metaUser->temporaryRFIX(); $metaUser->focusSubscription->lastpay_date = date('Y-m-d H:i:s', (int) gmdate('U')); $metaUser->focusSubscription->type = $processor; if (is_object($invoice)) { if (!empty($invoice->params)) { $tempparam = array(); if (!empty($invoice->params['creator_ip'])) { $tempparam['creator_ip'] = $invoice->params['creator_ip']; } if (!empty($tempparam)) { $metaUser->focusSubscription->addParams($tempparam, 'params', false); $metaUser->focusSubscription->storeload(); } } } $pp = new PaymentProcessor(); if ($pp->loadName(strtolower($processor))) { $pp->init(); $pp->getInfo(); $recurring_choice = null; if (is_object($invoice)) { if (!empty($invoice->params)) { if (isset($invoice->params["userselect_recurring"])) { $recurring_choice = $invoice->params["userselect_recurring"]; } } } // Check whether we have a custome choice set if (!is_null($recurring_choice)) { $metaUser->focusSubscription->recurring = $pp->is_recurring($recurring_choice); } else { $metaUser->focusSubscription->recurring = $pp->is_recurring(); } } else { $metaUser->focusSubscription->recurring = 0; } $metaUser->focusSubscription->storeload(); if (empty($invoice->id)) { $invoice = new stdClass(); $invoice->amount = $amount; } $exchange = $add = null; $result = $this->triggerMIs('action', $metaUser, $exchange, $invoice, $add, $silent); if ($result === false) { return false; } elseif ($result === true) { // MIs might have changed the subscription. Reload it. $metaUser->focusSubscription->reload(); } if ($this->params['gid_enabled']) { $metaUser->instantGIDchange($this->params['gid']); } $metaUser->focusSubscription->storeload(); if (!($silent || $aecConfig->cfg['noemails']) || $aecConfig->cfg['noemails_adminoverride']) { $adminonly = $this->id == $aecConfig->cfg['entry_plan'] || $aecConfig->cfg['noemails'] && $aecConfig->cfg['noemails_adminoverride']; $metaUser->focusSubscription->sendEmailRegistered($renew, $adminonly, $invoice); } $metaUser->meta->addPlanID($this->id); $result = $this->triggerMIs('afteraction', $metaUser, $exchange, $invoice, $add, $silent); if ($result === false) { return false; } $this->incrementInventory(); return $renew; }