Exemplo n.º 1
0
 public function pay($multiplicator = 1, $noclear = false)
 {
     $metaUser = false;
     $new_plan = false;
     $plans = array();
     if (!empty($this->userid)) {
         $metaUser = new metaUser($this->userid);
     }
     if (!empty($this->params['target_user'])) {
         $targetUser = new metaUser($this->params['target_user']);
     } else {
         $targetUser =& $metaUser;
     }
     if (!empty($this->params['aec_pickup'])) {
         if (is_array($this->params['aec_pickup'])) {
             foreach ($this->params['aec_pickup'] as $key) {
                 if (isset($this->params[$key])) {
                     unset($this->params[$key]);
                 }
             }
         }
         unset($this->params['aec_pickup']);
     }
     $override_permissioncheck = $this->isRecurring() && $this->counter > 1;
     if (!empty($this->usage)) {
         $usage = explode('.', $this->usage);
         // Update old notation
         if (!isset($usage[1])) {
             $temp = $usage[0];
             $usage[0] = 'p';
             $usage[1] = $temp;
         }
         switch (strtolower($usage[0])) {
             case 'c':
             case 'cart':
                 if (empty($this->params['cart']->content)) {
                     $this->params['cart'] = new aecCart();
                     $this->params['cart']->load($usage[1]);
                     if (!empty($this->params['cart']->content)) {
                         foreach ($this->params['cart']->content as $c) {
                             $new_plan = new SubscriptionPlan();
                             $new_plan->load($c['id']);
                             for ($i = 0; $i < $c['quantity']; $i++) {
                                 $plans[] = $new_plan;
                             }
                         }
                     }
                     $this->params['cart']->clear();
                     $this->storeload();
                     // Load and delete original entry
                     $cart = new aecCart();
                     $cart->load($usage[1]);
                     if ($cart->id) {
                         $cart->delete();
                     }
                 } else {
                     foreach ($this->params['cart']->content as $c) {
                         $new_plan = new SubscriptionPlan();
                         $new_plan->load($c['id']);
                         if (!$override_permissioncheck) {
                             if ($new_plan->checkPermission($metaUser) === false) {
                                 return false;
                             }
                         }
                         for ($i = 0; $i < $c['quantity']; $i++) {
                             $plans[] = $new_plan;
                         }
                     }
                 }
                 break;
             case 'p':
             case 'plan':
             default:
                 $new_plan = new SubscriptionPlan();
                 $new_plan->load($this->usage);
                 if (!$override_permissioncheck) {
                     if ($new_plan->checkPermission($metaUser) === false) {
                         return false;
                     }
                 }
                 $plans[] = $new_plan;
                 break;
         }
     }
     if (is_object($metaUser)) {
         if (!empty($this->params['userMIParams'])) {
             foreach ($this->params['userMIParams'] as $plan => $mis) {
                 foreach ($mis as $mi_id => $content) {
                     $metaUser->meta->setMIParams($mi_id, $plan, $content);
                     if ($metaUser->userid !== $targetUser->userid) {
                         $targetUser->meta->setMIParams($mi_id, $plan, $content);
                     }
                 }
             }
             $metaUser->meta->storeload();
             if ($metaUser->userid !== $targetUser->userid) {
                 $targetUser->meta->storeload();
             }
         }
     }
     foreach ($plans as $plan) {
         if (is_object($targetUser) && is_object($plan)) {
             if ($targetUser->userid) {
                 if (!empty($this->subscr_id)) {
                     $targetUser->establishFocus($plan, $this->method, false, $this->subscr_id);
                 } else {
                     $targetUser->establishFocus($plan, $this->method);
                 }
                 $this->subscr_id = $targetUser->focusSubscription->id;
                 // Apply the Plan
                 $application = $targetUser->focusSubscription->applyUsage($plan->id, $this->method, 0, $multiplicator, $this);
             } else {
                 $application = $plan->applyPlan(0, $this->method, 0, $multiplicator, $this);
             }
         }
     }
     $micro_integrations = false;
     if (!empty($this->conditions)) {
         if (strpos($this->conditions, 'mi_attendevents')) {
             $start_position = strpos($this->conditions, '<registration_id>') + strlen('<registration_id>');
             $end_position = strpos($this->conditions, '</registration_id>');
             $micro_integration['name'] = 'mi_attendevents';
             $micro_integration['parameters'] = array('registration_id' => substr($this->conditions, $start_position, $end_position - $start_position));
             $micro_integrations = array();
             $micro_integrations[] = $micro_integration;
         }
     }
     // Reload the meta- & targetUser since they have been updated
     if (!empty($this->userid)) {
         $metaUser = new metaUser($this->userid);
     }
     if (!empty($this->params['target_user'])) {
         $targetUser = new metaUser($this->params['target_user']);
     } else {
         $targetUser =& $metaUser;
     }
     if (!empty($micro_integrations)) {
         if (is_array($micro_integrations)) {
             foreach ($micro_integrations as $micro_int) {
                 $mi = new microIntegration();
                 if (isset($micro_integration['parameters'])) {
                     $exchange = $micro_integration['parameters'];
                 } else {
                     $exchange = null;
                 }
                 if (isset($micro_int['name'])) {
                     if ($mi->callDry($micro_int['name'])) {
                         if (is_object($targetUser)) {
                             $mi->action($targetUser, $exchange, $this, $new_plan);
                         } else {
                             $mi->action($targetUser, $exchange, $this, $new_plan);
                         }
                     }
                 } elseif (isset($micro_int['id'])) {
                     if ($mi->mi_exists($micro_int['id'])) {
                         $mi->load($micro_int['id']);
                         if ($mi->callIntegration()) {
                             if (is_object($metaUser)) {
                                 $mi->action($targetUser, $exchange, $this, $new_plan);
                             } else {
                                 $mi->action($targetUser, $exchange, $this, $new_plan);
                             }
                         }
                     }
                 }
                 unset($mi);
             }
         }
     }
     if ($this->coupons) {
         foreach ($this->coupons as $coupon_code) {
             $cph = new couponHandler();
             $cph->load($coupon_code);
             $cph->triggerMIs($targetUser, $this, $plans[0]);
         }
     }
     // We need to at least warn the admin if there is an invoice with nothing to do
     if (empty($this->usage) && empty($this->conditions) && empty($this->coupons)) {
         $short = 'Nothing to do';
         $event = JText::_('AEC_MSG_PROC_INVOICE_ACTION_EV_VALID_APPFAIL');
         $tags = 'invoice,application,payment,action_failed';
         $params = array('invoice_number' => $this->invoice_number);
         $eventlog = new eventLog();
         $eventlog->issue($short, $tags, $event, 32, $params);
     }
     if (!$noclear) {
         $this->setTransactionDate();
     }
     return true;
 }