OneShopNotificationListener.pl

From Developer's API

(Difference between revisions)
m (Updated OneShopNotificationListener.pl)
m (Changed Token RegEx Group From \d+ to [^<>\s]+)
 
(One intermediate revision by one user not shown)
Line 3: Line 3:
 
use strict;
 
use strict;
 
use CGI qw(:standard);
 
use CGI qw(:standard);
 
print "Content-Type: text/html\n\n";
 
  
 
sub read_raw_post {
 
sub read_raw_post {
Line 25: Line 23:
 
my ($current_xml_response) = @_;
 
my ($current_xml_response) = @_;
 
my $current_return = {type => "",token => ""};
 
my $current_return = {type => "",token => ""};
if($current_xml_response =~ /<(\w+)>\s*<Token>\s*(\d)\s*<\/Token>\s*<\/\1>/)
+
if($current_xml_response =~ /<(\w+)>\s*<Token>\s*([^<>\s]+)\s*<\/Token>\s*<\/\1>/)
 
{
 
{
 
$current_return->{type} = $1;
 
$current_return->{type} = $1;
Line 42: Line 40:
 
open(OSCLOG, ">>oneshop_notifications.log");
 
open(OSCLOG, ">>oneshop_notifications.log");
  
#perform action according to type
+
# perform action according to type
 
if($current_return->{type} eq "NewOrder")
 
if($current_return->{type} eq "NewOrder")
 
{
 
{
 
# perform action with the new order notification
 
# perform action with the new order notification
print OSCLOG join(' ',localtime(time)) . " : new order notification received.\n";
+
print OSCLOG join(' ',localtime(time)) . " : $current_return->{type} notification received with token $current_return->{token}.\n";
 
}
 
}
 
elsif($current_return->{type} eq "OtherNotification")
 
elsif($current_return->{type} eq "OtherNotification")
 
{
 
{
 
# perform action with the other notification
 
# perform action with the other notification
print OSCLOG join(' ',localtime(time)) . " : other notification received.\n";
+
 +
print OSCLOG join(' ',localtime(time)) . " : $current_return->{type} notification received with token $current_return->{token}.\n";
 
}
 
}
 
else
 
else
 
{
 
{
 
# script received invalid post information
 
# script received invalid post information
print OSCLOG join(' ',localtime(time)) . " : invalid post information received.\n";
+
print OSCLOG join(' ',localtime(time)) . " : invalid post information received. \"$current_xml_response\"\n";
 
}
 
}
 
close(OSCLOG);
 
close(OSCLOG);
 
</pre>
 
</pre>

Latest revision as of 15:25, 30 May 2008

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);