Exemplo n.º 1
0
 public function changeSubscription($data, $form)
 {
     //Get the record ID
     $id = Controller::curr()->request->param('ID');
     // Get the subscription
     $subscription = Subscription::get()->byID($id);
     //Get the new product ID
     $newProductID = $data['ProductID'];
     if ($subscription->ProductID == $newProductID) {
         $form->sessionMessage("Please select a new subscription.", 'bad');
         return $this->edit(Controller::curr()->getRequest());
     }
     // Get the Page controller
     $Pg_Ctrl = new Page_Controller();
     // Get InfusionSoft Api
     $app = $Pg_Ctrl->getInfusionSoftApi();
     // Get AttentionWizard member
     $member = Member::get()->byID($data['MemberID']);
     // Get IndusionSoft contact ID
     $isConID = $member->ISContactID;
     //Get current date
     $curdate = $app->infuDate(date('j-n-Y'));
     //Get old order
     $oldOrder = $subscription->Order();
     //Get new product
     $product = Product::get()->byID($newProductID);
     $credits = $product->Credits;
     $isProductID = $product->ISInitialProductID;
     // Get the current InfusionSoft credit card ID
     $creditCard = $Pg_Ctrl->getCurrentCreditCard($member->ID);
     if (!$creditCard) {
         $form->sessionMessage("The user does not have a Credit Card on account, please add a credit card.", 'bad');
         return $this->edit(Controller::curr()->getRequest());
     }
     $ccID = $creditCard->ISCCID;
     $subscriptionID = $Pg_Ctrl->createISSubscription($isConID, $product->ISProductID, $product->RecurringPrice, $ccID, 30);
     if ($subscriptionID && is_int($subscriptionID)) {
         // Create an order
         $order = new Order();
         $order->OrderStatus = 'P';
         $order->Amount = $product->RecurringPrice;
         $order->MemberID = $member->ID;
         $order->ProductID = $newProductID;
         $order->CreditCardID = $creditCard->ID;
         $orderID = $order->write();
         //Create an infusionsoft order
         $config = SiteConfig::current_site_config();
         $invoiceId = $app->blankOrder($isConID, $product->Name, $curdate, 0, 0);
         $orderItem = $app->addOrderItem($invoiceId, $isProductID, 9, floatval($product->RecurringPrice), 1, $product->Name, $product->Name);
         $result = $app->chargeInvoice($invoiceId, $product->Name, $ccID, $config->MerchantAccount, false);
         if ($result['Successful']) {
             $nextBillDate = $Pg_Ctrl->getSubscriptionNextBillDate($subscriptionID);
             $expireDate = date('Y-m-d H:i:s', strtotime($nextBillDate));
             $startDate = date('Y-m-d H:i:s', strtotime($expireDate . "-30 days"));
             //Set the current subscription to Inactive
             $Pg_Ctrl->setSubscriptionStatus($subscription->SubscriptionID, 'Inactive');
             //Remove trial tag if exists
             $app->grpRemove($isConID, 2216);
             //get old Tag ID
             if ($Pg_Ctrl->isTrialMember($member->ID)) {
                 $oldISTagID = 2216;
             } else {
                 $oldISTagID = $Pg_Ctrl->getISTagIdByProduct($oldOrder->ProductID);
             }
             //Remove old tag ID
             $app->grpRemove($isConID, $oldISTagID);
             $newISTagID = $Pg_Ctrl->getISTagIdByProduct($newProductID);
             //Add new tag ID
             $app->grpAssign($isConID, $newISTagID);
             //Add a note
             $conActionDat = array('ContactId' => $isConID, 'ActionType' => 'UPDATE', 'ActionDescription' => "Payment made for AW service", 'CreationDate' => $curdate, 'ActionDate' => $curdate, 'CompletionDate' => $curdate, 'CreationNotes' => "{$product->Name} Subscription", 'UserID' => 1);
             $app->dsAdd("ContactAction", $conActionDat);
             $returnFields = array('_AWofmonths');
             $conData = $app->loadCon($isConID, $returnFields);
             $conDat = array('_AWofmonths' => (isset($conData['_AWofmonths']) ? $conData['_AWofmonths'] : 0) + 1, '_AttentionWizard' => 'Paid and Current');
             $app->updateCon($isConID, $conDat);
             //Create a new Subscription
             $newSubscription = new Subscription();
             $newSubscription->StartDate = $startDate;
             $newSubscription->ExpireDate = $expireDate;
             $newSubscription->SubscriptionID = $subscriptionID;
             $newSubscription->Status = 1;
             $newSubscription->IsTrial = 0;
             $newSubscription->SubscriptionCount = 1;
             $newSubscription->MemberID = $member->ID;
             $newSubscription->ProductID = $newProductID;
             $newSubscription->OrderID = $orderID;
             $newSubscription->write();
             // Create a MemberCredits record
             $memberCredits = new MemberCredits();
             $memberCredits->Credits = $credits;
             $memberCredits->Expire = 1;
             $memberCredits->ExpireDate = $expireDate;
             $memberCredits->MemberID = $member->ID;
             $memberCredits->ProductID = $newProductID;
             $memberCredits->SubscriptionID = $newSubscription->ID;
             $memberCredits->write();
             // Update order
             $order->OrderStatus = 'c';
             $order->write();
             //Update old subscription status
             $subscription->Status = 0;
             $subscription->IsTrial = 0;
             $subscription->write();
         } else {
             //Set the subscription to Inactive
             $Pg_Ctrl->setSubscriptionStatus($subscriptionID, 'Inactive');
             $form->sessionMessage("Sorry,the payment failed due to some reason.please update your credit card", 'bad');
             return $this->edit(Controller::curr()->getRequest());
         }
     } else {
         $form->sessionMessage("Sorry,the subscription failed due to some reason.please try again", 'bad');
         return $this->edit(Controller::curr()->getRequest());
     }
     $form->sessionMessage("The Subscription is changed successfully.", 'good');
     return Controller::curr()->redirect("admin/manage-data/Subscription/EditForm/field/Subscription/item/{$newSubscription->ID}/edit");
 }