Exemplo n.º 1
0
 public function UpdateBillingAddress($data, $form)
 {
     $member = Member::currentUser();
     $isConID = $member->ISContactID;
     //Get InfusionSoft Api
     $creditCardType = Controller::curr()->getISCreditCardType($data['CreditCardType']);
     $app = Controller::curr()->getInfusionSoftApi();
     if (!isset($data['CreditCardNumber']) || $data['CreditCardNumber'] != $data['CreditCardNumberCopy'] && !is_numeric($data['CreditCardNumber'])) {
         Controller::curr()->setMessage('Error', 'The credit card is invalid!');
         return Controller::curr()->redirect(Controller::curr()->Link('#tabs-2'));
     }
     if ($data['CreditCardNumber'] == $data['CreditCardNumberCopy']) {
         $cardNumber = $data['CreditCardNumberCur'];
     } else {
         $cardNumber = $data['CreditCardNumber'];
     }
     // Validate credit card
     $card = array('CardType' => $creditCardType, 'ContactId' => $isConID, 'CardNumber' => $cardNumber, 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear'], 'CVV2' => $data['CreditCardCVV']);
     $result = $app->validateCard($card);
     if ($result['Valid'] != 'true') {
         Controller::curr()->setMessage('Error', 'The credit card is invalid!');
         return Controller::curr()->redirect(Controller::curr()->Link('#tabs-2'));
     }
     // Get country text from code
     $country = Geoip::countryCode2name($data['Country']);
     if ($data['CreditCardNumber'] == $data['CreditCardNumberCopy']) {
         //Get the credit card
         $creditCard = CreditCard::get()->filter(array('CreditCardNumber' => $data['CreditCardNumberCur'], 'MemberID' => $member->ID))->First();
         //Update the credit card on InfusionSoft
         $ccData = array('FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CVV2' => $data['CreditCardCVV'], 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear']);
         $app->dsUpdate("CreditCard", $creditCard->ISCCID, $ccData);
         //Update the credit card on site
         $creditCard->CreditCardType = $data['CreditCardType'];
         $creditCard->NameOnCard = $data['NameOnCard'];
         $creditCard->CreditCardCVV = $data['CreditCardCVV'];
         $creditCard->ExpiryMonth = $data['ExpiryMonth'];
         $creditCard->ExpiryYear = $data['ExpiryYear'];
         $creditCard->Company = $data['Company'];
         $creditCard->StreetAddress1 = $data['StreetAddress1'];
         $creditCard->StreetAddress2 = $data['StreetAddress2'];
         $creditCard->City = $data['City'];
         $creditCard->State = $data['State'];
         $creditCard->PostalCode = $data['PostalCode'];
         $creditCard->Country = $data['Country'];
         $creditCard->Current = 1;
         $creditCard->write();
     } else {
         //Find if the credit card exist
         $creditCard = CreditCard::get()->filter(array('CreditCardNumber' => $data['CreditCardNumber'], 'MemberID' => $member->ID))->First();
         if ($creditCard) {
             //Get current credit card, un-mark it as current
             Controller::curr()->unsetCurrentCreditCard($member->ID);
             $ccID = $creditCard->ISCCID;
             //Update the credit card on InfusionSoft
             $ccData = array('FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CVV2' => $data['CreditCardCVV'], 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear']);
             $app->dsUpdate("CreditCard", $ccID, $ccData);
             //Update the credit card on site
             $creditCard->CreditCardType = $data['CreditCardType'];
             $creditCard->NameOnCard = $data['NameOnCard'];
             $creditCard->CreditCardCVV = $data['CreditCardCVV'];
             $creditCard->ExpiryMonth = $data['ExpiryMonth'];
             $creditCard->ExpiryYear = $data['ExpiryYear'];
             $creditCard->Company = $data['Company'];
             $creditCard->StreetAddress1 = $data['StreetAddress1'];
             $creditCard->StreetAddress2 = $data['StreetAddress2'];
             $creditCard->City = $data['City'];
             $creditCard->State = $data['State'];
             $creditCard->PostalCode = $data['PostalCode'];
             $creditCard->Country = $data['Country'];
             $creditCard->Current = 1;
             $creditCard->write();
         } else {
             // Add the credit card on InfusionSoft
             $ccData = array('ContactId' => $isConID, 'FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CardNumber' => $data['CreditCardNumber'], 'CVV2' => $data['CreditCardCVV'], 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear']);
             $ccID = $app->dsAdd("CreditCard", $ccData);
             //Get current credit card un-mark it as current
             Controller::curr()->unsetCurrentCreditCard($member->ID);
             // Store Credit card on site and mark it as current
             $newCreditCard = new CreditCard();
             $newCreditCard->CreditCardType = $data['CreditCardType'];
             $newCreditCard->CreditCardNumber = $data['CreditCardNumber'];
             $newCreditCard->NameOnCard = $data['NameOnCard'];
             $newCreditCard->CreditCardCVV = $data['CreditCardCVV'];
             $newCreditCard->ExpiryMonth = $data['ExpiryMonth'];
             $newCreditCard->ExpiryYear = $data['ExpiryYear'];
             $newCreditCard->Company = $data['Company'];
             $newCreditCard->StreetAddress1 = $data['StreetAddress1'];
             $newCreditCard->StreetAddress2 = $data['StreetAddress2'];
             $newCreditCard->City = $data['City'];
             $newCreditCard->State = $data['State'];
             $newCreditCard->PostalCode = $data['PostalCode'];
             $newCreditCard->Country = $data['Country'];
             $newCreditCard->Current = 1;
             $newCreditCard->ISCCID = $ccID;
             $newCreditCard->MemberID = $member->ID;
             $newCreditCard->write();
         }
         //Update the user active Infusionsoft subscription Credit Card
         $subscription = Subscription::get()->filter(array('MemberID' => $member->ID, 'Status' => 1))->first();
         if ($subscription) {
             $subData = array('CC1' => $ccID);
             $app->dsUpdate("RecurringOrder", $subscription->SubscriptionID, $subData);
         }
     }
     //Update Member
     $member->FirstName = $data['FirstName'];
     $member->Surname = $data['LastName'];
     $member->write();
     // Update billing address on InfusionSoft
     $conDat = array('FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'Company' => $data['Company'], 'StreetAddress1' => $data['StreetAddress1'], 'StreetAddress2' => $data['StreetAddress2'], 'City' => $data['City'], 'State' => $data['State'], 'PostalCode' => $data['PostalCode'], 'Country' => $country);
     $conID = $app->updateCon($isConID, $conDat);
     Controller::curr()->setMessage('Success', 'Credit Card and Billing Address successfully updated.');
     return Controller::curr()->redirect(Controller::curr()->Link('#tabs-2'));
 }
Exemplo n.º 2
0
 function doPrepaidSignup()
 {
     $data = $_POST;
     //Check for existing member email address
     if ($member = DataObject::get_one("Member", "`Email` = '" . Convert::raw2sql($data['Email']) . "'")) {
         return "inlineMsg1";
     }
     $currentYear = date('Y');
     $currentMonth = date('n');
     //Stop sign-up when the credit card is expired
     if ($data['ExpirationYear'] < $currentYear) {
         return "inlineMsg6";
     }
     if ($data['ExpirationYear'] == $currentYear) {
         if ($data['ExpirationMonth'] < $currentMonth) {
             return "inlineMsg6";
         }
     }
     //Get InfusionSoft Api
     $app = $this->getInfusionSoftApi();
     // Get country text from code
     $country = Geoip::countryCode2name($data['Country']);
     // Create IndusionSoft contact
     $returnFields = array('Id');
     $conInfo = $app->findByEmail($data['Email'], $returnFields);
     if (count($conInfo)) {
         $isConID = $conInfo[0]['Id'];
     } else {
         $conDat = array('FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'Company' => $data['Company'], 'StreetAddress1' => $data['StreetAddress1'], 'StreetAddress2' => $data['StreetAddress2'], 'City' => $data['City'], 'State' => $data['State'], 'PostalCode' => $data['PostalCode'], 'Country' => $country, 'Email' => $data['Email']);
         $isConID = $app->addCon($conDat);
     }
     // Locate existing credit card
     $ccID = $app->locateCard($isConID, substr($data['CreditCardNumber'], -4, 4));
     $creditCardType = $this->getISCreditCardType($data['CreditCardType']);
     if (!$ccID) {
         //Validate the credit card
         $card = array('CardType' => $creditCardType, 'ContactId' => $isConID, 'CardNumber' => $data['CreditCardNumber'], 'ExpirationMonth' => sprintf("%02s", $data['ExpirationMonth']), 'ExpirationYear' => $data['ExpirationYear'], 'CVV2' => $data['CVVCode']);
         $result = $app->validateCard($card);
         if ($result['Valid'] == 'false') {
             return "inlineMsg5";
         }
         $ccData = array('ContactId' => $isConID, 'FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CardNumber' => $data['CreditCardNumber'], 'CVV2' => $data['CVVCode'], 'ExpirationMonth' => sprintf("%02s", $data['ExpirationMonth']), 'ExpirationYear' => $data['ExpirationYear']);
         $ccID = $app->dsAdd("CreditCard", $ccData);
     }
     // Create AttentionWizard member
     $member = new Member();
     $member->FirstName = $data['FirstName'];
     $member->Surname = $data['LastName'];
     $member->Email = $data['Email'];
     $member->Password = $data['Password']['_Password'];
     $member->ISContactID = $isConID;
     $memberID = $member->write();
     //Find or create the 'user' group
     if (!($userGroup = DataObject::get_one('Group', "Code = 'customers'"))) {
         $userGroup = new Group();
         $userGroup->Code = "customers";
         $userGroup->Title = "Customers";
         $userGroup->Write();
     }
     //Add member to user group
     $userGroup->Members()->add($member);
     //Get the current date
     $curdate = $app->infuDate(date('j-n-Y'));
     $product = Product::get()->byID(7);
     // Store credit card info
     $creditCard = new CreditCard();
     $creditCard->CreditCardType = $data['CreditCardType'];
     $creditCard->CreditCardNumber = $data['CreditCardNumber'];
     $creditCard->NameOnCard = $data['NameOnCard'];
     $creditCard->CreditCardCVV = $data['CVVCode'];
     $creditCard->ExpiryMonth = $data['ExpirationMonth'];
     $creditCard->ExpiryYear = $data['ExpirationYear'];
     $creditCard->Company = $data['Company'];
     $creditCard->StreetAddress1 = $data['StreetAddress1'];
     $creditCard->StreetAddress2 = $data['StreetAddress2'];
     $creditCard->City = $data['City'];
     $creditCard->State = $data['State'];
     $creditCard->PostalCode = $data['PostalCode'];
     $creditCard->Country = $data['Country'];
     $creditCard->Current = 1;
     $creditCard->ISCCID = $ccID;
     $creditCard->MemberID = $memberID;
     $creditCardID = $creditCard->write();
     // Create an Infusionsoft order
     $config = SiteConfig::current_site_config();
     $invoiceId = $app->blankOrder($isConID, $product->Name, $curdate, 0, 0);
     $orderItem = $app->addOrderItem($invoiceId, $this->getNonExpiringIsProductId(7), 3, floatval($data['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 = $data['Price'] * $data['Quantity'];
     $order->MemberID = $memberID;
     $order->ProductID = 7;
     $order->CreditCardID = $creditCardID;
     $orderID = $order->write();
     $returnFields = array('_AttentionWizard', 'Leadsource');
     $conDat1 = $app->loadCon($isConID, $returnFields);
     if ($result['Successful']) {
         // Add tag Paid member - prepaid
         $app->grpAssign($isConID, 2290);
         $conDat = array('ContactType' => 'AW Customer');
         if (!isset($conDat1['_AttentionWizard'])) {
             $conDat['_AttentionWizard'] = 'Prepaid only - no subscription';
         }
         if (isset($conDat1['_AttentionWizard']) && $conDat1['_AttentionWizard'] != 'Paid and Current' && $conDat1['_AttentionWizard'] != 'Free') {
             $conDat['_AttentionWizard'] = 'Prepaid only - no subscription';
         }
         if (!isset($conDat1['Leadsource']) || !$conDat1['Leadsource']) {
             $conDat['Leadsource'] = 'AttentionWizard';
         }
         $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 = $memberID;
         $subscription->ProductID = 7;
         $subscription->OrderID = $orderID;
         $subscription->write();
         // Create a MemberCredits record
         $memberCredits = new MemberCredits();
         $memberCredits->Credits = $product->Credits * $data['Quantity'];
         $memberCredits->MemberID = $memberID;
         $memberCredits->ProductID = 7;
         $memberCredits->SubscriptionID = $subscription->ID;
         $memberCredits->write();
         $member->logIn();
         $this->setMessage('Success', 'Purchased non-expiring heatmaps successfully.');
         return 'url1';
     } else {
         //Update Infusionsoft contact
         $conDat = array('_AttentionWizard' => 'Unsuccessful prepaid sign-up', 'ContactType' => 'AW Prospect');
         if (!isset($conDat1['Leadsource'])) {
             $conDat['Leadsource'] = 'AttentionWizard';
         }
         $app->updateCon($isConID, $conDat);
         // Add an AW prospect tag
         $app->grpAssign($isConID, $this->getISTagIdByPaymentCode(strtoupper($result['Code'])));
         // Add a note
         $conActionDat = array('ContactId' => $isConID, 'ActionType' => 'UPDATE', 'ActionDescription' => "Unsuccessful attempt to sign-up prepaid plan", 'CreationDate' => $curdate, 'ActionDate' => $curdate, 'CompletionDate' => $curdate, 'UserID' => 1);
         $conActionID = $app->dsAdd("ContactAction", $conActionDat);
         $member->logIn();
         $this->setMessage('Error', 'Sorry,the payment failed due to some reason.please update your credit card.');
         return 'url2';
     }
 }
Exemplo n.º 3
0
 public function addCreditCard($data, $form)
 {
     //Check if the credit card exists
     $creditCard = CreditCard::get()->filter(array('CreditCardNumber' => $data['CreditCardNumber']))->First();
     if ($creditCard) {
         $form->sessionMessage("The credit card exists already!", 'bad');
         return $this->edit(Controller::curr()->getRequest());
     }
     //Get the member
     $member = Member::get()->byID($data['MemberID']);
     $isConID = $member->ISContactID;
     // Get the Page controller
     $Pg_Ctrl = new Page_Controller();
     //Get InfusionSoft Api
     $creditCardType = $Pg_Ctrl->getISCreditCardType($data['CreditCardType']);
     $app = $Pg_Ctrl->getInfusionSoftApi();
     // Validate credit card
     $card = array('CardType' => $creditCardType, 'ContactId' => $isConID, 'CardNumber' => $data['CreditCardNumber'], 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear'], 'CVV2' => $data['CreditCardCVV']);
     $result = $app->validateCard($card);
     if ($result['Valid'] != 'true') {
         $form->sessionMessage("The credit card details are invalid!", 'bad');
         return $this->edit(Controller::curr()->getRequest());
     }
     //Get country text
     $country = Geoip::countryCode2name($data['Country']);
     // Locate existing credit card
     $ccID = $app->locateCard($isConID, substr($data['CreditCardNumber'], -4, 4));
     if (!$ccID) {
         //Add credit card on InfusionSoft
         $ccData = array('ContactId' => $isConID, 'BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CardNumber' => $data['CreditCardNumber'], 'CVV2' => $data['CreditCardCVV'], 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear']);
         $ccID = $app->dsAdd("CreditCard", $ccData);
     } else {
         //Update credit card on InfusionSoft
         $ccData = array('BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CVV2' => $data['CreditCardCVV'], 'ExpirationMonth' => sprintf("%02s", $data['ExpiryMonth']), 'ExpirationYear' => $data['ExpiryYear']);
         $app->dsUpdate("CreditCard", $ccID, $ccData);
     }
     if (isset($data['Current'])) {
         $Pg_Ctrl->unsetCurrentCreditCard($member->ID);
     }
     // Update billing address on InfusionSoft
     $conDat = array('Company' => $data['Company'], 'StreetAddress1' => $data['StreetAddress1'], 'StreetAddress2' => $data['StreetAddress2'], 'City' => $data['City'], 'State' => $data['State'], 'PostalCode' => $data['PostalCode'], 'Country' => $country);
     $conID = $app->updateCon($isConID, $conDat);
     //Add the credit card on site
     $creditCard = new CreditCard();
     $creditCard->CreditCardType = $data['CreditCardType'];
     $creditCard->CreditCardNumber = $data['CreditCardNumber'];
     $creditCard->NameOnCard = $data['NameOnCard'];
     $creditCard->CreditCardCVV = $data['CreditCardCVV'];
     $creditCard->ExpiryMonth = $data['ExpiryMonth'];
     $creditCard->ExpiryYear = $data['ExpiryYear'];
     $creditCard->Company = $data['Company'];
     $creditCard->StreetAddress1 = $data['StreetAddress1'];
     $creditCard->StreetAddress2 = $data['StreetAddress2'];
     $creditCard->City = $data['City'];
     $creditCard->State = $data['State'];
     $creditCard->PostalCode = $data['PostalCode'];
     $creditCard->Country = $data['Country'];
     $creditCard->ISCCID = $ccID;
     $creditCard->MemberID = $member->ID;
     if (isset($data['Current'])) {
         $creditCard->Current = 1;
     }
     if (isset($data['UsedForTrial'])) {
         $creditCard->UsedForTrial = 1;
     }
     $creditCard->write();
     $form->sessionMessage("Credit Card and Billing Address successfully added.", 'good');
     return Controller::curr()->redirect("admin/manage-data/CreditCard/EditForm/field/CreditCard/item/{$creditCard->ID}/edit");
 }
Exemplo n.º 4
0
 public function moveCreditCardsFromJoomlaToSS()
 {
     die('test2');
     error_reporting(E_ALL);
     ini_set('display_errors', 1);
     ini_set('max_execution_time', 0);
     ini_set('memory_limit', '-1');
     $countries = Geoip::getCountryDropDown();
     $mysqli = $this->getDbConnection();
     $result = $mysqli->query("SELECT *, u.id uid FROM jos_users u\n\t\tINNER JOIN jos_aw_creditcard c ON u.id = c.creditcard_userid\n\t\tLEFT JOIN jos_aw_iscontacts ic ON c.creditcard_userid = ic.member_id\n\t\tLEFT JOIN jos_osemsc_billinginfo bi ON c.creditcard_userid = bi.user_id");
     $totalRec = 0;
     while ($obj = $result->fetch_object()) {
         $ssMember = $this->getSSMember($obj->email);
         if (!$ssMember) {
             continue;
         }
         $creditCard = new CreditCard();
         $creditCard->Created = $obj->creditcard_date;
         $creditCard->LastEdited = $obj->creditcard_date;
         $creditCard->CreditCardType = strtolower($obj->creditcard_type);
         $creditCard->NameOnCard = $obj->creditcard_name;
         $creditCard->CreditCardNumber = $obj->creditcard_number;
         $creditCard->CreditCardCVV = $obj->creditcard_cvv;
         $creditCard->ExpiryMonth = $obj->creditcard_month;
         $creditCard->ExpiryYear = $obj->creditcard_year;
         $creditCard->Company = $obj->company;
         $creditCard->StreetAddress1 = $obj->street1;
         $creditCard->StreetAddress2 = $obj->street2;
         $creditCard->City = $obj->city;
         $creditCard->State = $obj->state_id;
         $creditCard->PostalCode = $obj->postcode;
         $countryCode = array_search($obj->country_id, $countries);
         $creditCard->Country = $countryCode;
         if ($this->joomlaCurrentCreditCard($obj->creditcard_number, $obj->uid)) {
             $creditCard->Current = 1;
         }
         if ($this->joomlaTrialCreditCard($obj->creditcard_number)) {
             $creditCard->UsedForTrial = 1;
         }
         if ($obj->is_contact_id && ($ccID = $this->joomlaGetISCreditCardId($obj->creditcard_number, $obj->is_contact_id))) {
             $creditCard->ISCCID = $ccID;
         }
         $creditCard->MemberID = $ssMember->ID;
         $creditCard->write();
         $totalRec++;
     }
     $mysqli->close();
     echo "Total Cards moved: {$totalRec}";
 }
Exemplo n.º 5
0
 function doSignup()
 {
     $data = $_POST;
     //Stop sign-up if the user does not check the chebox
     if (!isset($data['Agreement'])) {
         return "inlineMsg5";
     }
     if ($this->isCCUsedForTrial("{$data['CreditCardNumber']}") && $data['SubscriptionType'] == 1) {
         return "inlineMsg1";
     }
     $currentYear = date('Y');
     $currentMonth = date('n');
     //Stop sign-up when the credit card is expired
     if ($data['ExpirationYear'] < $currentYear) {
         return "inlineMsg4";
     }
     if ($data['ExpirationYear'] == $currentYear) {
         if ($data['ExpirationMonth'] < $currentMonth) {
             return "inlineMsg4";
         }
     }
     //Get InfusionSoft Api
     $app = $this->getInfusionSoftApi();
     //Get current date
     $curdate = $app->infuDate(date('j-n-Y'));
     //Get the registration form from session
     $regFormData = Session::get('RegistrationFormData');
     // Get country text from code
     $country = Geoip::countryCode2name($data['Country']);
     // Get InfusionSoft Contact ID
     $returnFields = array('Id', 'Leadsource');
     $conInfo = $app->findByEmail($regFormData['Email'], $returnFields);
     if (empty($conInfo)) {
         // If IS contact doesn't exist create one
         $conDat = array('FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'Company' => $data['Company'], 'StreetAddress1' => $data['StreetAddress1'], 'StreetAddress2' => $data['StreetAddress2'], 'City' => $data['City'], 'State' => $data['State'], 'PostalCode' => $data['PostalCode'], 'Country' => $country, 'Email' => $regFormData['Email']);
         if (empty($conInfo)) {
             $conDat['Leadsource'] = 'AttentionWizard';
         }
         $isConID = $app->addCon($conDat);
     } else {
         $isConID = $conInfo[0]['Id'];
     }
     // Locate existing credit card
     $ccID = $app->locateCard($isConID, substr($data['CreditCardNumber'], -4, 4));
     $creditCardType = $this->getISCreditCardType($data['CreditCardType']);
     if (!$ccID) {
         //Validate the credit card
         $card = array('CardType' => $creditCardType, 'ContactId' => $isConID, 'CardNumber' => $data['CreditCardNumber'], 'ExpirationMonth' => sprintf("%02s", $data['ExpirationMonth']), 'ExpirationYear' => $data['ExpirationYear'], 'CVV2' => $data['CVVCode']);
         $result = $app->validateCard($card);
         if ($result['Valid'] == 'false') {
             return "inlineMsg3";
         }
         $ccData = array('ContactId' => $isConID, 'FirstName' => $data['FirstName'], 'LastName' => $data['LastName'], 'BillAddress1' => $data['StreetAddress1'], 'BillAddress2' => $data['StreetAddress2'], 'BillCity' => $data['City'], 'BillState' => $data['State'], 'BillZip' => $data['PostalCode'], 'BillCountry' => $country, 'CardType' => $creditCardType, 'NameOnCard' => $data['NameOnCard'], 'CardNumber' => $data['CreditCardNumber'], 'CVV2' => $data['CVVCode'], 'ExpirationMonth' => sprintf("%02s", $data['ExpirationMonth']), 'ExpirationYear' => $data['ExpirationYear']);
         $ccID = $app->dsAdd("CreditCard", $ccData);
     }
     // Create AttentionWizard member
     $member = new Member();
     $member->FirstName = $data['FirstName'];
     $member->Surname = $data['LastName'];
     $member->Email = $regFormData['Email'];
     $member->Password = $regFormData['Password']['_Password'];
     $member->ISContactID = $isConID;
     $memberID = $member->write();
     //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);
     }
     //Get product details
     $product = Product::get()->byID($data['SubscriptionType']);
     $credits = $product->Credits;
     if ($data['SubscriptionType'] == 1) {
         $orderAmount = $product->TrialPrice;
         $productName = "30 days 1-dollar Trial";
         $isProductID = 38;
     } else {
         $productName = $product->Name;
         $orderAmount = $product->RecurringPrice;
         $isProductID = $product->ISInitialProductID;
     }
     // Store credit card info
     $creditCard = new CreditCard();
     $creditCard->CreditCardType = $data['CreditCardType'];
     $creditCard->CreditCardNumber = $data['CreditCardNumber'];
     $creditCard->NameOnCard = $data['NameOnCard'];
     $creditCard->CreditCardCVV = $data['CVVCode'];
     $creditCard->ExpiryMonth = $data['ExpirationMonth'];
     $creditCard->ExpiryYear = $data['ExpirationYear'];
     $creditCard->Company = $data['Company'];
     $creditCard->StreetAddress1 = $data['StreetAddress1'];
     $creditCard->StreetAddress2 = $data['StreetAddress2'];
     $creditCard->City = $data['City'];
     $creditCard->State = $data['State'];
     $creditCard->PostalCode = $data['PostalCode'];
     $creditCard->Country = $data['Country'];
     $creditCard->Current = 1;
     $creditCard->ISCCID = $ccID;
     $creditCard->MemberID = $memberID;
     $creditCardID = $creditCard->write();
     // Create an order
     $order = new Order();
     $order->OrderStatus = 'P';
     $order->Amount = $orderAmount;
     $order->MemberID = $memberID;
     $order->ProductID = $data['SubscriptionType'];
     $order->CreditCardID = $creditCardID;
     $orderID = $order->write();
     //Create the Infusionsoft subscription
     $subscriptionID = $this->createISSubscription($isConID, $product->ISProductID, $product->RecurringPrice, $ccID, 30);
     if ($subscriptionID && is_int($subscriptionID)) {
         //Create an infusionsoft order
         $config = SiteConfig::current_site_config();
         $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']) {
             // Create a Subscription record
             $nextBillDate = $this->getSubscriptionNextBillDate($subscriptionID);
             $expireDate = date('Y-m-d H:i:s', strtotime($nextBillDate));
             $startDate = date('Y-m-d H:i:s', strtotime($expireDate . "-30 days"));
             $subscription = new Subscription();
             $subscription->StartDate = $startDate;
             $subscription->ExpireDate = $expireDate;
             $subscription->MemberID = $memberID;
             $subscription->ProductID = $data['SubscriptionType'];
             $subscription->OrderID = $orderID;
             $subscription->Status = 1;
             $subscription->SubscriptionID = $subscriptionID;
             $subscription->write();
             // Create a MemberCredits record
             $memberCredits = new MemberCredits();
             $memberCredits->Credits = $credits;
             $memberCredits->ExpireDate = $expireDate;
             $memberCredits->MemberID = $memberID;
             $memberCredits->ProductID = $data['SubscriptionType'];
             $memberCredits->SubscriptionID = $subscription->ID;
             $memberCredits->write();
             // Update order
             $order->OrderStatus = 'c';
             $order->write();
             // If product selected is bronze do a trial signup
             if ($data['SubscriptionType'] == 1) {
                 //Add the InfusionSoft tag
                 $app->grpAssign($isConID, 2216);
                 //Update the InfusionSoft contact details
                 $conDat = array('ContactType' => 'AW Customer', '_AWstartdate' => $curdate, '_AttentionWizard' => 'Free');
                 $app->updateCon($isConID, $conDat);
                 // Update Subscription
                 $subscription->IsTrial = 1;
                 $subscription->SubscriptionCount = 0;
                 $subscription->write();
                 // Update Member
                 $member->SignUpTrial = 1;
                 $member->write();
                 // Update Order
                 $order->IsTrial = 1;
                 $order->write();
                 // Update credit card
                 $creditCard->UsedForTrial = 1;
                 $creditCard->write();
             } else {
                 // Update Subscription
                 $subscription->SubscriptionCount = 1;
                 $subscription->write();
                 // Add the InfusionSoft tag
                 $isTagId = $this->getISTagIdByProduct($data['SubscriptionType']);
                 $app->grpAssign($isConID, $isTagId);
                 //Update the InfusionSoft contact details
                 $returnFields = array('_AWofmonths');
                 $conDat1 = $app->loadCon($isConID, $returnFields);
                 if (!isset($conDat1['_AWofmonths'])) {
                     $conDat1['_AWofmonths'] = 0;
                 }
                 $conDat = array('_AWofmonths' => $conDat1['_AWofmonths'] + 1, 'ContactType' => 'AW Customer', '_AttentionWizard' => 'Paid and Current', '_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);
             }
             $member->logIn();
             $this->setMessage('Success', 'You have signed-up & the Subscription is created successfully');
             return 'url1';
         } else {
             //Set the subscription to Inactive
             $this->setSubscriptionStatus($subscriptionID, 'Inactive');
             //Update InfusionSoft contact
             if ($data['SubscriptionType'] == 1) {
                 $aw = 'Unsuccessful trial sign-up';
             } else {
                 $aw = 'Unsuccessful paid sign-up';
             }
             $conDat = array('ContactType' => 'AW Prospect', '_AttentionWizard' => $aw);
             $app->updateCon($isConID, $conDat);
             // Add an AW prospect tag
             //$app->grpAssign($isConID, $this->getISTagIdByPaymentCode(strtoupper($result['Code'])));
             $app->grpAssign($isConID, $this->getISTagIdByPaymentCode('DECLINED'));
             // 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);
             $member->logIn();
             $this->setMessage('Error', 'Sorry,the payment failed due to some reason.please update your credit card');
             return "url2";
         }
     } else {
         $member->logIn();
         // Add an AW prospect tag
         $app->grpAssign($isConID, 3097);
         //Update InfusionSoft contact
         if ($data['SubscriptionType'] == 1) {
             $aw = 'Unsuccessful trial sign-up';
         } else {
             $aw = 'Unsuccessful paid sign-up';
         }
         $conDat = array('ContactType' => 'AW Prospect', '_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);
         $this->setMessage('Error', 'You have signed-up successfully but the Subscription is not created,please try again.');
         return "url3";
     }
 }