欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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)

  1. 沙盒环境

    先登陆paypal开发者网站https://developer.paypal.com/developer/applications
    再选择以下菜单

PayPal接口查询账户各币种余额
点击需要查询账号的相关信息
PayPal接口查询账户各币种余额
PayPal接口查询账户各币种余额

  1. 正式环境

    通过访问链接https://www.paypal.com/businessprofile/mytools/apiaccess/firstparty获取

参考文档

  1. https://developer.paypal.com/docs/nvp-soap-api/get-balance-nvp/

本文地址:https://blog.csdn.net/ccc_12345/article/details/107259106