1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: set_include_path(get_include_path().PS.Mage::getBaseDir('lib').DS.'googlecheckout');
28:
29: require_once('googleresponse.php');
30: require_once('googlemerchantcalculations.php');
31: require_once('googleresult.php');
32: require_once('googlerequest.php');
33:
34: abstract class Mage_GoogleCheckout_Model_Api_Xml_Abstract extends Varien_Object
35: {
36: public function log($text, $nl=true)
37: {
38: error_log(print_r($text, 1) . ($nl ? "\n" : ''), 3, Mage::getBaseDir('log') . DS . 'callback.log');
39: return $this;
40: }
41:
42: public function __()
43: {
44: $args = func_get_args();
45: $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), 'Mage_GoogleCheckout');
46: array_unshift($args, $expr);
47: return Mage::app()->getTranslator()->translate($args);
48: }
49:
50: public function getMerchantId()
51: {
52: if (!$this->hasData('merchant_id')) {
53: $this->setData('merchant_id', Mage::getStoreConfig('google/checkout/merchant_id', $this->getStoreId()));
54: }
55: return $this->getData('merchant_id');
56: }
57:
58: public function getMerchantKey()
59: {
60: if (!$this->hasData('merchant_key')) {
61: $this->setData('merchant_key', Mage::getStoreConfig('google/checkout/merchant_key', $this->getStoreId()));
62: }
63: return $this->getData('merchant_key');
64: }
65:
66: public function getServerType()
67: {
68: if (!$this->hasData('server_type')) {
69: $this->setData(
70: 'server_type',
71: Mage::getStoreConfig('google/checkout/sandbox', $this->getStoreId()) ? "sandbox" : ""
72: );
73: }
74: return $this->getData('server_type');
75: }
76:
77: public function getLocale()
78: {
79: if (!$this->hasData('locale')) {
80: $this->setData('locale', Mage::getStoreConfig('google/checkout/locale', $this->getStoreId()));
81: }
82: return $this->getData('locale');
83: }
84:
85: public function getCurrency()
86: {
87: if (!$this->hasData('currency')) {
88: $this->setData('currency', Mage::app()->getStore()->getBaseCurrencyCode());
89:
90: }
91: return $this->getData('currency');
92: }
93:
94: 95: 96: 97: 98:
99: public function getGRequest()
100: {
101: if (!$this->hasData('g_request')) {
102: $this->setData('g_request', new GoogleRequest(
103: $this->getMerchantId(),
104: $this->getMerchantKey(),
105: $this->getServerType(),
106: $this->getCurrency()
107: ));
108:
109:
110: $logDir = Mage::getBaseDir('log');
111: $this->getData('g_request')->SetLogFiles(
112: $logDir . DS . 'googleerror.log',
113: $logDir . DS . 'googlemessage.log',
114: L_ALL
115: );
116: }
117: return $this->getData('g_request');
118: }
119:
120: 121: 122: 123: 124:
125: public function getGResponse()
126: {
127: if (!$this->hasData('g_response')) {
128: $this->setData('g_response', new GoogleResponse(
129: $this->getMerchantId(),
130: $this->getMerchantKey()
131: ));
132:
133:
134: $logDir = Mage::getBaseDir('log');
135: $this->getData('g_response')->SetLogFiles(
136: $logDir . DS . 'googleerror.log',
137: $logDir . DS . 'googlemessage.log',
138: L_ALL
139: );
140: }
141: return $this->getData('g_response');
142: }
143:
144: protected function _getBaseApiUrl()
145: {
146: $url = 'https://';
147: if ($this->getServerType()=='sandbox') {
148: $url .= 'sandbox.google.com/checkout/api/checkout/v2/';
149: } else {
150: $url .= 'checkout.google.com/api/checkout/v2/';
151: }
152: return $url;
153: }
154:
155: abstract protected function _getApiUrl();
156:
157: public function _call($xml)
158: {
159: $auth = 'Basic ' . base64_encode($this->getMerchantId() . ':' . $this->getMerchantKey());
160:
161: $headers = array(
162: 'Authorization: ' . $auth,
163: 'Content-Type: application/xml;charset=UTF-8',
164: 'Accept: application/xml;charset=UTF-8',
165: );
166:
167: $url = $this->_getApiUrl();
168: $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . $xml;
169:
170: $debugData = array('request' => $xml, 'dir' => 'out');
171:
172: try {
173: $http = new Varien_Http_Adapter_Curl();
174: $http->write('POST', $url, '1.1', $headers, $xml);
175: $response = $http->read();
176: $response = preg_split('/^\r?$/m', $response, 2);
177: $response = trim($response[1]);
178: $debugData['result'] = $response;
179: $http->close();
180: }
181: catch (Exception $e) {
182: $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
183: $this->getApi()->debugData($debugData);
184: throw $e;
185: }
186:
187: $this->getApi()->debugData($debugData);
188: $result = @simplexml_load_string($response);
189: if (!$result) {
190: $result = simplexml_load_string(
191: '<error><error-message>Invalid response from Google Checkout server</error-message></error>'
192: );
193: }
194: if ($result->getName() == 'error') {
195: $this->setError($this->__('Google Checkout: %s', (string)$result->{'error-message'}));
196: $this->setWarnings((array)$result->{'warning-messages'});
197: } else {
198: $this->unsError()->unsWarnings();
199: }
200:
201: $this->setResult($result);
202:
203: return $result;
204: }
205:
206: protected function _getCallbackUrl()
207: {
208: return Mage::getUrl(
209: 'googlecheckout/api',
210: array('_forced_secure'=>Mage::getStoreConfig('google/checkout/use_secure_callback_url',$this->getStoreId()))
211: );
212: }
213:
214: 215: 216: 217: 218: 219: 220:
221: protected function _reCalculateToStoreCurrency($amount, $quote)
222: {
223: if ($quote->getQuoteCurrencyCode() != $quote->getBaseCurrencyCode()) {
224: $amount = $amount * $quote->getStoreToQuoteRate();
225: $amount = Mage::app()->getStore()->roundPrice($amount);
226: }
227: return $amount;
228: }
229:
230: 231: 232: 233: 234: 235:
236: protected function _getTaxClassForShipping($quote)
237: {
238: return Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $quote->getStoreId());
239: }
240: }
241: