PageRenderTime 151ms CodeModel.GetById 40ms app.highlight 42ms RepoModel.GetById 59ms app.codeStats 1ms

/catalog/controller/payment/google_checkout.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 173 lines | 101 code | 25 blank | 47 comment | 9 complexity | 1d0b46bea149d816648efcb5390135b2 MD5 | raw file
  1<?php
  2class ControllerPaymentGoogleCheckout extends Controller {
  3	public function index() {
  4		if (!$this->config->get('google_checkout_test')) {
  5			$this->data['action'] = 'https://checkout.google.com/api/checkout/v2/checkout/Merchant/' . $this->config->get('google_checkout_merchant_id');	
  6		} else {
  7			$this->data['action'] = 'https://sandbox.google.com/checkout/api/checkout/v2/checkout/Merchant/' . $this->config->get('google_checkout_merchant_id');
  8		}
  9		
 10		$xml  = '<?xml version="1.0" encoding="UTF-8"?>';
 11		$xml .= '<checkout-shopping-cart xmlns="http://checkout.google.com/schema/2">';
 12		$xml .= '	<shopping-cart>';
 13		$xml .= '		<items>';
 14		
 15		$products = $this->cart->getProducts();
 16		
 17		foreach ($products as $product) { 
 18			$option_data = array();
 19			
 20			foreach ($product['option'] as $option) {
 21				$option_data[] = $option['name'] . ': ' . $option['value'];
 22			}
 23		
 24			if ($option_data) {
 25				$name = $product['name'] . ' ' . implode('; ', $option_data);
 26			} else {
 27				$name = $product['name'];
 28			}
 29			
 30			$xml .= '			<item>';
 31			$xml .= '				<merchant-item-id>' . $product['product_id'] . '</merchant-item-id>';
 32			$xml .= '				<item-name>' . $name . '</item-name>'; 
 33			$xml .= '				<item-description>' . substr(strip_tags($product['description']), 0, 299) . '</item-description>';   
 34			$xml .= '				<unit-price currency="' . $this->currency->getCode() . '">' . $product['price'] . '</unit-price>';
 35			$xml .= '				<quantity>' . $product['quantity'] . '</quantity>';
 36			$xml .= '			</item>'; 
 37		}
 38		
 39		$xml .= '		</items>';
 40		$xml .= '	</shopping-cart>';
 41		$xml .= '	<checkout-flow-support>';  
 42		$xml .= '		<merchant-checkout-flow-support>';
 43		$xml .= '			<merchant-calculations>'; 
 44		$xml .= '				<merchant-calculations-url>' . $this->url->link('payment/google_checkout/shipping', '', 'SSL') . '</merchant-calculations-url>';
 45		$xml .= '			</merchant-calculations>';
 46		$xml .= '			<shipping-methods>';
 47		$xml .= '				<merchant-calculated-shipping name="SuperShip International">';
 48		$xml .= '					<price currency="' . $this->currency->getCode() . '">11.00</price>';
 49		$xml .= '					<address-filters>';
 50		$xml .= '						<allowed-areas>';
 51		$xml .= '							<us-country-area country-area="ALL" />';
 52		$xml .= '						</allowed-areas>';
 53		$xml .= '						<allow-us-po-box>false</allow-us-po-box>';
 54		$xml .= '					</address-filters>';
 55		$xml .= '				</merchant-calculated-shipping>';
 56		$xml .= '			</shipping-methods>';
 57		$xml .= '		</merchant-checkout-flow-support>';
 58
 59		$xml .= '	</checkout-flow-support>';
 60		$xml .= '</checkout-shopping-cart>';
 61
 62		$key = $this->config->get('google_checkout_merchant_key');
 63
 64		$blocksize = 64;
 65		$hashfunc = 'sha1';
 66		
 67		if (strlen($key) > $blocksize) {
 68			$key = pack('H*', $hashfunc($key));
 69		}
 70		
 71		$key = str_pad($key, $blocksize, chr(0x00));
 72		$ipad = str_repeat(chr(0x36), $blocksize);
 73		$opad = str_repeat(chr(0x5c), $blocksize);
 74		$hmac = pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $xml))));
 75
 76		$this->data['cart'] = base64_encode($xml);
 77		$this->data['signature'] = base64_encode($hmac);
 78		
 79		if (!$this->config->get('google_checkout_test')) {
 80			$this->data['button'] = 'http://checkout.google.com/checkout/buttons/checkout.gif?merchant_id=' . $this->config->get('google_checkout_merchant_id') . '&w=180&h=46&style=white&variant=text&loc=en_US';	
 81		} else {
 82			$this->data['button'] = 'http://sandbox.google.com/checkout/buttons/checkout.gif?merchant_id=' . $this->config->get('google_checkout_merchant_id') . '&w=180&h=46&style=white&variant=text&loc=en_US';
 83		}
 84		
 85		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/google_checkout.tpl')) {
 86			$this->template = $this->config->get('config_template') . '/template/payment/google_checkout.tpl';
 87		} else {
 88			$this->template = 'default/template/payment/google_checkout.tpl';
 89		}	
 90		
 91		$this->render();
 92	}
 93
 94	public function shipping() {
 95		ob_start();
 96		
 97		print_r($this->request->get);
 98		print_r($this->request->post);
 99
100		$content = ob_get_contents();
101		
102		ob_end_clean();
103		
104		$log = new Logger('error.txt');
105		$log->write($content);
106		
107/*
108<calculate>
109    <addresses>
110        <anonymous-address>
111            <country-code>US</country-code>
112            <city>Mountain View</city>
113            <region>CA</region>
114            <postal-code>94043</postal-code>
115        </anonymous-address>
116    </addresses>
117    <shipping>
118        <method name="FedEx Overnight Shipping"/>
119        <method name="UPS Ground"/>
120    </shipping>
121</calculate>
122
123
124
125		$this->load->model('setting/extension');
126		
127		$quote_data = array();
128		
129		$results = $this->model_setting_extension->getExtensions('shipping');
130		
131		foreach ($results as $result) {
132			$this->load->model('shipping/' . $result['code']);
133			
134			$quote = $this->{'model_shipping_' . $result['code']}->getQuote(); 
135
136			if ($quote) {
137				$quote_data[$result['code']] = array(
138					'title'      => $quote['title'],
139					'quote'      => $quote['quote'], 
140					'sort_order' => $quote['sort_order'],
141					'error'      => $quote['error']
142				);
143			}
144		}
145
146		$sort_order = array();
147	  
148		foreach ($quote_data as $key => $value) {
149      		$sort_order[$key] = $value['sort_order'];
150    	}
151
152    	array_multisort($sort_order, SORT_ASC, $quote_data);
153*/
154		$xml = '<shipping-methods>';
155		
156		foreach ($quote_data as $shipping) {
157			$xml .= '<merchant-calculated-shipping name="UPS Next Day Air">';
158			$xml .= '	<price currency="' . $this->currency->getCode() . '">20.00</price>';
159			$xml .= '		<address-filters>';
160			$xml .= '			<allow-us-po-box>false<allow-us-po-box>';
161			$xml .= '		</address-filters>';
162			$xml .= '</merchant-calculated-shipping>';
163			$xml .= '<merchant-calculated-shipping name="UPS Ground">';
164			$xml .= '	<price currency="' . $this->currency->getCode() . '">15.00</price>';
165			$xml .= '</merchant-calculated-shipping>';
166		}
167		
168		$xml .= '</shipping-methods>';
169		
170		$this->response->setOutput($xml);
171	}
172}
173?>