PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  2. class 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. $xml = '<?xml version="1.0" encoding="UTF-8"?>';
  10. $xml .= '<checkout-shopping-cart xmlns="http://checkout.google.com/schema/2">';
  11. $xml .= ' <shopping-cart>';
  12. $xml .= ' <items>';
  13. $products = $this->cart->getProducts();
  14. foreach ($products as $product) {
  15. $option_data = array();
  16. foreach ($product['option'] as $option) {
  17. $option_data[] = $option['name'] . ': ' . $option['value'];
  18. }
  19. if ($option_data) {
  20. $name = $product['name'] . ' ' . implode('; ', $option_data);
  21. } else {
  22. $name = $product['name'];
  23. }
  24. $xml .= ' <item>';
  25. $xml .= ' <merchant-item-id>' . $product['product_id'] . '</merchant-item-id>';
  26. $xml .= ' <item-name>' . $name . '</item-name>';
  27. $xml .= ' <item-description>' . substr(strip_tags($product['description']), 0, 299) . '</item-description>';
  28. $xml .= ' <unit-price currency="' . $this->currency->getCode() . '">' . $product['price'] . '</unit-price>';
  29. $xml .= ' <quantity>' . $product['quantity'] . '</quantity>';
  30. $xml .= ' </item>';
  31. }
  32. $xml .= ' </items>';
  33. $xml .= ' </shopping-cart>';
  34. $xml .= ' <checkout-flow-support>';
  35. $xml .= ' <merchant-checkout-flow-support>';
  36. $xml .= ' <merchant-calculations>';
  37. $xml .= ' <merchant-calculations-url>' . $this->url->link('payment/google_checkout/shipping', '', 'SSL') . '</merchant-calculations-url>';
  38. $xml .= ' </merchant-calculations>';
  39. $xml .= ' <shipping-methods>';
  40. $xml .= ' <merchant-calculated-shipping name="SuperShip International">';
  41. $xml .= ' <price currency="' . $this->currency->getCode() . '">11.00</price>';
  42. $xml .= ' <address-filters>';
  43. $xml .= ' <allowed-areas>';
  44. $xml .= ' <us-country-area country-area="ALL" />';
  45. $xml .= ' </allowed-areas>';
  46. $xml .= ' <allow-us-po-box>false</allow-us-po-box>';
  47. $xml .= ' </address-filters>';
  48. $xml .= ' </merchant-calculated-shipping>';
  49. $xml .= ' </shipping-methods>';
  50. $xml .= ' </merchant-checkout-flow-support>';
  51. $xml .= ' </checkout-flow-support>';
  52. $xml .= '</checkout-shopping-cart>';
  53. $key = $this->config->get('google_checkout_merchant_key');
  54. $blocksize = 64;
  55. $hashfunc = 'sha1';
  56. if (strlen($key) > $blocksize) {
  57. $key = pack('H*', $hashfunc($key));
  58. }
  59. $key = str_pad($key, $blocksize, chr(0x00));
  60. $ipad = str_repeat(chr(0x36), $blocksize);
  61. $opad = str_repeat(chr(0x5c), $blocksize);
  62. $hmac = pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $xml))));
  63. $this->data['cart'] = base64_encode($xml);
  64. $this->data['signature'] = base64_encode($hmac);
  65. if (!$this->config->get('google_checkout_test')) {
  66. $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';
  67. } else {
  68. $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';
  69. }
  70. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/google_checkout.tpl')) {
  71. $this->template = $this->config->get('config_template') . '/template/payment/google_checkout.tpl';
  72. } else {
  73. $this->template = 'default/template/payment/google_checkout.tpl';
  74. }
  75. $this->render();
  76. }
  77. public function shipping() {
  78. ob_start();
  79. print_r($this->request->get);
  80. print_r($this->request->post);
  81. $content = ob_get_contents();
  82. ob_end_clean();
  83. $log = new Logger('error.txt');
  84. $log->write($content);
  85. /*
  86. <calculate>
  87. <addresses>
  88. <anonymous-address>
  89. <country-code>US</country-code>
  90. <city>Mountain View</city>
  91. <region>CA</region>
  92. <postal-code>94043</postal-code>
  93. </anonymous-address>
  94. </addresses>
  95. <shipping>
  96. <method name="FedEx Overnight Shipping"/>
  97. <method name="UPS Ground"/>
  98. </shipping>
  99. </calculate>
  100. $this->load->model('setting/extension');
  101. $quote_data = array();
  102. $results = $this->model_setting_extension->getExtensions('shipping');
  103. foreach ($results as $result) {
  104. $this->load->model('shipping/' . $result['code']);
  105. $quote = $this->{'model_shipping_' . $result['code']}->getQuote();
  106. if ($quote) {
  107. $quote_data[$result['code']] = array(
  108. 'title' => $quote['title'],
  109. 'quote' => $quote['quote'],
  110. 'sort_order' => $quote['sort_order'],
  111. 'error' => $quote['error']
  112. );
  113. }
  114. }
  115. $sort_order = array();
  116. foreach ($quote_data as $key => $value) {
  117. $sort_order[$key] = $value['sort_order'];
  118. }
  119. array_multisort($sort_order, SORT_ASC, $quote_data);
  120. */
  121. $xml = '<shipping-methods>';
  122. foreach ($quote_data as $shipping) {
  123. $xml .= '<merchant-calculated-shipping name="UPS Next Day Air">';
  124. $xml .= ' <price currency="' . $this->currency->getCode() . '">20.00</price>';
  125. $xml .= ' <address-filters>';
  126. $xml .= ' <allow-us-po-box>false<allow-us-po-box>';
  127. $xml .= ' </address-filters>';
  128. $xml .= '</merchant-calculated-shipping>';
  129. $xml .= '<merchant-calculated-shipping name="UPS Ground">';
  130. $xml .= ' <price currency="' . $this->currency->getCode() . '">15.00</price>';
  131. $xml .= '</merchant-calculated-shipping>';
  132. }
  133. $xml .= '</shipping-methods>';
  134. $this->response->setOutput($xml);
  135. }
  136. }
  137. ?>