function UseCase2() { // 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("Dry Food Pack AA1453", "A pack of highly nutritious dried food for emergency", 2, 24.99); $item_1->SetTaxTableSelector("food"); $item_2 = new GoogleItem("MegaSound 2GB MP3 Player", "Portable MP3 player - stores 500 songs", 1, 175.49); $item_2->SetMerchantPrivateItemData(new MerchantPrivateItemData(array("color" => "blue", "weight" => "3.2"))); $item_2->SetMerchantItemId("Item#012345"); $cart->AddItem($item_1); $cart->AddItem($item_2); // Add shipping options $ship_1 = new GoogleFlatRateShipping("Ground", 15); $restriction_1 = new GoogleShippingFilters(); $restriction_1->SetAllowedWorldArea(true); $ship_1->AddShippingRestrictions($restriction_1); $ship_2 = new GooglePickup("Pick Up", 5); $cart->AddShipping($ship_1); $cart->AddShipping($ship_2); // Add default tax rules $tax_rule_1 = new GoogleDefaultTaxRule(0.17); $tax_rule_1->AddPostalArea("GB", "SW*"); $tax_rule_1->AddPostalArea("FR"); $tax_rule_1->AddPostalArea("DE"); $tax_rule_2 = new GoogleDefaultTaxRule(0.1); $tax_rule_2->SetWorldArea(true); $cart->AddDefaultTaxRules($tax_rule_1); $cart->AddDefaultTaxRules($tax_rule_2); // Add alternate tax table $tax_table = new GoogleAlternateTaxTable("food"); $tax_rule_1 = new GoogleAlternateTaxRule(0.05); $tax_rule_1->AddPostalArea("GB"); $tax_rule_1->AddPostalArea("FR"); $tax_rule_1->AddPostalArea("DE"); $tax_rule_2 = new GoogleAlternateTaxRule(0.03); $tax_rule_2->SetWorldArea(true); $tax_table->AddAlternateTaxRules($tax_rule_1); $tax_table->AddAlternateTaxRules($tax_rule_2); $cart->AddAlternateTaxTables($tax_table); // Add <merchant-private-data> $cart->SetMerchantPrivateData(new MerchantPrivateData(array("animals" => array("type" => "cat,dog")))); // Specify <edit-cart-url> $cart->SetEditCartUrl("http://www.example.com/edit"); // Specify "Return to xyz" link $cart->SetContinueShoppingUrl("http://www.example.com/continue"); // Request buyer's phone number $cart->SetRequestBuyerPhone(true); // Define rounding policy $cart->AddRoundingPolicy("CEILING", "TOTAL"); // Display XML data // echo "<pre>"; // echo htmlentities($cart->GetXML()); // echo "</pre>"; // Display a medium size button echo $cart->GetXML(); }
function process() { $this->order->load($this->order->getReference()); $results = $this->modelPayment->get_orderstatus_id($this->language->get('order_status_paid_unconfirmed'), $this->language->getId()); //$sql = "select `order_status_id` from `order_status` where `name` = '?' and `language_id` = '?'"; //$parsed = $this->database->parse($sql, $this->language->get('order_status_paid_unconfirmed'), $this->language->getId()); //$results = $this->database->getRow($parsed); if ($results) { // copy cart and order-total to a new GoogleCart object $merchantId = $this->config->get('google_merchantid'); $merchantKey = $this->config->get('google_merchantkey'); chdir('library/google'); require_once 'library/googlecart.php'; require_once 'library/googleitem.php'; require_once 'library/googleshipping.php'; require_once 'library/googletax.php'; $serverType = $this->config->get('google_test') ? 'sandbox' : 'production'; $currencyGoogle = $this->config->get('google_currency'); $currencyCart = $this->currency->getCode(); $totalCart = $this->order->get('total'); if ($currencyCart != $currencyGoogle) { $result = $this->modelPayment->get_currency($currencyCart); $baseValCart = isset($result['value']) ? $result['value'] : NULL; $result = $this->modelPayment->get_currency($currencyGoogle); $baseValGoogle = isset($result['value']) ? $result['value'] : NULL; if ($baseValCart == NULL || $baseValGoogle == NULL) { // this should never happen, we use cart's currency let GoogleCheckout return an error $totalGoogle = $totalCart; $currencyGoogle = $currencyCart; } else { // convert cart's total into the currency used by Google Checkout $totalGoogle = round($totalCart * $baseValCart / $baseValGoogle); } } else { $totalGoogle = $totalCart; } $cart = new GoogleCart($merchantId, $merchantKey, $serverType, $currencyGoogle); $item_1 = new GoogleItem($this->config->get('config_store'), 'order# ' . $this->order->getReference(), 1, $totalGoogle); $item_1->SetTaxTableSelector("including all taxes"); $cart->AddItem($item_1); $tax_table = new GoogleAlternateTaxTable("including all taxes"); $tax_rule_1 = new GoogleAlternateTaxRule(0.0); $tax_rule_1->SetWorldArea(true); $tax_table->AddAlternateTaxRules($tax_rule_1); $cart->AddAlternateTaxTables($tax_table); // $tax_rule_1 = new GoogleDefaultTaxRule(0.175); // $tax_rule_1->SetWorldArea(true); // $cart->AddDefaultTaxRules($tax_rule_1); // Have AlegroCart process the order and remove its cart from the session. // AlegroCart will store it in the database with a 'Paid Unconfirmed' order status. $this->order->process($results['order_status_id']); $this->cart->clear(); // This will do a server-to-server Google cart post and send an HTTP 302 redirect status // More info http://code.google.com/apis/checkout/developer/index.html#alternate_technique list($status, $error) = $cart->CheckoutServer2Server(); return TRUE; // If it reaches this point then something went wrong echo "An error had ocurred: <br />HTTP Status: " . $status . ":"; echo "<br />Error message:<br />"; echo $error; echo "<br />"; exit; } else { // I think it may be better to die here with a message as it is // a major configuration problem that should be found by even // the most basic testing and hence not impact upon a customer. die('Configuration error: You MUST have created an order status for "Paid Unconfirmed" for every installed language.'); // The following is a reasonable alternative but there is no way without making // changes to checkout_failure, to get a user defined message to the that page. // The message, as above, is a big help in tracking any teething problems with this code. //$this->response->redirect($this->url->ssl('checkout_failure')); } }
} else { $GDtaxRule = new GoogleDefaultTaxRule(0, 'false'); $GDtaxRule->SetWorldArea(true); $Gcart->AddDefaultTaxRules($GDtaxRule); } if (sizeof($tax_class_unique) > 1 || sizeof($tax_class_unique) == 1 && sizeof($module_info_enabled) != sizeof($tax_class)) { $Gcart->SetButtonVariant(false); $Gwarnings[] = GOOGLECHECKOUT_STRING_WARN_MULTIPLE_SHIP_TAX; } $i = 0; $tax_tables = array(); foreach ($tax_array as $tax_table) { $tax_result = $db->Execute("select distinct countries_name, coalesce(zone_code, 'All Areas') zone_code, tax_rate, countries_iso_code_2\n from " . TABLE_TAX_RATES . " as tr " . " inner join " . TABLE_ZONES_TO_GEO_ZONES . " as ztgz on tr.tax_zone_id = ztgz.geo_zone_id " . " inner join " . TABLE_COUNTRIES . " as c on ztgz.zone_country_id = c.countries_id " . " left join " . TABLE_ZONES . " as z on ztgz.zone_id=z.zone_id\n where tr.tax_class_id= '" . $tax_array[$i] . "'"); $num_rows = $tax_result->RecordCount(); $tax_rule = array(); $GAtaxTable = new GoogleAlternateTaxTable(!empty($tax_name_array[$i]) ? $tax_name_array[$i] : 'none', 'false'); for ($j = 0; $j < $num_rows; $j++) { $tax_result->MoveNext(); $rate = (double) $tax_result->fields['tax_rate'] / 100.0; $GAtaxRule = new GoogleAlternateTaxRule($rate); if ($tax_result->fields['countries_iso_code_2'] == 'US') { if ($tax_result->fields['zone_code'] == 'All Areas') { $GAtaxRule->SetCountryArea('ALL'); } else { $GAtaxRule->SetStateAreas($tax_result->fields['zone_code']); } } else { // TODO here should go the non use area $GAtaxRule->AddPostalArea($tax_result->fields['countries_iso_code_2']); } $GAtaxTable->AddAlternateTaxRules($GAtaxRule);
/** * Generate the Google Checkout button link **/ public function generate_googlecheckout_form($order_id) { global $woocommerce; require_once GOOGLE_CHECKOUT_LIB . 'googlecart.php'; require_once GOOGLE_CHECKOUT_LIB . 'googleitem.php'; require_once GOOGLE_CHECKOUT_LIB . 'googleshipping.php'; require_once GOOGLE_CHECKOUT_LIB . 'googletax.php'; $order = new WC_Order($order_id); $shipping_name = explode(' ', $order->shipping_method); // Check if this is a test purchase if ($this->testmode == 'yes') { $server_type = "sandbox"; } else { $server_type = "checkout"; } $merchant_id = $this->merchant_id; // Your Merchant ID $merchant_key = $this->merchant_key; // Your Merchant Key $currency = get_option('woocommerce_currency'); $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency); // Specify <edit-cart-url> $cart->SetEditCartUrl(get_permalink(get_option('woocommerce_cart_page_id'))); // Specify "Return to xyz" link $cart->SetContinueShoppingUrl(add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('woocommerce_thanks_page_id'))))); // Order key $cart->SetMerchantPrivateData(new MerchantPrivateData(array("cart-id" => $order->id))); // Request buyer's phone number $cart->SetRequestBuyerPhone(true); // Default tax - for shipping, if used if ($order->order_shipping_tax > 0) { // We manually calculate the shipping tax percentage here $calculated_shipping_tax_percentage = $order->order_shipping_tax / $order->order_shipping; $tax_rule_for_shipping = new GoogleDefaultTaxRule($calculated_shipping_tax_percentage, 'true'); $tax_rule_for_shipping->SetWorldArea(true); $cart->AddDefaultTaxRules($tax_rule_for_shipping); } // Shipping Cost if ($order->order_shipping > 0) { $ship_1 = new GoogleFlatRateShipping($order->shipping_method, number_format($order->order_shipping, 2)); $restriction_1 = new GoogleShippingFilters(); $restriction_1->SetAllowedWorldArea(true); $ship_1->AddShippingRestrictions($restriction_1); $cart->AddShipping($ship_1); } // Cart Contents $item_loop = 0; $myvat = array(); if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { $_product = new WC_Product($item['id']); if ($_product->exists() && $item['qty']) { $item_loop++; // Change tax format from 25.00 to 0.25 $item_tax_percentage = number_format($order->get_item_tax($item, false) / $order->get_item_total($item, false, false) * 100, 2, '.', ''); $item_vat = $item_tax_percentage / 100; $myvat[$item_loop] = $item_vat; ${"item_" . $item_loop} = new GoogleItem($item['name'], "", $item['qty'], $order->get_item_total($item, false, false)); // Name the alternate-tax-table $vat_name = "vat" . $item_vat; ${"item_" . $item_loop}->SetMerchantItemId($item['id']); ${"item_" . $item_loop}->SetTaxTableSelector($vat_name); $cart->AddItem(${"item_" . $item_loop}); } } } // Discount if ($order->order_discount > 0) { $item_loop++; ${"item_" . $item_loop} = new GoogleItem(__('Discount', 'woothemes'), "", "1", -$order->order_discount); ${"item_" . $item_loop}->SetTaxTableSelector("no_tax"); $cart->AddItem(${"item_" . $item_loop}); } // Tax // Loops through all tax classes that has been added to the cart and add these as Alternate tax tables to google Checkout. $taxrule_loop = 1; $no_duplicate_vat = array_unique($myvat); foreach ($no_duplicate_vat as $value) { // Name the alternate-tax-table $vat_name = "vat" . $value; $tax_table = new GoogleAlternateTaxTable($vat_name); ${"tax_rule_" . $taxrule_loop} = new GoogleAlternateTaxRule($value); ${"tax_rule_" . $taxrule_loop}->SetWorldArea(true); $tax_table->AddAlternateTaxRules(${"tax_rule_" . $taxrule_loop}); $cart->AddAlternateTaxTables($tax_table); $taxrule_loop++; } // The form return $cart->CheckoutButtonCode("SMALL") . '<script type="text/javascript"> jQuery(function(){ jQuery("body").block( { message: "<img src=\\"' . $woocommerce->plugin_url() . '/assets/images/ajax-loader.gif\\" alt=\\"Redirecting...\\" />' . __('Thank you for your order. We are now redirecting you to Google Checkout to make payment.', 'woothemes') . '", overlayCSS: { background: "#fff", opacity: 0.6 }, css: { padding: 20, textAlign: "center", color: "#555", border: "3px solid #aaa", backgroundColor:"#fff", cursor: "wait", lineHeight: "32px" } }); jQuery("#submit_googlecheckout_payment_form").click(); }); </script>'; }
/** * Add the taxation information to the google object representation of the customers cart * * @return void **/ private function AddTaxInformationToCart() { require_once dirname(__FILE__).'/library/googletax.php'; // Fetch available tax classes $taxClasses = getClass('ISC_TAX')->getTaxClasses(); foreach($taxClasses as $id => $name) { $taxClasses[$id] = array( 'name' => $name, 'rules' => array() ); } $taxClassIds = array_keys($taxClasses); // Fetch available tax zones $taxZones = array(); $query = " SELECT id, type, `default` FROM [|PREFIX|]tax_zones WHERE enabled=1 OR `default`=1 "; $result = $GLOBALS['ISC_CLASS_DB']->query($query); while($taxZone = $GLOBALS['ISC_CLASS_DB']->fetch($result)) { $taxClasses[$id]['rules'] = array(); $shippingTaxRate = getClass('ISC_TAX')->getEffectiveClassRate($taxZone['id'], getConfig('taxShippingTaxClass')); $shippingTaxed = 'false'; if($shippingTaxRate > 0) { $shippingTaxed = 'true'; } $defaultTaxRate = getClass('ISC_TAX')->getEffectiveClassRate($taxZone['id'], 0) / 100; $defaultTaxRule = new GoogleDefaultTaxRule($defaultTaxRate, $shippingTaxed); foreach($taxClassIds as $id) { $taxRate = getClass('ISC_TAX')->getEffectiveClassRate($taxZone['id'], $id) / 100; $taxClasses[$id]['rules'][$taxZone['id']] = new GoogleAlternateTaxRule($taxRate); } // Everywhere else tax zone - allow everywhere if($taxZone['default']) { $defaultTaxRule->setWorldArea(true); foreach($taxClassIds as $id) { $taxClasses[$id]['rules'][$taxZone['id']]->setWorldArea(true); } } // Location specific zone, so the tax becomes dependant on configured locations else { $zipPatterns = array(); $stateCodes = array(); $query = " SELECT * FROM [|PREFIX|]tax_zone_locations WHERE tax_zone_id='".$taxZone['id']."' "; $locationResult = $GLOBALS['ISC_CLASS_DB']->query($query); while($location = $GLOBALS['ISC_CLASS_DB']->fetch($locationResult)) { if($location['type'] == 'country') { $countryIso = getCountryISO2ById($location['value_id']); $defaultTaxRule->addPostalArea($countryIso); foreach($taxClassIds as $id) { $taxClasses[$id]['rules'][$taxZone['id']]->addPostalArea($countryIso); } } else if($location['type'] == 'zip') { $countryIso = getCountryISO2ById($location['country_id']); // US zip codes are handled with setZipPatterns below if($countryIso == 'US') { $zipPatterns[] = $location['value']; } else { $defaultTaxRule->addPostalArea($countryIso, $location['value']); foreach($taxClassIds as $id) { $taxClasses[$id]['rules'][$taxZone['id']]->addPostalArea($countryIso, $location['value']); } } } else if($location['type'] == 'state') { $countryIso = getCountryISO2ById($location['country_id']); // Google Checkout only supports US based states // Ref: http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Understanding_Areas.html if($countryIso != 'US') { continue; } $stateCodes[] = getStateISO2ById($location['value_id']); } } } $this->cart->addDefaultTaxRules($defaultTaxRule); // Add in the US zip codes if there are any if(!empty($zipPatterns)) { $defaultTaxRule->setZipPatterns($zipPatterns); foreach($taxClassIds as $id) { $taxClasses[$id]['rules'][$taxZone['id']]->setZipPatterns($zipPatterns); } } // Add in US states if there are any if(!empty($stateCodes)) { $defaultTaxRule->setStateAreas($stateCodes); foreach($taxClassIds as $id) { $taxClasses[$id]['rules'][$taxZone['id']]->setStateAreas($stateCodes); } } } foreach($taxClasses as $id => $taxClass) { $table = new GoogleAlternateTaxTable($id); foreach($taxClass['rules'] as $rule) { $table->addAlternateTaxRules($rule); } $this->cart->addAlternateTaxTables($table); } }