Skip to content

Instantly share code, notes, and snippets.

@centerax
Created April 4, 2013 13:08
Show Gist options
  • Save centerax/5310208 to your computer and use it in GitHub Desktop.
Save centerax/5310208 to your computer and use it in GitHub Desktop.
diff --git a/app/code/local/Ebizmarts/SagePaySuite/Model/Api/Payment.php b/app/code/local/Ebizmarts/SagePaySuite/Model/Api/Payment.php
index 0b191b3..f56efab 100755
--- a/app/code/local/Ebizmarts/SagePaySuite/Model/Api/Payment.php
+++ b/app/code/local/Ebizmarts/SagePaySuite/Model/Api/Payment.php
@@ -62,11 +62,11 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
/**
* BasketXML related error codes.
- *
- * @var type
+ *
+ * @var type
*/
private $_basketErrors = array(3021, 3195);
-
+
/**
* Can be edit order (renew order)
*
@@ -158,13 +158,13 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
}
public function getVpsProtocolVersion($mode = "live") {
-
+
$protocol = '3.00';
-
+
if("simulator" === strtolower($mode)) {
$protocol = '2.23';
}
-
+
return $protocol;
}
@@ -299,7 +299,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
$create = true;
}
else {
- $create = false;
+ $create = false;
}
}
@@ -931,17 +931,20 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
$payment->addData($quoteObj->getPayment()->toArray());
}
- //$billingAddressObj = $this->_getQuote()->getBillingAddress();
- //$shippingAddressObj = $this->_getQuote()->getShippingAddress();
-
$payment->setTransactionType(strtoupper($this->getConfigData('payment_action')));
- $payment->setAmountOrdered($this->formatAmount($this->_getQuote()->getGrandTotal(), $this->_getQuote()->getQuoteCurrencyCode()));
+ $payment->setAmountOrdered($this->formatAmount($quoteObj->getGrandTotal(), $quoteObj->getQuoteCurrencyCode()));
$payment->setRealCapture(true); //To difference invoice from capture
- $payment->setOrder(new Varien_Object($this->_getQuote()->toArray()));
+ $payment->setOrder( (clone $quoteObj) );
$payment->setAnetTransType(strtoupper($this->getConfigData('payment_action')));
- $payment->getOrder()->setOrderCurrencyCode($this->_getQuote()->getQuoteCurrencyCode());
- $payment->getOrder()->setBillingAddress($this->_getQuote()->getBillingAddress());
- $payment->getOrder()->setShippingAddress($this->_getQuote()->getShippingAddress());
+ $payment->getOrder()->setOrderCurrencyCode($quoteObj->getQuoteCurrencyCode());
+ $payment->getOrder()->setBillingAddress($quoteObj->getBillingAddress());
+
+ if($quoteObj->isVirtual()) {
+ $payment->getOrder()->setShippingAddress($quoteObj->getBillingAddress());
+ }
+ else {
+ $payment->getOrder()->setShippingAddress($quoteObj->getShippingAddress());
+ }
return $payment;
}
@@ -1321,7 +1324,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
//VOID payment if refunding full amount
if( abs($order->getGrandTotal() - $payment->getCreditmemo()->getGrandTotal()) < .0001 ) {
- try {
+ try {
$this->voidPayment($trn);
return $this;
} catch (Exception $e) {
@@ -1441,7 +1444,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
$result = $this->requestPost($this->getUrl('abort', false, $this->_getIntegrationCode($trn->getIntegration()), $trn->getMode()), $data);
} catch (Exception $e) {
Sage_Log::logException($e);
- Mage::throwException($this->_getHelper()->__('Transaction could not be aborted at SagePay. You may want to delete it from the local database and check the transaction at the SagePay admin panel.'));
+ Mage::throwException($this->_getHelper()->__('Transaction could not be aborted at SagePay. You may want to delete it from the local database and check the transaction at the SagePay admin panel.'));
}
if ($result['Status'] != 'OK') {
@@ -1455,14 +1458,14 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
}
public function voidPayment($trn) {
-
+
/**
* SecurityKey from the "Admin & Access API"
*/
if (!$trn->getSecurityKey() && strtoupper($trn->getIntegration()) == 'FORM') {
$this->_addSecurityKey($trn);
}
-
+
$data = array();
$data['VPSProtocol'] = $trn->getVpsProtocol();
$data['TxType'] = self::REQUEST_TYPE_VOID;
@@ -1721,7 +1724,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
->setStatus($result->getResponseStatus())
->setStatusDetail($result->getResponseStatusDetail())
->save();
-
+
switch ($result->getResponseStatus()) {
case 'FAIL':
$error = $result->getResponseStatusDetail();
@@ -1881,7 +1884,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
* @return array|string $result Result of POST
*/
public function requestPost($url, $data, $returnRaw = false) {
-
+
//$storeId = $this->getStoreId();
$aux = $data;
@@ -1902,7 +1905,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
$userAgent = $this->_sageHelper()->getUserAgent();
self::log($url, null, 'SagePaySuite_REQUEST.log');
- self::log(Mage::helper('core/http')->getHttpUserAgent(false), null, 'SagePaySuite_REQUEST.log');
+ self::log("User-Agent: " . Mage::helper('core/http')->getHttpUserAgent(false), null, 'SagePaySuite_REQUEST.log');
self::log($userAgent, null, 'SagePaySuite_REQUEST.log');
self::log($aux, null, 'SagePaySuite_REQUEST.log');
@@ -1964,24 +1967,24 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
//Resend same request if fails because of basket related errors.
if( isset($output['StatusDetail']) && (isset($output['Status']) && ($output['Status'] == 'INVALID')) ) {
-
+
for ($i = 0; $i < count($this->_basketErrors); $i++) {
if(1 === preg_match('/^' . $this->_basketErrors[$i] . '/i', $output['StatusDetail'])) {
-
+
if(isset($data['BasketXML'])) {
unset($data['BasketXML']);
-
+
self::log($output, null, 'SagePaySuite_REQUEST.log');
- self::log("Basket ERROR, retrying without BasketXML in POST ...", null, 'SagePaySuite_REQUEST.log');
-
+ self::log("Basket ERROR, retrying without BasketXML in POST ...", null, 'SagePaySuite_REQUEST.log');
+
return $this->requestPost($url, $data, false);
}
-
+
}
}
}
-
+
self::log($output, null, 'SagePaySuite_REQUEST.log');
return $output;
@@ -2125,7 +2128,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
unitGrossAmount * quantity must equal totalGrossAmount
totalGrossAmount + deliveryGrossAmount (if applicable) must match the Amount field being sent in the normal registration post.
*/
-
+
//<unitNetAmount>
$node->addChild('unitNetAmount', number_format($item->getPrice()-$item->getDiscountAmount(), 2, '.', ''));
@@ -2194,7 +2197,7 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
$node->addChild('recipientPostCode', substr(trim($_postCode), 0, 9));
}
-
+
//Sum up shipping totals when using SERVER with MAC
if($this->_isMultishippingCheckout() && ($quote->getPayment()->getMethod() == 'sagepayserver') ) {
@@ -2223,15 +2226,15 @@ class Ebizmarts_SagePaySuite_Model_Api_Payment extends Mage_Payment_Model_Method
//<deliveryGrossAmount>
$basket->addChild('deliveryGrossAmount', number_format($shippingInclTax, 2, '.', ''));
- //}
-
+ //}
+
//<shippingFaxNo>
if($shippingAdd->getFax()) {
$basket->addChild('shippingFaxNo', substr(trim($shippingAdd->getFax()), 0, 20));
}
//Mage::log("\n\n" . $basket->asNiceXml());
-
+
return str_replace("\n", "", trim($basket->asXml()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment