PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/3.7.1/transaction_result_functions.php

https://github.com/evadne/wp-e-commerce
PHP | 285 lines | 219 code | 42 blank | 24 comment | 77 complexity | c9858a8af5ab1abda2f23f6f75a04bdf MD5 | raw file
  1. <?php
  2. function transaction_results($sessionid, $echo_to_screen = true, $transaction_id = null) {
  3. global $wpdb,$wpsc_cart;
  4. //$curgateway = get_option('payment_gateway');
  5. $curgateway = $wpdb->get_var("SELECT gateway FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE sessionid='$sessionid'");
  6. $errorcode = 0;
  7. $order_status= 2;
  8. $siteurl = get_option('siteurl');
  9. /*
  10. * {Notes} Double check that $Echo_To_Screen is a boolean value
  11. */
  12. $echo_to_screen=(((!is_bool($echo_to_screen)))?((true)):(($echo_to_screen)));
  13. if(is_numeric($sessionid)) {
  14. $purchase_log = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid`= ".$sessionid." LIMIT 1",ARRAY_A) ;
  15. if(($purchase_log['gateway'] == "testmode") && ($purchase_log['processed'] < 2)) {
  16. $message = get_option('wpsc_email_receipt');
  17. $message_html = $message;
  18. } else {
  19. $message = get_option('wpsc_email_receipt');
  20. $message_html = $message;
  21. }
  22. $order_url = $siteurl."/wp-admin/admin.php?page=".WPSC_DIR_NAME."/display-log.php&amp;purchcaseid=".$purchase_log['id'];
  23. if(($_GET['ipn_request'] != 'true') and (get_option('paypal_ipn') == 1)) {
  24. if($purchase_log == null) {
  25. echo TXT_WPSC_ORDER_FAILED;
  26. if((get_option('purch_log_email') != null) && ($purchase_log['email_sent'] != 1)) {
  27. wp_mail(get_option('purch_log_email'), TXT_WPSC_NEW_ORDER_PENDING_SUBJECT, TXT_WPSC_NEW_ORDER_PENDING_BODY.$order_url, "From: ".get_option('return_email')."");
  28. }
  29. return false;
  30. } else if ($purchase_log['processed'] < 2) { //added by Thomas on 20/6/2007
  31. echo TXT_WPSC_ORDER_PENDING . "<p style='margin: 1em 0px 0px 0px;' >".nl2br(get_option('payment_instructions'))."</p>";
  32. /*if($purchase_log['gateway'] != 'testmode') {
  33. if((get_option('purch_log_email') != null) && ($purchase_log['email_sent'] != 1)) {
  34. mail(get_option('purch_log_email'), TXT_WPSC_NEW_ORDER_PENDING_SUBJECT, TXT_WPSC_NEW_ORDER_PENDING_BODY.$order_url, "From: ".get_option('return_email')."");
  35. }
  36. return false;
  37. }*/
  38. }
  39. }
  40. $cart = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE `purchaseid`='{$purchase_log['id']}'",ARRAY_A);
  41. if($purchase_log['shipping_country'] != '') {
  42. $billing_country = $purchase_log['billing_country'];
  43. $shipping_country = $purchase_log['shipping_country'];
  44. } else {
  45. $country = $wpdb->get_var("SELECT `value` FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id`=".$purchase_log['id']." AND `form_id` = '".get_option('country_form_field')."' LIMIT 1");
  46. $billing_country = $country;
  47. $shipping_country = $country;
  48. }
  49. $email_form_field = $wpdb->get_results("SELECT `id`,`type` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type` IN ('email') AND `active` = '1' ORDER BY `order` ASC LIMIT 1",ARRAY_A);
  50. $email = $wpdb->get_var("SELECT `value` FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id`=".$purchase_log['id']." AND `form_id` = '".$email_form_field[0]['id']."' LIMIT 1");
  51. $stock_adjusted = false;
  52. $previous_download_ids = array(0);
  53. $product_list='';
  54. if(($cart != null) && ($errorcode == 0)) {
  55. foreach($cart as $row) {
  56. $link = "";
  57. $product_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$row['prodid']}' LIMIT 1", ARRAY_A) ;
  58. if($purchase_log['email_sent'] != 1) {
  59. $wpdb->query("UPDATE `".WPSC_TABLE_DOWNLOAD_STATUS."` SET `active`='1' WHERE (`fileid` = '{$product_data['file']}' OR `cartid` = '{$row['id']}' ) AND `purchid` = '{$purchase_log['id']}'");
  60. }
  61. do_action('wpsc_transaction_result_cart_item', array("purchase_id" =>$purchase_log['id'], "cart_item"=>$row, "purchase_log"=>$purchase_log));
  62. if (($purchase_log['processed'] >= 2)) {
  63. //echo "SELECT * FROM `".WPSC_TABLE_DOWNLOAD_STATUS."` WHERE `active`='1' AND `purchid`='".$purchase_log['id']."' AND (`cartid` = '".$row['id']."' OR (`cartid` IS NULL AND `fileid` = '{$product_data['file']}') ) AND `id` NOT IN ('".implode("','",$previous_download_ids)."') LIMIT 1";
  64. $download_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_DOWNLOAD_STATUS."` WHERE `active`='1' AND `purchid`='".$purchase_log['id']."' AND (`cartid` = '".$row['id']."' OR (`cartid` IS NULL AND `fileid` = '{$product_data['file']}') ) AND `id` NOT IN ('".implode("','",$previous_download_ids)."') LIMIT 1",ARRAY_A);
  65. //exit('IM HERE'.$errorcode.'<pre>'.print_r($download_data).'</pre>');
  66. if($download_data != null) {
  67. if($download_data['uniqueid'] == null) { // if the uniqueid is not equal to null, its "valid", regardless of what it is
  68. $link = $siteurl."?downloadid=".$download_data['id'];
  69. } else {
  70. $link = $siteurl."?downloadid=".$download_data['uniqueid'];
  71. }
  72. }
  73. $previous_download_ids[] = $download_data['id'];
  74. $order_status= 4;
  75. }
  76. do_action('wpsc_confirm_checkout', $purchase_log['id']);
  77. $shipping = $row['pnp']*$row['quantity'];
  78. $total_shipping += $shipping;
  79. if($product_data['special']==1) {
  80. $price_modifier = $product_data['special_price'];
  81. } else {
  82. $price_modifier = 0;
  83. }
  84. $total+=($row['price']*$row['quantity']);
  85. $message_price = nzshpcrt_currency_display(($row['price']*$row['quantity']), $product_data['notax'], true);
  86. $shipping_price = nzshpcrt_currency_display($shipping, 1, true);
  87. $variation_values = $wpdb->get_col("SELECT `value_id` FROM `".WPSC_TABLE_CART_ITEM_VARIATIONS."` WHERE `cart_id`='{$row['id']}'");
  88. //echo "<pre>".print_r($product_data,true)."</pre>";
  89. $variation_count = count($variation_values);
  90. if($purchase['gateway'] != 'testmode') {
  91. if($gateway['internalname'] == $purch_data[0]['gateway'] ) {
  92. $gateway_name = $gateway['name'];
  93. }
  94. } else {
  95. $gateway_name = "Manual Payment";
  96. }
  97. //echo "<pre>".print_r($variation_values,true)."</pre>";
  98. $variation_list = '';
  99. if($variation_count > 0) {
  100. $value_names = $wpdb->get_col("SELECT `name` FROM `".WPSC_TABLE_VARIATION_VALUES."` WHERE `id` IN ('".implode("','",$variation_values)."')");
  101. $variation_list = " (".stripslashes(implode(", ",$value_names)).")";
  102. }
  103. if($link != '') {
  104. $additional_content = apply_filters('wpsc_transaction_result_content', array("purchase_id" =>$purchase_log['id'], "cart_item"=>$row, "purchase_log"=>$purchase_log));
  105. if(!is_string($additional_content)) {
  106. $additional_content = '';
  107. }
  108. $product_list .= " - ". $product_data['name'] . stripslashes($variation_list) ." ".$message_price ." ".TXT_WPSC_CLICKTODOWNLOAD.":\n $link\n".$additional_content;
  109. $product_list_html .= " - ". $product_data['name'] . stripslashes($variation_list) ." ".$message_price ."&nbsp;&nbsp;<a href='$link'>".TXT_WPSC_CLICKTODOWNLOAD."</a>\n". $additional_content;
  110. } else {
  111. $plural = '';
  112. if($row['quantity'] > 1) {
  113. $plural = "s";
  114. }
  115. $product_list.= " - ".$row['quantity']." ". $product_data['name'].stripslashes($variation_list )." ". $message_price ."\n - ". TXT_WPSC_SHIPPING.":".$shipping_price ."\n\r";
  116. $product_list_html.= " - ".$row['quantity']." ". $product_data['name'].stripslashes($variation_list )." ". $message_price ."\n &nbsp; ". TXT_WPSC_SHIPPING.":".$shipping_price ."\n\r";
  117. }
  118. $report = get_option('wpsc_email_admin');
  119. $report_product_list.= " - ". $product_data['name'] .stripslashes($variation_list)." ".$message_price ."\n";
  120. }
  121. // Decrement the stock here
  122. if (($purchase_log['processed'] >= 2)) {
  123. wpsc_decrement_claimed_stock($purchase_log['id']);
  124. }
  125. if($purchase_log['discount_data'] != '') {
  126. $coupon_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_COUPON_CODES."` WHERE coupon_code='".$wpdb->escape($purchase_log['discount_data'])."' LIMIT 1",ARRAY_A);
  127. if($coupon_data['use-once'] == 1) {
  128. $wpdb->query("UPDATE `".WPSC_TABLE_COUPON_CODES."` SET `active`='0', `is-used`='1' WHERE `id`='".$coupon_data['id']."' LIMIT 1");
  129. }
  130. }
  131. //$wpdb->query("UPDATE `".WPSC_TABLE_DOWNLOAD_STATUS."` SET `active`='1' WHERE `fileid`='".$product_data['file']."' AND `purchid` = '".$purchase_log['id']."' LIMIT 1");
  132. //if (!isset($_SESSION['quote_shipping']))
  133. //$total_shipping = nzshpcrt_determine_base_shipping($total_shipping, $shipping_country);
  134. $total_shipping += $purchase_log['base_shipping'];
  135. $total = $purchase_log['totalprice'];
  136. // echo $total;
  137. // $message.= "\n\r";
  138. $product_list.= "Your Purchase No.: ".$purchase_log['id']."\n\r";
  139. if($purchase_log['discount_value'] > 0) {
  140. $discount_email.= TXT_WPSC_DISCOUNT."\n\r: ";
  141. $discount_email .=$purchase_log['discount_data'].' : '.nzshpcrt_currency_display($purchase_log['discount_value'], 1, true)."\n\r";
  142. }
  143. $total_shipping_email.= TXT_WPSC_TOTALSHIPPING.": ".nzshpcrt_currency_display($total_shipping,1,true)."\n\r";
  144. $total_price_email.= TXT_WPSC_TOTAL.": ".nzshpcrt_currency_display($total,1,true)."\n\r";
  145. $product_list_html.= "Your Purchase No.: ".$purchase_log['id']."\n\n\r";
  146. if($purchase_log['discount_value'] > 0) {
  147. $report.= $discount_email."\n\r";
  148. $total_shipping_html.= TXT_WPSC_DISCOUNT.": ".nzshpcrt_currency_display($purchase_log['discount_value'], 1, true)."\n\r";
  149. }
  150. $total_shipping_html.= TXT_WPSC_TOTALSHIPPING.": ".nzshpcrt_currency_display($total_shipping,1,true)."\n";
  151. $total_price_html.= TXT_WPSC_TOTAL.": ".nzshpcrt_currency_display($total, 1,true)."\n";
  152. if(isset($_GET['ti'])) {
  153. $message.= "\n\r".TXT_WPSC_YOURTRANSACTIONID.": " . $_GET['ti'];
  154. $message_html.= "\n\r".TXT_WPSC_YOURTRANSACTIONID.": " . $_GET['ti'];
  155. $report.= "\n\r".TXT_WPSC_TRANSACTIONID.": " . $_GET['ti'];
  156. } else {
  157. $report_id = "Purchase No.: ".$purchase_log['id']."\n\r";
  158. }
  159. //echo "<pre>".print_r($purchase_log,true)."</pre>";
  160. $message = str_replace('%product_list%',$product_list,$message);
  161. $message = str_replace('%total_shipping%',$total_shipping_email,$message);
  162. $message = str_replace('%total_price%',$total_price_email,$message);
  163. //$message = str_replace('%order_status%',get_option('blogname'),$message);
  164. $message = str_replace('%shop_name%',get_option('blogname'),$message);
  165. $report = str_replace('%product_list%',$report_product_list,$report);
  166. $report = str_replace('%total_shipping%',$total_shipping_email,$report);
  167. $report = str_replace('%total_price%',$total_price_email,$report);
  168. $report = str_replace('%shop_name%',get_option('blogname'),$report);
  169. $message_html = str_replace('%product_list%',$product_list_html,$message_html);
  170. $message_html = str_replace('%total_shipping%',$total_shipping_html,$message_html);
  171. $message_html = str_replace('%total_price%',$total_price_email,$message_html);
  172. $message_html = str_replace('%shop_name%',get_option('blogname'),$message_html);
  173. //$message_html = str_replace('%order_status%',get_option('blogname'),$message_html);
  174. if(($email != '') && ($purchase_log['email_sent'] != 1)) {
  175. if($purchase_log['processed'] < 2) {
  176. $payment_instructions = strip_tags(get_option('payment_instructions'));
  177. $message = TXT_WPSC_ORDER_PENDING . "\n\r" . $payment_instructions ."\n\r". $message;
  178. wp_mail($email, TXT_WPSC_ORDER_PENDING_PAYMENT_REQUIRED, $message, "From: ".get_option('return_email')." <".get_option('return_email').">");
  179. } else {
  180. wp_mail($email, TXT_WPSC_PURCHASERECEIPT, $message, "From: ".get_option('return_email')." <".get_option('return_email').">");
  181. }
  182. }
  183. $report_user = TXT_WPSC_CUSTOMERDETAILS."\n\r";
  184. $form_sql = "SELECT * FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` WHERE `log_id` = '".$purchase_log['id']."'";
  185. $form_data = $wpdb->get_results($form_sql,ARRAY_A);
  186. if($form_data != null) {
  187. foreach($form_data as $form_field) {
  188. $form_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `id` = '".$form_field['form_id']."' LIMIT 1",ARRAY_A);
  189. switch($form_data['type']) {
  190. case "country":
  191. $report_user .= $form_data['name'].": ".wpsc_get_country($form_field['value'])."\n";
  192. $report_user .= TXT_WPSC_STATE.": ".wpsc_get_region($purchase_log['billing_region'])."\n";
  193. break;
  194. case "delivery_country":
  195. $report_user .= $form_data['name'].": ".wpsc_get_country($form_field['value'])."\n";
  196. $report_user .= TXT_WPSC_DELIVERY_STATE.": ".wpsc_get_region($purchase_log['shipping_region'])."\n";
  197. break;
  198. default:
  199. $report_user .= $form_data['name'].": ".$form_field['value']."\n";
  200. break;
  201. }
  202. }
  203. }
  204. $report_user .= "\n\r";
  205. $report = $report_user. $report_id . $report;
  206. if($stock_adjusted == true) {
  207. $wpdb->query("UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `stock_adjusted` = '1' WHERE `sessionid` = ".$sessionid." LIMIT 1") ;
  208. }
  209. if((get_option('purch_log_email') != null) && ($purchase_log['email_sent'] != 1)) {
  210. wp_mail(get_option('purch_log_email'), TXT_WPSC_PURCHASEREPORT, $report, "From: ".get_option('return_email')." <".get_option('return_email').">");
  211. }
  212. if($purchase_log['processed'] < 2) {
  213. echo "<br />" . nl2br(str_replace("$",'\$',$message_html));
  214. return;
  215. }
  216. /// Empty the cart
  217. $wpsc_cart->submit_stock_claims($purchase_log['id']);
  218. $wpsc_cart->empty_cart();
  219. if(true === $echo_to_screen) {
  220. echo '<div class="wrap">';
  221. if($sessionid != null) {
  222. echo TXT_WPSC_THETRANSACTIONWASSUCCESSFUL."<br />";
  223. echo "<br />" . nl2br(str_replace("$",'\$',$message_html));
  224. }
  225. echo '</div>';
  226. }
  227. } else {
  228. if(true === $echo_to_screen) {
  229. echo '<div class="wrap">';
  230. echo TXT_WPSC_BUYPRODUCTS;
  231. echo '</div>';
  232. }
  233. }
  234. if(($purchase_log['email_sent'] != 1) and ($sessionid != '')) {
  235. if(preg_match("/^[\w\s._,-]+$/",$transaction_id)) {
  236. $transact_id_sql = "`transactid` = '".$transaction_id."',";
  237. }
  238. $update_sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET $transact_id_sql `email_sent` = '1', `processed` = '$order_status' WHERE `sessionid` = ".$sessionid." LIMIT 1";
  239. $wpdb->query($update_sql) ;
  240. }
  241. }
  242. }
  243. ?>