1#!/usr/bin/perl -w
2use strict;
3
4use Google::Checkout::General::GCO;
5use Google::Checkout::General::MerchantItem;
6use Google::Checkout::General::ShoppingCart;
7use Google::Checkout::XML::CheckoutXmlWriter;
8use Google::Checkout::General::MerchantCheckoutFlow;
9use Google::Checkout::General::ShippingRestrictions;
10use Google::Checkout::General::Pickup;
11use Google::Checkout::General::FlatRateShipping;
12use Google::Checkout::General::MerchantCalculatedShipping;
13use Google::Checkout::General::TaxRule;
14use Google::Checkout::General::TaxTable;
15use Google::Checkout::General::TaxTableAreas;
16use Google::Checkout::General::MerchantCalculations;
17use Google::Checkout::General::ParameterizedUrls;
18
19use Google::Checkout::XML::Constants;
20use Google::Checkout::General::Util qw/is_gco_error/;
21
22#--
23#-- This example is the same as example 2 except it doesn't actuall
24#-- perform a checkout. Instead, it prints out the XML, signature,
25#-- etc. This gives the user a chance to manually inspect the XML
26#-- generated. Great for debug!
27#--
28
29my $config = $ARGV[0] || "../conf/GCOSystemGlobal.conf";
30
31my $gco = Google::Checkout::General::GCO->new(config_path => $config);
32
33#--
34#-- Create some shipping restrictions. The followings says we
35#-- can ship to CA (the state).
36#--
37my $restriction = Google::Checkout::General::ShippingRestrictions->new(
38                  allowed_state => ["CA"]);
39
40#--
41#-- Create a custom shipping method with the above
42#-- shipping restriction for a total of $45.99
43#--
44my $custom_shipping = Google::Checkout::General::MerchantCalculatedShipping->new(
45                      price         => 45.99,
46                      restriction   => $restriction,
47                      shipping_name => "custom shipping");
48
49#--
50#-- Create 2 more shipping methods: One for pickup
51#-- and the other flat rate shipping for $19.99. Notice
52#-- that it's common for the pickup method to not
53#-- include a price tag. It will be defaulted to 0 if
54#-- it's not supplied
55#--
56my $pickup_shipping    = Google::Checkout::General::Pickup->new(shipping_name => "Pickup");
57my $flat_rate_shipping = Google::Checkout::General::FlatRateShipping->new(
58                         shipping_name => "Flat rate UPS",
59                         price         => 19.99);
60
61#--
62#-- Now are are creating a tax rule. We set shipping
63#-- tax to the full 50 US states.
64#--
65my $tax_rule1 = Google::Checkout::General::TaxRule->new(
66                shipping_tax => 1,
67                rate => 0.025,
68                area => Google::Checkout::General::TaxTableAreas->new(
69                        country => [Google::Checkout::XML::Constants::FULL_50_STATES]));
70
71#--
72#-- We create another tax rule but we tell Checkout that shipping isn't taxable
73#--
74my $tax_rule2 = Google::Checkout::General::TaxRule->new(
75                shipping_tax => 0,
76                rate => 8.87,
77                area => [Google::Checkout::General::TaxTableAreas->new(state => ['NY'])]);
78
79#--
80#-- Create yet another tax rule similar to the first one
81#--
82my $tax_rule3 = Google::Checkout::General::TaxRule->new(
83                shipping_tax => 1,
84                rate => 0.025,
85                area => Google::Checkout::General::TaxTableAreas->new(
86                        country => [Google::Checkout::XML::Constants::FULL_50_STATES]));
87
88#--
89#-- Now we have 3 tax rules created, we need to create
90#-- a tax table to hold them. Notice that we only add
91#-- rule1 and rule3 to the table but discarded rule2. Also
92#-- notice that default is set to 1. This tell Checkout that
93#-- this is the default tax table
94#--
95my $tax_table1 = Google::Checkout::General::TaxTable->new(
96                 default => 1,
97                 rules => [$tax_rule1, $tax_rule3]);
98
99#--
100#-- We create another tax table with the name 'item'.
101#-- This is not a default table but we can reference
102#-- it using it's name
103#--
104my $tax_table2 = Google::Checkout::General::TaxTable->new(
105                 default => 0,
106                 name => "taxtable",
107                 standalone => 1,
108                 rules => [$tax_rule2]);
109
110#--
111#-- A merchant calculations object tells Checkout that we want to calculate
112#-- the shipping expense using a custom algorithm. The URL specify
113#-- the address that Checkout should call when it needs to find out the
114#-- shipping expense. We also specify that users can apply coupons and
115#-- gift certificates to the shipping cost
116#--
117my $merchant_calculation = Google::Checkout::General::MerchantCalculations->new(
118                             url => "http://callback/url",
119                             coupons => 1,
120                             certificates => 1);
121
122#--
123#-- Create a parameterized URL object so we can track the order
124#--
125my $purls = Google::Checkout::General::ParameterizedUrls->new(
126            url => 'http://www.yourcompany.com/tracking?parter=123&partnerName=Company',
127            url_params => {orderID => 'order-id', totalCost => 'order-total'});
128
129#--
130#-- Add a couple more params
131#--
132$purls->set_url_param(taxes => 'tax-amount');
133$purls->set_url_param(shipping => 'shipping-amount');
134
135#--
136#-- Now it's time to create the checkout flow.
137#-- This particular checkout flow only supports the flat rate
138#-- shipping method (you can add more). Edit cart and continue
139#-- shopping URL specify 2 addresses: one for editing the cart
140#-- and another for when the user click the continue shopping link.
141#-- The 2 tax tables (created above) is added and we tell Checkout what
142#-- we are interested in calculating our own shipping expense with
143#-- our own calculation. The buyer's phone number is also added
144#--
145my $checkout_flow = Google::Checkout::General::MerchantCheckoutFlow->new(
146                    shipping_method       => [$flat_rate_shipping],
147                    edit_cart_url         => "http://edit/cart/url",
148                    continue_shopping_url => "http://continue/shopping/url",
149                    buyer_phone           => "1-111-111-1111",
150                    tax_table             => [$tax_table1,$tax_table2],
151                    merchant_calculation  => $merchant_calculation,
152		    analytics_data        => "SW5zZXJ0IDxhbmFseXRpY3MtZGF0YT4gdmFsdWUgaGVyZS4=",
153                    parameterized_url     => $purls);
154
155#--
156#-- Once the merchant checkout flow is created, we can create the shopping
157#-- cart. The cart includes the checkout flow created above, it will expire
158#-- in 1 month and we include a private message in the cart
159#--
160my $cart = Google::Checkout::General::ShoppingCart->new(
161           expiration    => "+1 month",
162           private       => "Any private data you want",
163           checkout_flow => $checkout_flow);
164
165#--
166#-- Now we create a merchant item.
167#--
168my $item = Google::Checkout::General::MerchantItem->new(
169           name               => "Fish",
170           description        => "A fish" ,
171           price              => 12.34,
172           quantity           => 12,
173           private            => "gold",
174           tax_table_selector => $tax_table2->get_name());
175
176#--
177#-- We can the item to the cart
178#--
179$cart->add_item($item);
180
181#--
182#-- Add another item to the cart
183#--
184$cart->add_item(Google::Checkout::General::MerchantItem->new(
185                name               => "Coral",
186                description        => "A coral",
187                price              => 99.99,
188                quantity           => 1,
189                private            => "green",
190                tax_table_selector => $tax_table2->get_name()));
191
192#--
193#-- Get the signature and XML cart
194#--
195my $data = $gco->get_xml_and_signature($cart);
196
197#--
198#-- Print the XML and signature
199#--
200print "URL:       ",$gco->get_checkout_url,"\n",
201      "Raw XML:   $data->{raw_xml}\n",
202      "Key:       $data->{raw_key}\n",
203      "Signature: $data->{signature}\n",
204      "XML cart:  $data->{xml}\n";
205