/**
  * Prepare request to gateway
  * 
  * HERE WE NEED TO CHIME IN AND USE AN EXISTING CUSTOMER ACCOUNT IF A TOKEN
  * IS PRESENT
  *
  * @link http://www.authorize.net/support/AIM_guide.pdf
  * @param Mage_Payment_Model_Info $payment
  * @return Mage_Paygate_Model_Authorizenet_Request
  */
 protected function _buildRequest(Varien_Object $payment)
 {
     $order = $payment->getOrder();
     $this->setStore($order->getStoreId());
     $request = $this->_getRequest()->setXType($payment->getAnetTransType())->setXMethod(self::REQUEST_METHOD_CC);
     if ($order && $order->getIncrementId()) {
         $request->setXInvoiceNum($order->getIncrementId());
     }
     if ($payment->getAmount()) {
         $request->setXAmount($payment->getAmount(), 2);
         $request->setXCurrencyCode($order->getBaseCurrencyCode());
     }
     switch ($payment->getAnetTransType()) {
         case self::REQUEST_TYPE_AUTH_CAPTURE:
             $request->setXAllowPartialAuth($this->getConfigData('allow_partial_authorization') ? 'True' : 'False');
             if ($payment->getAdditionalInformation($this->_splitTenderIdKey)) {
                 $request->setXSplitTenderId($payment->getAdditionalInformation($this->_splitTenderIdKey));
             }
             break;
         case self::REQUEST_TYPE_AUTH_ONLY:
             $request->setXAllowPartialAuth($this->getConfigData('allow_partial_authorization') ? 'True' : 'False');
             if ($payment->getAdditionalInformation($this->_splitTenderIdKey)) {
                 $request->setXSplitTenderId($payment->getAdditionalInformation($this->_splitTenderIdKey));
             }
             break;
         case self::REQUEST_TYPE_CREDIT:
             /**
              * Send last 4 digits of credit card number to authorize.net
              * otherwise it will give an error
              * 
              * x_trans_id is the transaction ID we provide every 
              * transaction. It would be used to reference transactions in 
              * our system when doing export requests, etc.
              * 
              */
             $request->setXCardNum($payment->getCcLast4());
             $request->setXTransId($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_VOID:
             $request->setXTransId($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_PRIOR_AUTH_CAPTURE:
             $request->setXTransId($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_CAPTURE_ONLY:
             /**
              * x_auth_code is the authorization code you would pass if you 
              * were doing a forced sale type where you already had an 
              * approval and needed to force it into PayTrace.
              */
             $request->setXAuthCode($payment->getCcAuthCode());
             break;
     }
     if ($this->getIsCentinelValidationEnabled()) {
         $params = $this->getCentinelValidator()->exportCmpiData(array());
         $request = Varien_Object_Mapper::accumulateByMap($params, $request, $this->_centinelFieldMap);
     }
     /**
      * x_description is a description you can pass along with the 
      * transaction which will show in PayTrace reporting for reporting 
      * purposes.
      */
     $request->setXDescription('Magento Store Online Order');
     if (!empty($order)) {
         $billing = $order->getBillingAddress();
         if (!empty($billing)) {
             $request->setXDelimChar(self::RESPONSE_DELIM_CHAR)->setXEncapChar('')->setXFirstName($billing->getFirstname())->setXLastName($billing->getLastname())->setXCompany($billing->getCompany())->setXAddress($billing->getStreet(1))->setXCity($billing->getCity())->setXState($billing->getRegion())->setXZip($billing->getPostcode())->setXPhone($billing->getTelephone())->setXFax($billing->getFax())->setXEmail($order->getCustomerEmail())->setXMerchantEmail($this->getConfigData('merchant_email'));
         }
         $shipping = $order->getShippingAddress();
         if (!empty($shipping)) {
             $request->setXShipToFirstName($shipping->getFirstname())->setXShipToLastName($shipping->getLastname())->setXShipToCompany($shipping->getCompany())->setXShipToAddress($shipping->getStreet(1))->setXShipToCity($shipping->getCity())->setXShipToState($shipping->getRegion())->setXShipToZip($shipping->getPostcode())->setXShipToCountry($shipping->getCountry());
         }
         /*
          * x_po_num - * For Authorize.net this field is for Purchase 
          * Order numbers, for PayTrace this is only used for 
          * transactions that are identified as corporate or purchasing 
          * credit cards. This is an identifier that your customer may 
          * ask you to provide in order to reference the transaction to 
          * their credit card statement.
          */
         //e            $po_number = $order->getPoNumber()
         //                        ?$order->getPoNumber()
         //                        : ($order->getQuote()
         //                               ? $order->getQuote()->getPoNumber()
         //                                : '');
         //
         //            $request->setXPoNum($po_number);
         //
         //            $request->setXTax($order->getBaseTaxAmount())
         //f               ->setXFreight($order->getBaseShippingAmount());
         //
         //    //        Use these fields if we're using a stored credit card
         //            $useSavedCard = $order->getUseSavedCard()
         //                    ? $order->getUseSavedCard()
         //                    : ($order->getQuote()
         //                            ? $order->getQuote()->getUseSavedCard()
         //                            : false);
         //            if ($useSavedCard) {
         //                // setXCustid
         //                // setXCustomerProfileId
         //                // setXCustomerPaymentProfileId
         //                $request->setXCustId( $order->getCustomerId() );
         //            }
     }
     //        Use these fields if we're using a newly entered credit card
     if ($payment->getCcNumber()) {
         $request->setXCardNum($payment->getCcNumber())->setXExpDate(sprintf('%02d-%04d', $payment->getCcExpMonth(), $payment->getCcExpYear()))->setXCardCode($payment->getCcCid());
     }
     return $request;
 }
 /**
  * Prepare request to gateway
  *
  * @link http://www.authorize.net/support/AIM_guide.pdf
  * @param Mage_Payment_Model_Info $payment
  * @return Mage_Paygate_Model_Authorizenet_Request
  */
 protected function _buildRequest(Varien_Object $payment)
 {
     $order = $payment->getOrder();
     $this->setStore($order->getStoreId());
     $request = $this->_getRequest()->setXType($payment->getAnetTransType())->setXMethod(self::REQUEST_METHOD_CC);
     if ($order && $order->getIncrementId()) {
         $request->setXInvoiceNum($order->getIncrementId());
     }
     if ($payment->getAmount()) {
         $request->setXAmount($payment->getAmount(), 2);
         $request->setXCurrencyCode($order->getBaseCurrencyCode());
     }
     switch ($payment->getAnetTransType()) {
         case self::REQUEST_TYPE_AUTH_CAPTURE:
             $request->setXAllowPartialAuth($this->getConfigData('allow_partial_authorization') ? 'True' : 'False');
             if ($payment->getAdditionalInformation($this->_splitTenderIdKey)) {
                 $request->setXSplitTenderId($payment->getAdditionalInformation($this->_splitTenderIdKey));
             }
             break;
         case self::REQUEST_TYPE_AUTH_ONLY:
             $request->setXAllowPartialAuth($this->getConfigData('allow_partial_authorization') ? 'True' : 'False');
             if ($payment->getAdditionalInformation($this->_splitTenderIdKey)) {
                 $request->setXSplitTenderId($payment->getAdditionalInformation($this->_splitTenderIdKey));
             }
             break;
         case self::REQUEST_TYPE_CREDIT:
             /**
              * Send last 4 digits of credit card number to authorize.net
              * otherwise it will give an error
              */
             $request->setXCardNum($payment->getCcLast4());
             $request->setXTransId($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_VOID:
             $request->setXTransId($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_PRIOR_AUTH_CAPTURE:
             $request->setXTransId($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_CAPTURE_ONLY:
             $request->setXAuthCode($payment->getCcAuthCode());
             break;
     }
     if ($this->getIsCentinelValidationEnabled()) {
         $params = $this->getCentinelValidator()->exportCmpiData(array());
         $request = Varien_Object_Mapper::accumulateByMap($params, $request, $this->_centinelFieldMap);
     }
     if (!empty($order)) {
         $billing = $order->getBillingAddress();
         if (!empty($billing)) {
             $request->setXFirstName($billing->getFirstname())->setXLastName($billing->getLastname())->setXCompany($billing->getCompany())->setXAddress($billing->getStreet(1))->setXCity($billing->getCity())->setXState($billing->getRegion())->setXZip($billing->getPostcode())->setXCountry($billing->getCountry())->setXPhone($billing->getTelephone())->setXFax($billing->getFax())->setXCustId($order->getCustomerId())->setXCustomerIp($order->getRemoteIp())->setXCustomerTaxId($billing->getTaxId())->setXEmail($order->getCustomerEmail())->setXEmailCustomer($this->getConfigData('email_customer'))->setXMerchantEmail($this->getConfigData('merchant_email'));
         }
         $shipping = $order->getShippingAddress();
         if (!empty($shipping)) {
             $request->setXShipToFirstName($shipping->getFirstname())->setXShipToLastName($shipping->getLastname())->setXShipToCompany($shipping->getCompany())->setXShipToAddress($shipping->getStreet(1))->setXShipToCity($shipping->getCity())->setXShipToState($shipping->getRegion())->setXShipToZip($shipping->getPostcode())->setXShipToCountry($shipping->getCountry());
         }
         $request->setXPoNum($payment->getPoNumber())->setXTax($order->getBaseTaxAmount())->setXFreight($order->getBaseShippingAmount());
     }
     if ($payment->getCcNumber()) {
         $request->setXCardNum($payment->getCcNumber())->setXExpDate(sprintf('%02d-%04d', $payment->getCcExpMonth(), $payment->getCcExpYear()))->setXCardCode($payment->getCcCid());
     }
     return $request;
 }
 /**
  * Prepare request to gateway
  * 
  * HERE WE NEED TO CHIME IN AND USE AN EXISTING CUSTOMER ACCOUNT IF A TOKEN
  * IS PRESENT
  *
  * @link http://www.authorize.net/support/AIM_guide.pdf
  * @param Mage_Payment_Model_Info $payment
  * @return Mage_Paygate_Model_Authorizenet_Request
  */
 protected function _buildRequest(Varien_Object $payment)
 {
     $order = $payment->getOrder();
     $this->setStore($order->getStoreId());
     $request = $this->_getRequest()->setMethod(self::REQUEST_METHOD_CC);
     switch ($payment->getAnetTransType()) {
         case self::REQUEST_TYPE_AUTH_CAPTURE:
             $request->setTranxtype('Sale');
             break;
         case self::REQUEST_TYPE_AUTH_ONLY:
             //                $request->setTranxtype('Authorization');
             $request->setTranxtype('Sale');
             break;
         case self::REQUEST_TYPE_CREDIT:
             $request->setTranxtype('Refund');
             break;
         case self::REQUEST_TYPE_VOID:
             $request->setTranxtype('Void');
             $request->setTranxid($payment->getXTransId());
             break;
         case self::REQUEST_TYPE_PRIOR_AUTH_CAPTURE:
             $request->setTranxtype('Capture');
             break;
         case self::REQUEST_TYPE_CAPTURE_ONLY:
             $request->setTranxtype('Capture');
             break;
     }
     if (!empty($order)) {
         $billing = $order->getBillingAddress();
         if (!empty($billing)) {
             $request->setBname($billing->getFirstname() . ' ' . $billing->getLastname())->setBaddress($billing->getStreet(1))->setBcity($billing->getCity())->setBstate($billing->getRegion())->setBzip($billing->getPostcode())->setBcountry($billing->getCountry())->setEmail($order->getCustomerEmail());
         }
         $shipping = $order->getShippingAddress();
         if (!empty($shipping)) {
             $request->setSname($shipping->getFirstname() . ' ' . $shipping->getLastname())->setSaddress($shipping->getStreet(1))->setScity($shipping->getCity())->setSstate($shipping->getRegion())->setSzip($shipping->getPostcode())->setScountry($shipping->getCountry());
         }
         // loading the customer class
         $customer = $order->getCustomer();
         $request->setInvoice($order->getIncrementId())->setTax($order->getBaseTaxAmount())->setFreight($order->getBaseShippingAmount())->setCustid($customer->getId());
         /*
          * x_po_num - * For Authorize.net this field is for Purchase 
          * Order numbers, for PayTrace this is only used for 
          * transactions that are identified as corporate or purchasing 
          * credit cards. This is an identifier that your customer may 
          * ask you to provide in order to reference the transaction to 
          * their credit card statement.
          */
         $po_number = $order->getPoNumber() ? $order->getPoNumber() : $order->getQuote()->getPoNumber();
         // LEVEL 3 DATA
         if ($po_number && $customer->getCustomerTaxId()) {
             $request->setCustref($po_number);
             $request->setCustomertaxid($customer->getCustomerTaxId());
             $request->setMerchanttaxid($this->getConfigData('tax_id'));
             $request->setTax('-1');
             $request->setAddtaxrate('0');
             $request->setAddtax('0');
             $request->setDuty('0');
             $request->setDiscount('0');
             //Commodity code that generally applies to each product included
             //in the order. Commodity codes are generally assigned by your
             //merchant service provider
             //                $request->setCcode();
             $sourceZip = Mage::getStoreConfig('shipping/origin/postcode/', $this->getStore());
             $request->setSourcezip($sourceZip);
         }
     }
     /**
      * x_description is a description you can pass along with the 
      * transaction which will show in PayTrace reporting for reporting 
      * purposes.
      */
     $request->setDescription('Magento Store Online Order')->setAmount($payment->getAmount(), 2);
     return $request;
 }