Exemplo n.º 1
0
 public function processNew($formData)
 {
     if (!isset($formData['Plan'])) {
         //check the key exists and value is not NULL
         throw new MWException('Cannot process new subscription, no data.');
     }
     $plan = WpPlan::newFromId($formData['Plan']);
     if ($plan === null) {
         throw new MWException('Cannot process new subscription, unknown plan.');
     }
     // displayNew() checked $user->canSubscribe() and validatePlanId() checked $user->canSubscribeTo()
     // so now, the subscription can be really done
     // NOTE: if an invitation code has been typed, it will be consumed even if the plan doesn't require it
     $user = $this->getUser();
     $subscription = WpSubscription::subscribe($user, $plan, $this->invitation);
     if ($subscription == null) {
         return wfMessage('sz-internal-error')->text();
     }
     if ($this->invitation instanceof WpInvitation) {
         $this->invitation->consume();
     }
     $this->just_subscribed = $subscription;
     return true;
 }
 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()));
         }
     }
 }