使用paypal中的错误代码和错误描述跟踪异常详细信息

时间:2014-03-13 05:53:46

标签: java exception paypal

这是我迄今为止为实现它所做的代码。

public void creditcardPayment(){
APIContext apiContext = new APIContext();
        String accessToken = "";

        try {
            accessToken = new OAuthTokenCredential(CLIENT_ID, CLIENT_SECRET,
                    sdkConfig).getAccessToken();
        } catch (PayPalRESTException e) {
            throw new PayPalRESTException("ACCESS TOKEN ERROR"
                    + e.getLocalizedMessage());
        }

        System.out.println(accessToken);

        apiContext = new APIContext(accessToken);
        apiContext.setConfigurationMap(sdkConfig);

        CreditCard creditCard = new CreditCard();
        creditCard.setType("visa");
        creditCard.setNumber("4204359776052315");
        creditCard.setExpireMonth(3);
        creditCard.setExpireYear(2019);
        creditCard.setFirstName("ppashish");
        creditCard.setLastName("pal");

        CreditCard createdCreditCard = null;
        FundingInstrument fundingInstrument = new FundingInstrument();
        List<FundingInstrument> fundingInstrumentList = new ArrayList<FundingInstrument>();

        try {
            createdCreditCard = creditCard.create(apiContext);
            String ccId = createdCreditCard.getId();
            System.out.println(ccId);

            CreditCardToken creditCardToken = new CreditCardToken();
            creditCardToken.setCreditCardId(ccId);
            System.out.println(creditCardToken.getType() + " last 4");

            fundingInstrument.setCreditCardToken(creditCardToken);
            fundingInstrumentList.add(fundingInstrument);

        } catch (PayPalRESTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new PayPalRESTException("INVALID CARD"
                    + e.getLocalizedMessage());

        }

        Payer payer = new Payer();
        payer.setFundingInstruments(fundingInstrumentList);
        payer.setPaymentMethod("credit_card");

        System.out.println();

        Amount amount = new Amount();
        amount.setCurrency("USD");
        amount.setTotal("3");

        Transaction transaction = new Transaction();
        transaction.setDescription("creating a payment with saved credit card");
        transaction.setAmount(amount);

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

        Payment payment = new Payment();
        payment.setIntent("authorize");
        payer.setPayerInfo(new PayerInfo().setPayerId("MD44W3Z67ZQZ4"));
        payment.setPayer(payer);
        payment.setTransactions(transactions);
        Payment createdPayment = null;

        try {
            createdPayment = payment.create(apiContext);
        } catch (PayPalRESTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new PayPalRESTException("TRANSACTION FAILURE..."
                    + e.getLocalizedMessage());
        }

        System.out.println(createdCreditCard.getId());
        System.out.println(createdPayment.getId());
        System.out.println(createdPayment.getState());
        Iterator<Transaction> v = createdPayment.getTransactions().iterator();

        while (v.hasNext()) {
            Transaction ttt = (Transaction) v.next();
            Iterator<RelatedResources> v1 = ttt.getRelatedResources()
                    .iterator();
            while (v1.hasNext()) {
                RelatedResources t = (RelatedResources) v1.next();
                System.out.println(t.toJSON());

            }
        }

}

我在catch块中处理异常,堆栈跟踪如下:

SEVERE: Error code : 400 with response : {"name":"VALIDATION_ERROR",
"details":[{"field":"number","issue":"Value is invalid"}],
"message":"Invalid request - see details",
"information_link":"https://developer.paypal.com/docs/api/#VALIDATION_ERROR","debug_id":"4c0b74d635ef0"}

com.paypal.core.rest.PayPalResource.execute(PayPalResource.java:374)
    at com.paypal.core.rest.PayPalResource.configureAndExecute(PayPalResource.java:225)
    at com.paypal.api.payments.CreditCard.create(CreditCard.java:384)
    at models.PayPalDAO.creditCardPayment(PayPalDAO.java:623)
    at models.PayPalDAO.main(PayPalDAO.java:788)
Caused by: com.paypal.exception.HttpErrorException: Error code : 400 with response : {"name":"VALIDATION_ERROR","details":[{"field":"number","issue":"Value is invalid"}],"message":"Invalid request - see details","information_link":"https://developer.paypal.com/docs/api/#VALIDATION_ERROR","debug_id":"4c0b74d635ef0"}
    at com.paypal.core.HttpConnection.execute(HttpConnection.java:108)
    at com.paypal.core.rest.PayPalResource.execute(PayPalResource.java:367)
    ... 4 more
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.sandbox.paypal.com/v1/vault/credit-card
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.paypal.core.HttpConnection.execute(HttpConnection.java:78)
    ... 5 more
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.sandbox.paypal.com/v1/vault/credit-card
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at com.paypal.core.HttpConnection.execute(HttpConnection.java:77)
    ... 5 more

我的问题如下:

  1. 我将如何获得异常错误代码和消息(错误代码:400,名称:VALIDATION_ERROR,问题:值无效
  2. 如何通过交易中出现的基本问题通知买家有关用户友好的例外情况..
  3. 在此先感谢,我是异常处理的新手。希望得到积极回应

1 个答案:

答案 0 :(得分:0)

REST API报告您传递的信用卡号无效。我要仔细检查以确保您发送了您期望的内容以及它是有效的号码。在现场,它应该是一张真正的牌。在沙箱中,最佳做法是使用与沙盒测试帐户关联的其中一个数字。

如果您仍然认为问题出在PayPal上,请在https://ppmts.custhelp.com/提交一张票,并在错误中报告debug_id。然后,支持代理可以查看此故障的具体细节。