OneShopNotificationListener.pl

From Developer's API

use Library::OneShopAPI;
use strict;
use CGI qw(:standard);

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*([^<>\s]+)\s*<\/Token>\s*<\/\1>/)
	{
		$current_return->{type} = $1;
		$current_return->{token} = $2;
	}

	return($current_return);
}

# retrieve xml response
my $current_xml_response = read_raw_post();
# retrieve notification type and token from xml response
my $current_return = lex_response($current_xml_response);

# open simple log for troubleshooting purposes
open(OSCLOG, ">>oneshop_notifications.log");

# perform action according to type
if($current_return->{type} eq "NewOrder")
{
	# perform action with the new order notification	
	print OSCLOG join(' ',localtime(time)) . " : $current_return->{type} notification received with token $current_return->{token}.\n";
}
elsif($current_return->{type} eq "OtherNotification")
{
	# perform action with the other notification
	
	print OSCLOG join(' ',localtime(time)) . " : $current_return->{type} notification received with token $current_return->{token}.\n";
}
else
{
	# script received invalid post information
	print OSCLOG join(' ',localtime(time)) . " : invalid post information received. \"$current_xml_response\"\n";
}	
close(OSCLOG);