Esempio n. 1
0
 public function createSubscription($data, $form)
 {
     //Get member id
     $memberId = $data['MemberID'];
     //Get product id
     $productId = $data['ProductID'];
     // Get the Page controller
     $Page_Ctrl = new Page_Controller();
     //Get InfusionSoft Api
     $app = $Page_Ctrl->getInfusionSoftApi();
     // Get curent date
     $curdate = $app->infuDate(date('j-n-Y'));
     $member = Member::get()->byID($memberId);
     //Find or create the 'user' group and add the member to the group
     if (!($userGroup = DataObject::get_one('Group', "Code = 'customers'"))) {
         $userGroup = new Group();
         $userGroup->Code = "customers";
         $userGroup->Title = "Customers";
         $userGroup->Write();
         $userGroup->Members()->add($member);
     } else {
         $userGroup->Members()->add($member);
     }
     $isConID = $member->ISContactID;
     $product = Product::get()->byID($productId);
     // Get existing credit card ID
     $creditCard = $Page_Ctrl->getCurrentCreditCard($memberId);
     // Get the current InfusionSoft credit card ID
     $ccID = $creditCard->ISCCID;
     //Get siteconfig object
     $config = SiteConfig::current_site_config();
     //Process non-expiring heatmaps purchase
     if ($productId == 4 || $productId == 5 || $productId == 6 || $productId == 7) {
         if (!$data['Quantity']) {
             $form->addErrorMessage('Quantity', "Quantity is required for non-expiring products.", 'bad');
             return $this->edit(Controller::curr()->getRequest());
         }
         // Create an Infusionsoft order
         $invoiceId = $app->blankOrder($isConID, $product->Name, $curdate, 0, 0);
         $orderItem = $app->addOrderItem($invoiceId, intval($Page_Ctrl->getNonExpiringIsProductId($data['ProductID'])), 3, floatval($product->Price), intval($data['Quantity']), $product->Name, $product->Name);
         $result = $app->chargeInvoice($invoiceId, $product->Name, $ccID, $config->MerchantAccount, false);
         // Create an order
         $order = new Order();
         $order->OrderStatus = 'P';
         $order->Amount = $product->Price * $data['Quantity'];
         $order->MemberID = $member->ID;
         $order->ProductID = $data['ProductID'];
         $order->CreditCardID = $creditCard->ID;
         $orderID = $order->write();
         if ($result['Successful']) {
             // Add tag Paid member - prepaid
             $app->grpAssign($isConID, 2290);
             $conDat = array('ContactType' => 'AW Customer');
             $returnFields = array('_AttentionWizard');
             $conDat1 = $app->loadCon($isConID, $returnFields);
             if (isset($conDat1['_AttentionWizard']) && $conDat1['_AttentionWizard'] != 'Paid and Current' && $conDat1['_AttentionWizard'] != 'Free') {
                 $conDat['_AttentionWizard'] = 'Prepaid only - no subscription';
             }
             if (!isset($conDat1['_AttentionWizard'])) {
                 $conDat['_AttentionWizard'] = 'Prepaid only - no subscription';
             }
             $conID = $app->updateCon($isConID, $conDat);
             // Note is added
             $conActionDat = array('ContactId' => $isConID, 'ActionType' => 'UPDATE', 'ActionDescription' => "Purchased AW Prepaid Credits", 'CreationDate' => $curdate, 'ActionDate' => $curdate, 'CompletionDate' => $curdate, 'UserID' => 1);
             $conActionID = $app->dsAdd("ContactAction", $conActionDat);
             // Update order
             $order->OrderStatus = 'c';
             $order->write();
             // Create a Subscription record
             $subscription = new Subscription();
             $subscription->StartDate = date("Y-m-d H:i:s");
             $subscription->MemberID = $member->ID;
             $subscription->ProductID = $data['ProductID'];
             $subscription->OrderID = $orderID;
             $subscription->write();
             // Create a MemberCredits record
             $memberCredits = new MemberCredits();
             $memberCredits->Credits = $product->Credits * $data['Quantity'];
             $memberCredits->MemberID = $member->ID;
             $memberCredits->ProductID = $product->ID;
             $memberCredits->SubscriptionID = $subscription->ID;
             $memberCredits->write();
             $form->sessionMessage("Purchased non-expiring heatmaps successfully.", 'good');
             return Controller::curr()->redirect("admin/manage-data/Subscription/EditForm/field/Subscription/item/{$subscription->ID}/edit");
         } else {
             // Add an AW prospect tag
             $app->grpAssign($isConID, $Page_Ctrl->getISTagIdByPaymentCode(strtoupper($result['Code'])));
             // Add a note
             $conActionDat = array('ContactId' => $isConID, 'ActionType' => 'UPDATE', 'ActionDescription' => "Unsuccessful attempt to purchase prepaid plan", 'CreationDate' => $curdate, 'ActionDate' => $curdate, 'CompletionDate' => $curdate, 'UserID' => 1);
             $conActionID = $app->dsAdd("ContactAction", $conActionDat);
             $form->sessionMessage("Sorry,the payment failed,please update the user credit card.", 'bad');
             return $this->edit(Controller::curr()->getRequest());
         }
     }
     //Process subscriptions
     if ($productId == 1 || $productId == 2 || $productId == 3) {
         $credits = $product->Credits;
         $subscriptionID = $Page_Ctrl->createISSubscription($isConID, $product->ISProductID, $product->RecurringPrice, $ccID, 30);
         if ($subscriptionID && is_int($subscriptionID)) {
             if ($productId == 1 && !$Page_Ctrl->isCCUsedForTrial($creditCard->CreditCardNumber)) {
                 $orderAmount = $product->TrialPrice;
                 $productName = "30 days 1-dollar Trial";
                 $isProductID = 38;
                 $trial = 1;
                 $subscriptionCount = 0;
             } else {
                 $productName = $product->Name;
                 $orderAmount = $product->RecurringPrice;
                 $isProductID = $product->ISInitialProductID;
                 $trial = 0;
                 $subscriptionCount = 1;
             }
             // Create an order
             $order = new Order();
             $order->OrderStatus = 'P';
             $order->Amount = $orderAmount;
             $order->MemberID = $member->ID;
             $order->ProductID = $productId;
             $order->CreditCardID = $creditCard->ID;
             $orderID = $order->write();
             //Create an infusionsoft order
             $invoiceId = $app->blankOrder($isConID, $productName, $curdate, 0, 0);
             $orderItem = $app->addOrderItem($invoiceId, $isProductID, 9, floatval($orderAmount), 1, $productName, $productName);
             $result = $app->chargeInvoice($invoiceId, $productName, $ccID, $config->MerchantAccount, false);
             if ($result['Successful']) {
                 $nextBillDate = $Page_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"));
                 // Update order
                 $order->OrderStatus = 'c';
                 $order->IsTrial = $trial;
                 $order->write();
                 // Create a Subscription record
                 $subscription = new Subscription();
                 $subscription->SubscriptionID = $subscriptionID;
                 $subscription->StartDate = $startDate;
                 $subscription->ExpireDate = $expireDate;
                 $subscription->Status = 1;
                 $subscription->IsTrial = $trial;
                 $subscription->SubscriptionCount = $subscriptionCount;
                 $subscription->MemberID = $member->ID;
                 $subscription->ProductID = $productId;
                 $subscription->OrderID = $orderID;
                 $subscription->write();
                 // Create a MemberCredits record
                 $memberCredits = new MemberCredits();
                 $memberCredits->Credits = $credits;
                 $memberCredits->Expire = 1;
                 $memberCredits->ExpireDate = $expireDate;
                 $memberCredits->MemberID = $member->ID;
                 $memberCredits->ProductID = $productId;
                 $memberCredits->SubscriptionID = $subscription->ID;
                 $memberCredits->write();
                 //Get contact custom fields
                 $returnFields = array('_AWofmonths', '_AWstartdate');
                 $conDat1 = $app->loadCon($isConID, $returnFields);
                 if ($productId == 1 && !$Page_Ctrl->isCCUsedForTrial($creditCard->CreditCardNumber)) {
                     // Update Member
                     $member->SignUpTrial = 1;
                     $member->write();
                     //Add the Trial tag
                     $app->grpAssign($isConID, 2216);
                     //Update the contact
                     $conDat = array('_AttentionWizard' => 'Free', 'ContactType' => 'AW Customer', '_AWcanceldate' => null);
                     if (!isset($conDat1['_AWstartdate'])) {
                         $conDat['_AWstartdate'] = $curdate;
                     }
                     $app->updateCon($isConID, $conDat);
                     // Mark credit card as TrialCreditCard
                     $creditCard->UsedForTrial = 1;
                     $creditCard->write();
                 } else {
                     if (!isset($conDat1['_AWofmonths'])) {
                         $conDat1['_AWofmonths'] = 0;
                     }
                     // Add the InfusionSoft tag
                     $isTagId = $Page_Ctrl->getISTagIdByProduct($productId);
                     $app->grpRemove($isConID, $isTagId);
                     $app->grpAssign($isConID, $isTagId);
                     //Remove trial tag if exists
                     $app->grpRemove($isConID, 2216);
                     //Update the InfusionSoft contact details
                     $conDat = array('_AWofmonths' => $conDat1['_AWofmonths'] + 1, 'ContactType' => 'AW Customer', '_AttentionWizard' => 'Paid and Current', '_AWcanceldate' => null);
                     if (!isset($conDat1['_AWstartdate'])) {
                         $conDat['_AWstartdate'] = $curdate;
                     }
                     $app->updateCon($isConID, $conDat);
                     //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);
                 }
                 // Remove previous cancel tags
                 $app->grpRemove($isConID, 2226);
                 $app->grpRemove($isConID, 2758);
                 $app->grpRemove($isConID, 2682);
                 $app->grpRemove($isConID, 2680);
                 $app->grpRemove($isConID, 2694);
                 $app->grpRemove($isConID, 3019);
                 $app->grpRemove($isConID, 3097);
                 $form->sessionMessage("The Subscription is created successfully.", 'good');
                 return Controller::curr()->redirect("admin/manage-data/Subscription/EditForm/field/Subscription/item/{$subscription->ID}/edit");
             } else {
                 //Set the subscription to Inactive
                 $Page_Ctrl->setSubscriptionStatus($subscriptionID, 'Inactive');
                 if ($productId == 1 && !$Page_Ctrl->isCCUsedForTrial($creditCard->CreditCardNumber)) {
                     $aw = 'Unsuccessful trial sign-up';
                 } else {
                     $aw = 'Unsuccessful paid sign-up';
                 }
                 $conDat = array('_AttentionWizard' => $aw);
                 $app->updateCon($isConID, $conDat);
                 // Add an AW prospect tag
                 $app->grpAssign($isConID, $Page_Ctrl->getISTagIdByPaymentCode($result['Code']));
                 // Add a note
                 $conActionDat = array('ContactId' => $isConID, 'ActionType' => 'UPDATE', 'ActionDescription' => "Unsuccessful attempt to sign-up for AW", 'CreationDate' => $curdate, 'ActionDate' => $curdate, 'CompletionDate' => $curdate, 'UserID' => 1);
                 $conActionID = $app->dsAdd("ContactAction", $conActionDat);
                 $form->sessionMessage("Sorry,the payment failed due to some reason.please update the user credit card.", 'bad');
                 return $this->edit(Controller::curr()->getRequest());
             }
         } else {
             // Add an AW prospect tag
             $app->grpAssign($isConID, 3097);
             //Update InfusionSoft contact
             if ($productId == 1 && !$Page_Ctrl->isCCUsedForTrial($creditCard->CreditCardNumber)) {
                 $aw = 'Unsuccessful trial sign-up';
             } else {
                 $aw = 'Unsuccessful paid sign-up';
             }
             $conDat = array('_AttentionWizard' => $aw);
             $app->updateCon($isConID, $conDat);
             // Add a note
             $conActionDat = array('ContactId' => $isConID, 'ActionType' => 'UPDATE', 'ActionDescription' => "Unsuccessful attempt to sign-up for AW", 'CreationDate' => $curdate, 'ActionDate' => $curdate, 'CompletionDate' => $curdate, 'UserID' => 1);
             $conActionID = $app->dsAdd("ContactAction", $conActionDat);
             $form->sessionMessage("Sorry,the subscription failed due to some reason.please try again.", 'bad');
             return $this->edit(Controller::curr()->getRequest());
         }
     }
 }