PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/googlecheckout/googlecart.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 1613 lines | 1089 code | 111 blank | 413 comment | 180 complexity | 5b11319b8489cd2cb50bf2f38797fbb0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * Copyright (C) 2007 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * Classes used to build a shopping cart and submit it to Google Checkout
  19. * @version $Id: googlecart.php 1234 2007-09-25 14:58:57Z ropu $
  20. */
  21. define('MAX_DIGITAL_DESC', 1024);
  22. /**
  23. * Creates a Google Checkout shopping cart and posts it
  24. * to the google checkout sandbox or production environment
  25. * Refer demo/cartdemo.php for different use case scenarios for this code
  26. */
  27. class GoogleCart {
  28. var $merchant_id;
  29. var $merchant_key;
  30. var $variant = false;
  31. var $currency;
  32. var $server_url;
  33. var $schema_url;
  34. var $base_url;
  35. var $checkout_url;
  36. var $checkout_diagnose_url;
  37. var $request_url;
  38. var $request_diagnose_url;
  39. var $cart_expiration = "";
  40. var $merchant_private_data = "";
  41. var $edit_cart_url = "";
  42. var $continue_shopping_url = "";
  43. var $request_buyer_phone = "";
  44. var $merchant_calculated_tax = "";
  45. var $merchant_calculations_url = "";
  46. var $accept_merchant_coupons = "";
  47. var $accept_gift_certificates = "";
  48. var $rounding_mode;
  49. var $rounding_rule;
  50. var $analytics_data;
  51. var $item_arr;
  52. var $shipping_arr;
  53. var $default_tax_rules_arr;
  54. var $alternate_tax_tables_arr;
  55. var $xml_data;
  56. var $googleAnalytics_id = false;
  57. var $thirdPartyTackingUrl = false;
  58. var $thirdPartyTackingParams = array();
  59. // For HTML API Conversion
  60. // This tags are those that can be used more than once as a sub tag
  61. // so a "-#" must be added always
  62. /**
  63. * used when using the html api
  64. * tags that can be used more than once, so they need to be numbered
  65. * ("-#" suffix)
  66. */
  67. var $multiple_tags = array(
  68. 'flat-rate-shipping' => array(),
  69. 'merchant-calculated-shipping' => array(),
  70. 'pickup' => array(),
  71. 'parameterized-url' => array(),
  72. 'url-parameter' => array(),
  73. 'item' => array(),
  74. 'us-state-area' => array('tax-area'),
  75. 'us-zip-area' => array('tax-area'),
  76. 'us-country-area' => array('tax-area'),
  77. 'postal-area' => array('tax-area'),
  78. 'alternate-tax-table' => array(),
  79. 'world-area' => array('tax-area'),
  80. 'default-tax-rule' => array(),
  81. 'alternate-tax-rule' => array(),
  82. 'gift-certificate-adjustment' => array(),
  83. 'coupon-adjustment' => array(),
  84. 'coupon-result' => array(),
  85. 'gift-certificate-result' => array(),
  86. 'method' => array(),
  87. 'anonymous-address' => array(),
  88. 'result' => array(),
  89. 'string' => array(),
  90. );
  91. var $ignore_tags = array(
  92. 'xmlns' => true,
  93. 'checkout-shopping-cart' => true,
  94. // Dont know how to translate these tag yet
  95. 'merchant-private-data' => true,
  96. 'merchant-private-item-data' => true,
  97. );
  98. /**
  99. * Has all the logic to build the cart's xml (or html) request to be
  100. * posted to google's servers.
  101. *
  102. * @param string $id the merchant id
  103. * @param string $key the merchant key
  104. * @param string $server_type the server type of the server to be used, one
  105. * of 'sandbox' or 'production'.
  106. * defaults to 'sandbox'
  107. * @param string $currency the currency of the items to be added to the cart
  108. * , as of now values can be 'USD' or 'GBP'.
  109. * defaults to 'USD'
  110. */
  111. function GoogleCart($id, $key, $server_type="sandbox", $currency="USD") {
  112. $this->merchant_id = $id;
  113. $this->merchant_key = $key;
  114. $this->currency = $currency;
  115. if(strtolower($server_type) == "sandbox") {
  116. $this->server_url = "https://sandbox.google.com/checkout/";
  117. } else {
  118. $this->server_url= "https://checkout.google.com/";
  119. }
  120. $this->schema_url = "http://checkout.google.com/schema/2";
  121. $this->base_url = $this->server_url . "api/checkout/v2/";
  122. $this->checkout_url = $this->base_url . "checkout/Merchant/" . $this->merchant_id;
  123. $this->checkoutForm_url = $this->base_url . "checkoutForm/Merchant/" . $this->merchant_id;
  124. //The item, shipping and tax table arrays are initialized
  125. $this->item_arr = array();
  126. $this->shipping_arr = array();
  127. $this->alternate_tax_tables_arr = array();
  128. }
  129. /**
  130. * Sets the cart's expiration date
  131. *
  132. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_good-until-date <good-until-date>}
  133. *
  134. * @param string $cart_expire a string representing a date in the
  135. * iso 8601 date and time format: {@link http://www.w3.org/TR/NOTE-datetime}
  136. *
  137. * @return void
  138. */
  139. function SetCartExpiration($cart_expire) {
  140. $this->cart_expiration = $cart_expire;
  141. }
  142. /**
  143. * Sets the merchant's private data.
  144. *
  145. * Google Checkout will return this data in the
  146. * <merchant-calculation-callback> and the
  147. * <new-order-notification> for the order.
  148. *
  149. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_merchant-private-data <merchant-private-data>}
  150. *
  151. * @param MerchantPrivateData $data an object which contains the data to be
  152. * sent as merchant-private-data
  153. *
  154. * @return void
  155. */
  156. function SetMerchantPrivateData($data) {
  157. $this->merchant_private_data = $data;
  158. }
  159. /**
  160. * Sets the url where the customer can edit his cart.
  161. *
  162. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_edit-cart-url <edit-cart-url>}
  163. *
  164. * @param string $url the merchant's site edit cart url
  165. * @return void
  166. */
  167. function SetEditCartUrl($url) {
  168. $this->edit_cart_url= $url;
  169. }
  170. /**
  171. * Sets the continue shopping url, which allows the customer to return
  172. * to the merchant's site after confirming an order.
  173. *
  174. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_continue-shopping-url <continue-shopping-url>}
  175. *
  176. * @param string $url the merchant's site continue shopping url
  177. * @return void
  178. */
  179. function SetContinueShoppingUrl($url) {
  180. $this->continue_shopping_url = $url;
  181. }
  182. /**
  183. * Sets whether the customer must enter a phone number to complete an order.
  184. * If set to true, the customer must enter a number, which Google Checkout
  185. * will return in the new order notification for the order.
  186. *
  187. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_request-buyer-phone-number <request-buyer-phone-number>}
  188. *
  189. * @param bool $req true if the customer's phone number is *required*
  190. * to complete an order.
  191. * defaults to false.
  192. * @return void
  193. */
  194. function SetRequestBuyerPhone($req) {
  195. $this->request_buyer_phone = $this->_GetBooleanValue($req, "false");
  196. }
  197. /**
  198. * Sets the information about calculations that will be performed by the
  199. * merchant.
  200. *
  201. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_merchant-calculations <merchant-calculations>}
  202. *
  203. * @param string $url the merchant calculations callback url
  204. * @param bool $tax_option true if the merchant has to do tax calculations.
  205. * defaults to false.
  206. * @param bool $coupons true if the merchant accepts discount coupons.
  207. * defaults to false.
  208. * @param bool $gift_cert true if the merchant accepts gift certificates.
  209. * defaults to false.
  210. * @return void
  211. */
  212. function SetMerchantCalculations($url, $tax_option = "false",
  213. $coupons = "false", $gift_cert = "false") {
  214. $this->merchant_calculations_url = $url;
  215. $this->merchant_calculated_tax = $this->_GetBooleanValue($tax_option, "false");
  216. $this->accept_merchant_coupons = $this->_GetBooleanValue($coupons, "false");
  217. $this->accept_gift_certificates = $this->_GetBooleanValue($gift_cert, "false");
  218. }
  219. /**
  220. * Add an item to the cart.
  221. *
  222. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_item <item>}
  223. *
  224. * @param GoogleItem $google_item an object that represents an item
  225. * (defined in googleitem.php)
  226. *
  227. * @return void
  228. */
  229. function AddItem($google_item) {
  230. $this->item_arr[] = $google_item;
  231. }
  232. /**
  233. * Add a shipping method to the cart.
  234. *
  235. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_shipping-methods <shipping-methods>}
  236. *
  237. * @param object $ship an object that represents a shipping method, must be
  238. * one of the methods defined in googleshipping.php
  239. *
  240. * @return void
  241. */
  242. function AddShipping($ship) {
  243. $this->shipping_arr[] = $ship;
  244. }
  245. /**
  246. * Add a default tax rule to the cart.
  247. *
  248. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_default-tax-rule <default-tax-rule>}
  249. *
  250. * @param GoogleDefaultTaxRule $rules an object that represents a default
  251. * tax rule (defined in googletax.php)
  252. *
  253. * @return void
  254. */
  255. function AddDefaultTaxRules($rules) {
  256. $this->default_tax_table = true;
  257. $this->default_tax_rules_arr[] = $rules;
  258. }
  259. /**
  260. * Add an alternate tax table to the cart.
  261. *
  262. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_alternate-tax-table <alternate-tax-table>}
  263. *
  264. * @param GoogleAlternateTaxTable $tax an object that represents an
  265. * alternate tax table
  266. * (defined in googletax.php)
  267. *
  268. * @return void
  269. */
  270. function AddAlternateTaxTables($tax) {
  271. $this->alternate_tax_tables_arr[] = $tax;
  272. }
  273. /**
  274. * Set the policy to be used to round monetary values.
  275. * Rounding policy explanation here:
  276. * {@link http://code.google.com/apis/checkout/developer/Google_Checkout_Rounding_Policy.html}
  277. *
  278. * GC tag: {@link http://code.google.com/apis/checkout/developer/index.html#tag_rounding-policy <rounding-policy>}
  279. *
  280. * @param string $mode one of "UP", "DOWN", "CEILING", "HALF_DOWN"
  281. * or "HALF_EVEN", described here: {@link http://java.sun.com/j2se/1.5.0/docs/api/java/math/RoundingMode.html}
  282. * @param string $rule one of "PER_LINE", "TOTAL"
  283. *
  284. * @return void
  285. */
  286. function AddRoundingPolicy($mode, $rule) {
  287. switch ($mode) {
  288. case "UP":
  289. case "DOWN":
  290. case "CEILING":
  291. case "HALF_UP":
  292. case "HALF_DOWN":
  293. case "HALF_EVEN":
  294. $this->rounding_mode = $mode;
  295. break;
  296. default:
  297. break;
  298. }
  299. switch ($rule) {
  300. case "PER_LINE":
  301. case "TOTAL":
  302. $this->rounding_rule = $rule;
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. /**
  309. * Set the google analytics data.
  310. *
  311. * {@link http://code.google.com/apis/checkout/developer/checkout_analytics_integration.html info on Checkout and Analytics integration}
  312. *
  313. * @param string $data the analytics data
  314. *
  315. * @return void
  316. */
  317. function SetAnalyticsData($data) {
  318. $this->analytics_data = $data;
  319. }
  320. /**
  321. * Add a google analytics tracking id.
  322. *
  323. * {@link http://code.google.com/apis/checkout/developer/checkout_analytics_integration.html info on Checkout and Analytics integration}
  324. *
  325. * @param string $GA_id the google analytics id
  326. *
  327. * @return void
  328. */
  329. function AddGoogleAnalyticsTracking($GA_id) {
  330. $this->googleAnalytics_id = $GA_id;
  331. }
  332. /**
  333. * Add third-party tracking to the cart
  334. *
  335. * Described here:
  336. * {@link http://code.google.com/apis/checkout/developer/checkout_analytics_integration.html#googleCheckoutAnalyticsIntegrationAlternate}
  337. *
  338. * @param $tracking_attr_types attributes to be tracked, one of
  339. * ('buyer-id',
  340. * 'order-id',
  341. * 'order-subtotal',
  342. * 'order-subtotal-plus-tax',
  343. * 'order-subtotal-plus-shipping',
  344. * 'order-total',
  345. * 'tax-amount',
  346. * 'shipping-amount',
  347. * 'coupon-amount',
  348. * 'coupon-amount',
  349. * 'billing-city',
  350. * 'billing-region',
  351. * 'billing-postal-code',
  352. * 'billing-country-code',
  353. * 'shipping-city',
  354. * 'shipping-region',
  355. * 'shipping-postal-code',
  356. * 'shipping-country-code')
  357. * More info http://code.google.com/apis/checkout/developer/checkout_pixel_tracking.html#googleCheckout_tag_url-parameter
  358. */
  359. function AddThirdPartyTracking($url, $tracking_param_types = array()) {
  360. $this->thirdPartyTackingUrl = $url;
  361. $this->thirdPartyTackingParams = $tracking_param_types;
  362. }
  363. /**
  364. * Builds the cart's xml to be sent to Google Checkout.
  365. *
  366. * @return string the cart's xml
  367. */
  368. function GetXML() {
  369. require_once('xml-processing/gc_xmlbuilder.php');
  370. $xml_data = new gc_XmlBuilder();
  371. $xml_data->Push('checkout-shopping-cart',
  372. array('xmlns' => $this->schema_url));
  373. $xml_data->Push('shopping-cart');
  374. //Add cart expiration if set
  375. if($this->cart_expiration != "") {
  376. $xml_data->Push('cart-expiration');
  377. $xml_data->Element('good-until-date', $this->cart_expiration);
  378. $xml_data->Pop('cart-expiration');
  379. }
  380. //Add XML data for each of the items
  381. $xml_data->Push('items');
  382. foreach($this->item_arr as $item) {
  383. $xml_data->Push('item');
  384. $xml_data->Element('item-name', $item->item_name);
  385. $xml_data->Element('item-description', $item->item_description);
  386. $xml_data->Element('unit-price', $item->unit_price,
  387. array('currency' => $this->currency));
  388. $xml_data->Element('quantity', $item->quantity);
  389. if($item->merchant_private_item_data != '') {
  390. // echo get_class($item->merchant_private_item_data);
  391. if(is_a($item->merchant_private_item_data,
  392. 'merchantprivate')) {
  393. $item->merchant_private_item_data->AddMerchantPrivateToXML($xml_data);
  394. }
  395. else {
  396. $xml_data->Element('merchant-private-item-data',
  397. $item->merchant_private_item_data);
  398. }
  399. }
  400. if($item->merchant_item_id != '')
  401. $xml_data->Element('merchant-item-id', $item->merchant_item_id);
  402. if($item->tax_table_selector != '')
  403. $xml_data->Element('tax-table-selector', $item->tax_table_selector);
  404. // Carrier calculation
  405. if($item->item_weight != '' && $item->numeric_weight !== '') {
  406. $xml_data->EmptyElement('item-weight', array( 'unit' => $item->item_weight,
  407. 'value' => $item->numeric_weight
  408. ));
  409. }
  410. // New Digital Delivery Tags
  411. if($item->digital_content) {
  412. $xml_data->push('digital-content');
  413. if(!empty($item->digital_url)) {
  414. $xml_data->element('description', substr($item->digital_description,
  415. 0, MAX_DIGITAL_DESC));
  416. $xml_data->element('url', $item->digital_url);
  417. // To avoid NULL key message in GC confirmation Page
  418. if(!empty($item->digital_key)) {
  419. $xml_data->element('key', $item->digital_key);
  420. }
  421. }
  422. else {
  423. $xml_data->element('email-delivery',
  424. $this->_GetBooleanValue($item->email_delivery, "true"));
  425. }
  426. $xml_data->pop('digital-content');
  427. }
  428. $xml_data->Pop('item');
  429. }
  430. $xml_data->Pop('items');
  431. if($this->merchant_private_data != '') {
  432. if(is_a($this->merchant_private_data, 'merchantprivate')) {
  433. $this->merchant_private_data->AddMerchantPrivateToXML($xml_data);
  434. }
  435. else {
  436. $xml_data->Element('merchant-private-data',
  437. $this->merchant_private_data);
  438. }
  439. }
  440. $xml_data->Pop('shopping-cart');
  441. $xml_data->Push('checkout-flow-support');
  442. $xml_data->Push('merchant-checkout-flow-support');
  443. if($this->edit_cart_url != '')
  444. $xml_data->Element('edit-cart-url', $this->edit_cart_url);
  445. if($this->continue_shopping_url != '')
  446. $xml_data->Element('continue-shopping-url',
  447. $this->continue_shopping_url);
  448. if(count($this->shipping_arr) > 0)
  449. $xml_data->Push('shipping-methods');
  450. //Add the shipping methods
  451. foreach($this->shipping_arr as $ship) {
  452. //Pickup shipping handled in else part
  453. if($ship->type == "flat-rate-shipping" ||
  454. $ship->type == "merchant-calculated-shipping"
  455. // If shipping-company calc support addr-filtering and shipping restrictions as a subatag of shipping-company-calculated-shipping
  456. // ||$ship->type == "shipping-company-calculated-shipping"
  457. ) {
  458. $xml_data->Push($ship->type, array('name' => $ship->name));
  459. $xml_data->Element('price', $ship->price,
  460. array('currency' => $this->currency));
  461. $shipping_restrictions = $ship->shipping_restrictions;
  462. if (isset($shipping_restrictions)) {
  463. $xml_data->Push('shipping-restrictions');
  464. if ($shipping_restrictions->allow_us_po_box === true) {
  465. $xml_data->Element('allow-us-po-box', "true");
  466. } else {
  467. $xml_data->Element('allow-us-po-box', "false");
  468. }
  469. //Check if allowed restrictions specified
  470. if($shipping_restrictions->allowed_restrictions) {
  471. $xml_data->Push('allowed-areas');
  472. if($shipping_restrictions->allowed_country_area != "")
  473. $xml_data->EmptyElement('us-country-area',
  474. array('country-area' =>
  475. $shipping_restrictions->allowed_country_area));
  476. foreach($shipping_restrictions->allowed_state_areas_arr as $current) {
  477. $xml_data->Push('us-state-area');
  478. $xml_data->Element('state', $current);
  479. $xml_data->Pop('us-state-area');
  480. }
  481. foreach($shipping_restrictions->allowed_zip_patterns_arr as $current) {
  482. $xml_data->Push('us-zip-area');
  483. $xml_data->Element('zip-pattern', $current);
  484. $xml_data->Pop('us-zip-area');
  485. }
  486. if($shipping_restrictions->allowed_world_area === true) {
  487. $xml_data->EmptyElement('world-area');
  488. }
  489. for($i=0; $i<count($shipping_restrictions->allowed_country_codes_arr); $i++) {
  490. $xml_data->Push('postal-area');
  491. $country_code = $shipping_restrictions->allowed_country_codes_arr[$i];
  492. $postal_pattern = $shipping_restrictions->allowed_postal_patterns_arr[$i];
  493. $xml_data->Element('country-code', $country_code);
  494. if ($postal_pattern != "") {
  495. $xml_data->Element('postal-code-pattern', $postal_pattern);
  496. }
  497. $xml_data->Pop('postal-area');
  498. }
  499. $xml_data->Pop('allowed-areas');
  500. }
  501. if($shipping_restrictions->excluded_restrictions) {
  502. if (!$shipping_restrictions->allowed_restrictions) {
  503. $xml_data->EmptyElement('allowed-areas');
  504. }
  505. $xml_data->Push('excluded-areas');
  506. if($shipping_restrictions->excluded_country_area != "")
  507. $xml_data->EmptyElement('us-country-area',
  508. array('country-area' =>
  509. $shipping_restrictions->excluded_country_area));
  510. foreach($shipping_restrictions->excluded_state_areas_arr as $current) {
  511. $xml_data->Push('us-state-area');
  512. $xml_data->Element('state', $current);
  513. $xml_data->Pop('us-state-area');
  514. }
  515. foreach($shipping_restrictions->excluded_zip_patterns_arr as $current) {
  516. $xml_data->Push('us-zip-area');
  517. $xml_data->Element('zip-pattern', $current);
  518. $xml_data->Pop('us-zip-area');
  519. }
  520. for($i=0; $i<count($shipping_restrictions->excluded_country_codes_arr); $i++) {
  521. $xml_data->Push('postal-area');
  522. $country_code = $shipping_restrictions->excluded_country_codes_arr[$i];
  523. $postal_pattern = $shipping_restrictions->excluded_postal_patterns_arr[$i];
  524. $xml_data->Element('country-code', $country_code);
  525. if ($postal_pattern != "") {
  526. $xml_data->Element('postal-code-pattern', $postal_pattern);
  527. }
  528. $xml_data->Pop('postal-area');
  529. }
  530. $xml_data->Pop('excluded-areas');
  531. }
  532. $xml_data->Pop('shipping-restrictions');
  533. }
  534. if ($ship->type == "merchant-calculated-shipping") {
  535. $address_filters = $ship->address_filters;
  536. if (isset($address_filters)) {
  537. $xml_data->Push('address-filters');
  538. if ($address_filters->allow_us_po_box === true) {
  539. $xml_data->Element('allow-us-po-box', "true");
  540. } else {
  541. $xml_data->Element('allow-us-po-box', "false");
  542. }
  543. //Check if allowed restrictions specified
  544. if($address_filters->allowed_restrictions) {
  545. $xml_data->Push('allowed-areas');
  546. if($address_filters->allowed_country_area != "")
  547. $xml_data->EmptyElement('us-country-area',
  548. array('country-area' =>
  549. $address_filters->allowed_country_area));
  550. foreach($address_filters->allowed_state_areas_arr as $current) {
  551. $xml_data->Push('us-state-area');
  552. $xml_data->Element('state', $current);
  553. $xml_data->Pop('us-state-area');
  554. }
  555. foreach($address_filters->allowed_zip_patterns_arr as $current) {
  556. $xml_data->Push('us-zip-area');
  557. $xml_data->Element('zip-pattern', $current);
  558. $xml_data->Pop('us-zip-area');
  559. }
  560. if($address_filters->allowed_world_area === true) {
  561. $xml_data->EmptyElement('world-area');
  562. }
  563. for($i=0; $i<count($address_filters->allowed_country_codes_arr); $i++) {
  564. $xml_data->Push('postal-area');
  565. $country_code = $address_filters->allowed_country_codes_arr[$i];
  566. $postal_pattern = $address_filters->allowed_postal_patterns_arr[$i];
  567. $xml_data->Element('country-code', $country_code);
  568. if ($postal_pattern != "") {
  569. $xml_data->Element('postal-code-pattern', $postal_pattern);
  570. }
  571. $xml_data->Pop('postal-area');
  572. }
  573. $xml_data->Pop('allowed-areas');
  574. }
  575. if($address_filters->excluded_restrictions) {
  576. if (!$address_filters->allowed_restrictions) {
  577. $xml_data->EmptyElement('allowed-areas');
  578. }
  579. $xml_data->Push('excluded-areas');
  580. if($address_filters->excluded_country_area != "")
  581. $xml_data->EmptyElement('us-country-area',
  582. array('country-area' =>
  583. $address_filters->excluded_country_area));
  584. foreach($address_filters->excluded_state_areas_arr as $current) {
  585. $xml_data->Push('us-state-area');
  586. $xml_data->Element('state', $current);
  587. $xml_data->Pop('us-state-area');
  588. }
  589. foreach($address_filters->excluded_zip_patterns_arr as $current) {
  590. $xml_data->Push('us-zip-area');
  591. $xml_data->Element('zip-pattern', $current);
  592. $xml_data->Pop('us-zip-area');
  593. }
  594. for($i=0; $i<count($address_filters->excluded_country_codes_arr); $i++) {
  595. $xml_data->Push('postal-area');
  596. $country_code = $address_filters->excluded_country_codes_arr[$i];
  597. $postal_pattern = $address_filters->excluded_postal_patterns_arr[$i];
  598. $xml_data->Element('country-code', $country_code);
  599. if ($postal_pattern != "") {
  600. $xml_data->Element('postal-code-pattern', $postal_pattern);
  601. }
  602. $xml_data->Pop('postal-area');
  603. }
  604. $xml_data->Pop('excluded-areas');
  605. }
  606. $xml_data->Pop('address-filters');
  607. }
  608. }
  609. $xml_data->Pop($ship->type);
  610. }
  611. else if ($ship->type == "carrier-calculated-shipping"){
  612. // $xml_data->Push($ship->type, array('name' => $ship->name));
  613. $xml_data->Push($ship->type);
  614. $xml_data->Push('carrier-calculated-shipping-options');
  615. $CCSoptions = $ship->CarrierCalculatedShippingOptions;
  616. foreach($CCSoptions as $CCSoption){
  617. $xml_data->Push('carrier-calculated-shipping-option');
  618. $xml_data->Element('price', $CCSoption->price,
  619. array('currency' => $this->currency));
  620. $xml_data->Element('shipping-company', $CCSoption->shipping_company);
  621. $xml_data->Element('shipping-type', $CCSoption->shipping_type);
  622. $xml_data->Element('carrier-pickup', $CCSoption->carrier_pickup);
  623. if(!empty($CCSoption->additional_fixed_charge)) {
  624. $xml_data->Element('additional-fixed-charge',
  625. $CCSoption->additional_fixed_charge,
  626. array('currency' => $this->currency));
  627. }
  628. if(!empty($CCSoption->additional_variable_charge_percent)) {
  629. $xml_data->Element('additional-variable-charge-percent',
  630. $CCSoption->additional_variable_charge_percent);
  631. }
  632. $xml_data->Pop('carrier-calculated-shipping-option');
  633. }
  634. $xml_data->Pop('carrier-calculated-shipping-options');
  635. // $ShippingPackage = $ship->ShippingPackage;
  636. $xml_data->Push('shipping-packages');
  637. $xml_data->Push('shipping-package');
  638. $xml_data->Push('ship-from', array('id' => $ship->ShippingPackage->ship_from->id));
  639. $xml_data->Element('city', $ship->ShippingPackage->ship_from->city);
  640. $xml_data->Element('region', $ship->ShippingPackage->ship_from->region);
  641. $xml_data->Element('postal-code', $ship->ShippingPackage->ship_from->postal_code);
  642. $xml_data->Element('country-code', $ship->ShippingPackage->ship_from->country_code);
  643. $xml_data->Pop('ship-from');
  644. $xml_data->EmptyElement('width', array('unit' => $ship->ShippingPackage->unit,
  645. 'value' => $ship->ShippingPackage->width
  646. ));
  647. $xml_data->EmptyElement('length', array('unit' => $ship->ShippingPackage->unit,
  648. 'value' => $ship->ShippingPackage->length
  649. ));
  650. $xml_data->EmptyElement('height', array('unit' => $ship->ShippingPackage->unit,
  651. 'value' => $ship->ShippingPackage->height
  652. ));
  653. $xml_data->Element('delivery-address-category',
  654. $ship->ShippingPackage->delivery_address_category);
  655. $xml_data->Pop('shipping-package');
  656. $xml_data->Pop('shipping-packages');
  657. $xml_data->Pop($ship->type);
  658. }
  659. else if ($ship->type == "pickup") {
  660. $xml_data->Push('pickup', array('name' => $ship->name));
  661. $xml_data->Element('price', $ship->price,
  662. array('currency' => $this->currency));
  663. $xml_data->Pop('pickup');
  664. }
  665. }
  666. if(count($this->shipping_arr) > 0)
  667. $xml_data->Pop('shipping-methods');
  668. if($this->request_buyer_phone != "")
  669. $xml_data->Element('request-buyer-phone-number',
  670. $this->request_buyer_phone);
  671. if($this->merchant_calculations_url != "") {
  672. $xml_data->Push('merchant-calculations');
  673. $xml_data->Element('merchant-calculations-url',
  674. $this->merchant_calculations_url);
  675. if($this->accept_merchant_coupons != "") {
  676. $xml_data->Element('accept-merchant-coupons',
  677. $this->accept_merchant_coupons);
  678. }
  679. if($this->accept_gift_certificates != "") {
  680. $xml_data->Element('accept-gift-certificates',
  681. $this->accept_gift_certificates);
  682. }
  683. $xml_data->Pop('merchant-calculations');
  684. }
  685. //Set Third party Tracking
  686. if($this->thirdPartyTackingUrl) {
  687. $xml_data->push('parameterized-urls');
  688. $xml_data->push('parameterized-url',
  689. array('url' => $this->thirdPartyTackingUrl));
  690. if(is_array($this->thirdPartyTackingParams)
  691. && count($this->thirdPartyTackingParams)>0) {
  692. $xml_data->push('parameters');
  693. foreach($this->thirdPartyTackingParams as $tracking_param_name =>
  694. $tracking_param_type) {
  695. $xml_data->emptyElement('url-parameter',
  696. array('name' => $tracking_param_name,
  697. 'type' => $tracking_param_type));
  698. }
  699. $xml_data->pop('parameters');
  700. }
  701. $xml_data->pop('parameterized-url');
  702. $xml_data->pop('parameterized-urls');
  703. }
  704. //Set Default and Alternate tax tables
  705. if( (count($this->alternate_tax_tables_arr) != 0)
  706. || (count($this->default_tax_rules_arr) != 0)) {
  707. if($this->merchant_calculated_tax != "") {
  708. $xml_data->Push('tax-tables',
  709. array('merchant-calculated' => $this->merchant_calculated_tax));
  710. }
  711. else {
  712. $xml_data->Push('tax-tables');
  713. }
  714. if(count($this->default_tax_rules_arr) != 0) {
  715. $xml_data->Push('default-tax-table');
  716. $xml_data->Push('tax-rules');
  717. foreach($this->default_tax_rules_arr as $curr_rule) {
  718. if($curr_rule->country_area != "") {
  719. $xml_data->Push('default-tax-rule');
  720. $xml_data->Element('shipping-taxed', $curr_rule->shipping_taxed);
  721. $xml_data->Element('rate', $curr_rule->tax_rate);
  722. $xml_data->Push('tax-area');
  723. $xml_data->EmptyElement('us-country-area',
  724. array('country-area' => $curr_rule->country_area));
  725. $xml_data->Pop('tax-area');
  726. $xml_data->Pop('default-tax-rule');
  727. }
  728. foreach($curr_rule->state_areas_arr as $current) {
  729. $xml_data->Push('default-tax-rule');
  730. $xml_data->Element('shipping-taxed', $curr_rule->shipping_taxed);
  731. $xml_data->Element('rate', $curr_rule->tax_rate);
  732. $xml_data->Push('tax-area');
  733. $xml_data->Push('us-state-area');
  734. $xml_data->Element('state', $current);
  735. $xml_data->Pop('us-state-area');
  736. $xml_data->Pop('tax-area');
  737. $xml_data->Pop('default-tax-rule');
  738. }
  739. foreach($curr_rule->zip_patterns_arr as $current) {
  740. $xml_data->Push('default-tax-rule');
  741. $xml_data->Element('shipping-taxed', $curr_rule->shipping_taxed);
  742. $xml_data->Element('rate', $curr_rule->tax_rate);
  743. $xml_data->Push('tax-area');
  744. $xml_data->Push('us-zip-area');
  745. $xml_data->Element('zip-pattern', $current);
  746. $xml_data->Pop('us-zip-area');
  747. $xml_data->Pop('tax-area');
  748. $xml_data->Pop('default-tax-rule');
  749. }
  750. for($i=0; $i<count($curr_rule->country_codes_arr); $i++) {
  751. $xml_data->Push('default-tax-rule');
  752. $xml_data->Element('shipping-taxed', $curr_rule->shipping_taxed);
  753. $xml_data->Element('rate', $curr_rule->tax_rate);
  754. $xml_data->Push('tax-area');
  755. $xml_data->Push('postal-area');
  756. $country_code = $curr_rule->country_codes_arr[$i];
  757. $postal_pattern = $curr_rule->postal_patterns_arr[$i];
  758. $xml_data->Element('country-code', $country_code);
  759. if ($postal_pattern != "") {
  760. $xml_data->Element('postal-code-pattern', $postal_pattern);
  761. }
  762. $xml_data->Pop('postal-area');
  763. $xml_data->Pop('tax-area');
  764. $xml_data->Pop('default-tax-rule');
  765. }
  766. if ($curr_rule->world_area === true) {
  767. $xml_data->Push('default-tax-rule');
  768. $xml_data->Element('shipping-taxed', $curr_rule->shipping_taxed);
  769. $xml_data->Element('rate', $curr_rule->tax_rate);
  770. $xml_data->Push('tax-area');
  771. $xml_data->EmptyElement('world-area');
  772. $xml_data->Pop('tax-area');
  773. $xml_data->Pop('default-tax-rule');
  774. }
  775. }
  776. $xml_data->Pop('tax-rules');
  777. $xml_data->Pop('default-tax-table');
  778. }
  779. if(count($this->alternate_tax_tables_arr) != 0) {
  780. $xml_data->Push('alternate-tax-tables');
  781. foreach($this->alternate_tax_tables_arr as $curr_table) {
  782. $xml_data->Push('alternate-tax-table',
  783. array('standalone' => $curr_table->standalone,
  784. 'name' => $curr_table->name));
  785. $xml_data->Push('alternate-tax-rules');
  786. foreach($curr_table->tax_rules_arr as $curr_rule) {
  787. if($curr_rule->country_area != "") {
  788. $xml_data->Push('alternate-tax-rule');
  789. $xml_data->Element('rate', $curr_rule->tax_rate);
  790. $xml_data->Push('tax-area');
  791. $xml_data->EmptyElement('us-country-area',
  792. array('country-area' => $curr_rule->country_area));
  793. $xml_data->Pop('tax-area');
  794. $xml_data->Pop('alternate-tax-rule');
  795. }
  796. foreach($curr_rule->state_areas_arr as $current) {
  797. $xml_data->Push('alternate-tax-rule');
  798. $xml_data->Element('rate', $curr_rule->tax_rate);
  799. $xml_data->Push('tax-area');
  800. $xml_data->Push('us-state-area');
  801. $xml_data->Element('state', $current);
  802. $xml_data->Pop('us-state-area');
  803. $xml_data->Pop('tax-area');
  804. $xml_data->Pop('alternate-tax-rule');
  805. }
  806. foreach($curr_rule->zip_patterns_arr as $current) {
  807. $xml_data->Push('alternate-tax-rule');
  808. $xml_data->Element('rate', $curr_rule->tax_rate);
  809. $xml_data->Push('tax-area');
  810. $xml_data->Push('us-zip-area');
  811. $xml_data->Element('zip-pattern', $current);
  812. $xml_data->Pop('us-zip-area');
  813. $xml_data->Pop('tax-area');
  814. $xml_data->Pop('alternate-tax-rule');
  815. }
  816. for($i=0; $i<count($curr_rule->country_codes_arr); $i++) {
  817. $xml_data->Push('alternate-tax-rule');
  818. $xml_data->Element('rate', $curr_rule->tax_rate);
  819. $xml_data->Push('tax-area');
  820. $xml_data->Push('postal-area');
  821. $country_code = $curr_rule->country_codes_arr[$i];
  822. $postal_pattern = $curr_rule->postal_patterns_arr[$i];
  823. $xml_data->Element('country-code', $country_code);
  824. if ($postal_pattern != "") {
  825. $xml_data->Element('postal-code-pattern', $postal_pattern);
  826. }
  827. $xml_data->Pop('postal-area');
  828. $xml_data->Pop('tax-area');
  829. $xml_data->Pop('alternate-tax-rule');
  830. }
  831. if ($curr_rule->world_area === true) {
  832. $xml_data->Push('alternate-tax-rule');
  833. $xml_data->Element('rate', $curr_rule->tax_rate);
  834. $xml_data->Push('tax-area');
  835. $xml_data->EmptyElement('world-area');
  836. $xml_data->Pop('tax-area');
  837. $xml_data->Pop('alternate-tax-rule');
  838. }
  839. }
  840. $xml_data->Pop('alternate-tax-rules');
  841. $xml_data->Pop('alternate-tax-table');
  842. }
  843. $xml_data->Pop('alternate-tax-tables');
  844. }
  845. $xml_data->Pop('tax-tables');
  846. }
  847. if (($this->rounding_mode != "") && ($this->rounding_rule != "")) {
  848. $xml_data->Push('rounding-policy');
  849. $xml_data->Element('mode', $this->rounding_mode);
  850. $xml_data->Element('rule', $this->rounding_rule);
  851. $xml_data->Pop('rounding-policy');
  852. }
  853. if($this->analytics_data != ''){
  854. $xml_data->Element('analytics-data', $this->analytics_data);
  855. }
  856. $xml_data->Pop('merchant-checkout-flow-support');
  857. $xml_data->Pop('checkout-flow-support');
  858. $xml_data->Pop('checkout-shopping-cart');
  859. return $xml_data->GetXML();
  860. }
  861. /**
  862. * Set the Google Checkout button's variant.
  863. * {@link http://code.google.com/apis/checkout/developer/index.html#google_checkout_buttons}
  864. *
  865. * @param bool $variant true for an enabled button, false for a
  866. * disabled one
  867. *
  868. * @return void
  869. */
  870. function SetButtonVariant($variant) {
  871. switch ($variant) {
  872. case false:
  873. $this->variant = "disabled";
  874. break;
  875. case true:
  876. default:
  877. $this->variant = "text";
  878. break;
  879. }
  880. }
  881. /**
  882. * Submit a server-to-server request.
  883. * Creates a GoogleRequest object (defined in googlerequest.php) and sends
  884. * it to the Google Checkout server.
  885. *
  886. * more info:
  887. * {@link http://code.google.com/apis/checkout/developer/index.html#alternate_technique}
  888. *
  889. * @return array with the returned http status code (200 if OK) in index 0
  890. * and the redirect url returned by the server in index 1
  891. */
  892. function CheckoutServer2Server($proxy=array(), $certPath='') {
  893. #ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'.');
  894. require_once('googlerequest.php');
  895. $GRequest = new GoogleRequest($this->merchant_id,
  896. $this->merchant_key,
  897. $this->server_url=="https://checkout.google.com/"?
  898. "Production":"sandbox",
  899. $this->currency);
  900. $GRequest->SetProxy($proxy);
  901. $GRequest->SetCertificatePath($certPath);
  902. return $GRequest->SendServer2ServerCart($this->GetXML());
  903. }
  904. /**
  905. * Get the Google Checkout button's html to be used in a server-to-server
  906. * request.
  907. *
  908. * {@link http://code.google.com/apis/checkout/developer/index.html#google_checkout_buttons}
  909. *
  910. * @param string $url the merchant's site url where the form will be posted
  911. * to
  912. * @param string $size the size of the button, one of 'large', 'medium' or
  913. * 'small'.
  914. * defaults to 'large'
  915. * @param bool $variant true for an enabled button, false for a
  916. * disabled one. defaults to true. will be ignored if
  917. * SetButtonVariant() was used before.
  918. * @param string $loc the locale of the button's text, the only valid value
  919. * is 'en_US' (used as default)
  920. * @param bool $showtext whether to show Google Checkout text or not,
  921. * defaults to true.
  922. * @param string $style the background style of the button, one of 'white'
  923. * or 'trans'. defaults to "trans"
  924. *
  925. * @return string the button's html
  926. */
  927. function CheckoutServer2ServerButton($url, $size="large", $variant=true,
  928. $loc="en_US",$showtext=true, $style="trans") {
  929. switch (strtolower($size)) {
  930. case "medium":
  931. $width = "168";
  932. $height = "44";
  933. break;
  934. case "small":
  935. $width = "160";
  936. $height = "43";
  937. break;
  938. case "large":
  939. default:
  940. $width = "180";
  941. $height = "46";
  942. break;
  943. }
  944. if($this->variant == false) {
  945. switch ($variant) {
  946. case false:
  947. $this->variant = "disabled";
  948. break;
  949. case true:
  950. default:
  951. $this->variant = "text";
  952. break;
  953. }
  954. }
  955. $data = "<div style=\"width: ".$width."px\">";
  956. if ($this->variant == "text") {
  957. $data .= "<div align=center><form method=\"POST\" action=\"".
  958. $url . "\"" . ($this->googleAnalytics_id?
  959. " onsubmit=\"setUrchinInputCode();\"":"") . ">
  960. <input type=\"image\" name=\"Checkout\" alt=\"Checkout\"
  961. src=\"". $this->server_url."buttons/checkout.gif?merchant_id=" .
  962. $this->merchant_id."&w=".$width. "&h=".$height."&style=".
  963. $style."&variant=".$this->variant."&loc=".$loc."\"
  964. height=\"".$height."\" width=\"".$width. "\" />";
  965. if($this->googleAnalytics_id) {
  966. $data .= "<input type=\"hidden\" name=\"analyticsdata\" value=\"\">";
  967. }
  968. $data .= "</form></div>";
  969. if($this->googleAnalytics_id) {
  970. $data .= "<!-- Start Google analytics -->
  971. <script src=\"https://ssl.google-analytics.com/urchin.js\" type=\"".
  972. "text/javascript\">
  973. </script>
  974. <script type=\"text/javascript\">
  975. _uacct = \"" . $this->googleAnalytics_id . "\";
  976. urchinTracker();
  977. </script>
  978. <script src=\"https://checkout.google.com/files/digital/urchin_po" .
  979. "st.js\" type=\"text/javascript\"></script>
  980. <!-- End Google analytics -->";
  981. } } else {
  982. $data .= "<div><img alt=\"Checkout\" src=\"" .
  983. "". $this->server_url."buttons/checkout.gif?merchant_id=" .
  984. "".$this->merchant_id."&w=".$width. "&h=".$height."&style=".$style.
  985. "&variant=".$this->variant."&loc=".$loc."\" height=\"".$height."\"".
  986. " width=\"".$width. "\" /></div>";
  987. }
  988. $data .= "</div>";
  989. return $data;
  990. }
  991. /**
  992. * Get the Google Checkout button's html.
  993. *
  994. * {@link http://code.google.com/apis/checkout/developer/index.html#google_checkout_buttons}
  995. *
  996. * @param string $size the size of the button, one of 'large', 'medium' or
  997. * 'small'.
  998. * defaults to 'large'
  999. * @param bool $variant true for an enabled button, false for a
  1000. * disabled one. defaults to true. will be ignored if
  1001. * SetButtonVariant() was used before.
  1002. * @param string $loc the locale of the button's text, the only valid value
  1003. * is 'en_US' (used as default)
  1004. * @param bool $showtext whether to show Google Checkout text or not,
  1005. * defaults to true.
  1006. * @param string $style the background style of the button, one of 'white'
  1007. * or 'trans'. defaults to "trans"
  1008. *
  1009. * @return string the button's html
  1010. */
  1011. function CheckoutButtonCode($size="large", $variant=true, $loc="en_US",
  1012. $showtext=true, $style="trans") {
  1013. switch (strtolower($size)) {
  1014. case "medium":
  1015. $width = "168";
  1016. $height = "44";
  1017. break;
  1018. case "small":
  1019. $width = "160";
  1020. $height = "43";
  1021. break;
  1022. case "large":
  1023. default:
  1024. $width = "180";
  1025. $height = "46";
  1026. break;
  1027. }
  1028. if($this->variant == false) {
  1029. switch ($variant) {
  1030. case false:
  1031. $this->variant = "disabled";
  1032. break;
  1033. case true:
  1034. default:
  1035. $this->variant = "text";
  1036. break;
  1037. }
  1038. }
  1039. $data = "<div style=\"width: ".$width."px\">";
  1040. if ($this->variant == "text") {
  1041. $data .= "<div align=center><form method=\"POST\" action=\"".
  1042. $this->checkout_url . "\"" . ($this->googleAnalytics_id?
  1043. " onsubmit=\"setUrchinInputCode();\"":"") . ">
  1044. <input type=\"hidden\" name=\"cart\" value=\"".
  1045. base64_encode($this->GetXML()) ."\">
  1046. <input type=\"hidden\" name=\"signature\" value=\"".
  1047. base64_encode($this->CalcHmacSha1($this->GetXML())). "\">
  1048. <input type=\"image\" name=\"Checkout\" alt=\"Checkout\"
  1049. src=\"". $this->server_url."buttons/checkout.gif?merchant_id=" .
  1050. $this->merchant_id."&w=".$width. "&h=".$height."&style=".
  1051. $style."&variant=".$this->variant."&loc=".$loc."\"
  1052. height=\"".$height."\" width=\"".$width. "\" />";
  1053. if($this->googleAnalytics_id) {
  1054. $data .= "<input type=\"hidden\" name=\"analyticsdata\" value=\"\">";
  1055. }
  1056. $data .= "</form></div>";
  1057. if($this->googleAnalytics_id) {
  1058. $data .= "<!-- Start Google analytics -->
  1059. <script src=\"https://ssl.google-analytics.com/urchin.js\" type=\"".
  1060. "text/javascript\">
  1061. </script>
  1062. <script type=\"text/javascript\">
  1063. _uacct = \"" . $this->googleAnalytics_id . "\";
  1064. urchinTracker();
  1065. </script>
  1066. <script src=\"https://checkout.google.com/files/digital/urchin_po" .
  1067. "st.js\" type=\"text/javascript\"></script>
  1068. <!-- End Google analytics -->";
  1069. }
  1070. } else {
  1071. $data .= "<div><img alt=\"Checkout\" src=\"" .
  1072. "". $this->server_url."buttons/checkout.gif?merchant_id=" .
  1073. "".$this->merchant_id."&w=".$width. "&h=".$height."&style=".$style.
  1074. "&variant=".$this->variant."&loc=".$loc."\" height=\"".$height."\"".
  1075. " width=\"".$width. "\" /></div>";
  1076. }
  1077. if($showtext) {
  1078. $data .="<div align=\"center\"><a href=\"javascript:void(window.ope".
  1079. "n('http://checkout.google.com/seller/what_is_google_checkout.html'" .
  1080. ",'whatischeckout','scrollbars=0,resizable=1,directories=0,height=2" .
  1081. "50,width=400'));\" onmouseover=\"return window.status = 'What is G" .
  1082. "oogle Checkout?'\" onmouseout=\"return window.status = ''\"><font " .
  1083. "size=\"-2\">What is Google Checkout?</font></a></div>";
  1084. }
  1085. $data .= "</div>";
  1086. return $data;
  1087. }
  1088. //Code for generating Checkout button
  1089. //@param $variant will be ignored if SetButtonVariant() was used before
  1090. function CheckoutButtonNowCode($size="large", $variant=true, $loc="en_US",
  1091. $showtext=true, $style="trans") {
  1092. switch (strtolower($size)) {
  1093. case "small":
  1094. $width = "121";
  1095. $height = "44";
  1096. break;
  1097. case "large":
  1098. default:
  1099. $width = "117";
  1100. $height = "48";
  1101. break;
  1102. }
  1103. if($this->variant == false) {
  1104. switch ($variant) {
  1105. case false:
  1106. $this->variant = "disabled"

Large files files are truncated, but you can click here to view the full file