public function callApi(Varien_Object $payment, $amount, $type)
 {
     $order = $payment->getOrder();
     $invoiceNumber = $order->getIncrementId();
     if ($type != 'authorizeandcaptureAIM') {
         /**
          * This will build the profile for the customer
          */
         $customerID = $order->getCustomerId();
         $customerEmail = $order->getCustomerEmail();
         if (!$customerID) {
             /**
              * This will build the guest customer ID since they do not exist in the database yet
              */
             $this->_guest = 1;
             //$customerEmail = 'guest-'. $customerEmail = $order->getCustomerEmail();
             $guestCheck = Mage::getModel('authorizenetcim/guests')->load($customerEmail, 'email');
             if (!$guestCheck->getData()) {
                 $guest = Mage::getModel('authorizenetcim/guests');
                 $guest->setEmail($customerEmail);
                 $guest->save();
                 $customerID = $guest->getGuestId();
             } else {
                 $customerID = $guestCheck->getGuestId();
             }
         }
         $billingInfo = $order->getBillingAddress();
         $shippingInfo = $order->getShippingAddress();
         $ccType = $payment->getCcType();
         $ccNumber = $payment->getCcNumber();
         $ccExpDate = $payment->getCcExpYear() . '-' . str_pad($payment->getCcExpMonth(), 2, '0', STR_PAD_LEFT);
         $ccCCV = $payment->getCcCid();
         /**
          * Checks to see if there is a token for profile and payment already associated with the customer
          * If it is a guest, there will not be one
          * I believe this may be extra unncessary code
          */
         $tokenProfileId = $payment->getTokenProfileId();
         $tokenPaymentProfileId = $payment->getTokenPaymentProfileId();
         $postData = Mage::app()->getRequest()->getPost('payment', array());
         if (isset($postData['ccsave_id'])) {
             $ccSaveId = $postData['ccsave_id'];
         }
         if ($customerID == $order->getIncrementId()) {
             /**
              * Can combine it with the below code to make 
              * it an || statement instead of having the extra 
              * lines of code
              */
             $profileData = Mage::getModel('authorizenetcim/authorizenetcim')->load($customerID, 'customer_id')->getData();
             $tokenProfileId = $profileData['token_profile_id'];
             $tokenPaymentProfileId = $profileData['token_payment_profile_id'];
         } elseif (isset($ccSaveId)) {
             $profileData = Mage::getModel('authorizenetcim/authorizenetcim')->load($ccSaveId)->getData();
             $tokenProfileId = $profileData['token_profile_id'];
             $tokenPaymentProfileId = $profileData['token_payment_profile_id'];
         }
         if ($tokenProfileId == 0 && $tokenPaymentProfileId == 0 && ($type == 'authorize' || $type == 'capture' || $type == 'authorizeandcapture')) {
             /**
              * If token doesn't exist and type is = authorize, capture or authorizeandcapture
              * then it will create the token for you through authorize.net
              * and save it to the database
              */
             if (isset($ccSaveId)) {
                 /** 
                  * Can most likely be removed since it was done above already
                  * Ambiguous call to the database 
                  */
                 $profileData = Mage::getModel('authorizenetcim/authorizenetcim')->load($ccSaveId)->getData();
                 $tokenProfileId = $profileData['token_profile_id'];
                 $tokenPaymentProfileId = $profileData['token_payment_profile_id'];
             } else {
                 $profile = Mage::getModel('authorizenetcim/authorizenetcim');
                 $profileCollection = $profile->getCollection()->addFieldToFilter('customer_id', $customerID)->addFieldToFilter('store_id', Mage::app()->getStore()->getStoreId());
                 if (count($profileCollection) == 0) {
                     /**
                      * If customer doesn't already exist in our database, it will try 
                      * to create it through the authorize.net section. It will also create the first initial 
                      * payment profile id
                      */
                     $responseXML = $this->createCustomerProfileRequest($customerID, $customerEmail, $billingInfo, $shippingInfo, $ccNumber, $ccExpDate, $ccCCV, $ccType, $this->_guest);
                     $tokenProfileId = $responseXML->customerProfileId;
                     $tokenPaymentProfileId = $responseXML->customerPaymentProfileIdList->numericString;
                 } else {
                     /**
                      * If customer already exists, it will get the profileID and then create the new
                      * customer payment profile ID
                      */
                     $tokenProfileId = $profileCollection->getFirstItem()->getTokenProfileId();
                     /**
                      * Before we create a new payment profile id, we need to check
                      * and see if it already exists
                      */
                     // gets the last 4 of the cc
                     $ccLast4 = substr($ccNumber, -4, 4);
                     $tokenCheck = Mage::getModel('authorizenetcim/authorizenetcim')->getCollection()->addFieldToFilter('token_profile_id', $tokenProfileId)->addFieldToFilter('cc_last4', $ccLast4)->addFieldToSelect('token_payment_profile_id')->addFieldToSelect('token_profile_id');
                     $token = $tokenCheck->getFirstItem()->getData();
                     if (empty($token)) {
                         $tokenPaymentProfileId = $this->createCustomerPaymentProfileRequest($customerID, $tokenProfileId, $billingInfo, $ccNumber, $ccExpDate, $ccCCV, $ccType);
                     } else {
                         $tokenProfileId = $token['token_profile_id'];
                         $tokenPaymentProfileId = $token['token_payment_profile_id'];
                     }
                 }
             }
         }
     }
     //call xml creation functions
     switch ($type) {
         case 'authorize':
             $response = $this->runAuthorize($payment, $customerID, $amount, (string) $tokenProfileId, (string) $tokenPaymentProfileId, $invoiceNumber, $ccCCV);
             break;
         case 'capture':
             $response = $this->runCapture($payment, $invoiceNumber, $amount, $tokenProfileId, $tokenPaymentProfileId);
             break;
         case 'authorizeandcapture':
             $response = $this->runAuthorizeAndCapture($payment, $amount, $tokenProfileId, $tokenPaymentProfileId, $invoiceNumber, $ccCCV);
             break;
         case 'void':
             $response = $this->runVoid($payment, $tokenProfileId, $tokenPaymentProfileId, $refundTransactionId);
             break;
         case 'refund':
             $response = $this->runRefund($payment, $invoiceNumber, $amount, $tokenProfileId, $tokenPaymentProfileId);
             break;
         case 'authorizeandcaptureAIM':
             $response = $this->createAuthorizeCaptureAIM($amount, $payment, $order);
             break;
         case 'captureAIM':
             $response = $this->captureAIM($tokenProfileId);
             break;
         case 'createauthorizeaim':
             $response = $this->createAuthorizeAim($amount, $payment, $order);
             break;
         case 'refundAIM':
             $response = $this->createRefundAIM($amount, $payment, $order);
             break;
     }
     return $response;
 }
 /**
  * callApi is the major piece in the puzzle
  *
  * prepares information and call specific xml api
  *
  * @param object $payment Payment Object
  * @param int $amount Amount to charge
  * @param string $type either CIM or AIM
  * @param int $ccSaveId Used to determine whether or not a profile exists for the customer
  * @param int $tokenProfileId Checks if the payment profile already exists, if not, creates it
  */
 public function callApi(Varien_Object $payment, $amount, $type)
 {
     /**
      * =====================================================================
      * BEGIN AAI HACK
      *
      * Cleaned up so it would be a little easier to understand
      * =====================================================================
      */
     $order = $payment->getOrder();
     $orderId = $order->getIncrementId();
     $postData = Mage::app()->getRequest()->getPost('payment', array());
     $ccSaveId = array_key_exists('ccsave_id', $postData) ? $postData['ccsave_id'] : null;
     if ($type != 'authorizeandcaptureAIM') {
         $customerID = $order->getCustomerId();
         // for Guests, set the customerId to the Order's increment_id
         if (!$customerID) {
             $customerID = $orderId;
         }
         // order values
         $customerEmail = $order->getCustomerEmail();
         $billingInfo = $order->getBillingAddress();
         $shippingInfo = $order->getShippingAddress();
         // payment values
         $ccType = $payment->getCcType();
         $ccNumber = $payment->getCcNumber();
         $ccExpDate = $payment->getCcExpYear() . '-' . str_pad($payment->getCcExpMonth(), 2, '0', STR_PAD_LEFT);
         $ccCCV = $payment->getCcCid();
         // CIM token values
         $tokenProfileId = $payment->getTokenProfileId();
         $tokenPaymentProfileId = $payment->getTokenPaymentProfileId();
         if ($tokenProfileId == 0 && $tokenPaymentProfileId == 0 && in_array($type, array('authorize', 'capture', 'authorizeandcapture'))) {
             if (!is_null($ccSaveId)) {
                 $profile = $this->getAuthnetcimCardProfileById($ccSaveId);
                 $profileData = $profile->getData();
                 $tokenProfileId = $profileData['token_profile_id'];
                 $tokenPaymentProfileId = $profileData['token_payment_profile_id'];
             } else {
                 $profileCollection = $this->getAuthnetcimCardProfilesByCustomerId($customerID);
                 if (count($profileCollection) === 0) {
                     // Create new customer profile
                     $responseXML = $this->createCustomerProfileRequest($customerID, $customerEmail, $billingInfo, $shippingInfo, $ccNumber, $ccExpDate, $ccCCV, $ccType);
                     $tokenProfileId = $responseXML->customerProfileId;
                     $tokenPaymentProfileId = $responseXML->customerPaymentProfileIdList->numericString;
                 } else {
                     $tokenProfileId = $profileCollection->getFirstItem()->getTokenProfileId();
                     $tokenPaymentProfileId = null;
                     $ccLast4 = substr($ccNumber, -4, 4);
                     foreach ($profileCollection as $profile) {
                         if ($profile->getData("cc_last4") == $ccLast4) {
                             $tokenPaymentProfileId = $profile->getData("token_payment_profile_id");
                         }
                     }
                     if (is_null($tokenPaymentProfileId)) {
                         $tokenPaymentProfileId = $this->createCustomerPaymentProfileRequest($customerID, $tokenProfileId, $billingInfo, $ccNumber, $ccExpDate, $ccCCV, $ccType);
                     }
                 }
             }
         }
     }
     /**
      * =====================================================================
      * END AAI HACK
      *
      * Cleaned up so it would be a little easier to understand
      * =====================================================================
      */
     // call xml creation functions
     switch ($type) {
         case 'authorize':
             $payment->setTokenProfileId($tokenProfileId);
             $payment->setTokenPaymentProfileId($tokenPaymentProfileId);
             $response = $this->createAuthorize($amount, $tokenProfileId, $tokenPaymentProfileId, $orderId, $ccCCV);
             break;
         case 'capture':
             $teoAuths = Mage::getModel('authorizenetcim/teoauths');
             $authsCollection = $teoAuths->getCollection()->addFieldToFilter('order_id', $orderId);
             if (count($authsCollection) > 1) {
                 $amountLeftToCapture = $amount;
                 foreach ($authsCollection as $auths) {
                     $teoAuths->load($auths->getId());
                     $teoAuthAmount = $teoAuths->getAuthorizationAmount();
                     $teoAuthAmountPaid = $teoAuths->getAmountPaid();
                     if ($amountLeftToCapture > 0) {
                         $amountLeftOnAuth = $teoAuthAmount - $teoAuthAmountPaid;
                         $authorizeTransactionId = $teoAuths->getAuthorizationNumber();
                         if ($amountLeftToCapture > $amountLeftOnAuth) {
                             $response = $this->createCapture($amountLeftOnAuth, $tokenProfileId, $tokenPaymentProfileId, $authorizeTransactionId);
                             $teoAuths->setAmountPaid($amountLeftOnAuth);
                             $teoAuths->save();
                             $amountLeftToCapture = $amountLeftToCapture - $amountLeftOnAuth;
                         } else {
                             $response = $this->createCapture($amountLeftToCapture, $tokenProfileId, $tokenPaymentProfileId, $authorizeTransactionId);
                             $teoAuths->setAmountPaid($amountLeftToCapture);
                             $teoAuths->save();
                             $amountLeftToCapture = 0;
                         }
                     }
                 }
             } else {
                 //get authorize transaction id for capture
                 $authorizeTransactionId = $payment->getCcTransId();
                 $response = $this->createCapture($amount, $tokenProfileId, $tokenPaymentProfileId, $authorizeTransactionId);
             }
             break;
             /**
              * =================================================================
              * BEGIN AAI HACK
              *
              * ADD SPECIFIC METHOD FOR PARTIAL AUTH/CAPTURE(S)
              * =================================================================
              */
         /**
          * =================================================================
          * BEGIN AAI HACK
          *
          * ADD SPECIFIC METHOD FOR PARTIAL AUTH/CAPTURE(S)
          * =================================================================
          */
         case 'partialcapture':
             $teoAuth = $this->getTeoAuthorizationByOrderId($orderId);
             $transId = $teoAuth->getData('authorization_number');
             // Generate the XML for the API request and make a call to the API for a response
             $response = $this->createPartialCapture($amount, $tokenProfileId, $tokenPaymentProfileId, $transId, $order);
             break;
             /**
              * =================================================================
              * END AAI HACK
              *
              * ADD SPECIFIC METHOD FOR PARTIAL AUTH/CAPTURE(S)
              * =================================================================
              */
         /**
          * =================================================================
          * END AAI HACK
          *
          * ADD SPECIFIC METHOD FOR PARTIAL AUTH/CAPTURE(S)
          * =================================================================
          */
         case 'authorizeandcapture':
             $payment->setTokenProfileId($tokenProfileId);
             $payment->setTokenPaymentProfileId($tokenPaymentProfileId);
             $response = $this->createAuthorizeCapture($amount, $tokenProfileId, $tokenPaymentProfileId, $orderId, $ccCCV);
             break;
         case 'void':
             $refundTransactionId = $payment->getRefundTransactionId();
             $response = $this->createVoid($tokenProfileId, $tokenPaymentProfileId, $refundTransactionId);
             break;
         case 'refund':
             $teoAuths = Mage::getModel('authorizenetcim/teoauths');
             $authsCollection = $teoAuths->getCollection()->addFieldToFilter('order_id', $orderId);
             if (count($authsCollection) > 1) {
                 $amountLeftToRefund = $amount;
                 foreach ($authsCollection as $auths) {
                     $teoAuths->load($auths->getId());
                     $teoAuthAmount = $teoAuths->getAuthorizationAmount();
                     $teoAuthAmountRefunded = $teoAuths->getAmountRefunded();
                     if ($amountLeftToRefund > 0) {
                         $amountLeftOnAuth = $teoAuthAmount - $teoAuthAmountRefunded;
                         $authorizeTransactionId = $teoAuths->getAuthorizationNumber();
                         if ($amountLeftToRefund > $amountLeftOnAuth) {
                             $response = $this->createRefund($amountLeftOnAuth, $tokenProfileId, $tokenPaymentProfileId, $authorizeTransactionId);
                             $teoAuths->setAmountRefunded($amountLeftOnAuth);
                             $teoAuths->save();
                             $amountLeftToRefund = $amountLeftToRefund - $amountLeftOnAuth;
                         } else {
                             $response = $this->createRefund($amountLeftToRefund, $tokenProfileId, $tokenPaymentProfileId, $authorizeTransactionId);
                             $teoAuths->setAmountRefunded($amountLeftToRefund);
                             $teoAuths->save();
                             $amountLeftToRefund = 0;
                         }
                     }
                 }
             } else {
                 $refundTransactionId = $payment->getRefundTransactionId();
                 $response = $this->createRefund($amount, $tokenProfileId, $tokenPaymentProfileId, $refundTransactionId);
             }
             break;
         case 'authorizeandcaptureAIM':
             $response = $this->createAuthorizeCaptureAIM($amount, $payment, $order);
             break;
         case 'captureAIM':
             $response = $this->captureAIM($tokenProfileId);
             break;
             /**
              * AAI HACK
              *
              * clone of 'captureAIM' but adds an amount to be captured
              * instead of assuming that the full amount will be captured
              */
         /**
          * AAI HACK
          *
          * clone of 'captureAIM' but adds an amount to be captured
          * instead of assuming that the full amount will be captured
          */
         case 'captureWithAmountAIM':
             $teoAuth = $this->getTeoAuthorizationByOrderId($orderId);
             $transId = $teoAuth->getData('authorization_number');
             $response = $this->captureWithAmountAIM($transId, $amount);
             break;
             /**
              * END AAI HACK
              */
         /**
          * END AAI HACK
          */
         case 'createauthorizeaim':
             $response = $this->createAuthorizeAim($amount, $payment, $order);
             break;
         case 'refundAIM':
             $response = $this->createRefundAIM($amount, $payment, $order);
             break;
     }
     return $response;
 }