使用Java中的PayPal API获取用于完成付款的银行卡详细信息

时间:2019-02-27 13:22:38

标签: java paypal paypal-sandbox

我正在使用 PayPal API 类在我的应用程序中使用 Java 进行付款。我的贝宝(Paypal)沙盒帐户中有三张卡,分别是万事达卡,签证和美国运通卡。

我正在设置付款前的必填信息,我正在为同一代码添加代码。

// ###Details
        Details details = new Details();
        details.setShipping("0");
        details.setSubtotal("1");
        details.setTax("0");

        // ###Amount
        Amount amount = new Amount();
        amount.setCurrency(jobLine.getCurrency());
        amount.setTotal("1");
        amount.setDetails(details);

        // ###Transaction
        Transaction transaction = new Transaction();
        transaction.setAmount(amount);
        transaction.setDescription("This is the payment transaction for vetjobs.com.au");

        // ### Items
        Item item = new Item();
        item.setName("vetjob post job").setQuantity("1").setCurrency(jobLine.getCurrency()).setPrice("1");
        ItemList itemList = new ItemList();
        List<Item> items = new ArrayList<Item>();
        items.add(item);
        itemList.setItems(items);

        transaction.setItemList(itemList);

        List<Transaction> transactions = new ArrayList<Transaction>();
        transactions.add(transaction);

        Payer payer = new Payer();
        payer.setPaymentMethod("paypal");

        // ###Payment

        Payment payment = new Payment();
        payment.setIntent("sale");
        payment.setPayer(payer);
        payment.setTransactions(transactions);

因此,在付款之前不会确定卡,在付款之后才能知道。

现在我已经使用Visa完成付款了,我想在我的jsp页面上显示该卡名。那么如何检索这些细节呢?

2 个答案:

答案 0 :(得分:0)

存储信用卡时,它会像card_id一样返回"CARD-1SW33690TH184423CKM2Z"。然后,您可以使用card_id获取卡的详细信息。 尝试此请求以获取卡的详细信息:

    Sample Request
        curl -v -X GET https://api.sandbox.paypal.com/v1/vault/credit-cards/CARD-54E6956910402550WKGRL6EA \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer Access-Token"

    Sample Response

    {
      "valid_until": "2016-05-26T00:00:00Z",
      "first_name": "Joe",
      "last_name": "Shopper",
      "expire_month": "11",
      "state": "ok",
      "billing_address": {
        "city": "Johnstown",
        "state": "OH",
        "postal_code": "43210",
        "country_code": "US",
        "line1": "52 N. Main St."
      },
      "expire_year": "2018",
      "number": "0000000000000000",
      "type": "visa",
      "id": "CARD-54E6956910402550WKGRL6EA",
      "links": [
        {
          "href": "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-54E6956910402550WKGRL6EA",
          "method": "GET",
          "rel": "self"
        },
        {
          "href": "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-54E6956910402550WKGRL6EA",
          "method": "DELETE",
          "rel": "delete"
        },
        {
          "href": "https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-54E6956910402550WKGRL6EA",
          "method": "PATCH",
          "rel": "patch"
        }
      ]
    }

有关PayPal保险柜集成的更多详细信息,您可以访问:[https://developer.paypal.com/docs/api/vault/v1/#credit-cards_get]

有关代码段,您可以访问: [https://github.com/paypal/PayPal-Java-SDK/blob/master/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetCreditCardServlet.java][1]

让我知道是否仍然无法正常工作 谢谢

答案 1 :(得分:0)

我解决了您的查询。 这是答案

获取卡片详细信息的功能。

public Map<String, Object> getCardDetails() throws PayPalRESTException {
        Map<String, Object> response = new HashMap<String, Object>();
        try {
            APIContext context = new APIContext(clientId, clientSecret, "sandbox");
            CreditCard creditCard = CreditCard.get(context, "CARD-9D2462350Y753123NLS4YVTY");
            response.put("status", "sucsess");
            response.put("Details", creditCard);
            return response;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

获取卡详细信息的API

@RequestMapping(value = "cardDetails", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> cardDetails() {
        Map<String, Object> response = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            Map<String, Object> cardResponse = paypalVaultPaymentService.getCardDetails();

            String json = cardResponse.get("Details").toString();
            Map<String, Object> map = toMap(new JSONObject(json));
            data.put("cardDetails", map);
            response.put("data", data);
            response.put("status", "OK");
            response.put("code", "200");
            response.put("message", "Card Details Found successfully.");
            return new ResponseEntity<Map<String, Object>>(response, HttpStatus.OK);

        } catch (Exception e) {
            e.printStackTrace();
            response.put("status", "ERROR");
            response.put("code", "500");
            response.put("message", "Something went wrong");
            return new ResponseEntity<Map<String, Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

public static Map<String, Object> toMap(JSONObject object) throws JSONException {
        Map<String, Object> map = new HashMap<String, Object>();

        Iterator<String> keysItr = object.keys();
        while (keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = object.get(key);

            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            }

            else if (value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            map.put(key, value);
        }
        return map;
    }

    public static List<Object> toList(JSONArray array) throws JSONException {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < array.length(); i++) {
            Object value = array.get(i);
            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            }

            else if (value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            list.add(value);
        }
        return list;
    }