function UseCase1()
{
    // Create a new shopping cart object
    $merchant_id = "";
    // Your Merchant ID
    $merchant_key = "";
    // Your Merchant Key
    $server_type = "sandbox";
    $currency = "USD";
    $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
    // Add items to the cart
    $item_1 = new GoogleItem("MegaSound 2GB MP3 Player", "Portable MP3 player - stores 500 songs", 1, 175.49);
    // Unit price
    $item_2 = new GoogleItem("AA Rechargeable Battery Pack", "Battery pack containing four AA rechargeable batteries", 1, 11.59);
    // Unit price
    $cart->AddItem($item_1);
    $cart->AddItem($item_2);
    // Add US shipping options
    $ship_1 = new GoogleFlatRateShipping("UPS Ground", 5.0);
    $restriction_1 = new GoogleShippingFilters();
    $restriction_1->SetAllowedCountryArea("CONTINENTAL_48");
    $ship_1->AddShippingRestrictions($restriction_1);
    $ship_2 = new GoogleFlatRateShipping("UPS 2nd Day", 10.0);
    $restriction_2 = new GoogleShippingFilters();
    $restriction_2->SetAllowedStateAreas(array("CA", "AZ", "CO", "WA", "OR"));
    $ship_2->AddShippingRestrictions($restriction_2);
    // Add international shipping options
    $ship_3 = new GoogleFlatRateShipping("Canada 3 Business Days", 5.0);
    $restriction_3 = new GoogleShippingFilters();
    $restriction_3->AddAllowedPostalArea("CA");
    $restriction_3->SetAllowUsPoBox("false");
    $ship_3->AddShippingRestrictions($restriction_3);
    $ship_4 = new GoogleFlatRateShipping("Europe 3 Business Days", 10.0);
    $restriction_4 = new GoogleShippingFilters();
    $restriction_4->AddAllowedPostalArea("GB", "SW*");
    $ship_4->AddShippingRestrictions($restriction_4);
    $cart->AddShipping($ship_1);
    $cart->AddShipping($ship_2);
    $cart->AddShipping($ship_3);
    $cart->AddShipping($ship_4);
    // Add US tax rules
    $tax_rule_1 = new GoogleDefaultTaxRule(0.0825);
    $tax_rule_1->SetStateAreas(array("CA", "NY"));
    $cart->AddDefaultTaxRules($tax_rule_1);
    // Add International tax rules
    $tax_rule_2 = new GoogleDefaultTaxRule(0.15);
    $tax_rule_2->AddPostalArea("GB");
    $tax_rule_2->AddPostalArea("FR");
    $tax_rule_2->AddPostalArea("DE");
    $cart->AddDefaultTaxRules($tax_rule_2);
    // Define rounding policy
    $cart->AddRoundingPolicy("HALF_UP", "PER_LINE");
    // Display XML data
    // echo "<pre>";
    // echo htmlentities($cart->GetXML());
    // echo "</pre>";
    // Display Google Checkout button
    echo $cart->CheckoutButtonCode("LARGE");
}
 /**
  * Get the shipping restrictions in the google filter module format for a specific zone
  * so we can add it as part of the shipping rules
  *
  * @return object
  **/
 private function GetShippingRestrictions($zone)
 {
     $Gfilter = new GoogleShippingFilters();
     // Handle the default zone
     if (!isset($zone['locationtype'])) {
         $Gfilter->SetAllowedWorldArea(true);
         return $Gfilter;
     }
     switch ($zone['locationtype']) {
         case 'zip':
             foreach ($zone['locations'] as $location) {
                 $pos = strpos($location['locationvalue'], '?');
                 if ($pos === false) {
                     $Gfilter->AddAllowedZipPattern($location['locationvalue']);
                 } else {
                     $tmp = substr($location['locationvalue'], 0, $pos);
                     $tmp .= '*';
                     $Gfilter->AddAllowedZipPattern($tmp);
                 }
             }
             break;
         case 'state':
             foreach ($zone['locations'] as $location) {
                 $country = GetCountryISO2ById($location['locationcountryid']);
                 $state = GetStateISO2ById($location['locationvalueid']);
                 if (empty($state)) {
                     $state = GetStateById($location['locationvalueid']);
                 }
                 if (empty($location['locationvalueid']) && $country == 'US') {
                     // If they have selected all states in the us, handle it differently
                     $Gfilter->SetAllowedCountryArea('ALL');
                     continue;
                 } elseif (empty($location['locationvalueid'])) {
                     $Gfilter->AddAllowedPostalArea($country);
                     continue;
                 }
                 if ($country == 'US' && $this->GetDefaultCurrencyCode() == 'USD') {
                     $Gfilter->AddAllowedStateArea($state);
                 } else {
                     $Gfilter->AddAllowedPostalArea($country, $state);
                 }
             }
             break;
         case 'country':
             foreach ($zone['locations'] as $location) {
                 $Gfilter->AddAllowedPostalArea(GetCountryISO2ById($location['locationvalueid']));
             }
             break;
     }
     return $Gfilter;
 }
function Usecase($seperator, $sessionid, $fromcheckout)
{
    global $wpdb, $wpsc_cart;
    $purchase_log_sql = "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid`= " . $sessionid . " LIMIT 1";
    $purchase_log = $wpdb->get_results($purchase_log_sql, ARRAY_A);
    $cart_sql = "SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`='" . $purchase_log[0]['id'] . "'";
    $wp_cart = $wpdb->get_results($cart_sql, ARRAY_A);
    $merchant_id = get_option('google_id');
    $merchant_key = get_option('google_key');
    $server_type = get_option('google_server_type');
    $currency = get_option('google_cur');
    $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
    $transact_url = get_option('transact_url');
    $returnURL = $transact_url . $seperator . "sessionid=" . $sessionid . "&gateway=google";
    $cart->SetContinueShoppingUrl($returnURL);
    $cart->SetEditCartUrl(get_option('shopping_cart_url'));
    $no = 1;
    //exit("<pre>".print_r($wpsc_cart,true)."</pre>");
    //new item code
    $no = 0;
    //	$cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
    //	foreach($wpsc_cart->cart_items as $item){
    //google prohibited items not implemented
    $curr = new CURRENCYCONVERTER();
    $currency_code = $wpdb->get_results("SELECT `code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`='" . get_option('currency_type') . "' LIMIT 1", ARRAY_A);
    $local_currency_code = $currency_code[0]['code'];
    //	    exit('<pre>'.print_r($_REQUEST,true).'</pre>');
    $google_curr = get_option('google_cur');
    while (wpsc_have_cart_items()) {
        wpsc_the_cart_item();
        if ($google_curr != $local_currency_code) {
            $google_currency_productprice = $curr->convert(wpsc_cart_item_price(false) / wpsc_cart_item_quantity(), $google_curr, $local_currency_code);
            $google_currency_shipping = $curr->convert($wpsc_cart->selected_shipping_amount, $google_curr, $local_currency_code);
        } else {
            $google_currency_productprice = wpsc_cart_item_price(false) / wpsc_cart_item_quantity() + wpsc_cart_item_shipping(false);
            $google_currency_shipping = $wpsc_cart->selected_shipping_amount;
        }
        //	exit('<pre>'.print_r(wpsc_cart_item_name(),true).'</pre>');
        $cartitem["{$no}"] = new GoogleItem(wpsc_cart_item_name(), '', wpsc_cart_item_quantity(), $google_currency_productprice);
        $cart->AddItem($cartitem["{$no}"]);
        $no++;
    }
    //If there are coupons applied add coupon as a product with negative price
    if ($wpsc_cart->coupons_amount > 0) {
        if ($google_curr != $local_currency_code) {
            $google_currency_productprice = $curr->convert($wpsc_cart->coupons_amount, $google_curr, $local_currency_code);
        } else {
            $google_currency_productprice = $wpsc_cart->coupons_amount;
        }
        $cartitem[$no] = new GoogleItem('Discount', 'Discount Price', 1, '-' . $google_currency_productprice);
        // Unit price
        $cart->AddItem($cartitem[$no]);
    }
    //	}
    // Add shipping options
    if (wpsc_uses_shipping() && $google_currency_shipping > 0) {
        $Gfilter = new GoogleShippingFilters();
        $google_checkout_shipping = get_option("google_shipping_country");
        $googleshippingcountries = count($google_checkout_shipping);
        //exit('<pre>'.print_r($googleshipping, true).'</pre>');
        if ($googleshippingcountries == 242) {
            $Gfilter->SetAllowedWorldArea(true);
        } else {
            if (is_array($google_checkout_shipping)) {
                $google_shipping_country_ids = implode(",", $google_checkout_shipping);
            }
            $google_shipping_country = $wpdb->get_col("SELECT `isocode` FROM " . WPSC_TABLE_CURRENCY_LIST . " WHERE id IN (" . $google_shipping_country_ids . ")");
            foreach ($google_shipping_country as $isocode) {
                //exit($isocode);
                $Gfilter->AddAllowedPostalArea($isocode);
                if ($isocode == 'US') {
                    $Gfilter->SetAllowedCountryArea('ALL');
                }
            }
        }
        $Gfilter->SetAllowUsPoBox(false);
        $ship_1 = new GoogleFlatRateShipping('Flat Rate Shipping', $google_currency_shipping);
        $ship_1->AddShippingRestrictions($Gfilter);
        $cart->AddShipping($ship_1);
    }
    //wpsc_google_shipping_quotes();
    // Add tax rules
    //if ($_SESSION['wpsc_selected_country']=='US'){
    //set default tax
    //exit('<pre>'.print_r($_SESSION,true).'</pre>');
    $sql = "SELECT `name`, `tax` FROM " . WPSC_TABLE_REGION_TAX . " WHERE id='" . $_SESSION['wpsc_selected_region'] . "'";
    //exit('<pre>'.print_r($sql, true).'</pre>');
    $state_name = $wpdb->get_row($sql, ARRAY_A);
    //exit('<pre>'.print_r($state_name, true).'</pre>');
    $defaultTax = $state_name['tax'] / 100;
    $tax_rule = new GoogleDefaultTaxRule($defaultTax);
    $sql = "SELECT `code` FROM " . WPSC_TABLE_REGION_TAX . " WHERE `country_id`='136' AND `tax` = " . $state_name['tax'];
    $states = $wpdb->get_col($sql);
    //exit('<pre>'.print_r($states, true).'</pre>');
    $tax_rule->SetStateAreas((array) $states);
    $cart->AddDefaultTaxRules($tax_rule);
    //get alternative tax rates
    $sql = "SELECT DISTINCT `tax` FROM " . WPSC_TABLE_REGION_TAX . " WHERE `tax` != 0 AND `tax` !=" . $state_name['tax'] . "  AND `country_id`='136' ORDER BY `tax`";
    $othertax = $wpdb->get_col($sql);
    $i = 1;
    //exit('<pre>'.print_r($othertax, true).'</pre>');
    foreach ($othertax as $altTax) {
        $sql = "SELECT `code` FROM " . WPSC_TABLE_REGION_TAX . " WHERE `country_id`='136' AND `tax`=" . $altTax;
        $alt = $wpdb->get_col($sql);
        $altTax = $altTax / 100;
        $alt_google_tax = new GoogleDefaultTaxRule($altTax);
        $alt_google_tax->SetStateAreas($alt);
        //$g = new GoogleAlternateTaxTable('Alt Tax'.$i);
        //$g->AddAlternateTaxRules($alt_google_tax);
        $cart->AddDefaultTaxRules($alt_google_tax);
        //			exit(print_r($alt,true));
        $i++;
    }
    //}
    if (get_option('google_button_size') == '0') {
        $google_button_size = 'BIG';
    } elseif (get_option('google_button_size') == '1') {
        $google_button_size = 'MEDIUM';
    } elseif (get_option('google_button_size') == '2') {
        $google_button_size = 'SMALL';
    }
    // Display Google Checkout button
    //echo '<pre>'.print_r($cart, true).'</pre>';
    //unset($_SESSION['wpsc_sessionid']);
    //if($fromCheckout){
    echo $cart->CheckoutButtonCode($google_button_size);
    //}
}
Example #4
0
 } else {
     $default_value = 1;
     $module = new $module_name();
     $quote = $module->quote($method);
     $price = $quote['methods'][0]['cost'];
     $shipping_price = $currencies->get_value(DEFAULT_CURRENCY) * ($price >= 0 ? $price : 0);
 }
 $Gfilter = new GoogleShippingFilters();
 if (MODULE_PAYMENT_GOOGLECHECKOUT_USPOBOX == 'False') {
     $Gfilter->SetAllowUsPoBox('false');
 }
 if (!empty($allowed_restriction_country)) {
     foreach ($allowed_restriction_state as $state_key => $state) {
         if ($allowed_restriction_country[$state_key][1] == 'US') {
             if ($state == 'All Areas') {
                 $Gfilter->SetAllowedCountryArea('ALL');
             } else {
                 $Gfilter->AddAllowedStateArea($state);
             }
         } else {
             // TODO here should go the non us area (not implemented in GC)
             // now just the country
             $Gfilter->AddAllowedPostalArea($allowed_restriction_country[$state_key][1]);
         }
     }
 } else {
     switch ($type) {
         case 'domestic_types':
             if ('US' == $shipping_origin_iso_code_2) {
                 $Gfilter->SetAllowedCountryArea('ALL');
             } else {
		/**
		 * Get the shipping restrictions in the google filter module format for a specific zone
		 * so we can add it as part of the shipping rules
		 *
		 * @return object
		 **/
		private function GetShippingRestrictions($zone)
		{
			$Gfilter = new GoogleShippingFilters();

			// Handle the default zone
			if (!isset($zone['locationtype'])) {
				$Gfilter->SetAllowedWorldArea(true);
				return $Gfilter;
			}

			switch ($zone['locationtype']) {
				case 'zip':
				{
					foreach ($zone['locations'] as $location) {

						$pos = strpos($location['locationvalue'], '?');
						$country = GetCountryISO2ById($location['locationcountryid']);

						if ($pos === false) {
							$Gfilter->AddAllowedPostalArea($country, $location['locationvalue']);
						}
						else {

							$tmp = substr($location['locationvalue'], 0, $pos);
							$tmp .= '*';
							$Gfilter->AddAllowedPostalArea($country, $tmp);
						}

					}
					break;
				}
				case 'state':
				{
					foreach ($zone['locations'] as $location) {
						$country = GetCountryISO2ById($location['locationcountryid']);
						$state = GetStateISO2ById($location['locationvalueid']);

						if (empty($state)) {
							$state = GetStateById($location['locationvalueid']);
						}

						if (empty($location['locationvalueid']) && $country == 'US') {
							// If they have selected all states in the us, handle it differently
							$Gfilter->SetAllowedCountryArea('ALL');
							continue;
						} elseif (empty($location['locationvalueid'])) {
							$Gfilter->AddAllowedPostalArea($country);
							continue;
						}

						if ($country == 'US' && $this->GetDefaultCurrencyCode() == 'USD') {
							// Google does not support Puerto Rico, Guam or the Marshall Islands as US-state-areas
							// Per Google Checkout support; ref ISC-155
							if ($state != 'PR' AND $state != 'MH' AND $state != 'GU')
							{
								$Gfilter->AddAllowedStateArea($state);
							}
						} else {
							$Gfilter->AddAllowedPostalArea($country, $state);
						}
					}
					break;
				}
				case 'country':
				{
					foreach ($zone['locations'] as $location) {
						$Gfilter->AddAllowedPostalArea(GetCountryISO2ById($location['locationvalueid']));
					}
					break;
				}
			}

			return $Gfilter;
		}
Example #6
0
function Usecase($seperator, $sessionid)
{
    global $wpdb;
    $purchase_log_sql = "SELECT * FROM `" . $wpdb->prefix . "purchase_logs` WHERE `sessionid`= " . $sessionid . " LIMIT 1";
    $purchase_log = $wpdb->get_results($purchase_log_sql, ARRAY_A);
    $cart_sql = "SELECT * FROM `" . $wpdb->prefix . "cart_contents` WHERE `purchaseid`='" . $purchase_log[0]['id'] . "'";
    $wp_cart = $wpdb->get_results($cart_sql, ARRAY_A);
    $merchant_id = get_option('google_id');
    $merchant_key = get_option('google_key');
    $server_type = get_option('google_server_type');
    $currency = get_option('google_cur');
    $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
    $cart->SetContinueShoppingUrl(get_option('product_list_url'));
    $cart->SetEditCartUrl(get_option('shopping_cart_url'));
    $no = 1;
    //exit("<pre>".print_r($wp_cart,true)."</pre>");
    foreach ($wp_cart as $item) {
        $product_data = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "product_list` WHERE `id`='" . $item['prodid'] . "' LIMIT 1", ARRAY_A);
        $product_data = $product_data[0];
        $prohibited = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "wpsc_productmeta` WHERE `product_id`='" . $item['prodid'] . "' AND meta_key='google_prohibited' LIMIT 1", ARRAY_A);
        $prohibited_data = $prohibited_data[0];
        if (count($prohibited) > 0) {
            $_SESSION['google_prohibited'] = '1';
        } else {
            $_SESSION['google_prohibited'] = '0';
        }
        $variation_count = count($product_variations);
        $variation_sql = "SELECT * FROM `" . $wpdb->prefix . "cart_item_variations` WHERE `cart_id`='" . $item['id'] . "'";
        $variation_data = $wpdb->get_results($variation_sql, ARRAY_A);
        $variation_count = count($variation_data);
        $extras_sql = "SELECT * FROM `" . $wpdb->prefix . "cart_item_extras` WHERE `cart_id`='" . $item['id'] . "'";
        $extras_data = $wpdb->get_results($extras_sql, ARRAY_A);
        $extras_count = count($extras_data);
        $price = nzshpcrt_calculate_tax($item['price'], $_SESSION['selected_country'], $_SESSION['selected_region']);
        if ($extras_count > 0) {
            foreach ($extras_data as $extras_datum) {
                $price += $wpdb->get_var("SELECT `price` FROM `" . $wpdb->prefix . "extras_values_associations` WHERE `product_id` = '" . $item['prodid'] . "' AND `extras_id` = '" . $extras_datum['extra_id'] . "' LIMIT 1");
            }
        }
        //exit("------->".$price);
        $local_currency_shipping = $item['pnp'];
        $base_shipping = $purchase_log[0]['base_shipping'];
        $total_shipping = $local_currency_shipping + $base_shipping;
        $cartitem["{$no}"] = new GoogleItem($product_data['name'], $product_data['description'], $item['quantity'], $price);
        // Unit price
        $cart->AddItem($cartitem["{$no}"]);
        $no++;
    }
    // Add shipping options
    $Gfilter = new GoogleShippingFilters();
    $Gfilter->SetAllowedCountryArea('ALL');
    $google_checkout_shipping = get_option("google_shipping_country");
    $google_shipping_country_ids = implode(",", $google_checkout_shipping);
    $google_shipping_country = $wpdb->get_var("SELECT isocode FROM " . $wpdb->prefix . "currency_list WHERE id IN (" . $google_shipping_country_ids . ")");
    $Gfilter->AddAllowedPostalArea($google_shipping_country);
    $ship_1 = new GoogleFlatRateShipping('Flat Rate Shipping', $total_shipping);
    $ship_1->AddShippingRestrictions($Gfilter);
    $cart->AddShipping($ship_1);
    // Add tax rules
    if ($_SESSION['selected_country'] == 'US') {
        $tax_rule = new GoogleDefaultTaxRule(0.05);
        $state_name = $wpdb->get_var("SELECT name FROM " . $wpdb->prefix . "region_tax WHERE id='" . $_SESSION['selected_region'] . "'");
        $tax_rule->SetStateAreas(array($state_name));
        $cart->AddDefaultTaxRules($tax_rule);
    }
    $_SESSION['nzshpcrt_cart'] = null;
    // Specify <edit-cart-url>
    // $cart->SetEditCartUrl("https://www.example.com/cart/");
    // Specify "Return to xyz" link
    //$cart->SetContinueShoppingUrl("https://www.example.com/goods/");
    // Request buyer's phone number
    //$cart->SetRequestBuyerPhone(true);
    // Display Google Checkout button
    echo $cart->CheckoutButtonCode("BIG");
}