/**
  * @see	\wcf\system\cronjob\ICronjob::execute()
  */
 public function execute(Cronjob $cronjob)
 {
     parent::execute($cronjob);
     // disable expired paid subscriptions
     if (MODULE_PAID_SUBSCRIPTION) {
         $subscriptionUserList = new PaidSubscriptionUserList();
         $subscriptionUserList->getConditionBuilder()->add('isActive = ?', array(1));
         $subscriptionUserList->getConditionBuilder()->add('endDate > 0 AND endDate < ?', array(TIME_NOW));
         $subscriptionUserList->readObjects();
         if (count($subscriptionUserList->getObjects())) {
             $action = new PaidSubscriptionUserAction($subscriptionUserList->getObjects(), 'revoke');
             $action->executeAction();
         }
     }
 }
 /**
  * @see	wcf\system\payment\type\IPaymentType::processTransaction()
  */
 public function processTransaction($paymentMethodObjectTypeID, $token, $amount, $currency, $transactionID, $status, $transactionDetails)
 {
     $userSubscription = $user = $subscription = null;
     try {
         $tokenParts = explode(':', $token);
         if (count($tokenParts) != 2) {
             throw new SystemException('invalid token');
         }
         list($userID, $subscriptionID) = $tokenParts;
         // get user object
         $user = new User(intval($userID));
         if (!$user->userID) {
             throw new SystemException('invalid user');
         }
         // get subscription object
         $subscription = new PaidSubscription(intval($subscriptionID));
         if (!$subscription->subscriptionID) {
             throw new SystemException('invalid subscription');
         }
         // search for existing subscription
         $userSubscription = PaidSubscriptionUser::getSubscriptionUser($subscription->subscriptionID, $user->userID);
         // search log for transaction id
         $logEntry = PaidSubscriptionTransactionLog::getLogByTransactionID($paymentMethodObjectTypeID, $transactionID);
         if ($logEntry !== null) {
             throw new SystemException('transaction already processed');
         }
         $logMessage = '';
         if ($status == 'completed') {
             // validate payment amout
             if ($amount != $subscription->cost || $currency != $subscription->currency) {
                 throw new SystemException('invalid payment amount');
             }
             // active/extend subscription
             if ($userSubscription === null) {
                 // create new subscription
                 $action = new PaidSubscriptionUserAction(array(), 'create', array('user' => $user, 'subscription' => $subscription));
                 $returnValues = $action->executeAction();
                 $userSubscription = $returnValues['returnValues'];
             } else {
                 // extend existing subscription
                 $action = new PaidSubscriptionUserAction(array($userSubscription), 'extend');
                 $action->executeAction();
             }
             $logMessage = 'payment completed';
         }
         if ($status == 'reversed') {
             if ($userSubscription !== null) {
                 // revoke subscription
                 $action = new PaidSubscriptionUserAction(array($userSubscription), 'revoke');
                 $action->executeAction();
             }
             $logMessage = 'payment reversed';
         }
         if ($status == 'canceled_reversal') {
             if ($userSubscription !== null) {
                 // restore subscription
                 $action = new PaidSubscriptionUserAction(array($userSubscription), 'restore');
                 $action->executeAction();
             }
             $logMessage = 'reversal canceled';
         }
         // log success
         $action = new PaidSubscriptionTransactionLogAction(array(), 'create', array('data' => array('subscriptionUserID' => $userSubscription->subscriptionUserID, 'userID' => $user->userID, 'subscriptionID' => $subscription->subscriptionID, 'paymentMethodObjectTypeID' => $paymentMethodObjectTypeID, 'logTime' => TIME_NOW, 'transactionID' => $transactionID, 'logMessage' => $logMessage, 'transactionDetails' => serialize($transactionDetails))));
         $action->executeAction();
     } catch (SystemException $e) {
         // log failure
         $action = new PaidSubscriptionTransactionLogAction(array(), 'create', array('data' => array('subscriptionUserID' => $userSubscription !== null ? $userSubscription->subscriptionUserID : null, 'userID' => $user !== null ? $user->userID : null, 'subscriptionID' => $subscription !== null ? $subscription->subscriptionID : null, 'paymentMethodObjectTypeID' => $paymentMethodObjectTypeID, 'logTime' => TIME_NOW, 'transactionID' => $transactionID, 'logMessage' => $e->getMessage(), 'transactionDetails' => serialize($transactionDetails))));
         $action->executeAction();
         throw $e;
     }
 }
 /**
  * @see	\wcf\form\IForm::save()
  */
 public function save()
 {
     parent::save();
     $userSubscription = PaidSubscriptionUser::getSubscriptionUser($this->subscriptionID, $this->user->userID);
     $data = array();
     if ($this->subscription->subscriptionLength) {
         $data['endDate'] = $this->endDateTime->getTimestamp();
     }
     if ($userSubscription === null) {
         // create new subscription
         $action = new PaidSubscriptionUserAction(array(), 'create', array('user' => $this->user, 'subscription' => $this->subscription, 'data' => $data));
         $returnValues = $action->executeAction();
         $userSubscription = $returnValues['returnValues'];
     } else {
         // extend existing subscription
         $action = new PaidSubscriptionUserAction(array($userSubscription), 'extend', array('data' => $data));
         $action->executeAction();
     }
     $this->saved();
     // reset values
     $this->username = $this->endDate = '';
     // show success
     WCF::getTPL()->assign(array('success' => true));
 }
 /**
  * Restores an existing subscription.
  */
 public function restore()
 {
     if (empty($this->objects)) {
         $this->readObjects();
     }
     foreach ($this->objects as $subscriptionUser) {
         $subscriptionUser->update(array('isActive' => 1));
         // update group memberships
         $action = new PaidSubscriptionUserAction(array($subscriptionUser), 'addGroupMemberships');
         $action->executeAction();
     }
 }