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



我正在使用 paypal api 类,用于在 java 我的应用程序中进行付款。我的贝宝(Paypal)砂盒帐户中有三张卡,这些卡是主,Visa和American Express。

我要在付款之前设置所需的信息,我正在为相同的代码提供代码。

// ###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页面上显示该卡名称。那么如何检索这些细节?

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

功能以获取卡细节。

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;
    }

存储信用卡时,它会返回卡_ 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 Vault集成的更多详细信息,您可以访问:[https://developer.paypal.com/docs/api/vault/v1/#credit-cards_get]

对于一件代码,您可以访问:[https://github.com/paypal/paypal-java-sdk/blob/master/rest-rest-rest-api-sample/src/src/main/java/java/java/com/paypal/paypal/paypal/payments/servlet/servlet/servlet/getcreditcardservlet.java]

让我知道是否仍然不起作用谢谢

相关内容

  • 没有找到相关文章

最新更新