2015-08-08 76 views
2

我一直在搜索PayPal文档两个小时,找不到答案,甚至搜索到Google。获取PayPal余额(GetBalance API)

这里经过: https://developer.paypal.com/docs/api/#api-operations

我添加PayPal的SDK中的Maven(REST API),现在我不知该怎么办。

我想要使用PayPal API(所有货币)获取帐户的余额。

+0

我会建议使用此[贝宝PHP SDK(https://www.angelleye.com/product/paypal-sdk-php/ )代替。它使用Classic API,但它具有功能齐全且准备就绪的样本/模板。当然,GetBalance是最简单的调用,并且样本本身可以为您提供开箱即用的功能。 [点击这里查看GetBalance示例](http://paypal.angelleye.com/paypal-php-library/samples/GetBalance.php)。 –

+0

啊,射击。没关系,我刚刚看到你在使用Java。 –

+0

我会看看,因为我是一名PHP开发人员。 :) – SysVoid

回答

0

为getBalance从Classsic API和尚未列入REST的API在这个时候,但你可以直接实现NVP呼叫与的HTTPRequest因为这样,

API端点:

https://api-3t.paypal.com/nvp 

POST请求有效载荷:

USER=seller_api1.paypal.com 
&PWD=56A9R4JPVFPMER2X 
&SIGNATURE=AFcWxV21C7fd0v3bYYYRCpSSRl31AociN2kspFBnMbzOGg6NdiC7ZXtg 
&VERSION=109.0 
&METHOD=GetBalance 
&RETURNALLCURRENCIES=1 

响应:

L_AMT0=265.17 
L_CURRENCYCODE0=USD 
TIMESTAMP=2015-08-09T14:21:25Z 
CORRELATIONID=802b0b6666022 
ACK=Success 
VERSION=109.0 
BUILD=000000 

您也可以使用您的首选编程语言获得Classic API SDKs,并且这次选择“Merchant”SDK。

我的样本PHP代码,以帮助您了解流量,

<?php 
$version = "124"; 
$user = "API UserName"; 
$pwd = "API Password"; 
$signature = "API Signature"; 
$API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp"; 

$resArray = CallGetBalance ($API_Endpoint, $version, $user, $pwd, $signature); 
$ack = strtoupper ($resArray ["ACK"]); 
if ($ack == "SUCCESS") { 
    $balance = urldecode ($resArray ["L_AMT0"]); 
    $currency = urldecode ($resArray ["L_CURRENCYCODE0"]); 
    echo "Account Balance: " . $balance . " " . $currency; 
} 


function CallGetBalance($API_Endpoint, $version, $user, $pwd, $signature) { 
    // setting the curl parameters. 
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $API_Endpoint); 
    curl_setopt ($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_POST, 1); 

    // NVPRequest for submitting to server 
    $nvpreq = "METHOD=GetBalance" . "&RETURNALLCURRENCIES=1" . "&VERSION=" . $version . "&PWD=" . $pwd . "&USER=" . $user . "&SIGNATURE=" . $signature; 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $nvpreq); 
    $response = curl_exec ($ch); 

    $nvpResArray = deformatNVP ($response); 

    curl_close ($ch); 

    return $nvpResArray; 
} 

/* 
* This function will take NVPString and convert it to an Associative Array and it will decode the response. It is usefull to search for a particular key and displaying arrays. @nvpstr is NVPString. @nvpArray is Associative Array. 
*/ 
function deformatNVP($nvpstr) { 
    $intial = 0; 
    $nvpArray = array(); 

    while (strlen ($nvpstr)) { 
     // postion of Key 
     $keypos = strpos ($nvpstr, '='); 
     // position of value 
     $valuepos = strpos ($nvpstr, '&') ? strpos ($nvpstr, '&') : strlen ($nvpstr); 

     /* getting the Key and Value values and storing in a Associative Array */ 
     $keyval = substr ($nvpstr, $intial, $keypos); 
     $valval = substr ($nvpstr, $keypos + 1, $valuepos - $keypos - 1); 
     // decoding the respose 
     $nvpArray [urldecode ($keyval)] = urldecode ($valval); 
     $nvpstr = substr ($nvpstr, $valuepos + 1, strlen ($nvpstr)); 
    } 
    return $nvpArray; 
} 

?> 
+0

是从网站签名他们的API关键的东西? – SysVoid

+0

准确地说,登录到贝宝账户,并在“个人资料”标签下找到“API访问” –

+0

你能为我添加一个代码示例吗? – SysVoid