my $ua = LWP::UserAgent->new;
$ua->env_proxy;
my $response = $ua->post($GATEWAY_URL, {
# add other parameters as necessary
'USER' => 'myid',
'VENDOR' => 'myid',
'PARTNER' => 'partner',
'TRXTYPE' => 'S',
'AMT' => '$paymentamount',
'PWD' => 'mypass',
'CREATESECURETOKEN' => 'Y',
'SECURETOKENID' => $sec_token_id } );
print "Response: $response
";
####
sub Get_Token {
use LWP::UserAgent;
use LWP::Protocol::https;
#my $GATEWAY_URL = 'https://payflowpro.paypal.com'; # For LIVE transactions
my $GATEWAY_URL = 'https://pilot-payflowpro.paypal.com'; # For TEST transactions
my $random_string=&Random_String(36);
$sec_token_id = $random_string;
#print "Random string: ".$random_string."\n";
print "Random Token Sent: ".$sec_token_id."
";
#print "Length: ".length($random_string)."
";
# send http post request to gateway
my $ua = LWP::UserAgent->new;
$ua->env_proxy;
my $response = $ua->post($GATEWAY_URL, {
# add other parameters as necessary
'USER' => 'myid',
'VENDOR' => 'myid',
'PARTNER' => 'partner',
'TRXTYPE' => 'S',
'AMT' => '$paymentamount',
'PWD' => 'mypass',
'CREATESECURETOKEN' => 'Y',
'SECURETOKENID' => $sec_token_id } );
print "Response Unhashed: $response
";
unless ($response->is_success) {
# request failed
print "Error: Problem with secure token. Please contact the admin.";
}
# parse server response
my $resdec = $response->decoded_content;
chomp $resdec;
# keys and values into %reshash
my @resdata = split(/\&/, $resdec);
my %reshash;
foreach (@resdata) {
my ($key,$val) = split(/=/, $_, 2);
$key =~ s/\[\d+\]//; # remove length of SECURETOKEN
$reshash{$key} = $val;
}
# validate response
unless (defined $reshash{'SECURETOKENID'} &&
defined $reshash{'SECURETOKEN'} &&
defined $reshash{'RESULT'} &&
defined $reshash{'RESPMSG'} &&
$reshash{'SECURETOKENID'} eq $sec_token_id &&
$reshash{'RESULT'} eq '0' &&
$reshash{'RESPMSG'} eq 'Approved') {
# bad response
print "Response from Gateway: $resdec
";
#print "Error: Cannot retrieve secure token. Please contact the admin.
";
}
# result: secure token
my $sec_token = $reshash{'SECURETOKEN'};
my $sec_token_id = $reshash{'SECURETOKENID'};
}
########
sub Random_String {
my $length_of_randomstring=shift;# the length of
# the random string to generate
my @chars=('a'..'z','A'..'Z','0'..'9');
my $random_string;
foreach (1..$length_of_randomstring)
{
# rand @chars will generate a random
# number between 0 and scalar @chars
$random_string.=$chars[rand @chars];
}
return $random_string;
}
#Generate the random string