Advertisement
Guest User

Untitled

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