Example #1
0
 /**
  * Adds a subscription.
  *
  * @param KCommandContext $context
  */
 protected function _actionAdd(KCommandContext $context)
 {
     $payload = $this->_order->getPayload();
     if (!$this->_gateway->process($payload)) {
         throw new ComSubscriptionsDomainPaymentException('Payment error. Check the log');
     }
     $this->getService('repos:people.person')->addBehavior('com://site/subscriptions.domain.behavior.subscriber');
     $person = $this->_order->getSubscriber();
     $package = $this->_order->getPackage();
     if (!$person->persisted()) {
         $person->getRepository()->getSpace()->setEntityState($person, AnDomain::STATE_NEW);
         $person->enable()->saveEntity();
         $this->_order->setSubscriber($person);
     }
     if ($person->hasSubscription() && !$package->recurring) {
         $subscription = $person->changeSubscriptionTo($package);
     } else {
         $subscription = $person->subscribeTo($package);
     }
     if ($payload->getRecurring()) {
         $subscription->setValue('profileId', $payload->getRecurring()->profile_id);
         $subscription->setValue('profileStatus', $payload->getRecurring()->profile_status);
     }
     if (!$this->commit()) {
         throw new RuntimeException("Subscription can not be added");
     }
     $this->getResponse()->status = KHttpResponse::CREATED;
     dispatch_plugin('subscriptions.onAfterSubscribe', array('subscription' => $subscription));
     $this->setItem($subscription);
     return $subscription;
 }
Example #2
0
 /**
  * Adds a subscription
  *
  * @param KCommandContext $context
  *
  * @return void
  */
 protected function _actionAdd(KCommandContext $context)
 {
     $payload = $this->_order->getPayload();
     if (!$this->_gateway->process($payload)) {
         throw new ComSubscriptionsDomainPaymentException('Payment error. Check the log');
     }
     $person = $this->_order->getSubscriber();
     $package = $this->_order->getPackage();
     $set = new AnObjectSet();
     $set->insert($this->_order);
     if (!$person->persisted()) {
         $person->reset();
         $user = $person->getJUserObject();
         //encrypt the password
         $user->set('password', $person->getPassword(true));
         $user->set('block', '0');
         $user->save();
         $person = $this->getService('repos://site/people.person')->find(array('userId' => $user->id));
         if ($person) {
             $set->insert($person);
             $this->_order->setSubscriber($person);
         }
     }
     if (!$package->recurring && $person->hasSubscription()) {
         $subscription = $person->changeSubscriptionTo($package);
     } else {
         $subscription = $person->subscribeTo($package);
     }
     $set->insert($subscription);
     if ($payload->getRecurring()) {
         $subscription->setValue('profileId', $payload->getRecurring()->profile_id);
         $subscription->setValue('profileStatus', $payload->getRecurring()->profile_status);
     }
     if (!$this->commit()) {
         $set->delete();
         $this->commit();
         throw new RuntimeException("Subscription can not be added");
     }
     $this->getResponse()->status = KHttpResponse::CREATED;
     dispatch_plugin('subscriptions.onAfterSubscribe', array('subscription' => $subscription));
     $this->setItem($subscription);
     return $subscription;
 }