PageRenderTime 31ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/merchants/GoogleCheckout-XML.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 630 lines | 530 code | 51 blank | 49 comment | 121 complexity | 385624dd38f04c24b67b03adebba350a MD5 | raw file
  1. <?php
  2. require_once('library/googlecart.php');
  3. require_once('library/googleitem.php');
  4. require_once('library/googleshipping.php');
  5. require_once('library/googletax.php');
  6. require_once('library/googleresponse.php');
  7. require_once('library/googlemerchantcalculations.php');
  8. require_once('library/googleresult.php');
  9. require_once('library/googlerequest.php');
  10. $nzshpcrt_gateways[$num]['name'] = 'Google Checkout';
  11. $nzshpcrt_gateways[$num]['internalname'] = 'google';
  12. $nzshpcrt_gateways[$num]['function'] = 'gateway_google';
  13. $nzshpcrt_gateways[$num]['form'] = "form_google";
  14. $nzshpcrt_gateways[$num]['submit_function'] = "submit_google";
  15. $nzshpcrt_gateways[$num]['is_exclusive'] = true;
  16. $nzshpcrt_gateways[$num]['payment_type'] = "google_checkout";
  17. function gateway_google($fromcheckout = false){
  18. global $wpdb, $wpsc_cart, $wpsc_checkout,$current_user, $purchlogs;
  19. //exit('<pre>'.print_r($fromcheckout, true).'</pre>');
  20. if(!isset($wpsc_checkout)){
  21. $wpsc_checkout = new wpsc_checkout();
  22. }
  23. if(!isset($_SESSION['wpsc_sessionid'])){
  24. $sessionid = (mt_rand(100,999).time());
  25. $_SESSION['wpsc_sessionid'] = $sessionid;
  26. }
  27. //exit('<pre>'.print_r($_SESSION,true).'</pre>');
  28. if($_SESSION['wpsc_delivery_region'] == null && $_SESSION['wpsc_selected_region'] == null){
  29. $_SESSION['wpsc_delivery_region'] = get_option('base_region');
  30. $_SESSION['wpsc_selected_region'] = get_option('base_region');
  31. }
  32. $wpsc_cart->get_shipping_option();
  33. $wpsc_cart->get_shipping_quotes();
  34. $wpsc_cart->get_shipping_method();
  35. $wpsc_cart->google_shipping_quotes();
  36. $subtotal = $wpsc_cart->calculate_subtotal();
  37. $base_shipping = $wpsc_cart->calculate_total_shipping();
  38. $tax = $wpsc_cart->calculate_total_tax();
  39. $total = $wpsc_cart->calculate_total_price();
  40. // exit('<pre>'.print_r($wpsc_cart, true).'</pre>');
  41. if($total > 0 ){
  42. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `totalprice` = ".$total.", `statusno` = '0',`user_ID`=".(int)$user_ID.", `date`= UNIX_TIMESTAMP() , `gateway`='google', `billing_country`='".$wpsc_cart->delivery_country."', shipping_country='".$wpsc_cart->selected_country."', `base_shipping`= '".$base_shipping."', shipping_method = '".$wpsc_cart->selected_shipping_method."', shipping_option= '".$wpsc_cart->selected_shipping_option."', `plugin_version`= '".WPSC_VERSION."' , `discount_value` = '".$wpsc_cart->coupons_amount."', `discount_data`='".$wpsc_cart->coupons_name."' WHERE `sessionid`=".$_SESSION['wpsc_sessionid']."";
  43. // exit($sql);
  44. $update = $wpdb->query($sql);
  45. $sql = "SELECT `id` FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE sessionid=".$_SESSION['wpsc_sessionid'];
  46. $purchase_log_id = $wpdb->get_var($sql);
  47. $sql = "DELETE FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE purchaseid = ".$purchase_log_id;
  48. $wpdb->query($sql);
  49. $wpsc_cart->save_to_db($purchase_log_id);
  50. if(! $update){
  51. $sql = "INSERT INTO `".WPSC_TABLE_PURCHASE_LOGS."` (`totalprice`,`statusno`, `sessionid`, `user_ID`, `date`, `gateway`, `billing_country`,`shipping_country`, `base_shipping`,`shipping_method`, `shipping_option`, `plugin_version`, `discount_value`, `discount_data`) VALUES ('$total' ,'0', '".$_SESSION['wpsc_sessionid']."', '".(int)$user_ID."', UNIX_TIMESTAMP(), 'google', '{$wpsc_cart->delivery_country}', '{$wpsc_cart->selected_country}', '{$base_shipping}', '".$wpsc_cart->selected_shipping_method."', '".$wpsc_cart->selected_shipping_option."', '".WPSC_VERSION."', '{$wpsc_cart->coupons_amount}','{$wpsc_cart->coupons_name}')";
  52. $wpdb->query($sql);
  53. $sql = "SELECT `id` FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE sessionid=".$_SESSION['wpsc_sessionid'];
  54. $purchase_log_id = $wpdb->get_var($sql);
  55. $wpsc_cart->save_to_db($purchase_log_id);
  56. }
  57. if(get_option('permalink_structure') != '') {
  58. $seperator = "?";
  59. } else {
  60. $seperator = "&";
  61. }
  62. Usecase($seperator, $_SESSION['wpsc_sessionid'], $fromcheckout);
  63. //exit();
  64. }
  65. }
  66. function Usecase($seperator, $sessionid, $fromcheckout) {
  67. global $wpdb, $wpsc_cart;
  68. $purchase_log_sql = "SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid`= ".$sessionid." LIMIT 1";
  69. $purchase_log = $wpdb->get_results($purchase_log_sql,ARRAY_A) ;
  70. $cart_sql = "SELECT * FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE `purchaseid`='".$purchase_log[0]['id']."'";
  71. $wp_cart = $wpdb->get_results($cart_sql,ARRAY_A) ;
  72. $merchant_id = get_option('google_id');
  73. $merchant_key = get_option('google_key');
  74. $server_type = get_option('google_server_type');
  75. $currency = get_option('google_cur');
  76. $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
  77. $transact_url = get_option('transact_url');
  78. $returnURL = $transact_url.$seperator."sessionid=".$sessionid."&gateway=google";
  79. $cart->SetContinueShoppingUrl($returnURL);
  80. $cart->SetEditCartUrl(get_option('shopping_cart_url'));
  81. $no=1;
  82. //exit("<pre>".print_r($wpsc_cart,true)."</pre>");
  83. //new item code
  84. $no = 0;
  85. // $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
  86. // foreach($wpsc_cart->cart_items as $item){
  87. //google prohibited items not implemented
  88. $curr=new CURRENCYCONVERTER();
  89. $currency_code = $wpdb->get_results("SELECT `code` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id`='".get_option('currency_type')."' LIMIT 1",ARRAY_A);
  90. $local_currency_code = $currency_code[0]['code'];
  91. // exit('<pre>'.print_r($_REQUEST,true).'</pre>');
  92. $google_curr = get_option('google_cur');
  93. while (wpsc_have_cart_items()) {
  94. wpsc_the_cart_item();
  95. if($google_curr != $local_currency_code) {
  96. $google_currency_productprice = $curr->convert( wpsc_cart_item_price(false)/wpsc_cart_item_quantity(),$google_curr,$local_currency_code);
  97. $google_currency_shipping = $curr->convert( $wpsc_cart->selected_shipping_amount,$google_curr,$local_currency_code);
  98. } else {
  99. $google_currency_productprice = wpsc_cart_item_price(false)/wpsc_cart_item_quantity();
  100. $google_currency_shipping = $wpsc_cart->selected_shipping_amount;
  101. }
  102. // exit('<pre>'.print_r(wpsc_cart_item_name(),true).'</pre>');
  103. $cartitem["$no"] = new GoogleItem(wpsc_cart_item_name(), // Item name
  104. '', // Item description
  105. wpsc_cart_item_quantity(), // Quantity
  106. ($google_currency_productprice)); // Unit price
  107. $cart->AddItem($cartitem["$no"]);
  108. $no++;
  109. }
  110. //If there are coupons applied add coupon as a product with negative price
  111. if($wpsc_cart->coupons_amount > 0){
  112. if($google_curr != $local_currency_code) {
  113. $google_currency_productprice = $curr->convert( $wpsc_cart->coupons_amount,$google_curr,$local_currency_code);
  114. } else {
  115. $google_currency_productprice = $wpsc_cart->coupons_amount;
  116. }
  117. $cartitem[$no] = new GoogleItem('Discount', // Item name
  118. 'Discount Price', // Item description
  119. 1, // Quantity
  120. ('-'.$google_currency_productprice)); // Unit price
  121. $cart->AddItem($cartitem[$no]);
  122. }
  123. // }
  124. // Add shipping options
  125. if(wpsc_uses_shipping() && $google_currency_shipping >0 ){
  126. $Gfilter = new GoogleShippingFilters();
  127. $google_checkout_shipping=get_option("google_shipping_country");
  128. $googleshippingcountries = count($google_checkout_shipping);
  129. //exit('<pre>'.print_r($googleshipping, true).'</pre>');
  130. if($googleshippingcountries == 242){
  131. $Gfilter->SetAllowedWorldArea(true);
  132. }else{
  133. if(is_array($google_checkout_shipping)){
  134. $google_shipping_country_ids = implode(",",$google_checkout_shipping);
  135. }
  136. $google_shipping_country = $wpdb->get_col("SELECT `isocode` FROM ".WPSC_TABLE_CURRENCY_LIST." WHERE id IN (".$google_shipping_country_ids.")");
  137. foreach($google_shipping_country as $isocode){
  138. //exit($isocode);
  139. $Gfilter->AddAllowedPostalArea($isocode);
  140. if($isocode == 'US'){
  141. $Gfilter->SetAllowedCountryArea('ALL');
  142. }
  143. }
  144. }
  145. $Gfilter->SetAllowUsPoBox(false);
  146. $ship_1 = new GoogleFlatRateShipping('Flat Rate Shipping', $google_currency_shipping);
  147. $ship_1->AddShippingRestrictions($Gfilter);
  148. $cart->AddShipping($ship_1);
  149. }
  150. //wpsc_google_shipping_quotes();
  151. // Add tax rules
  152. //if ($_SESSION['wpsc_selected_country']=='US'){
  153. //set default tax
  154. //exit('<pre>'.print_r($_SESSION,true).'</pre>');
  155. $sql = "SELECT `name`, `tax` FROM ".WPSC_TABLE_REGION_TAX." WHERE id='".$_SESSION['wpsc_selected_region']."'";
  156. //exit('<pre>'.print_r($sql, true).'</pre>');
  157. $state_name = $wpdb->get_row($sql, ARRAY_A);
  158. //exit('<pre>'.print_r($state_name, true).'</pre>');
  159. $defaultTax = $state_name['tax']/100;
  160. $tax_rule = new GoogleDefaultTaxRule($defaultTax);
  161. $sql = "SELECT `code` FROM ".WPSC_TABLE_REGION_TAX." WHERE `country_id`='136' AND `tax` = ".$state_name['tax'];
  162. $states = $wpdb->get_col($sql);
  163. //exit('<pre>'.print_r($states, true).'</pre>');
  164. $tax_rule->SetStateAreas((array)$states);
  165. $cart->AddDefaultTaxRules($tax_rule);
  166. //get alternative tax rates
  167. $sql = "SELECT DISTINCT `tax` FROM ".WPSC_TABLE_REGION_TAX." WHERE `tax` != 0 AND `tax` !=".$state_name['tax']." AND `country_id`='136' ORDER BY `tax`";
  168. $othertax = $wpdb->get_col($sql);
  169. $i = 1;
  170. //exit('<pre>'.print_r($othertax, true).'</pre>');
  171. foreach($othertax as $altTax){
  172. $sql = "SELECT `code` FROM ".WPSC_TABLE_REGION_TAX." WHERE `country_id`='136' AND `tax`=".$altTax;
  173. $alt = $wpdb->get_col($sql);
  174. $altTax = $altTax/100;
  175. $alt_google_tax = new GoogleDefaultTaxRule($altTax);
  176. $alt_google_tax->SetStateAreas($alt);
  177. //$g = new GoogleAlternateTaxTable('Alt Tax'.$i);
  178. //$g->AddAlternateTaxRules($alt_google_tax);
  179. $cart->AddDefaultTaxRules($alt_google_tax);
  180. // exit(print_r($alt,true));
  181. $i++;
  182. }
  183. //}
  184. if (get_option('google_button_size') == '0'){
  185. $google_button_size = 'BIG';
  186. } elseif(get_option('google_button_size') == '1') {
  187. $google_button_size = 'MEDIUM';
  188. } elseif(get_option('google_button_size') == '2') {
  189. $google_button_size = 'SMALL';
  190. }
  191. // Display Google Checkout button
  192. //echo '<pre>'.print_r($cart, true).'</pre>';
  193. //unset($_SESSION['wpsc_sessionid']);
  194. //if($fromCheckout){
  195. echo $cart->CheckoutButtonCode($google_button_size);
  196. //}
  197. }
  198. function wpsc_google_checkout_page(){
  199. global $wpsc_gateway;
  200. $script = "<script type='text/javascript'>
  201. jQuery(document).ready(
  202. function()
  203. {
  204. jQuery('div#wpsc_shopping_cart_container h2').hide();
  205. jQuery('div#wpsc_shopping_cart_container .wpsc_cart_shipping').hide();
  206. jQuery('.wpsc_checkout_forms').hide();
  207. });
  208. </script>";
  209. $options = get_option('payment_gateway');
  210. // exit('HELLO<pre>'.print_r(get_option('custom_gateway_options'), true).'</pre>');
  211. //foreach((array)get_option('custom_gateway_options') as $gateway){
  212. if(in_array('google', (array)get_option('custom_gateway_options'))){
  213. $options = 'google';
  214. }
  215. // }
  216. if($options == 'google' && isset($_SESSION['gateway'])){
  217. unset($_SESSION['gateway']);
  218. echo $script;
  219. gateway_google(true);
  220. }
  221. }
  222. add_action('wpsc_before_form_of_shopping_cart', 'wpsc_google_checkout_page');
  223. function submit_google() {
  224. if($_POST['google_id'] != null) {
  225. update_option('google_id', $_POST['google_id']);
  226. }
  227. if($_POST['google_key'] != null) {
  228. update_option('google_key', $_POST['google_key']);
  229. }
  230. if($_POST['google_cur'] != null) {
  231. update_option('google_cur', $_POST['google_cur']);
  232. }
  233. if($_POST['google_button_size'] != null) {
  234. update_option('google_button_size', $_POST['google_button_size']);
  235. }
  236. if($_POST['google_button_bg'] != null) {
  237. update_option('google_button_bg', $_POST['google_button_bg']);
  238. }
  239. if($_POST['google_server_type'] != null) {
  240. update_option('google_server_type', $_POST['google_server_type']);
  241. }
  242. if($_POST['google_auto_charge'] != null) {
  243. update_option('google_auto_charge', $_POST['google_auto_charge']);
  244. }
  245. return true;
  246. }
  247. function form_google()
  248. {
  249. if (get_option('google_button_size') == '0'){
  250. $button_size1="checked='checked'";
  251. } elseif(get_option('google_button_size') == '1') {
  252. $button_size2="checked='checked'";
  253. } elseif(get_option('google_button_size') == '2') {
  254. $button_size3="checked='checked'";
  255. }
  256. if (get_option('google_server_type') == 'sandbox'){
  257. $google_server_type1="checked='checked'";
  258. } elseif(get_option('google_server_type') == 'production') {
  259. $google_server_type2="checked='checked'";
  260. }
  261. if (get_option('google_auto_charge') == '1'){
  262. $google_auto_charge1="checked='checked'";
  263. } elseif(get_option('google_auto_charge') == '0') {
  264. $google_auto_charge2="checked='checked'";
  265. }
  266. if (get_option('google_button_bg') == 'trans'){
  267. $button_bg1="selected='selected'";
  268. } else {
  269. $button_bg2="selected='selected'";
  270. }
  271. $output = "
  272. <tr>
  273. <td>Merchant ID </td>
  274. <td>
  275. <input type='text' size='40' value='".get_option('google_id')."' name='google_id' />
  276. </td>
  277. </tr>
  278. <tr>
  279. <td>Merchant Key
  280. </td>
  281. <td>
  282. <input type='text' size='40' value='".get_option('google_key')."' name='google_key' />
  283. </td>
  284. </tr>
  285. <tr>
  286. <td>
  287. Turn on auto charging
  288. </td>
  289. <td>
  290. <input $google_auto_charge1 type='radio' name='google_auto_charge' value='1' /> Yes
  291. <input $google_auto_charge2 type='radio' name='google_auto_charge' value='0' /> No
  292. </td>
  293. </tr>
  294. <tr>
  295. <td>Server Type
  296. </td>
  297. <td>
  298. <input $google_server_type1 type='radio' name='google_server_type' value='sandbox' /> Sandbox (For testing)
  299. <input $google_server_type2 type='radio' name='google_server_type' value='production' /> Production
  300. </td>
  301. </tr>
  302. <tr>
  303. <td>
  304. Select your currency
  305. </td>
  306. <td>
  307. <select name='google_cur'>";
  308. if (get_option('google_cur') == 'USD') {
  309. $output.=
  310. "<option selected='selected' value='USD'>USD</option>
  311. <option value='GBP'>GBP</option>";
  312. } else {
  313. $output.=
  314. "<option value='USD'>USD</option>
  315. <option value='GBP' selected='selected'>GBP</option>";
  316. }
  317. $output.="</select>
  318. </td>
  319. </tr>
  320. <tr>
  321. <td>
  322. Select Shipping Countries
  323. </td>
  324. <td>
  325. <a href='".add_query_arg(array("googlecheckoutshipping" => 1, "page" =>
  326. "wpsc-settings"))."' alt='Set Shipping Options'>Set Shipping countries</a> </td>
  327. </tr>
  328. <tr>
  329. <td>Button Styles
  330. </td>
  331. <td><div>Size:
  332. <input $button_size1 type='radio' name='google_button_size' value='0' /> 180&times;46
  333. <input $button_size2 type='radio' name='google_button_size' value='1' /> 168&times;44
  334. <input $button_size3 type='radio' name='google_button_size' value='2' /> 160&times;43
  335. </div>
  336. <div>
  337. Background:
  338. <select name='google_button_bg'>
  339. <option $button_bg1 value='trans'>Transparent</option>
  340. <option $button_bg2 value='white'>White</option>
  341. </select>
  342. </div>
  343. </td>
  344. </tr>
  345. <tr>
  346. <td colspan='2'>
  347. Note: Please put this link to your Google API callback url field on your Google checkout account: <strong>".get_option('siteurl')."/index.php</strong>
  348. </td>
  349. </tr>";
  350. return $output;
  351. }
  352. function nzsc_googleResponse() {
  353. global $wpdb, $user_ID;
  354. $merchant_id = get_option('google_id');
  355. $merchant_key = get_option('google_key');
  356. $server_type = get_option('google_server_type');
  357. $currency = get_option('google_cur');
  358. define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'library/googleerror.log');
  359. define('RESPONSE_HANDLER_LOG_FILE', 'library/googlemessage.log');
  360. if (stristr($_SERVER['HTTP_USER_AGENT'],"Google Checkout Notification Agent")) {
  361. $Gresponse = new GoogleResponse($merchant_id, $merchant_key);
  362. $xml_response = isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:file_get_contents("php://input");
  363. if (get_magic_quotes_gpc()) {
  364. $xml_response = stripslashes($xml_response);
  365. }
  366. list($root, $data) = $Gresponse->GetParsedXML($xml_response);
  367. $message = "<pre>".print_r($user_marketing_preference,1)."</pre>";
  368. $sessionid = (mt_rand(100,999).time());
  369. if ($root == "new-order-notification") {
  370. $_SESSION['nzshpcrt_cart'] = '';
  371. $cart_items = $data['new-order-notification']['shopping-cart']['items'];
  372. $user_marketing_preference=$data['new-order-notification']['buyer-marketing-preferences']['email-allowed']['VALUE'];
  373. $shipping_name = $data['new-order-notification']['buyer-shipping-address']['contact-name']['VALUE'];
  374. $shipping_name = explode(" ",$shipping_name);
  375. $shipping_firstname = $shipping_name[0];
  376. $shipping_lastname = $shipping_name[count($shipping_name)-1];
  377. $shipping_country = $data['new-order-notification']['buyer-shipping-address']['country-code']['VALUE'];
  378. $shipping_address1 = $data['new-order-notification']['buyer-shipping-address']['address1']['VALUE'];
  379. $shipping_address2 = $data['new-order-notification']['buyer-shipping-address']['address2']['VALUE'];
  380. $shipping_city = $data['new-order-notification']['buyer-shipping-address']['city']['VALUE'];
  381. $shipping_region = $data['new-order-notification']['buyer-shipping-address']['region']['VALUE'];
  382. $billing_name = $data['new-order-notification']['buyer-billing-address']['contact-name']['VALUE'];
  383. $billing_name = explode(" ",$shipping_name);
  384. $billing_firstname = $shipping_name[0];
  385. $billing_lastname = $shipping_name[count($shipping_name)-1];
  386. $billing_region = $data['new-order-notification']['buyer-billing-address']['region']['VALUE'];
  387. $billing_country = $data['new-order-notification']['buyer-billing-address']['country-code']['VALUE'];
  388. $total_price = $data['new-order-notification']['order-total']['VALUE'];
  389. $billing_email = $data['new-order-notification']['buyer-billing-address']['email']['VALUE'];
  390. $billing_phone = $data['new-order-notification']['buyer-billing-address']['phone']['VALUE'];
  391. $billing_address = $data['new-order-notification']['buyer-billing-address']['address1']['VALUE'];
  392. $billing_address .= " ".$data['new-order-notification']['buyer-billing-address']['address2']['VALUE'];
  393. $billing_address .= " ". $data['new-order-notification']['buyer-billing-address']['city']['VALUE'];
  394. $billing_city = $data['new-order-notification']['buyer-billing-address']['city']['VALUE'];
  395. $google_order_number = $data['new-order-notification']['google-order-number']['VALUE'];
  396. $pnp = $data['new-order-notification']['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-cost']['VALUE'];
  397. $affiliate_id=$data['new-order-notification']['shopping-cart']['merchant-private-data'];
  398. $affiliate_id=explode('=',$affiliate_id);
  399. if ($affiliate_id[0]=='affiliate_id') {
  400. if ($affiliate_id[1] == '') {
  401. $affiliate_id = null;
  402. } else {
  403. $affiliate_id = $affiliate_id[1];
  404. }
  405. }
  406. //$tax = $data['new-order-notification']['order-adjustment'][];
  407. $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type,$currency);
  408. $result = $Grequest->SendProcessOrder($google_order_number);
  409. $region_number = $wpdb->get_var("SELECT id FROM ".WPSC_TABLE_REGION_TAX."` WHERE code ='".$billing_region."'");
  410. $sql = "INSERT INTO `".WPSC_TABLE_PURCHASE_LOGS."` ( `totalprice` , `sessionid` , `date`, `billing_country`, `shipping_country`,`base_shipping`,`shipping_region`, `user_ID`, `discount_value`,`gateway`, `google_order_number`, `google_user_marketing_preference`, `affiliate_id`) VALUES ( '".$total_price."', '".$sessionid."', '".time()."', '".$billing_country."', '".$shipping_country."', '".$pnp."','".$region_number."' , '".$user_ID."' , '".$_SESSION['wpsc_discount']."','".get_option('payment_gateway')."','".$google_order_number."','".$user_marketing_preference."', '".$affiliate_id."')";
  411. // mail('hanzhimeng@gmail.com',"",$sql);
  412. $wpdb->query($sql) ;
  413. $log_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid` IN('".$sessionid."') LIMIT 1") ;
  414. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET firstname='".$shipping_firstname."', lastname='".$shipping_lastname."', email='".$billing_email."', phone='".$billing_phone."' WHERE id='".$log_id."'";
  415. $wpdb->query($sql) ;
  416. if (array_key_exists(0,$cart_items['item'])) {
  417. $cart_items = $cart_items['item'];
  418. }
  419. //logging to submited_form_data
  420. $billing_fname_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='first_name' LIMIT 1") ;
  421. $sql = "INSERT INTO `".WPSC_TABLE_SUBMITED_FORM_DATA."` (log_id, form_id, value) VALUES ('".$log_id."','".$billing_fname_id."','".$billing_firstname."')";
  422. //$wpdb->query($sql) ;
  423. $billing_lname_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='last_name' LIMIT 1") ;
  424. $sql .= ", ('".$log_id."','".$billing_lname_id."','".$billing_lastname."')";
  425. $billing_address_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='address' LIMIT 1") ;
  426. $sql .= ", ('".$log_id."','".$billing_address_id."','".$billing_address."')";
  427. $billing_city_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='city' LIMIT 1") ;
  428. $sql .= ", ('".$log_id."','".$billing_city_id."','".$billing_city."')";
  429. $billing_country_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='country' LIMIT 1") ;
  430. $sql .= ", ('".$log_id."','".$billing_country_id."','".$billing_country."')";
  431. $billing_state_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='state' LIMIT 1") ;
  432. $sql .= ", ('".$log_id."','".$billing_state_id."','".$billing_region."')";
  433. $shipping_fname_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='delivery_first_name' LIMIT 1") ;
  434. $sql .= ", ('".$log_id."','".$shipping_fname_id."','".$shipping_firstname."')";
  435. $shipping_lname_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='delivery_last_name' LIMIT 1") ;
  436. $sql .= ", ('".$log_id."','".$shipping_lname_id."','".$shipping_lastname."')";
  437. $shipping_address_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='delivery_address' LIMIT 1") ;
  438. $sql .= ", ('".$log_id."','".$shipping_address_id."','".$shipping_address1." ".$shipping_address2."')";
  439. $shipping_city_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='delivery_city' LIMIT 1") ;
  440. $sql .= ", ('".$log_id."','".$shipping_city_id."','".$shipping_city."')";
  441. $shipping_state_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='delivery_state' LIMIT 1") ;
  442. $sql .= ", ('".$log_id."','".$shipping_state_id."','".$shipping_region."')";
  443. $shipping_country_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type`='delivery_country' LIMIT 1") ;
  444. $sql .= ", ('".$log_id."','".$shipping_country_id."','".$shipping_country."')";
  445. $wpdb->query($sql) ;
  446. //$variations = $cart_item->product_variations;
  447. foreach($cart_items as $cart_item) {
  448. $product_id = $cart_item['merchant-item-id']['VALUE'];
  449. $item_name = $cart_item['item-name']['VALUE'];
  450. $item_desc = $cart_item['item-description']['VALUE'];
  451. $item_unit_price = $cart_item['unit-price']['VALUE'];
  452. $item_quantity = $cart_item['quantity']['VALUE'];
  453. $product_info = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE id='".$product_id."' LIMIT 1", ARRAY_A) ;
  454. $product_info = $product_info[0];
  455. //mail("hanzhimeng@gmail.com","",print_r($product_info,1));
  456. if($product_info['notax'] != 1) {
  457. //$price = nzshpcrt_calculate_tax($item_unit_price, $billing_country, $region_number);
  458. if(get_option('base_country') == $billing_country) {
  459. $country_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode` IN('".get_option('base_country')."') LIMIT 1",ARRAY_A);
  460. if(($country_data['has_regions'] == 1)) {
  461. if(get_option('base_region') == $region_number) {
  462. $region_data = $wpdb->get_row("SELECT `".WPSC_TABLE_REGION_TAX."`.* FROM `".WPSC_TABLE_REGION_TAX."` WHERE `".WPSC_TABLE_REGION_TAX."`.`country_id` IN('".$country_data['id']."') AND `".WPSC_TABLE_REGION_TAX."`.`id` IN('".get_option('base_region')."') ",ARRAY_A) ;
  463. }
  464. $gst = $region_data['tax'];
  465. } else {
  466. $gst = $country_data['tax'];
  467. }
  468. } else {
  469. $gst = 0;
  470. }
  471. } else {
  472. $gst = 0;
  473. }
  474. if ($product_info['no_shipping'] == '0') {
  475. if ($shipping_country == get_option('base_country')) {
  476. $pnp = $product_info['pnp'];
  477. } else {
  478. $pnp = $product_info['international_pnp'];
  479. }
  480. } else {
  481. $pnp=0;
  482. }
  483. $cartsql = "INSERT INTO `".WPSC_TABLE_CART_CONTENTS."` ( `prodid` , `purchaseid`, `price`, `pnp`, `gst`, `quantity`, `donation`, `no_shipping` ) VALUES ('".$product_id."', '".$log_id."','".$item_unit_price."','".$pnp."', '".$gst."','".$item_quantity."', '".$product_info['donation']."', '".$product_info['no_shipping']."')";
  484. $wpdb->query($cartsql) ;
  485. }
  486. }
  487. if ($root == "order-state-change-notification") {
  488. $google_order_number = $data['order-state-change-notification']['google-order-number']['VALUE'];
  489. $google_status=$wpdb->get_var("SELECT google_status FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  490. $google_status = unserialize($google_status);
  491. if (($google_status[0]!='Partially Charged') && ($google_status[0]!='Partially Refunded')) {
  492. $google_status[0]=$data['order-state-change-notification']['new-financial-order-state']['VALUE'];
  493. $google_status[1]=$data['order-state-change-notification']['new-fulfillment-order-state']['VALUE'];
  494. }
  495. $google_status = serialize($google_status);
  496. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET google_status='".$google_status."' WHERE google_order_number='".$google_order_number."'";
  497. $wpdb->query($sql) ;
  498. if (($data['order-state-change-notification']['new-financial-order-state']['VALUE'] == 'CHARGEABLE') && (get_option('google_auto_charge') == '1')) {
  499. $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type,$currency);
  500. $result = $Grequest->SendChargeOrder($google_order_number);
  501. $_SESSION['nzshpcrt_cart'] = '';
  502. unset($_SESSION['coupon_num'], $_SESSION['google_session']);
  503. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET processed='2' WHERE google_order_number='".$google_order_number."'";
  504. $wpdb->query($sql) ;
  505. }
  506. }
  507. if ($root == "charge-amount-notification") {
  508. $google_order_number = $data['charge-amount-notification']['google-order-number']['VALUE'];
  509. $google_status=$wpdb->get_var("SELECT google_status FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  510. $google_status = unserialize($google_status);
  511. $total_charged = $data['charge-amount-notification']['total-charge-amount']['VALUE'];
  512. $google_status['partial_charge_amount'] = $total_charged;
  513. $totalprice=$wpdb->get_var("SELECT totalprice FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  514. if ($totalprice>$total_charged) {
  515. $google_status[0] = 'Partially Charged';
  516. } else if ($totalprice=$total_charged) {
  517. $google_status[0] = 'CHARGED';
  518. }
  519. $google_status = serialize($google_status);
  520. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET google_status='".$google_status."' WHERE google_order_number='".$google_order_number."'";
  521. $wpdb->query($sql) ;
  522. }
  523. if ($root == "refund-amount-notification") {
  524. $google_order_number = $data['refund-amount-notification']['google-order-number']['VALUE'];
  525. $google_status=$wpdb->get_var("SELECT google_status FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  526. $google_status = unserialize($google_status);
  527. $total_charged = $data['refund-amount-notification']['total-refund-amount']['VALUE'];
  528. $google_status['partial_refund_amount'] = $total_charged;
  529. $totalprice=$wpdb->get_var("SELECT totalprice FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  530. if ($totalprice>$total_charged) {
  531. $google_status[0] = 'Partially refunded';
  532. } else if ($totalprice=$total_charged) {
  533. $google_status[0] = 'REFUNDED';
  534. }
  535. $google_status = serialize($google_status);
  536. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET google_status='".$google_status."' WHERE google_order_number='".$google_order_number."'";
  537. $wpdb->query($sql) ;
  538. }
  539. // <avs-response>Y</avs-response>
  540. // <cvn-response>M</cvn-response>
  541. if ($root == "risk-information-notification") {
  542. $google_order_number = $data['risk-information-notification']['google-order-number']['VALUE'];
  543. $google_status=$wpdb->get_var("SELECT google_status FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  544. $google_status = unserialize($google_status);
  545. $google_status['cvn']=$data['risk-information-notification']['risk-information']['cvn-response']['VALUE'];
  546. $google_status['avs']=$data['risk-information-notification']['risk-information']['avs-response']['VALUE'];
  547. $google_status['protection']=$data['risk-information-notification']['risk-information']['eligible-for-protection']['VALUE'];
  548. $google_status = serialize($google_status);
  549. $google_status=$wpdb->query("UPDATE ".WPSC_TABLE_PURCHASE_LOGS." SET google_status='".$google_status."' WHERE google_order_number='".$google_order_number."'");
  550. if ($data['risk-information-notification']['risk-information']['cvn-response']['VALUE'] == 'E') {
  551. $google_risk='cvn';
  552. }
  553. if (in_array($data['risk-information-notification']['risk-information']['avs-response']['VALUE'],array('N','U'))) {
  554. if (isset($google_risk)) {
  555. $google_risk = 'cvn+avs';
  556. } else {
  557. $google_risk='avs';
  558. }
  559. }
  560. if (isset($google_risk)) {
  561. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET google_risk='".$google_risk."' WHERE google_order_number='".$google_order_number."'";
  562. $wpdb->query($sql);
  563. }
  564. }
  565. if ($root == "order-state-change-notification") {
  566. $google_order_number = $data['order-state-change-notification']['google-order-number']['VALUE'];
  567. if ($data['order-state-change-notification']['new-financial-order-state']['VALUE'] == "CANCELLED_BY_GOOGLE") {
  568. $google_status = $wpdb->get_var("SELECT google_status FROM ".WPSC_TABLE_PURCHASE_LOGS." WHERE google_order_number='".$google_order_number."'");
  569. $google_status = unserialize($google_status);
  570. $google_status[0] = "CANCELLED_BY_GOOGLE";
  571. $wpdb->get_var("UPDATE ".WPSC_TABLE_PURCHASE_LOGS." SET google_status='".serialize($google_status)."' WHERE google_order_number='".$google_order_number."'");
  572. }
  573. }
  574. // mail('hanzhimeng@gmail.com',"",$root . " <pre>". print_r($data,1)."</pre>");
  575. exit();
  576. }
  577. }
  578. add_action('init', 'nzsc_googleResponse');
  579. ?>