#!/usr/local/bin/perl # # Install (a modified version of) this program in your webserver's cgi-bin directory. # # This demo program calculates tax & shipping for an order # # Relevant Order fields are: # # {Ship,Bill}-Address1, -Address2, -City, -State, -Zip, -Country # The shipping and billing address. Both will be filled in, and will be the same if the # user only gave one address # # Item-Id-N, -Code-N, -Quantity-N, -Unit-Price-N, -Description-N, -Url-N, # For values of N from 1 to Item-Count, the relevant attributes of each item are given. # This script contains code to separate these into an @items array. # Code is from the "Code" field when editing the item, typically an SKU or ISBN # Unit-Price takes any quantity pricing into account # require 5.001; use strict; if ($ENV{'REQUEST_METHOD'} ne "POST") { die("Expecting a POST, bailing"); } my $o; read(STDIN,$o,$ENV{'CONTENT_LENGTH'}); my %o; for (split(/&/,$o)) { $_ =~ s/\+/ /g; my($key,$val) = split(/=/,$_,2); for ($key,$val) { $_ =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/ge; } $o{$key} = $val; } # Unpack the named item fields into an array of items # The fields are Item-Id-1, Item-Code-1, Item-Quantity-1, Item-Unit-Price-1, Item-Description-1, Item-Url-1 # and the second item is Item-Code-2, ... # Item-Count gives the total number. my @items; my $i; for ($i=1; $i<=$o{'Item-Count'}; $i++) { push(@items,{map {($_,$o{"Item-$_-$i"})} qw(Id Code Quantity Unit-Price Description Url)}); } open(RAW,">/tmp/yahoo-order.raw"); for (sort keys %o) { print RAW "$_ = $o{$_}\n"; } close(RAW); &handle_order(\%o,@items); # A successful delivery is indicated by a good HTTP result code. print "Status: 200 OK\n"; print "\n"; exit(0); ###################################################################### # Replace this function with something that does what you want # $info gets a hash ref of all the order fields, like Ship-Zip. # @items gets an array of hash refs, one for each item. sub handle_order { my($info,@items)=@_; my $tax; my $shipping; if ($info->{'Ship-Country'} eq 'US United States' && $info->{'Ship-State'} eq 'CA') { $tax = 0.085 * $info->{'Taxable-Total'}; } print "Tax-Charge: $tax\n"; if ($info->{'Ship-Country'} eq 'US United States' && $info->{'Ship-State'} eq 'GU' && $info->{'Shipping'} eq 'UPS') { print "Shipping-Error: Cannot ship UPS outside contintal US\n"; } else { if ($info->{'Total'} > 100) { $shipping=$info->{'Total'} * 0.10; } else { $shipping=10.00; } print "Shipping-Charge: $shipping\n"; } }