我正在尝试弄清楚为什么我的Curl请求不适用于PayPal API。我有以下查询正常工作以获取身份验证令牌:
sub get_token {
my $curl = WWW::Curl::Easy->new();
my $fh = 'README';
$curl->setopt(CURLOPT_HEADER,0); # dont want the headers, or they will break the json reading!
$curl->setopt(CURLOPT_URL,"https://".$config->{$config->{mode}}->{transaction_endpoint}."/v1/oauth2/token");
$curl->setopt(CURLOPT_POSTFIELDS,"grant_type=client_credentials");
$curl->setopt(CURLOPT_USERPWD, $config->{$config->{mode}}->{system_client_id}.":".$config->{$config->{mode}}->{system_secret});
$curl->setopt(CURLOPT_SSL_VERIFYPEER,0);
$curl->setopt(CURLOPT_SSL_VERIFYHOST,0);
$curl->setopt('CURLOPT_RETURNTRANSFER',1);
$curl->setopt(CURLOPT_POST,1);
my $response_body;
open(my $fh, '>', \$response_body);
$curl->setopt(CURLOPT_WRITEDATA, $fh);
$curl->setopt(CURLOPT_VERBOSE,0);
my $retcode = $curl->perform();
if ($retcode != 0) {
warn "An error happened: ", $curl->strerror($retcode), " ($retcode)\n";
warn "errbuf: ", $curl->errbuf;
}
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
use JSON;
my $json_vals = decode_json($response_body);
return $json_vals->{access_token};
}
..但是当我尝试使用下面相同的逻辑来获取事务时,它会因500 IS错误而失败:
sub get_proper_trans_id {
my $token = get_token($_[0]);
print $IN->header( 'utf8' );
my $curl = WWW::Curl::Easy->new();
my $fh = 'README';
my @headers = ("Authorization: Bearer $token","Content-Type: application/json; charset=utf-8");
print qq|Getting: https://$config->{$config->{mode}}->{transaction_endpoint}/v1/payments/payment/$_[0] \n|;
$curl->setopt(CURLOPT_HTTPHEADER,\@headers);
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL,"https://$config->{$config->{mode}}->{transaction_endpoint}/v1/payments/payment/$_[0]");
$curl->setopt(CURLOPT_SSL_VERIFYPEER,0);
$curl->setopt(CURLOPT_SSL_VERIFYHOST,0);
$curl->setopt('CURLOPT_RETURNTRANSFER',1);
$curl->setopt(CURLOPT_POST,1);
my $response_body;
open(my $fh, '>', \$response_body);
$curl->setopt(CURLOPT_WRITEDATA, $fh);
$curl->setopt(CURLOPT_VERBOSE,1);
my $retcode = $curl->perform();
if ($retcode != 0) {
warn "An error happened: ", $curl->strerror($retcode), " ($retcode)\n";
warn "errbuf: ", $curl->errbuf;
}
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
}
传递到$_[0]
的{{1}}的值是从PayPal-Checkout.JS系统传回的 PAY-xxxx 交易ID。
如果我手动运行此curl命令,它可以工作:
get_proper_trans_id()
问题是我在使用该方法时遇到了utf8问题,因此我正在尝试找到一个可以帮我完成工作的模块。
这是curl的输出(带有调试和标题);
curl https://$config->{$config->{mode}}->{transaction_endpoint}/v1/payments/payment/$_[0] -H "Content-Type: application/json; charset=utf-8" -H "Authorization: Bearer $token"
我错过了什么?谢谢!
答案 0 :(得分:0)
好的,我收到了PayPal Tech的答复。这个特定的功能似乎需要 GET 而不是 POST :
我看到你正在尝试使用POST,你需要使用GET( REST API流程需要GET才能获得事务 细节。得到 https://api.sandbox.paypal.com/v1/payments/payment/可以 你试着换GET吗?我希望对此进行排序。
......果然,那就修好了:)
对于任何想知道我是如何做出这种改变的人,我只是注释掉了:
$curl->setopt(CURLOPT_POST,1);
发送请求时,curl默认为GET。