无法通过cURL连接到PayPal API

14
我正在尝试通过"NVP API"建立PayPal Express支付。尝试使用cURL从我的服务器连接到PayPal沙盒,但连接会在大约2分钟后停滞并超时。我正在使用文档中的示例调用
curl -v --insecure  https://api-3t.sandbox.paypal.com/nvp  -d  "USER=platfo_1255077030_biz_api1.gmail.com&PWD=1255077037&SIGNATURE=Abg0gYcQyxQvnf2HDJkKtA-p6pqhA1k-KTYE0Gcy1diujFio4io5Vqjf&METHOD=SetExpressCheckout&VERSION=78&PAYMENTREQUEST_0_PAYMENTACTION=SALE&PAYMENTREQUEST_0_AMT=19&PAYMENTREQUEST_0_CURRENCYCODE=USD&cancelUrl=http://www.yourdomain.com/cancel.html&returnUrl=http://www.yourdomain.com/success.html"

Shell输出是:

* About to connect() to api-3t.sandbox.paypal.com port 443 (#0)
* Trying 173.0.82.83... Connection timed out
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

当我试图通过PHP的curl来做这件事时,没有任何错误提示,只是得到了一个空的资源句柄。
我可以轻松地从我的本地机器和其他我可以访问的服务器上发出请求(并获取正确的数据),因此我猜测这是一些服务器端的错误配置。作为一个非服务器人员,我有点不知所措。
cURL已启用,并在phpinfo中记录以下内容:
libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6

openSSL已启用。尝试连接API的实时版本时也遇到了相同的问题。

3
也许您的服务器上有一些防火墙规则,阻止了这些请求,您能否使用curl访问该服务器上的其他网站?对于其他HTTPS网站呢? - talkol
1
请查看这里的评论http://stackoverflow.com/questions/12936733/timeout-on-connecting-to-https,它们都与出站防火墙限制有关.. 甚至有人提到了PayPal :) - talkol
1
如果您有良好的服务器支持,我建议您打开一个工单来解决此问题。如果没有,尝试找出您安装了哪个防火墙,然后搜索如何查看活动规则。也许您可以从cpanel等工具中获得一些提示。 - talkol
1
(我在PayPal)是的,API用户现在很好。我同意@talkol说的一切。 - Robert
请查看此帖子:http://stackoverflow.com/questions/16301987/why-are-my-dodirectpayment-calls-ignored/17871400#17871400 - captainhero70
显示剩余7条评论
5个回答

1
尝试。
$ch = curl_init();
# Merchant Account Credentials
$ppUserID = "User Id Email"; //Take it from sandbox dashboard for test mode or take it from paypal.com account in production mode, help: https://developer.paypal.com/docs/classic/api/apiCredentials/
$ppPass = "User Pass"; //Take it from sandbox dashboard for test mode or take it from paypal.com account in production mode, help: https://developer.paypal.com/docs/classic/api/apiCredentials/
$ppSign = "Paypal Sign"; //Take it from sandbox dashboard for test mode or take it from paypal.com account in production mode, help: https://developer.paypal.com/docs/classic/api/apiCredentials/

$ppAppID = "APP-80W284485P519543T"; //if it is sandbox then app id is always: APP-80W284485P519543T

$paypal_header_options = array();
    $paypal_header_options[] = "X-PAYPAL-SECURITY-USERID: $ppUserID";
    $paypal_header_options[] = "X-PAYPAL-SECURITY-PASSWORD: $ppPass";
    $paypal_header_options[] = "X-PAYPAL-SECURITY-SIGNATURE: $ppSign";
    $paypal_header_options[] = "X-PAYPAL-REQUEST-DATA-FORMAT: NV";
    $paypal_header_options[] = "X-PAYPAL-RESPONSE-DATA-FORMAT: NV";
    $paypal_header_options[] = "X-PAYPAL-APPLICATION-ID: $ppAppID";

$URL = 'https://api-3t.sandbox.paypal.com/nvp'
    .'?USER='.$ppUserID
    .'&PWD='.$ppPass
.'&SIGNATURE='.$ppSign
    .'&METHOD=SetExpressCheckout' 
    .'&VERSION=93'
    .'&RETURNURL=https://localhost/express-checkout-single-product/success.php?success=true'
    .'&CANCELURL=https://localhost/express-checkout-single-product/index.php'
.'&PAYMENTREQUEST_0_CURRENCYCODE=USD'
.'&PAYMENTREQUEST_0_AMT=250.00'    #The payment amount for the first receiver   # Merchant(Primary AC) Account Amount
.'&PAYMENTREQUEST_0_ITEMAMT=225.00'                         # Merchant(Primary AC) Account Email
.'&PAYMENTREQUEST_0_TAXAMT=25.00'    #Receiver designation (there can be only 1 primary receiver)
.'&PAYMENTREQUEST_0_PAYMENTACTION=Order' 
.'&PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID=email' 
.'&PAYMENTREQUEST_0_PAYMENTREQUESTID=CART110-PAYMENT0'    #The payment amount for the second receiver   # Seller(Secondry AC) Account Amount
.'&PAYMENTREQUEST_1_CURRENCYCODE=USD'
.'&PAYMENTREQUEST_1_AMT=75.00'    #The payment amount for the first receiver    # Merchant(Primary AC) Account Amount
.'&PAYMENTREQUEST_1_ITEMAMT=65.00'                          # Merchant(Primary AC) Account Email
.'&PAYMENTREQUEST_1_TAXAMT=10.00'    #Receiver designation (there can be only 1 primary receiver)
.'&PAYMENTREQUEST_1_PAYMENTACTION=Order' 
.'&PAYMENTREQUEST_1_SELLERPAYPALACCOUNTID=email' 
.'&PAYMENTREQUEST_1_PAYMENTREQUESTID=CART110-PAYMENT1' 
    .'&L_PAYMENTREQUEST_0_NAME0=Departs2'
    .'&L_PAYMENTREQUEST_0_NAME0=Sunset'
.'&L_PAYMENTREQUEST_0_QTY0=1'
    .'&L_PAYMENTREQUEST_0_AMT0=125'
    .'&L_PAYMENTREQUEST_0_TAXAMT0=15'
    .'&L_PAYMENTREQUEST_0_NAME1=Departs'
.'&L_PAYMENTREQUEST_0_QTY1=1'
    .'&L_PAYMENTREQUEST_0_AMT1=100.00'
    .'&L_PAYMENTREQUEST_0_TAXAMT1=10.00';
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  //  curl_setopt($ch, CURLOPT_HTTPHEADER, $paypal_header_options);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $paypal_response = curl_exec($ch);

$responseAr = explode('&', $paypal_response);
$parsedResponseAr = array();
foreach($responseAr as $i => $value) {
    $tmpAr = explode('=', $value);
    if(!empty($tmpAr))
        $parsedResponseAr[strtoupper($tmpAr[0])] = urldecode($tmpAr[1]);
}
print_r(json_encode($parsedResponseAr));
curl_close($ch);

0

A) 首先尝试使用 wget https://api-3t.sandbox.paypal.com/nvp 命令,查看是否能够与Paypal服务器建立简单的连接并收到“Bad Request”回复。如果不能,可能是您被Paypal服务器阻止了或者您的防火墙在端口443上阻止了您访问Paypal。

B) 尝试添加自定义头部:-H "Host: api-3t.sandbox.paypal.com"

curl -v --insecure https://api-3t.sandbox.paypal.com/nvp -d "USER=platfo_1255077030_biz_api1.gmail.com&PWD=1255077037&SIGNATURE=Abg0gYcQyxQvnf2HDJkKtA-p6pqhA1k-KTYE0Gcy1diujFio4io5Vqjf&METHOD=SetExpressCheckout&VERSION=78&PAYMENTREQUEST_0_PAYMENTACTION=SALE&PAYMENTREQUEST_0_AMT=19&PAYMENTREQUEST_0_CURRENCYCODE=USD&cancelUrl=http://www.yourdomain.com/cancel.html&returnUrl=http://www.yourdomain.com/success.html" -H "Host: api-3t.sandbox.paypal.com"

C)您在什么平台上使用什么版本的CURL?我有:

curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2

0
在我的情况下,我已经启用了服务器上的ICMP流量,现在curl和get正常工作。
iptables -P INPUT ACCEPT; iptables -P OUTPUT ACCEPT
iptables -F INPUT; iptables -F OUTPUT

0

你的错误是由于你的服务器版本的nss包和api-3t.sandbox.paypal.com使用了SNI证书。你可以通过在bash中执行以下命令进行验证:

openssl s_client -servername api-3t.sandbox.paypal.com -tlsextdebug -connect api-3t.sandbox.paypal.com:443 2>/dev/null | grep "server name"

输出结果为TLS server extension "server name" (id=0), len=0,这表明该服务器使用了SNI证书。

解决方案实际上很简单。

对于RedHat变体:

sudo yum -y update nss*

对于 Debian 变种:

sudo apt-get update nss*

你其实不需要改变你的代码(针对连接问题)


0
很有可能,PayPal Express付款服务会因为错误的身份验证或无效的SSL证书而中断/切断您的连接。在这种情况下,请尝试向服务提供商询问此问题。
希望能够解决您的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接