PayPal接口查询账户各币种余额
程序员文章站
2022-11-23 11:00:03
PayPal查询账户各币种余额一、废话不多说,先上代码import javax.net.ssl.HttpsURLConnection;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.math.BigDecimal;import java.net.URL;import java.net.URLDecoder;import jav...
PayPal接口查询账户各币种余额
一、废话不多说,先上代码
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
public class PayPalBalances {
private final String USER_AGENT = "Mozilla/5.0";
// 请求url
private String url;
// 账户api用户名
private String userName;
// 账户api密码
private String pwd;
// 账户api signature
private String signature;
/**
* 返回币种选项
* 0 只返回主币种
* 1 返回所有拥有的币种
*/
private final Integer RETURN_ALL_CURRENCIES = 1;
public PayPalBalances(){}
public PayPalBalances(String url, String userName, String pwd, String signature) {
this.url = url;
this.userName = userName;
this.pwd = pwd;
this.signature = signature;
}
public void getPayPalBalances() throws Exception {
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//添加请求头
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "USER=" + userName +
"&PWD=" + pwd +
"&SIGNATURE=" + signature +
"&VERSION=109.0" +
"&METHOD=GetBalance" +
"&RETURNALLCURRENCIES" + RETURN_ALL_CURRENCIES;
//发送Post请求
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 处理返回值
String responseData = response.toString();
responseData = URLDecoder.decode(responseData, "UTF-8");
String[] arr = responseData.split("&");
Map<String, BigDecimal> balanceMap = new HashMap<>();
BigDecimal[] amount = new BigDecimal[100];
String[] currency = new String[100];
int length = 0;
for(String s : arr) {
if(s.startsWith("L_AMT")) {
s = s.replace("L_AMT","");
Integer i = Integer.parseInt(s.substring(0, s.indexOf("=")));
amount[i] = new BigDecimal(s.substring(s.indexOf("=")+1));
length++;
} else {
if(s.startsWith("L_CURRENCYCODE")) {
s = s.replace("L_CURRENCYCODE","");
Integer j = Integer.parseInt(s.substring(0, s.indexOf("=")));
currency[j] = s.substring(s.indexOf("=") + 1);
}
}
}
for(int i = 0; i < length; i++) {
System.out.println("币种:" + currency[i] + " 余额:" + amount[i]);
}
}
public static void main(String[] args) {
// 沙盒 https://api-3t.sandbox.paypal.com/nvp
// 正式 https://api-3t.paypal.com/nvp
String url = "https://api-3t.sandbox.paypal.com/nvp";
String userName = "userName";
String pwd = "pwd";
String signature = "sginature";
PayPalBalances payPalBalances = new PayPalBalances(url, userName, pwd, signature);
try {
payPalBalances.getPayPalBalances();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、相关参数(userName, pwd, signature)
-
沙盒环境
先登陆paypal开发者网站https://developer.paypal.com/developer/applications
再选择以下菜单
点击需要查询账号的相关信息
参考文档
- https://developer.paypal.com/docs/nvp-soap-api/get-balance-nvp/
本文地址:https://blog.csdn.net/ccc_12345/article/details/107259106
上一篇: 张俭:东汉末年名士,得罪权贵逃亡半生
下一篇: Android小程序实现切换背景颜色