00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 set_include_path(get_include_path().PS.Mage::getBaseDir('lib').DS.'googlecheckout');
00028
00029 require_once('googleresponse.php');
00030 require_once('googlemerchantcalculations.php');
00031 require_once('googleresult.php');
00032 require_once('googlerequest.php');
00033
00034 abstract class Mage_GoogleCheckout_Model_Api_Xml_Abstract extends Varien_Object
00035 {
00036 public function log($text, $nl=true)
00037 {
00038 error_log(print_r($text,1).($nl?"\n":''), 3, Mage::getBaseDir('log').DS.'callback.log');
00039 return $this;
00040 }
00041
00042 public function __()
00043 {
00044 $args = func_get_args();
00045 $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), 'Mage_GoogleCheckout');
00046 array_unshift($args, $expr);
00047 return Mage::app()->getTranslator()->translate($args);
00048 }
00049
00050 public function getMerchantId()
00051 {
00052 if (!$this->hasData('merchant_id')) {
00053 $this->setData('merchant_id', Mage::getStoreConfig('google/checkout/merchant_id', $this->getStoreId()));
00054 }
00055 return $this->getData('merchant_id');
00056 }
00057
00058 public function getMerchantKey()
00059 {
00060 if (!$this->hasData('merchant_key')) {
00061 $this->setData('merchant_key', Mage::getStoreConfig('google/checkout/merchant_key', $this->getStoreId()));
00062 }
00063 return $this->getData('merchant_key');
00064 }
00065
00066 public function getServerType()
00067 {
00068 if (!$this->hasData('server_type')) {
00069 $this->setData('server_type', Mage::getStoreConfig('google/checkout/sandbox', $this->getStoreId()) ? "sandbox" : "");
00070 }
00071 return $this->getData('server_type');
00072 }
00073
00074 public function getLocale()
00075 {
00076 if (!$this->hasData('locale')) {
00077 $this->setData('locale', Mage::getStoreConfig('google/checkout/locale', $this->getStoreId()));
00078 }
00079 return $this->getData('locale');
00080 }
00081
00082 public function getCurrency()
00083 {
00084 if (!$this->hasData('currency')) {
00085 $this->setData('currency', Mage::app()->getStore()->getBaseCurrencyCode());
00086
00087 }
00088 return $this->getData('currency');
00089 }
00090
00091
00092
00093
00094
00095
00096 public function getGRequest()
00097 {
00098 if (!$this->hasData('g_request')) {
00099 $this->setData('g_request', new GoogleRequest(
00100 $this->getMerchantId(),
00101 $this->getMerchantKey(),
00102 $this->getServerType(),
00103 $this->getCurrency()
00104 ));
00105
00106
00107 $logDir = Mage::getBaseDir('log');
00108 $this->getData('g_request')->SetLogFiles(
00109 $logDir.DS.'googleerror.log',
00110 $logDir.DS.'googlemessage.log',
00111 L_ALL
00112 );
00113 }
00114 return $this->getData('g_request');
00115 }
00116
00117
00118
00119
00120
00121
00122 public function getGResponse()
00123 {
00124 if (!$this->hasData('g_response')) {
00125 $this->setData('g_response', new GoogleResponse(
00126 $this->getMerchantId(),
00127 $this->getMerchantKey()
00128 ));
00129
00130
00131 $logDir = Mage::getBaseDir('log');
00132 $this->getData('g_response')->SetLogFiles(
00133 $logDir.DS.'googleerror.log',
00134 $logDir.DS.'googlemessage.log',
00135 L_ALL
00136 );
00137 }
00138 return $this->getData('g_response');
00139 }
00140
00141 protected function _getBaseApiUrl()
00142 {
00143 $url = 'https://';
00144 if ($this->getServerType()=='sandbox') {
00145 $url .= 'sandbox.google.com/checkout/api/checkout/v2/';
00146 } else {
00147 $url .= 'checkout.google.com/api/checkout/v2/';
00148 }
00149 return $url;
00150 }
00151
00152 abstract protected function _getApiUrl();
00153
00154 public function _call($xml)
00155 {
00156 $auth = 'Basic '.base64_encode($this->getMerchantId().':'.$this->getMerchantKey());
00157
00158 $headers = array(
00159 'Authorization: '.$auth,
00160 'Content-Type: application/xml;charset=UTF-8',
00161 'Accept: application/xml;charset=UTF-8',
00162 );
00163
00164 $url = $this->_getApiUrl();
00165
00166 $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n".$xml;
00167
00168 if (Mage::getStoreConfig('google/checkout/debug', $this->getStoreId())) {
00169 $debug = Mage::getModel('googlecheckout/api_debug');
00170 $debug->setDir('out')->setUrl($url)->setRequestBody($xml)->save();
00171 }
00172
00173 $http = new Varien_Http_Adapter_Curl();
00174 $http->write('POST', $url, '1.1', $headers, $xml);
00175 $response = $http->read();
00176 $response = preg_split('/^\r?$/m', $response, 2);
00177 $response = trim($response[1]);
00178
00179 if (!empty($debug)) {
00180 $debug->setResponseBody($response)->save();
00181 }
00182
00183 $result = @simplexml_load_string($response);
00184 if (!$result) {
00185 $result = simplexml_load_string('<error><error-message>Invalid response from Google Checkout server</error-message></error>');
00186 }
00187 if ($result->getName()=='error') {
00188 $this->setError($this->__('Google Checkout: %s', (string)$result->{'error-message'}));
00189 $this->setWarnings((array)$result->{'warning-messages'});
00190 } else {
00191 $this->unsError()->unsWarnings();
00192 }
00193
00194 $this->setResult($result);
00195
00196 return $result;
00197 }
00198
00199 protected function _getCallbackUrl()
00200 {
00201 return Mage::getUrl('googlecheckout/api', array('_forced_secure'=>Mage::getStoreConfig('google/checkout/use_secure_callback_url', $this->getStoreId())));
00202 }
00203 }