public function validateRenewPlanId($id, $allData)
 {
     if (!preg_match('/^[0-9]{1,10}$/', $id)) {
         return 'Error: Invalid Renewal Plan ID';
     }
     if ($id == WPP_ID_NORENEW) {
         return true;
         // "no next plan"
     }
     $plan = WpPlan::newFromId($id);
     if ($plan == null) {
         return 'Error: Invalid Plan ID';
     }
     $user_id = $this->getUser()->getId();
     $curr_sub = WpSubscription::newActiveByUserId($user_id);
     if ($curr_sub == null) {
         return 'Error: No Active Subscription';
     }
     if (!$plan->isAvailableForRenewal($curr_sub->getEnd())) {
         return 'Error: Plan Not Available For Renewal';
     }
     if (!$plan->hasSufficientQuotas(WpWikiplace::countWikiplacesOwnedByUser($user_id), WpPage::countPagesOwnedByUser($user_id), WpPage::countDiskspaceUsageByUser($user_id))) {
         return 'Error: Plan Quotas Unsufficients';
     }
     return true;
 }
 /**
  *
  * @param ResultWrapper $results Results from SQL select
  * @param DatabaseBase $databaseBase Optoional, default = wfGetDB(DB_SLAVE)
  * @return array Array of WpInvitationCategory (indexes are categories identifiers)
  */
 private static function factoryFromResults($results)
 {
     if (!$results instanceof ResultWrapper) {
         return array();
     }
     $categories = array();
     $category = null;
     foreach ($results as $row) {
         if ($category == null) {
             $category = self::constructFromDatabaseRow($row);
             $category->plans = array();
         } elseif ($category->getId() != $row->wpic_id) {
             $categories[$category->getId()] = $category;
             $category = self::constructFromDatabaseRow($row);
             $category->plans = array();
         }
         if ($row->wpp_id != null) {
             $plan = WpPlan::constructFromDatabaseRow($row);
             if ($plan != null) {
                 $category->plans[] = $plan;
             }
         }
     }
     if ($category != null) {
         $categories[$category->getId()] = $category;
     }
     return $categories;
 }
Example #3
0
 /**
  * Return true if $this is the same as another plan but cheaper.
  *
  * @param WpPlan $plan
  * @return Boolean 
  */
 private function isCheaper($plan)
 {
     return $this->getPrice() < $plan->getPrice();
 }
 public function execute()
 {
     $deadline = intval($this->getOption('deadline', 0));
     if ($deadline < 0) {
         $deadline = 0;
     }
     $when = WpSubscription::now(0, 0, 0, $deadline);
     $this->output("[" . WpSubscription::now() . ": Searching subscriptions to renew before {$when} which has not been notified ...]\n");
     $subs = WpSubscription::factoryActiveEndSoonToNotify($when);
     $this->output("[" . WpSubscription::now() . ": " . count($subs) . " subscription(s) to check...]\n");
     foreach ($subs as $sub) {
         $next_plan_id = $sub->getRenewalPlanId();
         $msg = "wps_id[{$sub->getId()}], ";
         if ($next_plan_id == WPP_ID_NORENEW) {
             $msg .= 'do not renew';
             $sub->sendOnNoRenewalSoon();
             $sub->setRenewalPlanNotified();
         } else {
             $msg .= "renew_wpp_id[{$next_plan_id}]: ";
             // a plan is defined has renewal, check if it's a good choice...
             $renewal_date = $sub->getEnd();
             $next_plan = $sub->getRenewalPlan();
             $user_id = $sub->getBuyerUserId();
             if ($next_plan == null) {
                 // should not occur, but ensure database is not corrupted, correct if needed
                 // change to the current plan suggested renewal one
                 $curr_plan = $sub->getPlan();
                 $new_next_plan;
                 if ($curr_plan == null) {
                     // should not occur, but ... just in case
                     $new_next_plan = WpPlan::newFromId(WP_FALLBACK_PLAN_ID);
                 } else {
                     $new_next_plan = $curr_plan->getRenewalPlan($renewal_date);
                 }
                 $new_next_plan_id = $new_next_plan->getId();
                 // update and flag as problem reported, as we will send an email to user
                 $sub->setRenewalPlanId($new_next_plan_id, true);
                 $msg .= "doesn't exist, changed to = {$new_next_plan_id}";
                 $sub->sendOnRenewalSoonWarning('wp-plan-not-available-renewal', $new_next_plan);
             } elseif (!$next_plan->hasSufficientQuotas(WpWikiplace::countWikiplacesOwnedByUser($user_id), WpPage::countPagesOwnedByUser($user_id), WpPage::countDiskspaceUsageByUser($user_id))) {
                 // change to the current plan suggested renewal one
                 $curr_plan = $sub->getPlan();
                 $new_next_plan = $curr_plan->getRenewalPlan($renewal_date);
                 $new_next_plan_id = $new_next_plan->getId();
                 // update and flag as problem reported, as we will send an email to user
                 $sub->setRenewalPlanId($new_next_plan_id, true);
                 $msg .= "unsufficient quotas, changed to = {$new_next_plan_id}";
                 $sub->sendOnRenewalSoonWarning('wp-insufficient-quota', $next_plan);
             } elseif (!$next_plan->isAvailableForRenewal($renewal_date)) {
                 // ensure the next plan will still be available
                 // change to the planned renwal plan suggested renewal one
                 $new_next_plan = $next_plan->getRenewalPlan($renewal_date);
                 $new_next_plan_id = $new_next_plan->getId();
                 // update and flag as problem reported, as we will send an email to user
                 $sub->setRenewalPlanId($new_next_plan_id, true);
                 $msg .= "will not be available, changed to = {$new_next_plan_id}";
                 $sub->sendOnRenewalSoonWarning('wp-plan-not-available-renewal', $next_plan);
             } else {
                 // it seems to be ok :)
                 $msg .= 'renewal will be ok';
                 $sub->sendOnRenewalSoonValid();
                 $sub->setRenewalPlanNotified();
             }
         }
         $this->output("{$msg}\n");
     }
     $this->output("[" . WpSubscription::now() . ": END]\n");
 }
 private function subscribeFor($plan_id = null, $user_name = null, $confirm = false)
 {
     $output = $this->getOutput();
     $output->addWikiText("=== Subscribe For ===");
     $this->prettyOutput($output, array('plan' => $plan_id, 'user' => $user_name));
     $output->addWikiText("----");
     if (empty($plan_id) || !is_numeric($plan_id)) {
         $output->addWikiText("=== Specify a plan id in integer format. ===");
         return;
     }
     $plan = WpPlan::newFromId($plan_id);
     if (is_null($plan)) {
         $output->addWikiText("=== No plan with that identifier was found. ===");
         return;
     }
     $output->addWikiText("=== Plan ===");
     $price = $plan->getPrice();
     $this->prettyOutput($output, array('id' => $plan->getId(), 'name' => $plan->getName(), 'period' => $plan->getPeriod() . ' month(s)', 'by invitation only' => $plan->isInvitationRequired() ? 'yes' : 'no', 'price' => $price['amount'] . ' ' . $price['currency']));
     $output->addWikiText("----");
     if (empty($user_name)) {
         $output->addWikiText("=== Specify a user name. ===");
         return;
     }
     $user = User::newFromName($user_name);
     if (!$user || $user->getId() == 0) {
         $output->addWikiText("=== ERROR The user doesn't exist ! ===");
         return;
     }
     $output->addWikiText("=== User to subscribe for ===");
     $this->prettyOutput($output, array('id' => $user->getId(), 'name' => $user->getName(), 'email' => $user->getEmail(), 'email confirmed' => $user->isEmailConfirmed() ? 'yes' : 'no', 'timestamp of account creation' => $user->getRegistration()));
     if (!$user->isEmailConfirmed()) {
         $output->addWikiText("==== WARNING Email is not confirmed ! ====");
     }
     $last_subscription = WpSubscription::newByUserId($user->getId());
     if (is_null($last_subscription)) {
         $output->addWikiText("=== The user doesn't have any subscription. ===");
     } else {
         $output->addWikiText("==== Last subscription ====");
         $this->prettyOutput($output, array('id' => $last_subscription->getId(), 'starts' => $last_subscription->getStart(), 'ends' => $last_subscription->getEnd(), 'active' => $last_subscription->isActive() ? "yes" : "no", 'transaction status' => $last_subscription->getTmrStatus()));
         if ($last_subscription->isActive()) {
             $output->addWikiText("==== ERROR Last subscription is still active ! ====");
             return;
         }
         $lastPlan = $last_subscription->getPlan();
         if (is_null($lastPlan)) {
             $output->addWikiText("=== ERROR The subscribed plan doesn't exist ! ===");
             return;
         }
         $output->addWikiText("==== Subscribed plan ====");
         $price = $lastPlan->getPrice();
         $this->prettyOutput($output, array('id' => $lastPlan->getId(), 'name' => $lastPlan->getName(), 'period' => $lastPlan->getPeriod() . ' month(s)', 'by invitation only' => $lastPlan->isInvitationRequired() ? 'yes' : 'no', 'price' => $price['amount'] . ' ' . $price['currency']));
     }
     $check = WpSubscription::canSubscribe($user);
     if (is_string($check)) {
         $output->addWikiText("=== ERROR The user cannot take a subscription ! ===");
         $output->addWikiText($check);
         return;
     } else {
         $output->addWikiText("=== The user can take a subscription. ===");
     }
     $output->addWikiText("----");
     if ($confirm !== true) {
         $output->addWikiText("=== To confirm ===");
         $output->addHTML($this->getLink(self::ACTION_SUBSCRIBER_FOR, array('plan' => $plan_id, 'user' => $user_name), true));
     } else {
         $subscription = WpSubscription::subscribe($user, $plan);
         if (is_null($subscription)) {
             $output->addWikiText("=== An error occured ! ===");
         } else {
             $output->addWikiText("== Done ! ==");
             $output->addWikiText("==== New subscription ====");
             $this->prettyOutput($output, array('id' => $subscription->getId(), 'buyer user id' => $subscription->getBuyerUserId(), 'starts' => $subscription->getStart(), 'ends' => $subscription->getEnd(), 'active' => $subscription->isActive() ? "yes" : "no", 'transaction status' => $subscription->getTmrStatus()));
         }
     }
 }
Example #6
0
 /**
  * Create a transaction record and return it. 
  * @param int $user_id
  * @param string $user_email
  * @param WpPlan $plan
  * @param string $type
  * @return array TMR as array
  */
 private static function createTMR($user_id, $user_email, $plan, $type)
 {
     $price = $plan->getPrice();
     $tmr = array('tmr_type' => $type, 'tmr_user_id' => $user_id, 'tmr_mail' => $user_email, 'tmr_ip' => IP::sanitizeIP(wfGetIP()), 'tmr_amount' => -$price['amount'], 'tmr_currency' => $price['currency'], 'tmr_desc' => 'wpp-' . $plan->getName(), 'tmr_status' => 'PE');
     wfRunHooks('CreateTransaction', array(&$tmr));
     return $tmr;
 }