0
這是我迄今爲止完成的代碼。跟蹤異常詳細信息,其中包含錯誤代碼和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
我的問題如下:
- 如何我會得到異常錯誤代碼和消息,如(錯誤代碼:400,名稱:VALIDATION_ERROR,問題:值是無效的
- 我該如何告知有關與交易發生中的基本問題用戶友好的例外買家..
在此先感謝,我在異常處理是新..希望爲積極響應
親愛的我發送錯誤信息,以便我可以得到異常響應將它們存儲到數據庫中,我的問題是不同的... –
請閱讀下面的代碼部分,我問了兩個問題 –