#!/usr/local/bin/perl # # Install (a modified version of) this program in your webserver's cgi-bin directory. # # This demo program prints the order into /tmp/yahoo-order; you will probably want # to do something more interesting with it. Replace the function handle_order # It also puts the raw key-value fields into /tmp/yahoo-order.raw # # Order fields are: # # ID # A unique order identifier including the Y! Store account name, such as acme-451 # # Date # Standard date format, in GMT # # {Ship,Bill}-Name, -Firstname, -Lastname # These fields are always defined. If your store is configured to have separate first and last # name fields on the order page, then -Name will be the concatenation of them. If it is configured to have # a single entry field, then the split into Firstname and Lastname will be a guess. # # If you have custom fields, they will appear here too. The field names look like "Ship-Pack in dry ice" # for an extra shipping field, and similarily for a billing field. # # {Ship,Bill}-Address1, -Address2, -City, -State, -Zip, -Country, -Phone, -Email # The shipping and billing address. Both will be filled in, and will be the same if the # user only gave one address # # Card-Name, -Number, -Expiry # Credit card info # # 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 # # Tax-Charge, Shipping-Charge, Total # Extra charges, and the order total # require 5.005; use strict; use CGI; my $q = new CGI(); if ($q->request_method() ne "POST") { bail("Expecting a POST, bailing", '501 Not Implemented'); } my %o = $q->Vars(); 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)}); } if ( !open(RAW,'>','/tmp/yahoo-order.raw') ) { bail("opening /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 $q->header(-status => '200 OK'); exit(0); sub bail { my($msg, $status)=@_; $status ||= '500 Internal Server Error'; print $q->header(-status => $status); exit(-1); } ###################################################################### # Replace this function with something that does what you want # $info gets a hash ref of all the order fields, like Ship-Name. # @items gets an array of hash refs, one for each item. sub handle_order { my($info, @items)=@_; if ( !open(OUT,'>', '/tmp/yahoo-order') ) { bail("opening /tmp/yahoo-order: $!"); } print OUT "Order $info->{ID} at $info->{Date}\n"; print OUT "\n"; print OUT "Ship to:\n"; print OUT " $info->{'Ship-Name'}\n"; print OUT " $info->{'Ship-Address1'}\n"; print OUT " $info->{'Ship-Address2'}\n"; print OUT " $info->{'Ship-City'} $info->{'Ship-State'} $info->{'Ship-Zip'}\n"; print OUT " $info->{'Ship-Country'}\n"; print OUT " $info->{'Ship-Phone'}\n"; print OUT "\n"; print OUT "Bill to:\n"; print OUT " $info->{'Bill-Name'}\n"; print OUT " $info->{'Bill-Address1'}\n"; print OUT " $info->{'Bill-Address2'}\n"; print OUT " $info->{'Bill-City'} $info->{'Bill-State'} $info->{'Bill-Zip'}\n"; print OUT " $info->{'Bill-Country'}\n"; print OUT " $info->{'Bill-Phone'}\n"; print OUT " $info->{'Bill-Email'}\n"; print OUT "\n"; print OUT "Shipping: $info->{'Shipping'}\n"; print OUT "\n"; print OUT "Payment: $info->{'Card-Name'} $info->{'Card-Number'}, exp $info->{'Card-Expiry'}\n"; print OUT "\n"; print OUT "Items:\n"; printf(OUT " %-15s %-40s %6s %8s %8s\n", "Code","Desc","Qty","Each","Total"); my $item; for $item (@items) { printf(OUT " %-15s %-40s %6d %8.2f %8.2f\n", $item->{'Code'}, $item->{'Description'}, $item->{'Quantity'}, $item->{'Price'}, $item->{'Quantity'} * $item->{'Price'}); } printf(OUT " %-15s %-40s %6s %8s %8.2f\n", "","Tax","","",$info->{'Tax-Charge'}); printf(OUT " %-15s %-40s %6s %8s %8.2f\n", "","Shipping","","",$info->{'Shipping-Charge'}); printf(OUT " %-15s %-40s %6s %8s %8.2f\n", "","Total","","",$info->{'Total'}); close(OUT); }