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; }
/** * Returns the renewal suggested plan. Please note that the returned plan * can be different of the renewal plan id stored in wpp_renew_wpp_id field. * This function ensure that the returned plan is renewable. In the worst * case, it returns an instance of WP_FALLBACK_PLAN_ID plan. * @param string $when Optional, to ensure that the returned plan is available at this date * @return WpPlan */ public function getRenewalPlan($when = null) { if ($this->isAvailableForRenewal($when)) { return $this; } $checked = array(); // avoid infinite loop $next_plan_id = $this->wpp_renew_wpp_id; // starts with the suggested plan $next_plan = null; while ($next_plan_id != null && !in_array($next_plan_id, $checked)) { $checked[] = $next_plan_id; $next_plan = WpPlan::newFromId($next_plan_id); if ($next_plan == null) { $next_plan_id = null; // problem } if ($next_plan->isAvailableForRenewal($when)) { wfDebugLog('wikiplaces-debug', 'WpPlan[' . $this->wpp_id . ']->getRenewalPlan(' . $when . ') in base was [' . $this->wpp_renew_wpp_id . '], returned is [' . $next_plan_id . ']'); if ($this->wpp_renew_wpp_id != $next_plan_id) { wfDebugLog('wikiplaces', 'WpPlan[' . $this->wpp_id . ']->getRenewalPlan(' . $when . ') WARNING in base was [' . $this->wpp_renew_wpp_id . '], but was not renewable (not available at this date or db pb), so returned is [' . $next_plan_id . ']'); } return $next_plan; } else { $next_plan_id = $next_plan->wpp_renew_wpp_id; // continue search } } // if we arrive here, there was a problem, so return fallback wfDebugLog('wikiplaces', 'WpPlan[' . $this->wpp_id . ']->getRenewalPlan(' . $when . ') WARNING in base was [' . $this->wpp_renew_wpp_id . '], but fallback returned [' . WP_FALLBACK_PLAN_ID . ']'); return WpPlan::newFromId(WP_FALLBACK_PLAN_ID); }
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())); } } }
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"); }
/** * Set the renewal plan fro wps_renew_wpp_id */ private function setRenewalPlan() { $this->renewalPlan = WpPlan::newFromId($this->getRenewalPlanId()); }