PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/GoogleCheckout/Model/Api/Xml/Abstract.php

https://github.com/ticean/magento-mirror
PHP | 231 lines | 155 code | 26 blank | 50 comment | 14 complexity | 897ff52189a714a461be6dedd80746fa MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_GoogleCheckout
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. set_include_path(get_include_path().PS.Mage::getBaseDir('lib').DS.'googlecheckout');
  27. require_once('googleresponse.php');
  28. require_once('googlemerchantcalculations.php');
  29. require_once('googleresult.php');
  30. require_once('googlerequest.php');
  31. abstract class Mage_GoogleCheckout_Model_Api_Xml_Abstract extends Varien_Object
  32. {
  33. public function log($text, $nl=true)
  34. {
  35. error_log(print_r($text,1).($nl?"\n":''), 3, Mage::getBaseDir('log').DS.'callback.log');
  36. return $this;
  37. }
  38. public function __()
  39. {
  40. $args = func_get_args();
  41. $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), 'Mage_GoogleCheckout');
  42. array_unshift($args, $expr);
  43. return Mage::app()->getTranslator()->translate($args);
  44. }
  45. public function getMerchantId()
  46. {
  47. if (!$this->hasData('merchant_id')) {
  48. $this->setData('merchant_id', Mage::getStoreConfig('google/checkout/merchant_id', $this->getStoreId()));
  49. }
  50. return $this->getData('merchant_id');
  51. }
  52. public function getMerchantKey()
  53. {
  54. if (!$this->hasData('merchant_key')) {
  55. $this->setData('merchant_key', Mage::getStoreConfig('google/checkout/merchant_key', $this->getStoreId()));
  56. }
  57. return $this->getData('merchant_key');
  58. }
  59. public function getServerType()
  60. {
  61. if (!$this->hasData('server_type')) {
  62. $this->setData('server_type', Mage::getStoreConfig('google/checkout/sandbox', $this->getStoreId()) ? "sandbox" : "");
  63. }
  64. return $this->getData('server_type');
  65. }
  66. public function getLocale()
  67. {
  68. if (!$this->hasData('locale')) {
  69. $this->setData('locale', Mage::getStoreConfig('google/checkout/locale', $this->getStoreId()));
  70. }
  71. return $this->getData('locale');
  72. }
  73. public function getCurrency()
  74. {
  75. if (!$this->hasData('currency')) {
  76. $this->setData('currency', Mage::app()->getStore()->getBaseCurrencyCode());
  77. //$this->setData('currency', $this->getLocale()=='en_US' ? 'USD' : 'GBP');
  78. }
  79. return $this->getData('currency');
  80. }
  81. /**
  82. * Google Checkout Request instance
  83. *
  84. * @return GoogleRequest
  85. */
  86. public function getGRequest()
  87. {
  88. if (!$this->hasData('g_request')) {
  89. $this->setData('g_request', new GoogleRequest(
  90. $this->getMerchantId(),
  91. $this->getMerchantKey(),
  92. $this->getServerType(),
  93. $this->getCurrency()
  94. ));
  95. //Setup the log file
  96. $logDir = Mage::getBaseDir('log');
  97. $this->getData('g_request')->SetLogFiles(
  98. $logDir.DS.'googleerror.log',
  99. $logDir.DS.'googlemessage.log',
  100. L_ALL
  101. );
  102. }
  103. return $this->getData('g_request');
  104. }
  105. /**
  106. * Google Checkout Response instance
  107. *
  108. * @return GoogleResponse
  109. */
  110. public function getGResponse()
  111. {
  112. if (!$this->hasData('g_response')) {
  113. $this->setData('g_response', new GoogleResponse(
  114. $this->getMerchantId(),
  115. $this->getMerchantKey()
  116. ));
  117. //Setup the log file
  118. $logDir = Mage::getBaseDir('log');
  119. $this->getData('g_response')->SetLogFiles(
  120. $logDir.DS.'googleerror.log',
  121. $logDir.DS.'googlemessage.log',
  122. L_ALL
  123. );
  124. }
  125. return $this->getData('g_response');
  126. }
  127. protected function _getBaseApiUrl()
  128. {
  129. $url = 'https://';
  130. if ($this->getServerType()=='sandbox') {
  131. $url .= 'sandbox.google.com/checkout/api/checkout/v2/';
  132. } else {
  133. $url .= 'checkout.google.com/api/checkout/v2/';
  134. }
  135. return $url;
  136. }
  137. abstract protected function _getApiUrl();
  138. public function _call($xml)
  139. {
  140. $auth = 'Basic '.base64_encode($this->getMerchantId().':'.$this->getMerchantKey());
  141. $headers = array(
  142. 'Authorization: '.$auth,
  143. 'Content-Type: application/xml;charset=UTF-8',
  144. 'Accept: application/xml;charset=UTF-8',
  145. );
  146. $url = $this->_getApiUrl();
  147. $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n".$xml;
  148. $debugData = array('request' => $xml, 'dir' => 'out');
  149. try {
  150. $http = new Varien_Http_Adapter_Curl();
  151. $http->write('POST', $url, '1.1', $headers, $xml);
  152. $response = $http->read();
  153. $response = preg_split('/^\r?$/m', $response, 2);
  154. $response = trim($response[1]);
  155. $debugData['result'] = $response;
  156. }
  157. catch (Exception $e) {
  158. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  159. $this->getApi()->debugData($debugData);
  160. throw $e;
  161. }
  162. $this->getApi()->debugData($debugData);
  163. $result = @simplexml_load_string($response);
  164. if (!$result) {
  165. $result = simplexml_load_string('<error><error-message>Invalid response from Google Checkout server</error-message></error>');
  166. }
  167. if ($result->getName()=='error') {
  168. $this->setError($this->__('Google Checkout: %s', (string)$result->{'error-message'}));
  169. $this->setWarnings((array)$result->{'warning-messages'});
  170. } else {
  171. $this->unsError()->unsWarnings();
  172. }
  173. $this->setResult($result);
  174. return $result;
  175. }
  176. protected function _getCallbackUrl()
  177. {
  178. return Mage::getUrl('googlecheckout/api', array('_forced_secure'=>Mage::getStoreConfig('google/checkout/use_secure_callback_url', $this->getStoreId())));
  179. }
  180. /**
  181. * Recalculate amount to store currency
  182. *
  183. * @param float $amount
  184. * @param Mage_Sales_Model_Quote $quote
  185. * @return float
  186. */
  187. protected function _reCalculateToStoreCurrency($amount, $quote)
  188. {
  189. if ($quote->getQuoteCurrencyCode() != $quote->getBaseCurrencyCode()) {
  190. $amount = $amount * $quote->getStoreToQuoteRate();
  191. $amount = Mage::app()->getStore()->roundPrice($amount);
  192. }
  193. return $amount;
  194. }
  195. /**
  196. * Get Tax Class for Shipping option
  197. *
  198. * @param Mage_Sales_Model_Quote $quote
  199. * @return mixed
  200. */
  201. protected function _getTaxClassForShipping($quote)
  202. {
  203. return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $quote->getStoreId());
  204. }
  205. }