PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/checkout.class.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1007 lines | 784 code | 119 blank | 104 comment | 234 complexity | 02ffd762f331e7a0940d134b94c876b0 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_display_tax_label($checkout = false){
  39. global $wpsc_cart;
  40. if(wpsc_tax_isincluded()){
  41. if($checkout){
  42. return sprintf(__('Tax Included (%s%%)', 'wpsc'), $wpsc_cart->tax_percentage);
  43. }else{
  44. return __('Tax Included', 'wpsc');
  45. }
  46. }else{
  47. return __('Tax', 'wpsc');
  48. }
  49. }
  50. function wpsc_have_checkout_items() {
  51. global $wpsc_checkout;
  52. return $wpsc_checkout->have_checkout_items();
  53. }
  54. function wpsc_the_checkout_item() {
  55. global $wpsc_checkout;
  56. return $wpsc_checkout->the_checkout_item();
  57. }
  58. function wpsc_is_shipping_details(){
  59. global $wpsc_checkout;
  60. if($wpsc_checkout->checkout_item->unique_name == 'delivertoafriend' && get_option('shippingsameasbilling') == '1'){
  61. return true;
  62. }else{
  63. return false;
  64. }
  65. }
  66. function wpsc_shipping_details(){
  67. global $wpsc_checkout;
  68. if(stristr($wpsc_checkout->checkout_item->unique_name, 'shipping') != false){
  69. return ' wpsc_shipping_forms';
  70. }else{
  71. return "";
  72. }
  73. }
  74. function wpsc_the_checkout_item_error_class($as_attribute = true) {
  75. global $wpsc_checkout;
  76. if($_SESSION['wpsc_checkout_error_messages'][$wpsc_checkout->checkout_item->id] != '') {
  77. $class_name = 'validation-error';
  78. }
  79. if(($as_attribute == true)){
  80. $output = "class='".$class_name.wpsc_shipping_details()." wpsc_checkout_field".$wpsc_checkout->checkout_item->id."'";
  81. } else {
  82. $output = $class_name;
  83. }
  84. return $output;
  85. }
  86. function wpsc_the_checkout_item_error() {
  87. global $wpsc_checkout;
  88. $output = false;
  89. if($_SESSION['wpsc_checkout_error_messages'][$wpsc_checkout->checkout_item->id] != '') {
  90. $output = $_SESSION['wpsc_checkout_error_messages'][$wpsc_checkout->checkout_item->id];
  91. }
  92. return $output;
  93. }
  94. function wpsc_the_checkout_CC_validation(){
  95. $output = '';
  96. //exit('<pre>'.print_r($_SESSION['wpsc_gateway_error_messages'],true).'</pre>');
  97. if ($_SESSION['wpsc_gateway_error_messages']['card_number'] != ''){
  98. $output = $_SESSION['wpsc_gateway_error_messages']['card_number'];
  99. // $_SESSION['wpsc_gateway_error_messages']['card_number'] = '';
  100. }
  101. return $output;
  102. }
  103. function wpsc_the_checkout_CC_validation_class(){
  104. $output = '';
  105. if ($_SESSION['wpsc_gateway_error_messages']['card_number'] != ''){
  106. $output = 'class="validation-error"';
  107. }
  108. return $output;
  109. }
  110. function wpsc_the_checkout_CCexpiry_validation_class(){
  111. $output = '';
  112. if ($_SESSION['wpsc_gateway_error_messages']['expdate'] != ''){
  113. $output = 'class="validation-error"';
  114. }
  115. return $output;
  116. }
  117. function wpsc_the_checkout_CCexpiry_validation(){
  118. $output = '';
  119. if ($_SESSION['wpsc_gateway_error_messages']['expdate'] != ''){
  120. $output = $_SESSION['wpsc_gateway_error_messages']['expdate'];
  121. // $_SESSION['wpsc_gateway_error_messages']['expdate'] = '';
  122. }
  123. return $output;
  124. }
  125. function wpsc_the_checkout_CCcvv_validation_class(){
  126. $output = '';
  127. if ($_SESSION['wpsc_gateway_error_messages']['card_code'] != ''){
  128. $output = 'class="validation-error"';
  129. }
  130. return $output;
  131. }
  132. function wpsc_the_checkout_CCcvv_validation(){
  133. $output = '';
  134. if ($_SESSION['wpsc_gateway_error_messages']['card_code'] != ''){
  135. $output = $_SESSION['wpsc_gateway_error_messages']['card_code'];
  136. // $_SESSION['wpsc_gateway_error_messages']['card_code'] = '';
  137. }
  138. return $output;
  139. }
  140. function wpsc_the_checkout_CCtype_validation_class(){
  141. $output = '';
  142. if ($_SESSION['wpsc_gateway_error_messages']['cctype'] != ''){
  143. $output = 'class="validation-error"';
  144. }
  145. return $output;
  146. }
  147. function wpsc_the_checkout_CCtype_validation(){
  148. $output = '';
  149. if ($_SESSION['wpsc_gateway_error_messages']['cctype'] != ''){
  150. $output = $_SESSION['wpsc_gateway_error_messages']['cctype'];
  151. //$_SESSION['wpsc_gateway_error_messages']['cctype'] ='';
  152. }
  153. return $output;
  154. }
  155. function wpsc_checkout_form_is_header() {
  156. global $wpsc_checkout;
  157. if($wpsc_checkout->checkout_item->type == 'heading') {
  158. $output = true;
  159. } else {
  160. $output = false;
  161. }
  162. return $output;
  163. }
  164. function wpsc_checkout_form_name() {
  165. global $wpsc_checkout;
  166. return $wpsc_checkout->form_name();
  167. }
  168. function wpsc_checkout_form_element_id() {
  169. global $wpsc_checkout;
  170. return $wpsc_checkout->form_element_id();
  171. }
  172. function wpsc_checkout_form_field() {
  173. global $wpsc_checkout;
  174. return $wpsc_checkout->form_field();
  175. }
  176. function wpsc_shipping_region_list($selected_country, $selected_region, $shippingdetails = false){
  177. global $wpdb;
  178. //$region_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_REGION_TAX."` WHERE country_id='136'",ARRAY_A);
  179. $region_data = $wpdb->get_results("SELECT `regions`.* FROM `".WPSC_TABLE_REGION_TAX."` AS `regions` INNER JOIN `".WPSC_TABLE_CURRENCY_LIST."` AS `country` ON `country`.`id` = `regions`.`country_id` WHERE `country`.`isocode` IN('".$wpdb->escape($selected_country)."')",ARRAY_A);
  180. $js = '';
  181. if(!$shippingdetails){
  182. $js = "onchange='submit_change_country();'";
  183. }
  184. if (count($region_data) > 0) {
  185. $output .= "<select name='region' id='region' ".$js." >";
  186. foreach ($region_data as $region) {
  187. $selected ='';
  188. if($selected_region == $region['id']) {
  189. $selected = "selected='selected'";
  190. }
  191. $output .= "<option $selected value='{$region['id']}'>".htmlspecialchars($region['name'])."</option>";
  192. }
  193. $output .= "";
  194. $output .= "</select>";
  195. } else {
  196. $output .= " ";
  197. }
  198. return $output;
  199. }
  200. function wpsc_shipping_country_list($shippingdetails = false) {
  201. global $wpdb, $wpsc_shipping_modules;
  202. $js='';
  203. if(!$shippingdetails){
  204. $output = "<input type='hidden' name='wpsc_ajax_actions' value='update_location' />";
  205. $js =" onchange='submit_change_country();'";
  206. }
  207. $selected_country = $_SESSION['wpsc_delivery_country'];
  208. $selected_region = $_SESSION['wpsc_delivery_region'];
  209. if($selected_country == null) {
  210. $selected_country = get_option('base_country');
  211. }
  212. if($selected_region == null) {
  213. $selected_region = get_option('base_region');
  214. }
  215. $country_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CURRENCY_LIST."` ORDER BY `country` ASC",ARRAY_A);
  216. $output .= "<select name='country' id='current_country' ".$js." >";
  217. foreach ($country_data as $country) {
  218. // 23-02-09 fix for custom target market by jeffry
  219. // recon this should be taken out and put into a function somewhere maybe,,,
  220. if($country['visible'] == '1'){
  221. $selected ='';
  222. if($selected_country == $country['isocode']) {
  223. $selected = "selected='selected'";
  224. }
  225. $output .= "<option value='".$country['isocode']."' $selected>".htmlspecialchars($country['country'])."</option>";
  226. }
  227. }
  228. $output .= "</select>";
  229. $output .= wpsc_shipping_region_list($selected_country, $selected_region, $shippingdetails);
  230. if($_POST['wpsc_update_location'] == 'true') {
  231. $_SESSION['wpsc_update_location'] = true;
  232. } else {
  233. $_SESSION['wpsc_update_location'] = false;
  234. }
  235. if(isset($_POST['zipcode'])) {
  236. if ($_POST['zipcode']=='') {
  237. $zipvalue = '';
  238. $_SESSION['wpsc_zipcode'] = '';
  239. } else {
  240. $zipvalue = $_POST['zipcode'];
  241. $_SESSION['wpsc_zipcode'] = $_POST['zipcode'];
  242. }
  243. } else if(isset($_SESSION['wpsc_zipcode']) && ($_SESSION['wpsc_zipcode'] != '')) {
  244. $zipvalue = $_SESSION['wpsc_zipcode'];
  245. } else {
  246. $zipvalue = '';
  247. $_SESSION['wpsc_zipcode'] = '';
  248. }
  249. if(($zipvalue != '') && ($zipvalue != 'Your Zipcode')) {
  250. $color = '#000';
  251. } else {
  252. $zipvalue = 'Your Zipcode';
  253. $color = '#999';
  254. }
  255. $uses_zipcode = false;
  256. $custom_shipping = get_option('custom_shipping_options');
  257. foreach((array)$custom_shipping as $shipping) {
  258. if($wpsc_shipping_modules[$shipping]->needs_zipcode == true) {
  259. $uses_zipcode = true;
  260. }
  261. }
  262. if($uses_zipcode == true) {
  263. $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'>";
  264. }
  265. return $output;
  266. }
  267. /**
  268. * The WPSC Checkout class
  269. */
  270. class wpsc_checkout {
  271. // The checkout loop variables
  272. var $checkout_items = array();
  273. var $checkout_item;
  274. var $checkout_item_count = 0;
  275. var $current_checkout_item = -1;
  276. var $in_the_loop = false;
  277. //the ticket additions
  278. var $additional_fields = array();
  279. var $formfield_count =0;
  280. /**
  281. * wpsc_checkout method, gets the tax rate as a percentage, based on the selected country and region
  282. * @access public
  283. */
  284. function wpsc_checkout($checkout_set = 0) {
  285. global $wpdb;
  286. $this->checkout_items = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `active` = '1' AND `checkout_set`='".$checkout_set."' ORDER BY `order`;");
  287. $category_list = wpsc_cart_item_categories(true);
  288. $additional_form_list = array();
  289. foreach($category_list as $category_id) {
  290. $additional_form_list[] = wpsc_get_categorymeta($category_id, 'use_additonal_form_set');
  291. }
  292. if(function_exists('wpsc_get_ticket_checkout_set')){
  293. $checkout_form_fields_id = array_search(wpsc_get_ticket_checkout_set(),$additional_form_list);
  294. unset($additional_form_list[$checkout_form_fields_id]);
  295. }
  296. // exit('Checkout ticket set:'.wpsc_get_ticket_checkout_set().'additional checkout sets:<pre>'.print_r($additional_form_list, true).'</pre>');
  297. //echo "<pre>".print_r($additional_form_list,true)."</pre>";
  298. if(count($additional_form_list) > 0) {
  299. $this->category_checkout_items = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `active` = '1' AND `checkout_set` IN ('".implode("','", $additional_form_list)."') ORDER BY `checkout_set`, `order`;");
  300. $this->checkout_items = array_merge((array)$this->checkout_items,(array)$this->category_checkout_items);
  301. }
  302. //
  303. if(function_exists('wpsc_get_ticket_checkout_set')){
  304. $sql = "SELECT * FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `active` = '1' AND `checkout_set`='".wpsc_get_ticket_checkout_set()."' ORDER BY `order`;";
  305. $this->additional_fields = $wpdb->get_results($sql);
  306. //exit('<pre>'.print_r($this->additional_fields, true).'</pre>');
  307. $count = wpsc_ticket_checkoutfields();
  308. $j = 1;
  309. $fields = $this->additional_fields;
  310. $this->formfield_count = count($fields) + $this->checkout_item_count;
  311. while($j < $count){
  312. $this->additional_fields = array_merge((array)$this->additional_fields, (array)$fields);
  313. $j++;
  314. }
  315. //exit($sql.'<pre>'.print_r($this->additional_fields, true).'</pre>'.$count);
  316. if(wpsc_ticket_checkoutfields() >0){
  317. $this->checkout_items = array_merge((array)$this->checkout_items,(array)$this->additional_fields);
  318. }
  319. }
  320. $this->checkout_item_count = count($this->checkout_items);
  321. }
  322. function form_name() {
  323. if($this->form_name_is_required() && ($this->checkout_item->type != 'heading')){
  324. return stripslashes($this->checkout_item->name).': * ';
  325. }else{
  326. return stripslashes($this->checkout_item->name).': ';
  327. }
  328. }
  329. function form_name_is_required(){
  330. if($this->checkout_item->mandatory == 0){
  331. return false;
  332. }else{
  333. return true;
  334. }
  335. }
  336. /**
  337. * form_element_id method, returns the form html ID
  338. * @access public
  339. */
  340. function form_element_id() {
  341. return 'wpsc_checkout_form_'.$this->checkout_item->id;
  342. }
  343. /**
  344. * get_checkout_options, returns the form field options
  345. * @access public
  346. */
  347. function get_checkout_options($id){
  348. global $wpdb;
  349. $sql = 'SELECT `options` FROM `'.WPSC_TABLE_CHECKOUT_FORMS.'` WHERE `id`='.$id;
  350. $options = $wpdb->get_var($sql);
  351. $options = unserialize($options);
  352. return $options;
  353. }
  354. /**
  355. * form_field method, returns the form html
  356. * @access public
  357. */
  358. function form_field() {
  359. global $wpdb, $user_ID;
  360. //$meta_data[$form_field['id']]
  361. //exit('<pre>'.print_r($_SESSION['wpsc_checkout_saved_values'], true).'</pre>');
  362. if((count($_SESSION['wpsc_checkout_saved_values']) <= 0) && ($user_ID > 0)) {
  363. //$_SESSION['wpsc_checkout_saved_values'] = get_usermeta($user_ID, 'wpshpcrt_usr_profile');
  364. }
  365. $saved_form_data = htmlentities(stripslashes($_SESSION['wpsc_checkout_saved_values'][$this->checkout_item->id]), ENT_QUOTES, 'UTF-8');
  366. //exit('<pre>HERE'.print_r($_POST, true).'</pre>');
  367. $an_array = '';
  368. if(function_exists('wpsc_get_ticket_checkout_set')){
  369. if($this->checkout_item->checkout_set == wpsc_get_ticket_checkout_set()){
  370. $an_array = '[]';
  371. }
  372. }
  373. switch($this->checkout_item->type) {
  374. case "address":
  375. case "delivery_address":
  376. case "textarea":
  377. $output = "<textarea title='".$this->checkout_item->unique_name."' class='text' id='".$this->form_element_id()."' name='collected_data[{$this->checkout_item->id}]".$an_array."' rows='3' cols='40' >".$saved_form_data."</textarea>";
  378. break;
  379. case "checkbox":
  380. $options = $this->get_checkout_options($this->checkout_item->id);
  381. if($options != ''){
  382. $i = mt_rand();
  383. $j=0;
  384. foreach($options as $label=>$value){
  385. $output .= "<input type='hidden' title='".$this->checkout_item->unique_name."' id='".$this->form_element_id().$j."' value='-1' name='collected_data[{$this->checkout_item->id}][".$i."][".$j."]'/><input type='checkbox' title='".$this->checkout_item->unique_name."' id='".$this->form_element_id()."' value='".$value."' name='collected_data[{$this->checkout_item->id}][".$i."][".$j."]'/> ";
  386. $output .= "<label for='".$this->form_element_id().$j."'>".$label."</label><br />";
  387. $j++;
  388. }
  389. }
  390. break;
  391. case "country":
  392. $output = wpsc_country_region_list($this->checkout_item->id , false, $_SESSION['wpsc_selected_country'], $_SESSION['wpsc_selected_region'], $this->form_element_id());
  393. break;
  394. case "delivery_country":
  395. if(wpsc_uses_shipping()){
  396. $country_name = $wpdb->get_var("SELECT `country` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode`='".$_SESSION['wpsc_delivery_country']."' LIMIT 1");
  397. $output = "<input title='".$this->checkout_item->unique_name."' type='hidden' id='".$this->form_element_id()."' class='shipping_country' name='collected_data[{$this->checkout_item->id}]' value='".$_SESSION['wpsc_delivery_country']."' /><span class='shipping_country_name'>".$country_name."</span> ";
  398. }else{
  399. $checkoutfields = true;
  400. //$output = wpsc_shipping_country_list($checkoutfields);
  401. $output = wpsc_country_region_list($this->checkout_item->id , false, $_SESSION['wpsc_selected_country'], $_SESSION['wpsc_selected_region'], $this->form_element_id(), $checkoutfields);
  402. }
  403. break;
  404. case "select":
  405. $options = $this->get_checkout_options($this->checkout_item->id);
  406. if($options != ''){
  407. $output = "<select name='collected_data[{$this->checkout_item->id}]".$an_array."'>";
  408. $output .= "<option value='-1'>Select an Option</option>";
  409. foreach((array)$options as $label => $value){
  410. $value = str_replace(' ', '',$value);
  411. $output .="<option value='".$value."'>".$label."</option>\n\r";
  412. }
  413. $output .="</select>";
  414. }
  415. //echo ('<pre>'.print_r($output, true).'</pre>');
  416. break;
  417. case "radio":
  418. $options = $this->get_checkout_options($this->checkout_item->id);
  419. if($options != ''){
  420. $i = mt_rand();
  421. foreach((array)$options as $label => $value){
  422. $output .= "<input type='radio' title='".$this->checkout_item->unique_name."' id='".$this->form_element_id()."'value='".$value."' name='collected_data[{$this->checkout_item->id}][".$i."]'/> ";
  423. $output .= "<label for='".$this->form_element_id()."'>".$label."</label>";
  424. }
  425. }
  426. break;
  427. case "text":
  428. case "city":
  429. case "delivery_city":
  430. case "email":
  431. case "coupon":
  432. default:
  433. $country_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode` IN('".$_SESSION['wpsc_delivery_country']."') LIMIT 1",ARRAY_A);
  434. if($this->checkout_item->unique_name == 'shippingstate'){
  435. if(wpsc_uses_shipping()&&($country_data['has_regions'] == 1)){
  436. $region_name = $wpdb->get_var("SELECT `name` FROM `".WPSC_TABLE_REGION_TAX."` WHERE `id`='".$_SESSION['wpsc_delivery_region']."' LIMIT 1");
  437. $output = "<input title='".$this->checkout_item->unique_name."' type='hidden' id='".$this->form_element_id()."' class='shipping_region' name='collected_data[{$this->checkout_item->id}]' value='".$_SESSION['wpsc_delivery_region']."' size='4' /><span class='shipping_region_name'>".$region_name."</span> ";
  438. }else{
  439. $output = "<input class='shipping_region' title='".$this->checkout_item->unique_name."' type='text' id='".$this->form_element_id()."' class='text' value='".$saved_form_data."' name='collected_data[{$this->checkout_item->id}]".$an_array."' />";
  440. }
  441. }else{
  442. $output = "<input title='".$this->checkout_item->unique_name."' type='text' id='".$this->form_element_id()."' class='text' value='".$saved_form_data."' name='collected_data[{$this->checkout_item->id}]".$an_array."' />";
  443. }
  444. break;
  445. }
  446. return $output;
  447. }
  448. /**
  449. * validate_forms method, validates the input from the checkout page
  450. * @access public
  451. */
  452. function validate_forms() {
  453. global $wpdb, $current_user, $user_ID;
  454. $any_bad_inputs = false;
  455. // Credit Card Number Validation for Paypal Pro and maybe others soon
  456. if(wpsc_cart_total(false) != 0){
  457. if(isset($_POST['card_number'])){
  458. if($_POST['card_number'] != ''){/*
  459. $ccregex='/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/';
  460. if(!preg_match($ccregex, $_POST['card_number'])){
  461. $any_bad_inputs = true;
  462. $bad_input = true;
  463. $_SESSION['wpsc_gateway_error_messages']['card_number'] = __('Please enter a valid', 'wpsc') . " " . strtolower('card number') . ".";
  464. $_SESSION['wpsc_checkout_saved_values']['card_number'] = '';
  465. }else{
  466. $_SESSION['wpsc_gateway_error_messages']['card_number'] = '';
  467. }
  468. */
  469. }else{
  470. $any_bad_inputs = true;
  471. $bad_input = true;
  472. $_SESSION['wpsc_gateway_error_messages']['card_number'] = __('Please enter a valid', 'wpsc') . " " . strtolower('card number') . ".";
  473. $_SESSION['wpsc_checkout_saved_values']['card_number'] = '';
  474. }
  475. }else{
  476. $_SESSION['wpsc_gateway_error_messages']['card_number'] = '';
  477. }
  478. if(isset($_POST['card_number1']) && isset($_POST['card_number2']) && isset($_POST['card_number3']) && isset($_POST['card_number4'])){
  479. if($_POST['card_number1'] != '' && $_POST['card_number2'] != '' && $_POST['card_number3'] != '' && $_POST['card_number4'] != '' && is_numeric($_POST['card_number1']) && is_numeric($_POST['card_number2']) && is_numeric($_POST['card_number3']) && is_numeric($_POST['card_number4'])){
  480. $_SESSION['wpsc_gateway_error_messages']['card_number'] = '';
  481. }else{
  482. $any_bad_inputs = true;
  483. $bad_input = true;
  484. $_SESSION['wpsc_gateway_error_messages']['card_number'] = __('Please enter a valid', 'wpsc') . " " . strtolower('card number') . ".";
  485. $_SESSION['wpsc_checkout_saved_values']['card_number'] = '';
  486. }
  487. }
  488. if(isset($_POST['expiry'])){
  489. if(($_POST['expiry']['month'] != '') && ($_POST['expiry']['month'] != '') && is_numeric($_POST['expiry']['month']) && is_numeric($_POST['expiry']['year'])){
  490. $_SESSION['wpsc_gateway_error_messages']['expdate'] = '';
  491. }else{
  492. $any_bad_inputs = true;
  493. $bad_input = true;
  494. $_SESSION['wpsc_gateway_error_messages']['expdate'] = __('Please enter a valid', 'wpsc') . " " . strtolower('Expiry Date') . ".";
  495. $_SESSION['wpsc_checkout_saved_values']['expdate'] = '';
  496. }
  497. }
  498. if(isset($_POST['card_code'])){
  499. if(($_POST['card_code'] == '') || (!is_numeric($_POST['card_code']))){
  500. $any_bad_inputs = true;
  501. $bad_input = true;
  502. $_SESSION['wpsc_gateway_error_messages']['card_code'] = __('Please enter a valid', 'wpsc') . " " . strtolower('CVV') . ".";
  503. $_SESSION['wpsc_checkout_saved_values']['card_code'] = '';
  504. }else{
  505. $_SESSION['wpsc_gateway_error_messages']['card_code'] = '';
  506. }
  507. }
  508. if(isset($_POST['cctype'])){
  509. if($_POST['cctype'] == ''){
  510. $any_bad_inputs = true;
  511. $bad_input = true;
  512. $_SESSION['wpsc_gateway_error_messages']['cctype'] = __('Please enter a valid', 'wpsc') . " " . strtolower('CVV') . ".";
  513. $_SESSION['wpsc_checkout_saved_values']['cctype'] = '';
  514. }else{
  515. $_SESSION['wpsc_gateway_error_messages']['cctype'] = '';
  516. }
  517. }
  518. }//closes main bracket
  519. if(isset($_POST['log']) || isset($_POST['pwd']) || isset($_POST['user_email']) ) {
  520. $results = wpsc_add_new_user($_POST['log'], $_POST['pwd'], $_POST['user_email']);
  521. $_SESSION['wpsc_checkout_user_error_messages'] = array();
  522. if(is_callable(array($results, "get_error_code")) && $results->get_error_code()) {
  523. foreach ( $results->get_error_codes() as $code ) {
  524. foreach ( $results->get_error_messages($code) as $error ) {
  525. $_SESSION['wpsc_checkout_user_error_messages'][] = $error;
  526. }
  527. $any_bad_inputs = true;
  528. }
  529. }
  530. //exit('<pre>'.print_r($results, true).'</pre>');
  531. if($results->ID > 0) {
  532. $our_user_id = $results->ID;
  533. } else {
  534. $any_bad_inputs = true;
  535. }
  536. }
  537. if($our_user_id < 1) {
  538. $our_user_id = $user_ID;
  539. }
  540. // check we have a user id
  541. if( $our_user_id > 0 ){
  542. $user_ID = $our_user_id;
  543. }
  544. //exit('<pre>'.print_r($_POST['collected_data'],true).'</pre>');
  545. //Basic Form field validation for billing and shipping details
  546. foreach($this->checkout_items as $form_data) {
  547. $value = $_POST['collected_data'][$form_data->id];
  548. $value_id = (int)$value_id;
  549. $_SESSION['wpsc_checkout_saved_values'][$form_data->id] = $value;
  550. $bad_input = false;
  551. if(($form_data->mandatory == 1) || ($form_data->type == "coupon")) {
  552. switch($form_data->type) {
  553. case "email":
  554. if(!preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,5}$/",$value)) {
  555. $any_bad_inputs = true;
  556. $bad_input = true;
  557. }
  558. break;
  559. case "delivery_country":
  560. case "country":
  561. case "heading":
  562. break;
  563. case "select":
  564. case 'checkbox':
  565. if(is_array($value)){
  566. $select_bad_input = 0;
  567. foreach($value as $v){
  568. if($v == '-1'){
  569. $select_bad_input++;
  570. }else{
  571. $valid_select_input++;
  572. }
  573. }
  574. if(count($value) == $select_bad_input){
  575. $any_bad_inputs = true;
  576. $bad_input = true;
  577. }
  578. }else{
  579. if($value == '-1'){
  580. $any_bad_inputs = true;
  581. $bad_input = true;
  582. }
  583. }
  584. break;
  585. default:
  586. if(is_array($value)){
  587. $select_bad_input = 0;
  588. foreach($value as $v){
  589. if($v == ''){
  590. $select_bad_input++;
  591. }else{
  592. $valid_select_input++;
  593. }
  594. }
  595. if(count($value) == $select_bad_input){
  596. $any_bad_inputs = true;
  597. $bad_input = true;
  598. }
  599. }else{
  600. if($value == ''){
  601. $any_bad_inputs = true;
  602. $bad_input = true;
  603. }
  604. }
  605. break;
  606. }
  607. if($bad_input === true) {
  608. $_SESSION['wpsc_checkout_error_messages'][$form_data->id] = __('Please enter a valid', 'wpsc') . " " . strtolower($form_data->name) . ".";
  609. $_SESSION['wpsc_checkout_saved_values'][$form_data->id] = '';
  610. }
  611. }
  612. }
  613. //exit('UserID >><pre>'.print_r($user_ID, true).'</pre>');
  614. if(($any_bad_inputs == false) && ($user_ID > 0)) {
  615. $saved_data_sql = "SELECT * FROM `".$wpdb->usermeta."` WHERE `user_id` = '".$user_ID."' AND `meta_key` = 'wpshpcrt_usr_profile';";
  616. $saved_data = $wpdb->get_row($saved_data_sql,ARRAY_A);
  617. //echo "<pre>".print_r($meta_data,true)."</pre>";
  618. $new_meta_data = serialize($_POST['collected_data']);
  619. if($saved_data != null) {
  620. $sql ="UPDATE `".$wpdb->usermeta."` SET `meta_value` = '$new_meta_data' WHERE `user_id` IN ('$user_ID') AND `meta_key` IN ('wpshpcrt_usr_profile');";
  621. $wpdb->query($sql);
  622. $changes_saved = true;
  623. //exit($sql);
  624. } else {
  625. $sql = "INSERT INTO `".$wpdb->usermeta."` ( `user_id` , `meta_key` , `meta_value` ) VALUES ( ".$user_ID.", 'wpshpcrt_usr_profile', '$new_meta_data');";
  626. $wpdb->query($sql);
  627. $changes_saved = true;
  628. //exit($sql);
  629. }
  630. }
  631. return array('is_valid' => !$any_bad_inputs, 'error_messages' => $bad_input_message);
  632. }
  633. /**
  634. * validate_forms method, validates the input from the checkout page
  635. * @access public
  636. */
  637. function save_forms_to_db($purchase_id) {
  638. global $wpdb;
  639. $count = $this->get_count_checkout_fields()+1;
  640. // exit($count.'<pre>'.print_r( $_POST['collected_data'], true).'</pre>');
  641. $i = 0;
  642. foreach( $this->checkout_items as $form_data) {
  643. $value = $_POST['collected_data'][$form_data->id];
  644. if($value == ''){
  645. $value = $form_data->value;
  646. }
  647. if($form_data->type != 'heading') {
  648. // echo '<pre>'.print_r($form_data,true).'</pre>';
  649. if(is_array($value) &&($form_data->type == 'country' ||$form_data->type == 'delivery_country') ){
  650. $value = serialize($value);
  651. $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));
  652. }elseif(is_array($value)) {
  653. // echo('<pre>'.print_r($value, true).'</pre>');
  654. foreach((array)$value as $v){
  655. if(is_array($v)){
  656. $options = array();
  657. // exit('<pre>'.print_r($v, true).'</pre>');
  658. foreach($v as $option){
  659. if($option != '-1'){
  660. $options[] = $option;
  661. }
  662. }
  663. $v = maybe_serialize($options);
  664. $v = implode(',', $options);
  665. }
  666. $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, $v));
  667. }
  668. }else{
  669. $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));
  670. }
  671. }
  672. if($i > $count){
  673. break;
  674. }
  675. $i++;
  676. }
  677. }
  678. /**
  679. * Function that checks how many checkout fields are stored in checkout form fields table
  680. */
  681. function get_count_checkout_fields(){
  682. global $wpdb;
  683. $sql = "SELECT COUNT(*) FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type` !='heading' AND `active`='1'";
  684. $count = $wpdb->get_var($sql);
  685. return $count;
  686. }
  687. /**
  688. * checkout loop methods
  689. */
  690. function next_checkout_item() {
  691. $this->current_checkout_item++;
  692. $this->checkout_item = $this->checkout_items[$this->current_checkout_item];
  693. return $this->checkout_item;
  694. }
  695. function the_checkout_item() {
  696. $this->in_the_loop = true;
  697. $this->checkout_item = $this->next_checkout_item();
  698. if ( $this->current_checkout_item == 0 ) // loop has just started
  699. do_action('wpsc_checkout_loop_start');
  700. }
  701. function have_checkout_items() {
  702. if ($this->current_checkout_item + 1 < $this->checkout_item_count) {
  703. return true;
  704. } else if ($this->current_checkout_item + 1 == $this->checkout_item_count && $this->checkout_item_count > 0) {
  705. do_action('wpsc_checkout_loop_end');
  706. // Do some cleaning up after the loop,
  707. $this->rewind_checkout_items();
  708. }
  709. $this->in_the_loop = false;
  710. return false;
  711. }
  712. function rewind_checkout_items() {
  713. $_SESSION['wpsc_checkout_error_messages'] = array();
  714. $this->current_checkout_item = -1;
  715. if ($this->checkout_item_count > 0) {
  716. $this->checkout_item = $this->checkout_items[0];
  717. }
  718. }
  719. }
  720. /**
  721. * The WPSC Gateway functions
  722. */
  723. function wpsc_gateway_count() {
  724. global $wpsc_gateway;
  725. return $wpsc_gateway->gateway_count;
  726. }
  727. function wpsc_have_gateways() {
  728. global $wpsc_gateway;
  729. return $wpsc_gateway->have_gateways();
  730. }
  731. function wpsc_the_gateway() {
  732. global $wpsc_gateway;
  733. return $wpsc_gateway->the_gateway();
  734. }
  735. function wpsc_gateway_name() {
  736. global $wpsc_gateway;
  737. $payment_gateway_names = get_option('payment_gateway_names');
  738. if($payment_gateway_names[$wpsc_gateway->gateway['internalname']] != '') {
  739. $display_name = $payment_gateway_names[$wpsc_gateway->gateway['internalname']];
  740. } else {
  741. switch($selected_gateway_data['payment_type']) {
  742. case "paypal";
  743. $display_name = "PayPal";
  744. break;
  745. case "manual_payment":
  746. $display_name = "Manual Payment";
  747. break;
  748. case "google_checkout":
  749. $display_name = "Google Checkout";
  750. break;
  751. case "credit_card":
  752. default:
  753. $display_name = "Credit Card";
  754. break;
  755. }
  756. }
  757. return $display_name;
  758. }
  759. function wpsc_gateway_internal_name() {
  760. global $wpsc_gateway;
  761. return $wpsc_gateway->gateway['internalname'];
  762. }
  763. function wpsc_gateway_is_checked() {
  764. global $wpsc_gateway;
  765. $is_checked = false;
  766. if(isset($_SESSION['wpsc_previous_selected_gateway'])) {
  767. if($wpsc_gateway->gateway['internalname'] == $_SESSION['wpsc_previous_selected_gateway']) {
  768. $is_checked = true;
  769. }
  770. } else {
  771. if($wpsc_gateway->current_gateway == 0 || ($wpsc_gateway->gateway['internalname'] == 'paypal_certified')) {
  772. $is_checked = true;
  773. }
  774. }
  775. if($is_checked == true) {
  776. $output = 'checked="checked"';
  777. } else {
  778. $output = '';
  779. }
  780. return $output;
  781. }
  782. function wpsc_gateway_cc_check(){
  783. }
  784. function wpsc_gateway_form_fields() {
  785. global $wpsc_gateway, $gateway_checkout_form_fields;
  786. //sprintf on paypalpro module
  787. if($wpsc_gateway->gateway['internalname'] == 'paypal_pro'){
  788. $output = sprintf($gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']] ,wpsc_the_checkout_CC_validation_class(), $_SESSION['wpsc_gateway_error_messages']['card_number'],
  789. wpsc_the_checkout_CCexpiry_validation_class(), $_SESSION['wpsc_gateway_error_messages']['expdate'],
  790. wpsc_the_checkout_CCcvv_validation_class(), $_SESSION['wpsc_gateway_error_messages']['card_code'],
  791. wpsc_the_checkout_CCtype_validation_class(), $_SESSION['wpsc_gateway_error_messages']['cctype']
  792. );
  793. return $output;
  794. }
  795. if($wpsc_gateway->gateway['internalname'] == 'authorize' || $wpsc_gateway->gateway['internalname'] == 'paypal_payflow'){
  796. $output = sprintf($gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']] ,wpsc_the_checkout_CC_validation_class(), $_SESSION['wpsc_gateway_error_messages']['card_number'],
  797. wpsc_the_checkout_CCexpiry_validation_class(), $_SESSION['wpsc_gateway_error_messages']['expdate'],
  798. wpsc_the_checkout_CCcvv_validation_class(), $_SESSION['wpsc_gateway_error_messages']['card_code']
  799. );
  800. return $output;
  801. }
  802. if($wpsc_gateway->gateway['internalname'] == 'eway' || $wpsc_gateway->gateway['internalname'] == 'bluepay' ){
  803. $output = sprintf($gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']] ,wpsc_the_checkout_CC_validation_class(), $_SESSION['wpsc_gateway_error_messages']['card_number'],
  804. wpsc_the_checkout_CCexpiry_validation_class(), $_SESSION['wpsc_gateway_error_messages']['expdate']
  805. );
  806. return $output;
  807. }
  808. if($wpsc_gateway->gateway['internalname'] == 'linkpoint'){
  809. $output = sprintf($gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']] ,wpsc_the_checkout_CC_validation_class(), $_SESSION['wpsc_gateway_error_messages']['card_number'],
  810. wpsc_the_checkout_CCexpiry_validation_class(), $_SESSION['wpsc_gateway_error_messages']['expdate']
  811. );
  812. return $output;
  813. }
  814. //$output = sprintf($gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']] , $size['width'], $size['height']);
  815. return $gateway_checkout_form_fields[$wpsc_gateway->gateway['internalname']];
  816. }
  817. function wpsc_gateway_form_field_style() {
  818. global $wpsc_gateway;
  819. $is_checked = false;
  820. if(isset($_SESSION['wpsc_previous_selected_gateway'])) {
  821. if($wpsc_gateway->gateway['internalname'] == $_SESSION['wpsc_previous_selected_gateway']) {
  822. $is_checked = true;
  823. }
  824. } else {
  825. if($wpsc_gateway->current_gateway == 0) {
  826. $is_checked = true;
  827. }
  828. }
  829. if($is_checked == true) {
  830. $output = 'checkout_forms';
  831. } else {
  832. $output = 'checkout_forms_hidden';
  833. }
  834. return $output;
  835. }
  836. /**
  837. * The WPSC Gateway class
  838. */
  839. class wpsc_gateways {
  840. var $wpsc_gateways;
  841. var $gateway;
  842. var $gateway_count = 0;
  843. var $current_gateway = -1;
  844. var $in_the_loop = false;
  845. function wpsc_gateways() {
  846. global $nzshpcrt_gateways;
  847. $gateway_options = get_option('custom_gateway_options');
  848. foreach($nzshpcrt_gateways as $gateway) {
  849. if(array_search($gateway['internalname'], (array)$gateway_options) !== false) {
  850. $this->wpsc_gateways[] = $gateway;
  851. }
  852. }
  853. $this->gateway_count = count($this->wpsc_gateways);
  854. }
  855. /**
  856. * checkout loop methods
  857. */
  858. function next_gateway() {
  859. $this->current_gateway++;
  860. $this->gateway = $this->wpsc_gateways[$this->current_gateway];
  861. return $this->gateway;
  862. }
  863. function the_gateway() {
  864. $this->in_the_loop = true;
  865. $this->gateway = $this->next_gateway();
  866. if ( $this->current_gateway == 0 ) // loop has just started
  867. do_action('wpsc_checkout_loop_start');
  868. }
  869. function have_gateways() {
  870. if ($this->current_gateway + 1 < $this->gateway_count) {
  871. return true;
  872. } else if ($this->current_gateway + 1 == $this->gateway_count && $this->gateway_count > 0) {
  873. do_action('wpsc_checkout_loop_end');
  874. // Do some cleaning up after the loop,
  875. $this->rewind_gateways();
  876. }
  877. $this->in_the_loop = false;
  878. return false;
  879. }
  880. function rewind_gateways() {
  881. $this->current_gateway = -1;
  882. if ($this->gateway_count > 0) {
  883. $this->gateway = $this->wpsc_gateways[0];
  884. }
  885. }
  886. }
  887. ?>