OneShopNotificationListener.pl

From Developer's API

Revision as of 13:55, 28 May 2008 by Jasonm (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
use Library::OneShopAPI;
use strict;
use CGI qw(:standard);

print "Content-Type: text/html\n\n";

sub read_raw_post {
    my $current_length = $ENV{'CONTENT_LENGTH'};
    my $current_remaining = 0;
    my $current_buffer;

    while($current_remaining < $current_length) {
        my $current_read = read(STDIN, $current_buffer, $current_length - $current_remaining, $current_remaining);
        die("Unable to read from stream : $!") unless defined($current_read);
        return $current_buffer if $current_read == 0;
        $current_remaining += $current_read;
    }

    return $current_buffer;
}

sub lex_response
{
	my ($current_xml_response) = @_;
	my $current_return = {type => "",token => ""};
	if($current_xml_response =~ /<(\w+)>\s*<Token>\s*(\d)\s*<\/Token>\s*<\/\1>/)
	{
		$current_return->{type} = $1;
		$current_return->{token} = $2;
	}

	return($current_return);
}

my $current_xml_response = read_raw_post();
my $current_return = lex_response($current_xml_response);

if($current_return->{type} eq "NewOrder")
{
	# perform action with the new order notification
}
elsif($current_return->{type} eq "OtherNotification")
{
	# perform action with the other notification
}
else
{
	# script received invalid post information
}