我对卷发感到力不从心。我想将PayMill集成到我的站点中(它是用Perl编写的)。现在还没有针对Paymill的Perl库,所以我需要通过curl连接到它们。
我已经完成了前端JS支付集成,并收到了PayMill的付款令牌。
现在我需要将从Paymill收到的令牌传递给我的后端,并使用curl请求PayMill完成交易并向用户收费。在这一点上我被卡住了。
要进行交易,PayMill文档说明我必须执行以下操作:
curl https://api.paymill.de/v2/transactions \
-u b94a7550bd908877cbae5d3cf0dc4b74: \
-d "amount=4200" \
-d "currency=EUR" \
-d "token=098f6bcd4621d373cade4e832627b4f6" \
-d "description=Test Transaction"我相信-u是用来验证我的请求的Paymill密钥,尽管这里的文档并不清楚。
我看过WWW::Curl::Easy、Net:Curl::Easy和LWP::Curl,但是这些方法的文档中没有任何内容说明如何构造上面的查询。
我已经尝试过了(不是真的相信它会工作),简单地按照上面的描述在perl中编码一个字符串;
my $request = '-u ' . $private_key . " ";
foreach my $key (keys %$params_in) {
$request .= '-d "' . lc($key) .'='.$params_in->{$key} . ' ';
}然后将$request传递给我的curl尝试,如下所示;
my $curl = WWW::Curl::Easy->new;
$curl->setopt(WWW::Curl::Easy::CURLOPT_HEADER(), 1);
$curl->setopt(WWW::Curl::Easy::CURLOPT_URL(), $paymill_server);
$curl->setopt(WWW::Curl::Easy::CURLOPT_POST(), 1);
$curl->setopt(WWW::Curl::Easy::CURLOPT_POSTFIELDS(), $request);
my $response;
$curl->setopt(WWW::Curl::Easy::CURLOPT_WRITEDATA(), \$response);
my $retcode = $curl->perform;然而,由于出现拒绝访问错误而失败,我认为这是因为Paymill找不到我的密钥,因为我搞乱了Curl (假设-u应该是secret_key)。
我觉得我在这里遗漏了一些明显的东西。
有人能给我指个方向吗?我们该怎么做?谢谢
更新
很好的答案,感谢大家的帮助,它现在工作了。我最终采用了Matthias的解决方案,最终的完整事务解决方案如下所示;
use LWP::UserAgent;
use MIME::Base64;
use JSON::XS;
my $ua = LWP::UserAgent->new;
$ua->default_header(Authorization => "Basic " . encode_base64(private_key));
my $response = $ua->post(https://api.paymill.de:443/v2/transactions , $params );
if ( $response->is_success ) {
my $obj = eval { decode_json $response->content } || {};
etc
}发布于 2013-01-09 23:55:22
我不知道带有用户名/密码和令牌的身份验证部分是否正确,因为我不知道“域”应该是什么。尽管如此,还是试试LWP吧。并不是我不喜欢Curl,我只是不知道它,但我知道LWP。
use strict; use warnings;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->credentials(
'api.paymill.de:80',
'Realm?',
'b94a7550bd908877cbae5d3cf0dc4b74'
);
my $response = $ua->post(
' https://api.paymill.de/v2/transactions',
{
amount => "4200",
currency => "EUR",
token => "098f6bcd4621d373cade4e832627b4f6",
description => "Test Transaction",
}
);
if ( $response->is_success ) {
print $response->decoded_content; # or whatever
} else {
die $response->status_line;
}编辑:我在Paymill documentation中读了一点。上面写着:
身份验证
示例
% curl https://api.paymill.de/v2/clients \ -u e73fa5e7b87620585b5ea5d73c4d23bb:
要在Paymill API进行身份验证,您需要测试帐户或实时帐户的私钥。您必须使用http基本访问身份验证。您的密钥必须设置为用户名。密码不是必需的,您也不必插入密码。但是,如果您愿意,可以随意插入任意字符串。
备注
请妥善保管您的私钥,不要将它们传递给任何人。这些私钥具有极其安全的信息
处理你店铺的交易。您的所有请求都必须通过https发出。以另一种方式发出的请求将会失败。这是出于提交数据的安全原因。
还有一个到http://en.wikipedia.org/wiki/HTTP_Secure的链接,我相信它很清楚-u部分。
发布于 2013-01-10 01:24:39
您可以使用LWP::Protocol::Net::Curl将LWP和libcurl有机集成。请检查以下内容:
#!/usr/bin/env perl
use common::sense;
use Data::Printer;
use JSON::XS;
use LWP::Protocol::Net::Curl verbose => 1;
use LWP::UserAgent;
# create user agent
my $ua = LWP::UserAgent->new;
# POST request
my $res = $ua->post(
'https://b94a7550bd908877cbae5d3cf0dc4b74:@api.paymill.de/v2/transactions',
'Accept-Encoding' => 'gzip',
Content => {
amount => 4200,
currency => 'EUR',
token => '098f6bcd4621d373cade4e832627b4f6',
description => 'Test Transaction',
},
);
# parse received data
my $obj = eval { decode_json $res->content } // {};
# output
p $obj;输出:
* About to connect() to api.paymill.de port 443 (#0)
* Trying 62.138.241.3...
* Connected to api.paymill.de (62.138.241.3) port 443 (#0)
* Connected to api.paymill.de (62.138.241.3) port 443 (#0)
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: none
* SSL connection using RC4-SHA
* Server certificate:
* subject: OU=Domain Control Validated; OU=PositiveSSL Wildcard; CN=*.paymill.de
* start date: 2012-07
* expire date: 2013-10
* subjectAltName: api.paymill.de matched
* issuer: C=GB; S
* SSL certificate verify ok.
* Server auth using Basic with user 'b94a7550bd908877cbae5d3cf0dc4b74'
> POST /v2/transactions HTTP/1.1
Authorization: Basic Yjk0YTc1NTBiZDkwODg3N2NiYWU1ZDNjZjBkYzRiNzQ6
User-Agent: libwww-perl/6.04 libcurl/7.28.0 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 libssh2/1.2.8
Host: api.paymill.de
Accept: */*
Accept-Encoding: gzip
Content-Length: 92
Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 92 out of 92 bytes
< HTTP/1.1 200 OK
< Server: nginx
< Date: Wed, 09 Jan 2013 17:22:54 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: close
< Set-Cookie: PHPSESSID=rmdo5a8c6u107gma28lotmmn24; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< X-Server: hrtt-frn5-de13
<
* Closing connection #0
Printing in line 28 of paymill.pl:
\ {
data {
amount 4200,
client {
created_at 1357752174,
description undef,
email undef,
id "client_85cb0bfc837f31c81015",
payment [],
subscription undef,
updated_at 1357752174
},
created_at 1357752174,
currency "EUR",
description "Test Transaction",
id "tran_c672daa0538e2a04e919",
livemode false,
origin_amount 4200,
payment {
card_holder undef,
card_type "visa",
client "client_85cb0bfc837f31c81015",
country undef,
created_at 1357752174,
expire_month 12,
expire_year 2014,
id "pay_2732689f44928301c769",
last4 1111,
type "creditcard",
updated_at 1357752174
},
preauthorization undef,
refunds undef,
status "closed",
updated_at 1357752174
},
mode "test"
}https://stackoverflow.com/questions/14238290
复制相似问题