4 * Copyright (C) 2007 Google Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 // Point to the correct directory
21 // Include all the required files
22 require_once('library/googlecart.php');
23 require_once('library/googleitem.php');
24 require_once('library/googleshipping.php');
25 require_once('library/googletax.php');
27 // Invoke any of the provided use cases
36 echo "<h2>Standard Checkout Request</h2>";
37 $merchant_id = ""; // Your Merchant ID
38 $merchant_key = ""; // Your Merchant Key
39 $server_type = "sandbox";
41 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type,
45 $item_1 = new GoogleItem("item name", // Item name
46 "item desc", // Item description
47 $total_count, // Quantity
49 $cart->AddItem($item_1);
51 // Add shipping options
53 $ship_1 = new GoogleFlatRateShipping("USPS Priority Mail", 4.55);
55 $ship_1 = new GoogleFlatRateShipping("USPS Priority Mail", 6.2);
57 $Gfilter = new GoogleShippingFilters();
58 $Gfilter->SetAllowedCountryArea('CONTINENTAL_48');
60 $ship_1->AddShippingRestrictions($Gfilter);
62 $cart->AddShipping($ship_1);
65 $tax_rule = new GoogleDefaultTaxRule(0.05);
66 $tax_rule->SetStateAreas(array("MA"));
67 $cart->AddDefaultTaxRules($tax_rule);
69 // Specify <edit-cart-url>
70 $cart->SetEditCartUrl("https://www.example.com/cart/");
72 // Specify "Return to xyz" link
73 $cart->SetContinueShoppingUrl("https://www.example.com/goods/");
75 // Request buyer's phone number
76 $cart->SetRequestBuyerPhone(true);
78 // Display Google Checkout button
79 echo $cart->CheckoutButtonCode("SMALL");
82 // The idea of this usecase is to show how to implement Server2Server
83 // Checkout API Requests
84 // http://code.google.com/apis/checkout/developer/index.html#alternate_technique
85 // It will only display the GC button, and when you click on it it will redirect
86 // to a script ('digitalCart.php') that will create the cart, send it to google
87 // Checkout and redirect the buyer to the corresponding page
88 function DigitalUsecase() {
89 echo "<h2>Server 2 Server Checkout Request</h2>";
90 $merchant_id = ""; // Your Merchant ID
91 $merchant_key = ""; // Your Merchant Key
92 $server_type = "sandbox";
94 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type,$currency);
96 echo $cart->CheckoutServer2ServerButton('digitalCart.php');
99 function CarrierCalcUsecase() {
100 echo "<h2>Carrier Calculation Checkout Request</h2>";
101 // Create a new shopping cart object
102 $merchant_id = ""; // Your Merchant ID
103 $merchant_key = ""; // Your Merchant Key
104 $server_type = "sandbox";
106 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
108 // Add items to the cart
109 $item_1 = new GoogleItem("MegaSound 2GB MP3 Player", // Item name
110 "Portable MP3 player - stores 500 songs", // Item description
115 $item_1->SetMerchantItemId('MS_2GB');
116 $item_2 = new GoogleItem("AA Rechargeable Battery Pack",
117 "Battery pack containing four AA rechargeable batteries",
122 $item_2->SetMerchantItemId('AAR_BP');
123 $cart->AddItem($item_1);
124 $cart->AddItem($item_2);
126 $ship_from = new GoogleShipFrom('Store_origin',
131 $GSPackage = new GoogleShippingPackage($ship_from,1,2,3,'IN');
132 $Gshipping = new GoogleCarrierCalculatedShipping('Carrier_shipping');
133 $Gshipping->addShippingPackage($GSPackage);
135 $CCSoption = new GoogleCarrierCalculatedShippingOption("10.99", "FedEx", "Ground", "0.99");
136 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
137 $CCSoption = new GoogleCarrierCalculatedShippingOption("22.99", "FedEx", "Express Saver");
138 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
139 $CCSoption = new GoogleCarrierCalculatedShippingOption("24.99", "FedEx", "2Day", "0", "10", 'REGULAR_PICKUP');
140 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
142 $CCSoption = new GoogleCarrierCalculatedShippingOption("11.99", "UPS", "Ground", "0.99", "5", 'REGULAR_PICKUP');
143 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
144 $CCSoption = new GoogleCarrierCalculatedShippingOption("18.99", "UPS", "3 Day Select");
145 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
146 $CCSoption = new GoogleCarrierCalculatedShippingOption("20.99", "UPS", "Next Day Air", "0", "10", 'REGULAR_PICKUP');
147 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
149 $CCSoption = new GoogleCarrierCalculatedShippingOption("9.99", "USPS", "Media Mail", "0", "2", 'REGULAR_PICKUP');
150 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
151 $CCSoption = new GoogleCarrierCalculatedShippingOption("15.99", "USPS", "Parcel Post");
152 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
153 $CCSoption = new GoogleCarrierCalculatedShippingOption("18.99", "USPS", "Express Mail", "2.99", "10", 'REGULAR_PICKUP');
154 $Gshipping->addCarrierCalculatedShippingOptions($CCSoption);
156 $cart->AddShipping($Gshipping);
158 $ship_1 = new GoogleFlatRateShipping("Flat Rate", 5.0);
159 $restriction_1 = new GoogleShippingFilters();
160 $restriction_1->SetAllowedCountryArea("CONTINENTAL_48");
161 $ship_1->AddShippingRestrictions($restriction_1);
162 $cart->AddShipping($ship_1);
165 $tax_rule_1 = new GoogleDefaultTaxRule(0.0825);
166 $tax_rule_1->SetStateAreas(array("CA", "NY"));
167 $cart->AddDefaultTaxRules($tax_rule_1);
169 // Add International tax rules
170 $tax_rule_2 = new GoogleDefaultTaxRule(0.15);
171 $tax_rule_2->AddPostalArea("GB");
172 $tax_rule_2->AddPostalArea("FR");
173 $tax_rule_2->AddPostalArea("DE");
174 $cart->AddDefaultTaxRules($tax_rule_2);
176 // Define rounding policy
177 $cart->AddRoundingPolicy("HALF_UP", "PER_LINE");
181 // echo htmlentities($cart->GetXML());
184 // Display Google Checkout button
185 echo $cart->CheckoutButtonCode("LARGE");
188 function UseCase1() {
189 // Create a new shopping cart object
190 $merchant_id = ""; // Your Merchant ID
191 $merchant_key = ""; // Your Merchant Key
192 $server_type = "sandbox";
194 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
196 // Add items to the cart
197 $item_1 = new GoogleItem("MegaSound 2GB MP3 Player", // Item name
198 "Portable MP3 player - stores 500 songs", // Item description
200 175.49); // Unit price
201 $item_2 = new GoogleItem("AA Rechargeable Battery Pack",
202 "Battery pack containing four AA rechargeable batteries",
204 11.59); // Unit price
205 $cart->AddItem($item_1);
206 $cart->AddItem($item_2);
208 // Add US shipping options
209 $ship_1 = new GoogleFlatRateShipping("UPS Ground", 5.0);
210 $restriction_1 = new GoogleShippingFilters();
211 $restriction_1->SetAllowedCountryArea("CONTINENTAL_48");
212 $ship_1->AddShippingRestrictions($restriction_1);
214 $ship_2 = new GoogleFlatRateShipping("UPS 2nd Day", 10.0);
215 $restriction_2 = new GoogleShippingFilters();
216 $restriction_2->SetAllowedStateAreas(array('fl', "CA", "AZ", "CO", "WA", "OR"));
217 $ship_2->AddShippingRestrictions($restriction_2);
219 // Add international shipping options
220 $ship_3 = new GoogleFlatRateShipping("Canada 3 Business Days", 5.0);
221 $restriction_3 = new GoogleShippingFilters();
222 $restriction_3->AddAllowedPostalArea("CA");
223 $restriction_3->SetAllowUsPoBox(false);
224 $ship_3->AddShippingRestrictions($restriction_3);
226 $ship_4 = new GoogleFlatRateShipping("Europe 3 Business Days", 10.0);
227 $restriction_4 = new GoogleShippingFilters();
228 $restriction_4->AddAllowedPostalArea("GB", "SW*");
229 $ship_4->AddShippingRestrictions($restriction_4);
231 $cart->AddShipping($ship_1);
232 $cart->AddShipping($ship_2);
233 $cart->AddShipping($ship_3);
234 $cart->AddShipping($ship_4);
237 $tax_rule_1 = new GoogleDefaultTaxRule(0.0825);
238 $tax_rule_1->SetStateAreas(array("CA", "NY"));
239 $cart->AddDefaultTaxRules($tax_rule_1);
241 // Add International tax rules
242 $tax_rule_2 = new GoogleDefaultTaxRule(0.15);
243 $tax_rule_2->AddPostalArea("GB");
244 $tax_rule_2->AddPostalArea("FR");
245 $tax_rule_2->AddPostalArea("DE");
246 $cart->AddDefaultTaxRules($tax_rule_2);
248 // Define rounding policy
249 $cart->AddRoundingPolicy("HALF_UP", "PER_LINE");
253 // echo htmlentities($cart->GetXML());
256 // Display Google Checkout button
257 echo $cart->CheckoutButtonCode("LARGE");
260 function UseCase2() {
261 // Create a new shopping cart object
262 $merchant_id = ""; // Your Merchant ID
263 $merchant_key = ""; // Your Merchant Key
264 $server_type = "sandbox";
266 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
268 // Add items to the cart
269 $item_1 = new GoogleItem("Dry Food Pack AA1453",
270 "A pack of highly nutritious dried food for emergency", 2, 24.99);
271 $item_1->SetTaxTableSelector("food");
273 $item_2 = new GoogleItem("MegaSound 2GB MP3 Player",
274 "Portable MP3 player - stores 500 songs", 1, 175.49);
275 $item_2->SetMerchantPrivateItemData(
276 new MerchantPrivateItemData(array("color" => "blue",
277 "weight" => "3.2")));
278 $item_2->SetMerchantItemId("Item#012345");
280 $cart->AddItem($item_1);
281 $cart->AddItem($item_2);
283 // Add shipping options
284 $ship_1 = new GoogleFlatRateShipping("Ground", 15);
285 $restriction_1 = new GoogleShippingFilters();
286 $restriction_1->SetAllowedWorldArea(true);
287 $ship_1->AddShippingRestrictions($restriction_1);
289 $ship_2 = new GooglePickup("Pick Up", 5);
291 $cart->AddShipping($ship_1);
292 $cart->AddShipping($ship_2);
294 // Add default tax rules
295 $tax_rule_1 = new GoogleDefaultTaxRule(0.17);
296 $tax_rule_1->AddPostalArea("GB", "SW*");
297 $tax_rule_1->AddPostalArea("FR");
298 $tax_rule_1->AddPostalArea("DE");
300 $tax_rule_2 = new GoogleDefaultTaxRule(0.10);
301 $tax_rule_2->SetWorldArea(true);
303 $cart->AddDefaultTaxRules($tax_rule_1);
304 $cart->AddDefaultTaxRules($tax_rule_2);
306 // Add alternate tax table
307 $tax_table = new GoogleAlternateTaxTable("food");
309 $tax_rule_1 = new GoogleAlternateTaxRule(0.05);
310 $tax_rule_1->AddPostalArea("GB");
311 $tax_rule_1->AddPostalArea("FR");
312 $tax_rule_1->AddPostalArea("DE");
314 $tax_rule_2 = new GoogleAlternateTaxRule(0.03);
315 $tax_rule_2->SetWorldArea(true);
317 $tax_table->AddAlternateTaxRules($tax_rule_1);
318 $tax_table->AddAlternateTaxRules($tax_rule_2);
320 $cart->AddAlternateTaxTables($tax_table);
322 // Add <merchant-private-data>
323 $cart->SetMerchantPrivateData(
324 new MerchantPrivateData(array("cart-id" => "ABC123")));
326 // Specify <edit-cart-url>
327 $cart->SetEditCartUrl("http://www.example.com/edit");
329 // Specify "Return to xyz" link
330 $cart->SetContinueShoppingUrl("http://www.example.com/continue");
332 // Request buyer's phone number
333 $cart->SetRequestBuyerPhone(true);
335 // Define rounding policy
336 $cart->AddRoundingPolicy("CEILING", "TOTAL");
340 // echo htmlentities($cart->GetXML());
343 // Display a medium size button
344 echo $cart->CheckoutButtonCode("MEDIUM");
347 function UseCase3() {
348 //Create a new shopping cart object
349 $merchant_id = ""; // Your Merchant ID
350 $merchant_key = ""; // Your Merchant Key
351 $server_type = "sandbox";
353 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
355 // Add items to the cart
356 $item = new GoogleItem("MegaSound 2GB MP3 Player",
357 "Portable MP3 player - stores 500 songs", 1, 175.49);
358 $item->SetMerchantPrivateItemData("<color>blue</color><weight>3.2</weight>");
359 $cart->AddItem($item);
361 // Add merchant calculations options
362 $cart->SetMerchantCalculations(
363 "http://200.69.205.154/~brovagnati/tools/unitTest/demo/responsehandlerdemo.php", // merchant-calculations-url
364 "false", // merchant-calculated tax
365 "true", // accept-merchant-coupons
366 "true"); // accept-merchant-gift-certificates
368 // Add merchant-calculated-shipping option
369 $ship = new GoogleMerchantCalculatedShipping("2nd Day Air", // Shippping method
370 10.00); // Default, fallback price
372 $restriction = new GoogleShippingFilters();
373 $restriction->AddAllowedPostalArea("GB");
374 $restriction->AddAllowedPostalArea("US");
375 $restriction->SetAllowUsPoBox(false);
376 $ship->AddShippingRestrictions($restriction);
378 $address_filter = new GoogleShippingFilters();
379 $address_filter->AddAllowedPostalArea("GB");
380 $address_filter->AddAllowedPostalArea("US");
381 $address_filter->SetAllowUsPoBox(false);
382 $ship->AddAddressFilters($address_filter);
384 $cart->AddShipping($ship);
386 // Set default tax options
387 $tax_rule = new GoogleDefaultTaxRule(0.15);
388 $tax_rule->SetWorldArea(true);
389 $cart->AddDefaultTaxRules($tax_rule);
391 $cart->AddRoundingPolicy("UP", "TOTAL");
395 // echo htmlentities($cart->GetXML());
398 // Display a disabled, small button
399 echo $cart->CheckoutButtonCode("SMALL", false);