IPN code samples
PERL version
use strict;
use CGI;
use Digest::SHA qw(hmac_sha256_hex);
# modify your settings here
# pass to compute HASH. Retrieve your secret key by accessing https://secure.2checkout.com/cpanel/webhooks_api.php
my $myKey='AABBCCDDEEFF';
# / end modifications area
my $q = new CGI;
my @name = $q->param;
print "Content-type: text/html\n\n";
my $in_sha256='';
my $first_product = '';
my $first_pid = '';
# Main loop
foreach my $par (@name) {
next if ($par=~/HASH|SIGNATURE_SHA2_256|SIGNATURE_SHA3_256/i);
if ($q->param($par) ne '') { #parameter not empty
my @myList = $q->param($par);
if (scalar(@myList)) { #multi valued
foreach my $el (@myList) {
$in_sha256.=length($el).$el;
if ($par eq "IPN_PNAME[]") { $first_product = $myList[0]; }
if ($par eq "IPN_PID[]") { $first_pid = $myList[0]; }
}
} else { #single valued
$in_sha256.=length($q->param($par)).$q->param($par);
}
} else { #empty parameter
$in_sha256.='0';
}
}
my $mySha = $q->param('SIGNATURE_SHA2_256');
my $shaVerify = hmac_sha256_hex($in_sha256, $myKey);
my $response='';
if ($mySha=~/^$shaVerify$/i) { # ok
# The notification is genuine / verified
# Write your own code here to insert the data into a database or email it in a specified format
# The code below will confirm the request
$response = length($first_pid).$first_pid.length($first_product).$first_product.length($q->param("IPN_DATE")).$q->param("IPN_DATE");
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
my $now = (1900+$year).sprintf("%02d", $mon+1).sprintf("%02d", $mday).sprintf("%02d", $hour).sprintf("%02d", $min).sprintf("%02d", $sec);
$response.=length($now).$now;
my $shaResponse = hmac_sha256_hex($response, $myKey);
print '<sig algo="sha256" date="' . $now . '">' . $shaResponse . '</sig>';
print "\n";
} else { # not ok
print '<EPAYMENT>Error. Cannot verify signature.</EPAYMENT>';
print "\n";
}C#
C# - IPN Signature Handler Test Example
Last updated
Was this helpful?