2013-01-09 23 views
4

我出我的深度捲曲到PayMill。 我想將PayMill整合到我的網站(用Perl編寫)中。 Paymill還沒有Perl庫,所以我需要通過curl連接到它們。形成一個捲曲的請求在Perl

我已經完成了前端JS Paymill整合,並從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 ::捲曲::易,網:捲曲::簡單,LWP ::捲曲,但沒有對這些方法的文檔中使其明顯,我如何形成上面的查詢。

我已經試過(不真正認爲這是可行的),只是編碼如上所述在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 
} 
+0

我認爲你可以用[LWP](http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm)以及Curl來完成這個任務。我不習慣捲曲,所以你的代碼看起來很難閱讀。 '-u'選項用於向請求中添加憑證(如用戶/密碼)(請參閱http://curl.haxx.se/docs/manpage.html#-u)。 – simbabque

+1

首先確保您可以通過命令行成功執行curl請求;只有然後嘗試將其集成到您的Perl代碼 – ysth

回答

4

像其他的答案提出的最好的辦法就是使用LWP :: UserAgent的做的請求。

編輯:由於PAYMILL發送質詢響應,因爲現在我更新了代碼。

由於Paymill不符合RFC 2616第14.47節(API未發送質詢響應),LWP :: UserAgent及類似服務器無法發送第二個帶有憑據的請求。解決的辦法是「力」 LWP ::用戶代理,以將其添加爲標題與發送的第一個請求憑據:

use LWP::UserAgent; 
use MIME::Base64; 

my $ua = LWP::UserAgent->new; 
# Use the following line no longer: 
# $ua->default_header(Authorization => "Basic " . encode_base64("your PRIVATE key")) 
$ua->credentials('api.paymill.de:443', '', 'YOUR PRIVATE KEY'); 

# Dumping only 
use Data::Dumper; 
print Dumper($ua->get("https://api.paymill.de:443/v2/clients")); 

披露:我在Paymill工作。

2

我不知道用戶名/密碼和令牌的認證部分是否正確,因爲我不知道'領域'應該是什麼。儘管如此,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讀一點。它說:

認證

%捲曲https://api.paymill.de/v2/clients \ -u e73fa5e7b87620585b5ea5d73c4d23bb:

要在Paymill API認證,您需要 測試或現場的私鑰帳戶。你必須使用http基本訪問 認證。您的密鑰必須設置爲用戶名。密碼 不是必需的,您不必插入密碼。但是如果你想要,可以自由地插入任意字符串。

Please keep your private keys secure and don’t pass them to anybody. These private keys have extreme secure information for 

處理你的店鋪的交易。 您的所有請求都必須通過https進行。以另一種方式提出的請求將失敗。這是出於安全原因提交的數據爲 。

還有以http://en.wikipedia.org/wiki/HTTP_Secure一個鏈接,這幾乎是我認爲清除了-u部分。

2

您可以使用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" 
}