Пример #1
0
 public function save()
 {
     $row = new SubscriptionPlan();
     $row->load($_POST['id']);
     $post = AECToolbox::cleanPOST($_POST, false);
     $row->savePOSTsettings($post);
     $row->storeload();
     if ($_POST['id']) {
         $id = $_POST['id'];
     } else {
         $id = $row->getMax();
     }
     if (empty($_POST['id'])) {
         ItemGroupHandler::setChildren(1, array($id), 'item');
     }
     if (!empty($row->params['lifetime']) && !empty($row->params['full_period'])) {
         $this->log("Plan Warning", "You have selected a regular period for a plan that" . " already has the 'lifetime' (i.e. 'non expiring') flag set." . " The period you have set will be overridden by" . " that setting.", 'settings,plan');
     }
     $terms = $row->getTerms();
     if (!$terms->checkFree() && empty($row->params['processors'])) {
         $this->log("Plan Warning", "You have set a plan to be non-free, yet did not select a payment processor." . " Without a processor assigned, the plan will not show up on the frontend.", 'settings,plan');
     }
     if (!empty($row->params['lifetime']) && !empty($row->params['processors'])) {
         $fcount = 0;
         $found = 0;
         foreach ($row->params['processors'] as $procid) {
             $fcount++;
             if (isset($row->custom_params[$procid . '_recurring'])) {
                 if (0 < $row->custom_params[$procid . '_recurring'] && $row->custom_params[$procid . '_recurring'] < 2) {
                     $found++;
                 } elseif ($row->custom_params[$procid . '_recurring'] == 2) {
                     $fcount++;
                 }
             } else {
                 $pp = new PaymentProcessor();
                 if (0 < $pp->is_recurring() && $pp->is_recurring() < 2) {
                     $found++;
                 } elseif ($pp->is_recurring() == 2) {
                     $fcount++;
                 }
             }
         }
         if ($found) {
             if ($found < $fcount && $fcount > 1) {
                 $event = "You have selected one or more processors that only support recurring payments" . ", yet the plan is set to a lifetime period." . " This is not possible and the processors will not be displayed as options.";
             } else {
                 $event = "You have selected a processor that only supports recurring payments" . ", yet the plan is set to a lifetime period." . " This is not possible and the plan will not be displayed.";
             }
             $short = "Plan Warning";
             $tags = 'settings,plan';
             $params = array();
             $eventlog = new eventLog();
             $eventlog->issue($short, $tags, $event, 32, $params);
         }
     }
     $this->setMessage(JText::_('AEC_MSG_SUCESSFULLY_SAVED'));
 }
 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;
 }
Пример #3
0
 public function isRecurring()
 {
     if (!empty($this->subscr_id)) {
         $query = 'SELECT `recurring`' . ' FROM #__acctexp_subscr' . ' WHERE `id` = \'' . $this->subscr_id . '\'';
         $this->_db->setQuery($query);
         return $this->_db->loadResult();
     }
     if (isset($this->params['userselect_recurring'])) {
         $recurring_choice = $this->params['userselect_recurring'];
     } else {
         $recurring_choice = null;
     }
     if (InvoiceFactory::isActualProcessorName($this->method)) {
         $pp = new PaymentProcessor();
         if ($pp->loadName($this->method)) {
             $pp->fullInit();
             return $pp->is_recurring($recurring_choice);
         }
     }
     return false;
 }
Пример #4
0
 static function getCartProcessorGroups($cart, $recurring = false)
 {
     $pgroups = array();
     foreach ($cart->content as $cid => $c) {
         $cartitem = aecCartHelper::getCartItemObject($cart, $cid);
         $pplist = array();
         $pplist_names = array();
         if (!empty($cartitem->params['processors'])) {
             foreach ($cartitem->params['processors'] as $n) {
                 $pp = new PaymentProcessor();
                 if (!$pp->loadId($n)) {
                     continue;
                 }
                 $pp->init();
                 $pp->getInfo();
                 $pp->exchangeSettingsByPlan($cartitem);
                 if (isset($recurring)) {
                     $recurring = $pp->is_recurring($recurring);
                 } else {
                     $recurring = $pp->is_recurring();
                 }
                 if ($recurring > 1) {
                     $pplist[] = $pp->id;
                     $pplist_names[] = $pp->info['longname'];
                     if (!$cartitem->params['lifetime']) {
                         $pplist[] = $pp->id . '_recurring';
                         $pplist_names[] = $pp->info['longname'];
                     }
                 } elseif (!$cartitem->params['lifetime'] && $recurring) {
                     $pplist[] = $pp->id . '_recurring';
                     $pplist_names[] = $pp->info['longname'];
                 } else {
                     $pplist[] = $pp->id;
                     $pplist_names[] = $pp->info['longname'];
                 }
             }
         }
         if (empty($pplist)) {
             continue;
         }
         if (empty($pgroups)) {
             $pg = array();
             $pg['members'] = array($cid);
             $pg['processors'] = $pplist;
             $pg['processor_names'] = $pplist_names;
             $pgroups[] = $pg;
         } else {
             $create = true;
             foreach ($pgroups as $pgid => $pgroup) {
                 $pg = array();
                 if (count($pplist) == count($pgroup['processors'])) {
                     $a = true;
                     foreach ($pplist as $k => $v) {
                         if ($pgroup['processors'][$k] != $v) {
                             $a = false;
                         }
                     }
                     if ($a) {
                         $pgroups[$pgid]['members'][] = $cid;
                         $create = false;
                     }
                 }
             }
             if ($create) {
                 $pg['members'] = array($cid);
                 $pg['processors'] = $pplist;
                 $pg['processor_names'] = $pplist_names;
                 $pgroups[] = $pg;
             }
         }
     }
     return $pgroups;
 }