PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/3.8pre/wpsc-includes/checkout.class.php

https://github.com/evadne/wp-e-commerce
PHP | 528 lines | 390 code | 82 blank | 56 comment | 90 complexity | 64f2813f9d6bb38b077d3009e1af04ca MD5 | raw file
  1. <?php
  2. /**
  3. * WP eCommerce checkout class
  4. *
  5. * These are the class for the WP eCommerce checkout
  6. * The checkout class handles dispaying the checkout form fields
  7. *
  8. * @package wp-e-commerce
  9. * @subpackage wpsc-checkout-classes
  10. */
  11. function wpsc_google_checkout_submit(){
  12. global $wpdb, $wpsc_cart, $current_user;
  13. $wpsc_checkout = new wpsc_checkout();
  14. $purchase_log_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid` IN('".$_SESSION['wpsc_sessionid']."') LIMIT 1") ;
  15. //$purchase_log_id = 1;
  16. get_currentuserinfo();
  17. // exit('<pre>'.print_r($current_user, true).'</pre>');
  18. if($current_user->display_name != ''){
  19. foreach($wpsc_checkout->checkout_items as $checkoutfield){
  20. // exit(print_r($checkoutfield,true));
  21. if($checkoutfield->unique_name == 'billingfirstname'){
  22. $checkoutfield->value = $current_user->display_name;
  23. }
  24. }
  25. }
  26. if($current_user->user_email != ''){
  27. foreach($wpsc_checkout->checkout_items as $checkoutfield){
  28. // exit(print_r($checkoutfield,true));
  29. if($checkoutfield->unique_name == 'billingemail'){
  30. $checkoutfield->value = $current_user->user_email;
  31. }
  32. }
  33. }
  34. $wpsc_checkout->save_forms_to_db($purchase_log_id);
  35. $wpsc_cart->save_to_db($purchase_log_id);
  36. $wpsc_cart->submit_stock_claims($purchase_log_id);
  37. }
  38. function wpsc_have_checkout_items() {
  39. global $wpsc_checkout;
  40. return $wpsc_checkout->have_checkout_items();
  41. }
  42. function wpsc_the_checkout_item() {
  43. global $wpsc_checkout;
  44. return $wpsc_checkout->the_checkout_item();
  45. }
  46. function wpsc_the_checkout_item_error_class($as_attribute = true) {
  47. global $wpsc_checkout;
  48. if($_SESSION['wpsc_checkout_error_messages'][$wpsc_checkout->checkout_item->id] != '') {
  49. $class_name = 'validation-error';
  50. }
  51. if(($as_attribute == true)){
  52. $output = "class='$class_name'";
  53. } else {
  54. $output = $class_name;
  55. }
  56. return $output;
  57. }
  58. function wpsc_the_checkout_item_error() {
  59. global $wpsc_checkout;
  60. $output = false;
  61. if($_SESSION['wpsc_checkout_error_messages'][$wpsc_checkout->checkout_item->id] != '') {
  62. $output = $_SESSION['wpsc_checkout_error_messages'][$wpsc_checkout->checkout_item->id];
  63. }
  64. return $output;
  65. }
  66. function wpsc_checkout_form_is_header() {
  67. global $wpsc_checkout;
  68. if($wpsc_checkout->checkout_item->type == 'heading') {
  69. $output = true;
  70. } else {
  71. $output = false;
  72. }
  73. return $output;
  74. }
  75. function wpsc_checkout_form_name() {
  76. global $wpsc_checkout;
  77. return $wpsc_checkout->form_name();
  78. }
  79. function wpsc_checkout_form_element_id() {
  80. global $wpsc_checkout;
  81. return $wpsc_checkout->form_element_id();
  82. }
  83. function wpsc_checkout_form_field() {
  84. global $wpsc_checkout;
  85. return $wpsc_checkout->form_field();
  86. }
  87. function wpsc_shipping_region_list($selected_country, $selected_region){
  88. global $wpdb;
  89. if ($selected_country == 'US') {
  90. $region_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_REGION_TAX."` WHERE country_id='136'",ARRAY_A);
  91. $output .= "<select name='region' id='region' onchange='submit_change_country();' >";
  92. foreach ($region_data as $region) {
  93. $selected ='';
  94. if($selected_region == $region['id']) {
  95. $selected = "selected='selected'";
  96. }
  97. $output .= "<option $selected value='{$region['id']}'>".htmlspecialchars($region['name'])."</option>";
  98. }
  99. $output .= "";
  100. $output .= "</select>";
  101. } else {
  102. $output .= " ";
  103. }
  104. return $output;
  105. }
  106. function wpsc_shipping_country_list() {
  107. global $wpdb, $wpsc_shipping_modules;
  108. $output = "<input type='hidden' name='wpsc_ajax_actions' value='update_location' />";
  109. $selected_country = $_SESSION['wpsc_delivery_country'];
  110. $selected_region = $_SESSION['wpsc_delivery_region'];
  111. if($selected_country == null) {
  112. $selected_country = get_option('base_country');
  113. }
  114. if($selected_region == null) {
  115. $selected_region = get_option('base_region');
  116. }
  117. $country_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CURRENCY_LIST."` ORDER BY `country` ASC",ARRAY_A);
  118. $output .= "<select name='country' id='current_country' onchange='submit_change_country();' >";
  119. foreach ($country_data as $country) {
  120. // 23-02-09 fix for custom target market by jeffry
  121. // recon this should be taken out and put into a function somewhere maybe,,,
  122. if($country['visible'] == '1'){
  123. $selected ='';
  124. if($selected_country == $country['isocode']) {
  125. $selected = "selected='selected'";
  126. }
  127. $output .= "<option value='".$country['isocode']."' $selected>".htmlspecialchars($country['country'])."</option>";
  128. }
  129. }
  130. $output .= "</select>";
  131. $output .= wpsc_shipping_region_list($selected_country, $selected_region);
  132. // $output .= "ZipCode:";
  133. if(isset($_POST['zipcode'])) {
  134. if ($_POST['zipcode']=='') {
  135. $zipvalue = 'Your Zipcode';
  136. $_SESSION['wpsc_zipcode'] = $_POST['zipcode'];
  137. $color = '#999';
  138. } else {
  139. $zipvalue = $_POST['zipcode'];
  140. $_SESSION['wpsc_zipcode'] = $_POST['zipcode'];
  141. $color = '#000';
  142. }
  143. } else if(isset($_SESSION['wpsc_zipcode']) && ($_SESSION['wpsc_zipcode'] != '')) {
  144. $zipvalue = $_SESSION['wpsc_zipcode'];
  145. $color = '#000';
  146. } else {
  147. $zipvalue = 'Your Zipcode';
  148. $_SESSION['wpsc_zipcode'] = '';
  149. $color = '#999';
  150. }
  151. $uses_zipcode = false;
  152. $custom_shipping = get_option('custom_shipping_options');
  153. foreach((array)$custom_shipping as $shipping) {
  154. if($wpsc_shipping_modules[$shipping]->needs_zipcode == true) {
  155. $uses_zipcode = true;
  156. }
  157. }
  158. if($uses_zipcode == true) {
  159. $output .= " <input type='text' style='color:".$color.";' onclick='if (this.value==\"Your Zipcode\") {this.value=\"\";this.style.color=\"#000\";}' onblur='if (this.value==\"\") {this.style.color=\"#999\"; this.value=\"Your Zipcode\"; }' value='".$zipvalue."' size='10' name='zipcode' id='zipcode'>";
  160. }
  161. return $output;
  162. }
  163. /**
  164. * The WPSC Checkout class
  165. */
  166. class wpsc_checkout {
  167. // The checkout loop variables
  168. var $checkout_items = array();
  169. var $checkout_item;
  170. var $checkout_item_count = 0;
  171. var $current_checkout_item = -1;
  172. var $in_the_loop = false;
  173. /**
  174. * wpsc_checkout method, gets the tax rate as a percentage, based on the selected country and region
  175. * @access public
  176. */
  177. function wpsc_checkout() {
  178. global $wpdb;
  179. $this->checkout_items = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `active` = '1' ORDER BY `order`;");
  180. $this->checkout_item_count = count($this->checkout_items);
  181. }
  182. function form_name() {
  183. if($this->form_name_is_required() && ($this->checkout_item->type != 'heading')){
  184. return $this->checkout_item->name.' * ';
  185. }else{
  186. return $this->checkout_item->name;
  187. }
  188. }
  189. function form_name_is_required(){
  190. if($this->checkout_item->mandatory == 0){
  191. return false;
  192. }else{
  193. return true;
  194. }
  195. }
  196. /**
  197. * form_element_id method, returns the form html ID
  198. * @access public
  199. */
  200. function form_element_id() {
  201. return 'wpsc_checkout_form_'.$this->checkout_item->id;
  202. }
  203. /**
  204. * form_field method, returns the form html
  205. * @access public
  206. */
  207. function form_field() {
  208. global $wpdb;
  209. switch($this->checkout_item->type) {
  210. case "address":
  211. case "delivery_address":
  212. case "textarea":
  213. $output = "<textarea class='text' id='".$this->form_element_id()."' name='collected_data[{$this->checkout_item->id}]' rows='3' cols='40' >".$_SESSION['wpsc_checkout_saved_values'][$this->checkout_item->id]."</textarea>";
  214. break;
  215. case "country":
  216. $output = wpsc_country_region_list($this->checkout_item->id , false, $_SESSION['wpsc_selected_country'], $_SESSION['wpsc_selected_region'], $this->form_element_id());
  217. break;
  218. case "delivery_country":
  219. $country_name = $wpdb->get_var("SELECT `country` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode`='".$_SESSION['wpsc_delivery_country']."' LIMIT 1");
  220. $output = "<input type='hidden' id='".$this->form_element_id()."' class='shipping_country' name='collected_data[{$this->checkout_item->id}]' value='".$_SESSION['wpsc_delivery_country']."' size='4' /><span class='shipping_country_name'>".$country_name."</span> ";
  221. break;
  222. case "text":
  223. case "city":
  224. case "delivery_city":
  225. case "email":
  226. case "coupon":
  227. default:
  228. $output = "<input type='text' id='".$this->form_element_id()."' class='text' value='".$_SESSION['wpsc_checkout_saved_values'][$this->checkout_item->id]."' name='collected_data[{$this->checkout_item->id}]' />";
  229. break;
  230. }
  231. return $output;
  232. }
  233. /**
  234. * validate_forms method, validates the input from the checkout page
  235. * @access public
  236. */
  237. function validate_forms() {
  238. global $wpdb;
  239. $any_bad_inputs = false;
  240. foreach($this->checkout_items as $form_data) {
  241. $value = $_POST['collected_data'][$form_data->id];
  242. $value_id = (int)$value_id;
  243. $_SESSION['wpsc_checkout_saved_values'][$form_data->id] = $value;
  244. $bad_input = false;
  245. if(($form_data->mandatory == 1) || ($form_data->type == "coupon")) {
  246. switch($form_data->type) {
  247. case "email":
  248. if(!preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,5}$/",$value)) {
  249. $any_bad_inputs = true;
  250. $bad_input = true;
  251. }
  252. break;
  253. case "delivery_country":
  254. case "country":
  255. case "heading":
  256. break;
  257. default:
  258. if($value == null) {
  259. $any_bad_inputs = true;
  260. $bad_input = true;
  261. }
  262. break;
  263. }
  264. if($bad_input === true) {
  265. $_SESSION['wpsc_checkout_error_messages'][$form_data->id] = TXT_WPSC_PLEASEENTERAVALID . " " . strtolower($form_data->name) . ".";
  266. $_SESSION['wpsc_checkout_saved_values'][$form_data->id] = '';
  267. }
  268. }
  269. }
  270. return array('is_valid' => !$any_bad_inputs, 'error_messages' => $bad_input_message);
  271. }
  272. /**
  273. * validate_forms method, validates the input from the checkout page
  274. * @access public
  275. */
  276. function save_forms_to_db($purchase_id) {
  277. global $wpdb;
  278. foreach($this->checkout_items as $form_data) {
  279. $value = $_POST['collected_data'][$form_data->id];
  280. if($value == ''){
  281. $value = $form_data->value;
  282. }
  283. // echo '<pre>'.print_r($form_data,true).'</pre>';
  284. if(is_array($value)){
  285. $value = $value[0];
  286. }
  287. if($form_data->type != 'heading') {
  288. //echo "INSERT INTO `".WPSC_TABLE_SUBMITED_FORM_DATA."` ( `log_id` , `form_id` , `value` ) VALUES ( '{$purchase_id}', '".(int)$form_data->id."', '".$value."');<br />";
  289. $prepared_query = $wpdb->query($wpdb->prepare("INSERT INTO `".WPSC_TABLE_SUBMITED_FORM_DATA."` ( `log_id` , `form_id` , `value` ) VALUES ( %d, %d, %s)", $purchase_id, $form_data->id, $value));
  290. }
  291. }
  292. }
  293. /**
  294. * checkout loop methods
  295. */
  296. function next_checkout_item() {
  297. $this->current_checkout_item++;
  298. $this->checkout_item = $this->checkout_items[$this->current_checkout_item];
  299. return $this->checkout_item;
  300. }
  301. function the_checkout_item() {
  302. $this->in_the_loop = true;
  303. $this->checkout_item = $this->next_checkout_item();
  304. if ( $this->current_checkout_item == 0 ) // loop has just started
  305. do_action('wpsc_checkout_loop_start');
  306. }
  307. function have_checkout_items() {
  308. if ($this->current_checkout_item + 1 < $this->checkout_item_count) {
  309. return true;
  310. } else if ($this->current_checkout_item + 1 == $this->checkout_item_count && $this->checkout_item_count > 0) {
  311. do_action('wpsc_checkout_loop_end');
  312. // Do some cleaning up after the loop,
  313. $this->rewind_checkout_items();
  314. }
  315. $this->in_the_loop = false;
  316. return false;
  317. }
  318. function rewind_checkout_items() {
  319. $_SESSION['wpsc_checkout_error_messages'] = array();
  320. $this->current_checkout_item = -1;
  321. if ($this->checkout_item_count > 0) {
  322. $this->checkout_item = $this->checkout_items[0];
  323. }
  324. }
  325. }
  326. /**
  327. * The WPSC Gateway functions
  328. */
  329. function wpsc_gateway_count() {
  330. global $wpsc_gateway;
  331. return $wpsc_gateway->gateway_count;
  332. }
  333. function wpsc_have_gateways() {
  334. global $wpsc_gateway;
  335. return $wpsc_gateway->have_gateways();
  336. }
  337. function wpsc_the_gateway() {
  338. global $wpsc_gateway;
  339. return $wpsc_gateway->the_gateway();
  340. }
  341. function wpsc_gateway_name() {
  342. global $wpsc_gateway;
  343. $payment_gateway_names = get_option('payment_gateway_names');
  344. if($payment_gateway_names[$wpsc_gateway->gateway['internalname']] != '') {
  345. $display_name = $payment_gateway_names[$wpsc_gateway->gateway['internalname']];
  346. } else {
  347. switch($selected_gateway_data['payment_type']) {
  348. case "paypal";
  349. $display_name = "PayPal";
  350. break;
  351. case "manual_payment":
  352. $display_name = "Manual Payment";
  353. break;
  354. case "google_checkout":
  355. $display_name = "Google Checkout";
  356. break;
  357. case "credit_card":
  358. default:
  359. $display_name = "Credit Card";
  360. break;
  361. }
  362. }
  363. return $display_name;
  364. }
  365. function wpsc_gateway_internal_name() {
  366. global $wpsc_gateway;
  367. return $wpsc_gateway->gateway['internalname'];
  368. }
  369. function wpsc_gateway_is_checked() {
  370. global $wpsc_gateway;
  371. $is_checked = false;
  372. if(isset($_SESSION['wpsc_previous_selected_gateway'])) {
  373. if($wpsc_gateway->gateway['internalname'] == $_SESSION['wpsc_previous_selected_gateway']) {
  374. $is_checked = true;
  375. }
  376. } else {
  377. if($wpsc_gateway->current_gateway == 0) {
  378. $is_checked = true;
  379. }
  380. }
  381. if($is_checked == true) {
  382. $output = 'checked="checked"';
  383. } else {
  384. $output = '';
  385. }
  386. return $output;
  387. }
  388. function wpsc_gateway_form_fields() {
  389. global $wpsc_gateway, $gateway_checkout_form_fields;
  390. return $gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']];
  391. }
  392. function wpsc_gateway_form_field_style() {
  393. return "checkout_forms_hidden";
  394. }
  395. /**
  396. * The WPSC Gateway class
  397. */
  398. class wpsc_gateways {
  399. var $wpsc_gateways;
  400. var $gateway;
  401. var $gateway_count = 0;
  402. var $current_gateway = -1;
  403. var $in_the_loop = false;
  404. function wpsc_gateways() {
  405. global $nzshpcrt_gateways;
  406. $gateway_options = get_option('custom_gateway_options');
  407. foreach($nzshpcrt_gateways as $gateway) {
  408. if(array_search($gateway['internalname'], (array)$gateway_options) !== false) {
  409. $this->wpsc_gateways[] = $gateway;
  410. }
  411. }
  412. $this->gateway_count = count($this->wpsc_gateways);
  413. }
  414. /**
  415. * checkout loop methods
  416. */
  417. function next_gateway() {
  418. $this->current_gateway++;
  419. $this->gateway = $this->wpsc_gateways[$this->current_gateway];
  420. return $this->gateway;
  421. }
  422. function the_gateway() {
  423. $this->in_the_loop = true;
  424. $this->gateway = $this->next_gateway();
  425. if ( $this->current_gateway == 0 ) // loop has just started
  426. do_action('wpsc_checkout_loop_start');
  427. }
  428. function have_gateways() {
  429. if ($this->current_gateway + 1 < $this->gateway_count) {
  430. return true;
  431. } else if ($this->current_gateway + 1 == $this->gateway_count && $this->gateway_count > 0) {
  432. do_action('wpsc_checkout_loop_end');
  433. // Do some cleaning up after the loop,
  434. $this->rewind_gateways();
  435. }
  436. $this->in_the_loop = false;
  437. return false;
  438. }
  439. function rewind_gateways() {
  440. $this->current_gateway = -1;
  441. if ($this->gateway_count > 0) {
  442. $this->gateway = $this->wpsc_gateways[0];
  443. }
  444. }
  445. }
  446. ?>