PageRenderTime 68ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/mengqing/magento-mirror
PHP | 240 lines | 164 code | 26 blank | 50 comment | 15 complexity | 77ffc2761b72d955496ea712270b0ac0 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) 2012 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(
  63. 'server_type',
  64. Mage::getStoreConfig('google/checkout/sandbox', $this->getStoreId()) ? "sandbox" : ""
  65. );
  66. }
  67. return $this->getData('server_type');
  68. }
  69. public function getLocale()
  70. {
  71. if (!$this->hasData('locale')) {
  72. $this->setData('locale', Mage::getStoreConfig('google/checkout/locale', $this->getStoreId()));
  73. }
  74. return $this->getData('locale');
  75. }
  76. public function getCurrency()
  77. {
  78. if (!$this->hasData('currency')) {
  79. $this->setData('currency', Mage::app()->getStore()->getBaseCurrencyCode());
  80. //$this->setData('currency', $this->getLocale()=='en_US' ? 'USD' : 'GBP');
  81. }
  82. return $this->getData('currency');
  83. }
  84. /**
  85. * Google Checkout Request instance
  86. *
  87. * @return GoogleRequest
  88. */
  89. public function getGRequest()
  90. {
  91. if (!$this->hasData('g_request')) {
  92. $this->setData('g_request', new GoogleRequest(
  93. $this->getMerchantId(),
  94. $this->getMerchantKey(),
  95. $this->getServerType(),
  96. $this->getCurrency()
  97. ));
  98. //Setup the log file
  99. $logDir = Mage::getBaseDir('log');
  100. $this->getData('g_request')->SetLogFiles(
  101. $logDir . DS . 'googleerror.log',
  102. $logDir . DS . 'googlemessage.log',
  103. L_ALL
  104. );
  105. }
  106. return $this->getData('g_request');
  107. }
  108. /**
  109. * Google Checkout Response instance
  110. *
  111. * @return GoogleResponse
  112. */
  113. public function getGResponse()
  114. {
  115. if (!$this->hasData('g_response')) {
  116. $this->setData('g_response', new GoogleResponse(
  117. $this->getMerchantId(),
  118. $this->getMerchantKey()
  119. ));
  120. //Setup the log file
  121. $logDir = Mage::getBaseDir('log');
  122. $this->getData('g_response')->SetLogFiles(
  123. $logDir . DS . 'googleerror.log',
  124. $logDir . DS . 'googlemessage.log',
  125. L_ALL
  126. );
  127. }
  128. return $this->getData('g_response');
  129. }
  130. protected function _getBaseApiUrl()
  131. {
  132. $url = 'https://';
  133. if ($this->getServerType()=='sandbox') {
  134. $url .= 'sandbox.google.com/checkout/api/checkout/v2/';
  135. } else {
  136. $url .= 'checkout.google.com/api/checkout/v2/';
  137. }
  138. return $url;
  139. }
  140. abstract protected function _getApiUrl();
  141. public function _call($xml)
  142. {
  143. $auth = 'Basic ' . base64_encode($this->getMerchantId() . ':' . $this->getMerchantKey());
  144. $headers = array(
  145. 'Authorization: ' . $auth,
  146. 'Content-Type: application/xml;charset=UTF-8',
  147. 'Accept: application/xml;charset=UTF-8',
  148. );
  149. $url = $this->_getApiUrl();
  150. $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . $xml;
  151. $debugData = array('request' => $xml, 'dir' => 'out');
  152. try {
  153. $http = new Varien_Http_Adapter_Curl();
  154. $http->write('POST', $url, '1.1', $headers, $xml);
  155. $response = $http->read();
  156. $response = preg_split('/^\r?$/m', $response, 2);
  157. $response = trim($response[1]);
  158. $debugData['result'] = $response;
  159. $http->close();
  160. }
  161. catch (Exception $e) {
  162. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  163. $this->getApi()->debugData($debugData);
  164. throw $e;
  165. }
  166. $this->getApi()->debugData($debugData);
  167. $result = @simplexml_load_string($response);
  168. if (!$result) {
  169. $result = simplexml_load_string(
  170. '<error><error-message>Invalid response from Google Checkout server</error-message></error>'
  171. );
  172. }
  173. if ($result->getName() == 'error') {
  174. $this->setError($this->__('Google Checkout: %s', (string)$result->{'error-message'}));
  175. $this->setWarnings((array)$result->{'warning-messages'});
  176. } else {
  177. $this->unsError()->unsWarnings();
  178. }
  179. $this->setResult($result);
  180. return $result;
  181. }
  182. protected function _getCallbackUrl()
  183. {
  184. return Mage::getUrl(
  185. 'googlecheckout/api',
  186. array('_forced_secure'=>Mage::getStoreConfig('google/checkout/use_secure_callback_url',$this->getStoreId()))
  187. );
  188. }
  189. /**
  190. * Recalculate amount to store currency
  191. *
  192. * @param float $amount
  193. * @param Mage_Sales_Model_Quote $quote
  194. * @return float
  195. */
  196. protected function _reCalculateToStoreCurrency($amount, $quote)
  197. {
  198. if ($quote->getQuoteCurrencyCode() != $quote->getBaseCurrencyCode()) {
  199. $amount = $amount * $quote->getStoreToQuoteRate();
  200. $amount = Mage::app()->getStore()->roundPrice($amount);
  201. }
  202. return $amount;
  203. }
  204. /**
  205. * Get Tax Class for Shipping option
  206. *
  207. * @param Mage_Sales_Model_Quote $quote
  208. * @return mixed
  209. */
  210. protected function _getTaxClassForShipping($quote)
  211. {
  212. return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $quote->getStoreId());
  213. }
  214. }