PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sunil_nextbits/magento2
PHP | 238 lines | 163 code | 25 blank | 50 comment | 15 complexity | c8894c15788fbb08b98389f2dea95aa3 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 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. require_once('googlecheckout/googleresponse.php');
  27. require_once('googlecheckout/googlemerchantcalculations.php');
  28. require_once('googlecheckout/googleresult.php');
  29. require_once('googlecheckout/googlerequest.php');
  30. abstract class Mage_GoogleCheckout_Model_Api_Xml_Abstract extends Varien_Object
  31. {
  32. public function log($text, $nl=true)
  33. {
  34. error_log(print_r($text, 1) . ($nl ? "\n" : ''), 3, Mage::getBaseDir('log') . DS . 'callback.log');
  35. return $this;
  36. }
  37. public function __()
  38. {
  39. $args = func_get_args();
  40. $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), 'Mage_GoogleCheckout');
  41. array_unshift($args, $expr);
  42. return Mage::app()->getTranslator()->translate($args);
  43. }
  44. public function getMerchantId()
  45. {
  46. if (!$this->hasData('merchant_id')) {
  47. $this->setData('merchant_id', Mage::getStoreConfig('google/checkout/merchant_id', $this->getStoreId()));
  48. }
  49. return $this->getData('merchant_id');
  50. }
  51. public function getMerchantKey()
  52. {
  53. if (!$this->hasData('merchant_key')) {
  54. $this->setData('merchant_key', Mage::getStoreConfig('google/checkout/merchant_key', $this->getStoreId()));
  55. }
  56. return $this->getData('merchant_key');
  57. }
  58. public function getServerType()
  59. {
  60. if (!$this->hasData('server_type')) {
  61. $this->setData(
  62. 'server_type',
  63. Mage::getStoreConfig('google/checkout/sandbox', $this->getStoreId()) ? "sandbox" : ""
  64. );
  65. }
  66. return $this->getData('server_type');
  67. }
  68. public function getLocale()
  69. {
  70. if (!$this->hasData('locale')) {
  71. $this->setData('locale', Mage::getStoreConfig('google/checkout/locale', $this->getStoreId()));
  72. }
  73. return $this->getData('locale');
  74. }
  75. public function getCurrency()
  76. {
  77. if (!$this->hasData('currency')) {
  78. $this->setData('currency', Mage::app()->getStore()->getBaseCurrencyCode());
  79. //$this->setData('currency', $this->getLocale()=='en_US' ? 'USD' : 'GBP');
  80. }
  81. return $this->getData('currency');
  82. }
  83. /**
  84. * Google Checkout Request instance
  85. *
  86. * @return GoogleRequest
  87. */
  88. public function getGRequest()
  89. {
  90. if (!$this->hasData('g_request')) {
  91. $this->setData('g_request', new GoogleRequest(
  92. $this->getMerchantId(),
  93. $this->getMerchantKey(),
  94. $this->getServerType(),
  95. $this->getCurrency()
  96. ));
  97. //Setup the log file
  98. $logDir = Mage::getBaseDir('log');
  99. $this->getData('g_request')->SetLogFiles(
  100. $logDir . DS . 'googleerror.log',
  101. $logDir . DS . 'googlemessage.log',
  102. L_ALL
  103. );
  104. }
  105. return $this->getData('g_request');
  106. }
  107. /**
  108. * Google Checkout Response instance
  109. *
  110. * @return GoogleResponse
  111. */
  112. public function getGResponse()
  113. {
  114. if (!$this->hasData('g_response')) {
  115. $this->setData('g_response', new GoogleResponse(
  116. $this->getMerchantId(),
  117. $this->getMerchantKey()
  118. ));
  119. //Setup the log file
  120. $logDir = Mage::getBaseDir('log');
  121. $this->getData('g_response')->SetLogFiles(
  122. $logDir . DS . 'googleerror.log',
  123. $logDir . DS . 'googlemessage.log',
  124. L_ALL
  125. );
  126. }
  127. return $this->getData('g_response');
  128. }
  129. protected function _getBaseApiUrl()
  130. {
  131. $url = 'https://';
  132. if ($this->getServerType()=='sandbox') {
  133. $url .= 'sandbox.google.com/checkout/api/checkout/v2/';
  134. } else {
  135. $url .= 'checkout.google.com/api/checkout/v2/';
  136. }
  137. return $url;
  138. }
  139. abstract protected function _getApiUrl();
  140. public function _call($xml)
  141. {
  142. $auth = 'Basic ' . base64_encode($this->getMerchantId() . ':' . $this->getMerchantKey());
  143. $headers = array(
  144. 'Authorization: ' . $auth,
  145. 'Content-Type: application/xml;charset=UTF-8',
  146. 'Accept: application/xml;charset=UTF-8',
  147. );
  148. $url = $this->_getApiUrl();
  149. $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . $xml;
  150. $debugData = array('request' => $xml, 'dir' => 'out');
  151. try {
  152. $http = new Varien_Http_Adapter_Curl();
  153. $http->write('POST', $url, '1.1', $headers, $xml);
  154. $response = $http->read();
  155. $response = preg_split('/^\r?$/m', $response, 2);
  156. $response = trim($response[1]);
  157. $debugData['result'] = $response;
  158. $http->close();
  159. }
  160. catch (Exception $e) {
  161. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  162. $this->getApi()->debugData($debugData);
  163. throw $e;
  164. }
  165. $this->getApi()->debugData($debugData);
  166. $result = @simplexml_load_string($response);
  167. if (!$result) {
  168. $result = simplexml_load_string(
  169. '<error><error-message>Invalid response from Google Checkout server</error-message></error>'
  170. );
  171. }
  172. if ($result->getName() == 'error') {
  173. $this->setError($this->__('Google Checkout: %s', (string)$result->{'error-message'}));
  174. $this->setWarnings((array)$result->{'warning-messages'});
  175. } else {
  176. $this->unsError()->unsWarnings();
  177. }
  178. $this->setResult($result);
  179. return $result;
  180. }
  181. protected function _getCallbackUrl()
  182. {
  183. return Mage::getUrl(
  184. 'googlecheckout/api',
  185. array('_forced_secure'=>Mage::getStoreConfig('google/checkout/use_secure_callback_url',$this->getStoreId()))
  186. );
  187. }
  188. /**
  189. * Recalculate amount to store currency
  190. *
  191. * @param float $amount
  192. * @param Mage_Sales_Model_Quote $quote
  193. * @return float
  194. */
  195. protected function _reCalculateToStoreCurrency($amount, $quote)
  196. {
  197. if ($quote->getQuoteCurrencyCode() != $quote->getBaseCurrencyCode()) {
  198. $amount = $amount * $quote->getStoreToQuoteRate();
  199. $amount = Mage::app()->getStore()->roundPrice($amount);
  200. }
  201. return $amount;
  202. }
  203. /**
  204. * Get Tax Class for Shipping option
  205. *
  206. * @param Mage_Sales_Model_Quote $quote
  207. * @return mixed
  208. */
  209. protected function _getTaxClassForShipping($quote)
  210. {
  211. return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $quote->getStoreId());
  212. }
  213. }