Invalid request parameter in Paypal API - perl

Hi I just picked up the code sample for perl and tried to do a payment to my sandbox developer test account with the following
sub promote {
my $headers = HTTP::Headers->new(
'X-PAYPAL-SECURITY-USERID'=> 'alexjaquet-facilitator_api1.gmail.com',
'X-PAYPAL-SECURITY-PASSWORD' => 'xxxxx',
'X-PAYPAL-SECURITY-SIGNATURE' => 'An5ns1Kso7MWUdW4ErQKJJJ4qi4-
3fjxb5G1RfUPmQ5kIdwdsH7znTJ',
'X-PAYPAL-SERVICE-VERSION' => '1.1.0',
'X-PAYPAL-APPLICATION-ID' => 'APP-80W284485P519543T',
'X-PAYPAL-REQUEST-DATA-FORMAT' => 'NV',
'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'NV');
my $content = sprintf("%s&%s&%s&%s&%s&%s&%s&%s&%s&%s&%s&%s&%s&%s&%s",
'senderEmail=alexjaquet-facilitator#gmail.com',
'actionType=PAY',
'currencyCode=USD',
'feesPayer=EACHRECEIVER',
'receiverList.receiver(0).email=alexjaquet-facilitator#gmail.com ',
'receiverList.receiver(0).primary=false',
'receiverList.receiver(0).amount=20.00',
'returnUrl=http:myreturnurl',
'cancelUrl=http:mycancelurl',
'requestEnvelope.errorLanguage=en_US',
'clientDetails.ipAddress=127.0.0.1',
'clientDetails.deviceId=mydevice',
'clientDetails.applicationId=PayNvpDemo');
my $req = HTTP::Request->new($method,'https://svcs.sandbox.paypal.com/AdaptivePayments
/Pay',$headers, $content);
my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);
if ($res->is_success) {
print "Content-Type: text/html\n\n";
print $res->content;}
else {
print "Content-Type: text/html\n\n";
print $res->status_line, "\n";}
}
but the response I get he's the following :
error(0).domain=PLATFORM&error(0).subdomain=Application&error(0).severity=Error&error(0).category=Application&error(0).message=Invalid+request+parameter%3A+email+alexjaquet-facilitator%40gmail.com++is+invalid&error(0).parameter(0)=email&error(0).parameter(1)=alexjaquet-facilitator%40gmail.com+

Related

Perl Why doesn't POST work when the variable is set by <> rather than directly assigned?

When I directly assign a name to the variable $connection_name, this script works, but I would like to take a user input and assign that to the variable. When I do that, it doesn't work. I get an error 400 bad request.
#!/usr/bin/perl
use strict;
use warnings;
use LWP;
my $ua = LWP::UserAgent->new;
my $server_endpoint = "XXXX"
# Get name for connection
print "Connection Name?";
my $connection_name= <>;
print $connection_name;
# HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');
# POST data in the HTTP request body
my $post_data = "{
\"name\":\"$connection_name\",
\"origin_country\":\"us\",
\"datasource_type\":\"cfdcb449-1204-44ba-baa6-9a8a878e6aa7\"
}"
$req->content($post_data);
my $resp = $ua->request($req);
if ($resp->is_success) {
my $message = $resp->decoded_content;
print "Received reply: $message\n";
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
}
When you hardcoded $connection_name, you probably didn't include a line feed as you do now. Add
chomp($connection_name);
And please use a proper JSON generator.
use JSON::XS qw( encode_json );
my $post_data = encode_json({
name => $connection_name,
origin_country => "us",
datasource_type => "cfdcb449-1204-44ba-baa6-9a8a878e6aa7",
});

Print http request response in JSON format

#!/usr/bin/perl
use strict;
use warnings;
use JSON qw(decode_json);
use JSON;
use LWP::UserAgent;
use HTTP::Request;
#GET_METHOD
my $usagnt_get = LWP::UserAgent->new;
my $server_end_point_get = "http://192.168.201.1:8000/c/r";
my $reqst_get = HTTP::Request->new( GET => $server_endpoint_get );
$reqst_get->header( 'content-type' => 'application/json' );
#Request User Agent
my $respnse_get = $usagnt->request( $reqst_get );
if ( $resp_get->is_success ) {
my $message = $respnse_get->decoded_content;
print "\n Received GET Response:\n$res_message\n";
print "\n****** GET operations SUCCESS..!\n";
}
else {
print "HTTP_GET error code:", $respnse_get->code, "\n";
print "HTTP_GET error message:", $respnse_get->res_message, "\n";
}
Please help me to get output with JSON format of ie requesting method with HTTP req and get method is capturing with all the projects list in getting method.
# Here is the successfully compiled code
!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $token="";
my $uri = 'http://xxx.xxx.xxx.xxx:8000/a/b';
my $json => '{"username":"user","password":"pwd"}';
my $req = HTTP::Request->new('POST', $uri );
$request->header( 'Content-Type' => 'application/json');
$request->content( $json );
my $lwp = LWP::UserAgent->new;
my $message = $lwp->request( $request );
if ( $message->is_success ) {
my $token= $message->content;
print "\n Received POST Response:\n$token\n";
} else {
print "error code:", $message->code,"\n";
print "error message:", $message->as_string(), "\n";
}

How to handle HTTP Post Errors

An error displays when I run my Perl script. It says, Internal Server Error. Error Code 500. How can I handle this?
use warnings;
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $url = "https://myhost/test/server.php";
my $req = HTTP::Request->new(POST => $url);
$req->header('content-type' => 'application/json');
my $post_data = {"value": "sample", "value2":"sample2"};
$req->content($post_data);
#print $req->as_string;
my $resp = $ua->request($req);
if ($resp->is_success) {
my $message = $resp->decoded_content;
print "Received reply: $message\n";
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
}
This is not valid perl:
my $post_data = {"value": "sample", "value2":"sample2"};
You need to enclose your the value in single quotes:
my $post_data = '{"value": "sample", "value2":"sample2"}';
Alternatively, you can start with a perl hash, and build the json string using JSON:
use JSON;
my %hash = ( value => 'sample', value2 => 'sample2' );
my $post_data = to_json( \%hash );

Twitter OAuth 1.0 authentication with signature in perl

I'm getting this problem with my app in perl for oauth authentication:
401 Unauthorized Failed to validate oauth signature and token
Here is my code:
sub Twitter {
my $IN = new CGI;
my $qs = build_query({
oauth_callback => $callback_url,
oauth_consumer_key => $consumer_key,
oauth_nonce => time,
oauth_signature_method => "HMAC-SHA1",
oauth_timestamp => time,
oauth_version => "1.0"
});
# Create Signature
my $signing_key = $IN->escape($consumer_secret)."&";
my $base_signature = "POST&".$IN->escape($request_token_url)."&".$qs;
use Digest::HMAC_SHA1;
my $hmac = Digest::HMAC_SHA1->new($signing_key);
$hmac->add($base_signature);
$qs .= "&oauth_signature=".$IN->escape($hmac->b64digest);
# Fetch the page
use LWP;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => $request_token_url);
$req->content_type('application/x-www-form-urlencoded');
$req->content($qs);
my $res = $ua->request($req);
# Check the outcome of the response
unless ($res->is_success) {
print $IN->header.$res->status_line, "\n";
print $res->content;
exit;
}
print $IN->header.$res->content;
}
sub build_query {
my $input = shift;
use URI;
my $uri = URI->new;
$uri->query_form($input);
return $uri->query;
}
I have obviously deleted my callback url and key information.
I figured it out. I was encoding the signature wrong, I had to sort my query strings, and the call back URL is not needed in this instance. Here is my working code:
sub Twitter {
my $IN = new CGI;
my $params = {
oauth_consumer_key => $consumer_key,
oauth_nonce => time,
oauth_signature_method => "HMAC-SHA1",
oauth_timestamp => time,
oauth_version => "1.0"
};
my $qs = build_sorted_query($params);
my $signing_key = $IN->escape($consumer_secret)."&";
my $signature_base = "POST&".$IN->escape($request_token_url)."&".$IN->escape($qs);
use Digest::HMAC_SHA1;
use MIME::Base64;
my $hmac = Digest::HMAC_SHA1->new($signing_key);
$hmac->add($signature_base);
$params->{oauth_signature} = $IN->escape(encode_base64($hmac->digest));
$qs = build_sorted_query($params);
use LWP;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => $request_token_url);
$req->content_type('application/x-www-form-urlencoded');
$req->content($qs);
my $res = $ua->request($req);
# Check the outcome of the response
unless ($res->is_success) {
print $IN->header.$res->status_line, "\n";
print $res->content;
exit;
}
print $IN->header.$res->content;
return;
}
sub build_sorted_query {
my $input = shift;
my $qs;
foreach (sort keys %$input) {
$qs .= $_."=".$input->{$_}."&";
}
return substr ($qs, 0, -1);
}
Thanks for looking!

Verif Receipt with perl script

I have to use a perl Script to interface to verifReceipt for in App Purchase on iOS.
I use too phoneGap 1.5.0
1/ When I try to use external url it does not work.
2/ I try to use a perl script interface between phonegap html page and itunes.
I do not understand how to send data. I try this :
$val contains JSON object with receiptvalue and password, receiving from phonegap.
my %transformfields = (
"receipt-data" => $val,
);
my $ua = LWP::UserAgent->new;
my $req = POST($urlsend,
Content_Type => 'application/json ; charset=UTF-8',
Content => \%transformfields,
#Content => $val,
);
my $res = $ua->request($req);
I have always this error : 21002 java.lang.NullPointerException
I do not understand how is this possible to send data with perl script.
I have problems with json encode and understanding how I receive data from phonegap plugins.
For help :
Phongap plugins returns transactionReceipt in Base64.
Do not encode password in Base64 too.
my %transformfields = ("receipt-data" => $val , "password" => $sharedsecretpass ) ;
my $jsonval = encode_json (\%transformfields);
print "Content-Type: text/html \n\n";
#my $urlsend="https://buy.itunes.apple.com/verifyReceipt" ;
my $urlsend="https://sandbox.itunes.apple.com/verifyReceipt" ;
my $req = POST($urlsend,
Content_Type => 'application/x-www-form-urlencoded ; charset=utf-8',
Content => $jsonval,
);
my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);
if ($res->is_success)
{
#print "cgi Content : " . $res->content . "<br />\n";
my $retour = decode_json( $res->content );
if ($retour->{'status'} eq "0")
{
print "--OK--<br />\n";
print $retour->{receipt}->{product_id} . "<br />";
print $retour->{receipt}->{transaction_id} . "<br />";
print $retour->{receipt}->{purchase_date} . "<br />";
print $retour->{receipt}->{expires_date} . "<br />";
#$res->content
}