Source for file gShipping.php

Documentation is available at gShipping.php

  1. <?php
  2. /**
  3. * ========================================================
  4. * phpGCheckout, Open Source PHP G Checkout Library
  5. * http://www.phpgcheckout.com
  6. * ========================================================
  7. *
  8. * Copyright (c) 2006 Expert Database Solutions, LLC
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation the
  13. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  21. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  25. * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28.  
  29. /**
  30. * Enter description here...
  31. *
  32. */
  33. class gShipping {
  34. var $_name;
  35. var $_type; /*flat-rate, pickup or merchant-calculated-shipping */
  36. var $_arr_allowed_states;
  37. var $_allowed_country;
  38. var $_arr_allowed_zips;
  39. var $_arr_excluded_states;
  40. var $_excluded_country;
  41. var $_arr_excluded_zips;
  42. var $_price;
  43. var $_arr_validate_shipping_restrictions;
  44. /**
  45. * Default Constructor
  46. *
  47. * @param unknown_type $name
  48. * @param unknown_type $type
  49. * @return gShipping
  50. */
  51. function gShipping($name, $price, $type) {
  52. $this->_name = $name;
  53. $this->_type = $type;
  54. $this->_price = $price;
  55. /**
  56. * Set Keys for Validating Shipping Restrictions
  57. */
  58. $this->_arr_validate_shipping_restrictions = array('_allowed_country',
  59. '_excluded_country',
  60. '_arr_allowed_states',
  61. '_arr_allowed_zips',
  62. '_arr_excluded_states',
  63. '_arr_excluded_zips');
  64. }
  65. /**
  66. * Add Allowed Areas to Shipping
  67. *
  68. * @param unknown_type $country_area
  69. * @param unknown_type $arr_state_areas
  70. * @param unknown_type $arr_zip_areas
  71. */
  72. function addAllowedAreas($country_area = null, $arr_state_areas = null, $arr_zip_areas = null) {
  73. $this->_allowed_country = $country_area;
  74. $this->_arr_allowed_states = $arr_state_areas;
  75. $this->_arr_allowed_zips = $arr_zip_areas;
  76. }
  77. /**
  78. * Add Excluded Areas to Shipping
  79. *
  80. * @param unknown_type $country_area
  81. * @param unknown_type $arr_state_areas
  82. * @param unknown_type $arr_zip_areas
  83. */
  84. function addExcludedAreas($country_area = null, $arr_state_areas = null, $arr_zip_areas = null) {
  85. $this->_excluded_country = $country_area;
  86. $this->_arr_excluded_states = $arr_state_areas;
  87. $this->_arr_excluded_zips = $arr_zip_areas;
  88.  
  89. }
  90. /**
  91. * get Google Checkout XML
  92. *
  93. */
  94. function getXML() {
  95. $currency = $GLOBALS['GCheckout_currency'];
  96. $str_xml = " <$this->_type name=\"$this->_name\" >";
  97. $str_xml .= " <price currency=\"$currency\" >$this->_price</price>";
  98. /**
  99. * Check if we have any shipping restrictions
  100. */
  101. $has_shipping_restrictions = false;
  102. foreach ($this->_arr_validate_shipping_restrictions as $property_name){
  103. if(!empty($this->$property_name)) $has_shipping_restrictions = true;;
  104. }
  105. if($has_shipping_restrictions) {
  106. $str_xml .= " <shipping-restrictions>";
  107. /**
  108. * Check for allowed areas
  109. */
  110. if(!empty($this->_allowed_country) || !empty($this->_arr_allowed_states) || !empty($this->_arr_allowed_zips)){
  111. $str_xml .= $this->_getAllowedExcludedAreas($this->_allowed_country, $this->_arr_allowed_states, $this->_arr_allowed_zips, 'allowed');
  112. }
  113. /**
  114. * Check for excluded areas
  115. */
  116. if(!empty($this->_excluded_country) || !empty($this->_arr_excluded_states) || !empty($this->_arr_excluded_zips)){
  117. $str_xml .= $this->_getAllowedExcludedAreas($this->_excluded_country, $this->_arr_excluded_states, $this->_arr_excluded_zips, 'excluded');
  118. }
  119.  
  120. $str_xml .= " </shipping-restrictions>";
  121. }
  122. $str_xml .= " </$this->_type>";
  123. return $str_xml;
  124. }
  125. /**
  126. * Builds XML structure for Shipping Restrictions
  127. *
  128. * @param unknown_type $country
  129. * @param unknown_type $arr_states
  130. * @param unknown_type $arr_zips
  131. * @param unknown_type $type
  132. * @return unknown
  133. */
  134. function _getAllowedExcludedAreas($country, $arr_states, $arr_zips, $type) {
  135. $str_xml = "";
  136. if(!empty($country) || !empty($arr_states) || !empty($arr_zips)){
  137. $str_xml .= " <$type-areas>";
  138. /**
  139. * us-country-area
  140. */
  141. if(!empty($country))
  142. $str_xml .= $this->_getCountryArea($country);
  143. /**
  144. * us-sate-areas
  145. */
  146. if(!empty($arr_states))
  147. $str_xml .= $this->_getUsStateArea($arr_states);
  148. /**
  149. * us-zip-areas
  150. */
  151. if(!empty($arr_zips))
  152. $str_xml .= $this->_getUsZipArea($arr_zips);
  153. $str_xml .= " </$type-areas>";
  154. }
  155. return $str_xml;
  156. }
  157. /**
  158. * Builds XML structure for Shipping Restrictions
  159. *
  160. * @param unknown_type $country_area
  161. * @return unknown
  162. */
  163. function _getCountryArea($country_area) {
  164. return " <us-country-area country-area=\"$country_area\" />";
  165. }
  166. /**
  167. * Builds XML structure for Shipping Restrictions
  168. *
  169. * @param unknown_type $arr_state_aras
  170. * @return unknown
  171. */
  172. function _getUsStateArea($arr_state_aras) {
  173. $str = "";
  174. foreach ($arr_state_aras as $state) {
  175. $str .= " <us-state-area>
  176. <state>$state</state>
  177. </us-state-area> ";
  178. }
  179. return $str;
  180. }
  181. /**
  182. * Builds XML structure for Shipping Restrictions
  183. *
  184. * @param unknown_type $arr_zip_areas
  185. * @return unknown
  186. */
  187. function _getUsZipArea($arr_zip_areas) {
  188. $str = "";
  189. foreach ($arr_zip_areas as $zip) {
  190. $str .= " <us-zip-area>
  191. <zip-pattern>$zip</zip-pattern>
  192. </us-zip-area> ";
  193. }
  194. return $str;
  195. }
  196. }
  197. ?>

Documentation generated on Mon, 04 Dec 2006 11:09:39 -0500 by phpDocumentor 1.3.0RC3