PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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