如何在 spring JPA 应用程序中集成 paypal rest api。错误代码 400
How to integrate paypal rest api in spring JPA application. ERROR CODE 400
根据 java SDK 提供的 here ,我创建了一个新项目并尝试集成代码以进行 paypal api 调用。但是当我 运行 应用程序时,它给我错误:-
Error code : 401 with response : Server returned HTTP response code: 401 for URL: https://api.sandbox.paypal.com/v1/payments/payment
这是我的控制器class
package com.main.controller;
public class PaymentController{
@RequestMapping(method = RequestMethod.POST, produces = "application/JSON")
public Payment getString(){
InputStream is = PaymentController.class
.getResourceAsStream("/sdk_config.properties");
try {
PayPalResource.initConfig(is);
System.out.println("initiialization done");
} catch (PayPalRESTException e) {
System.out.println("Paypal Rest Exception : " + e.getMessage());
}
Map<String, String > map = new HashMap<String, String>();
map.put("mode", "sandbox");
String clientID = "AYDNebhrsuqiUKPU_ab-tCvGGVkzaxw2y4bIJFIl4rMuCW..........................";
String clientSecret="ENgjkFRgy1yGhal0aobwdF8kLNglkDaDeDItLN-lgQJZV4W1FpNQ27g3FC...............";
try {
accessToken = new OAuthTokenCredential(clientID, clientSecret,map).getAccessToken();
} catch (PayPalRESTException e) {
// TODO Auto-generated catch block
System.out.println("Cannot make the OAuthentication :" + e.getMessage());
}
Payment payment = createPayment();
return payment;
}
public Payment createPayment(){
Address billingAddress = new Address();
billingAddress.setCity("Johnstown");
billingAddress.setCountryCode("US");
billingAddress.setLine1("52 N Main ST");
billingAddress.setPostalCode("43210");
billingAddress.setState("OH");
CreditCard creditCard = new CreditCard();
creditCard.setBillingAddress(billingAddress);
creditCard.setCvv2(111);
creditCard.setExpireMonth(11);
creditCard.setExpireYear(2018);
creditCard.setFirstName("Joe");
creditCard.setLastName("Shopper");
creditCard.setNumber("5500005555555559");
creditCard.setType("mastercard");
Details details = new Details();
details.setShipping("1");
details.setSubtotal("5");
details.setTax("1");
Amount amount = new Amount();
amount.setCurrency("USD");
amount.setTotal("7");
amount.setDetails(details);
Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction
.setDescription("This is the payment transaction description.");
List<Transaction> transactions = new ArrayList<Transaction>();
transactions.add(transaction);
FundingInstrument fundingInstrument = new FundingInstrument();
fundingInstrument.setCreditCard(creditCard);
List<FundingInstrument> fundingInstrumentList = new ArrayList<FundingInstrument>();
fundingInstrumentList.add(fundingInstrument);
Payer payer = new Payer();
payer.setFundingInstruments(fundingInstrumentList);
payer.setPaymentMethod("credit_card");
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setTransactions(transactions);
Payment createdPayment = null;
try {
String accessToken = GenerateAccessToken.getAccessToken();
String realAccessToken = "A101.kPIsO7eGXhg420XIjnZmPboCS27CeDF6TZjVfGR31f6ja1IotK3e6U-E_k9MwOO5.....";
/*
* String requestId = Long.toString(System.nanoTime(); APIContext
* apiContext = new APIContext(accessToken, requestId ));
*/
createdPayment = payment.create(apiContext);
System.out.println("Created payment with id = " + createdPayment.getId()
+ " and status = " + createdPayment.getState());
} catch (PayPalRESTException e) {
System.out.println("Cannot make the payment from here: " + e.getMessage());
}
return createdPayment;
}
}
更新:- 我已经添加了 client_id
和 secret
身份验证凭据,现在我得到的是 400
错误,即 Validation Error
401 未授权。因此,首先您必须授权并创建授权令牌。 Here 是如何创建您的第一个电话的示例。
Make a /token call using your application's OAuth keys for the basic authentication values (the keys are the values of your client_id and secret). In the request body, set grant_type to client_credentials. When you run the command, PayPal generates and returns a new access token.
将您的代币添加到您要创建的付款中。
所以使用 Sandbox 和类似的东西:
Map<String, String> configurationMap = new HashMap<String, String>();
configurationMap.put("mode", "sandbox");
APIContext apiContext = new APIContext();
apiContext.setConfigurationMap(configurationMap);
tokeninfo = Tokeninfo.createFromAuthorizationCodeForFpp(apiContext, params);
tokeninfo.setAccessToken(tokeninfo.getTokenType() + " " + tokeninfo.getAccessToken());
之后您应该可以调用 tokeninfo.getAccessToken() 来创建付款。
根据 java SDK 提供的 here ,我创建了一个新项目并尝试集成代码以进行 paypal api 调用。但是当我 运行 应用程序时,它给我错误:-
Error code : 401 with response : Server returned HTTP response code: 401 for URL: https://api.sandbox.paypal.com/v1/payments/payment
这是我的控制器class
package com.main.controller;
public class PaymentController{
@RequestMapping(method = RequestMethod.POST, produces = "application/JSON")
public Payment getString(){
InputStream is = PaymentController.class
.getResourceAsStream("/sdk_config.properties");
try {
PayPalResource.initConfig(is);
System.out.println("initiialization done");
} catch (PayPalRESTException e) {
System.out.println("Paypal Rest Exception : " + e.getMessage());
}
Map<String, String > map = new HashMap<String, String>();
map.put("mode", "sandbox");
String clientID = "AYDNebhrsuqiUKPU_ab-tCvGGVkzaxw2y4bIJFIl4rMuCW..........................";
String clientSecret="ENgjkFRgy1yGhal0aobwdF8kLNglkDaDeDItLN-lgQJZV4W1FpNQ27g3FC...............";
try {
accessToken = new OAuthTokenCredential(clientID, clientSecret,map).getAccessToken();
} catch (PayPalRESTException e) {
// TODO Auto-generated catch block
System.out.println("Cannot make the OAuthentication :" + e.getMessage());
}
Payment payment = createPayment();
return payment;
}
public Payment createPayment(){
Address billingAddress = new Address();
billingAddress.setCity("Johnstown");
billingAddress.setCountryCode("US");
billingAddress.setLine1("52 N Main ST");
billingAddress.setPostalCode("43210");
billingAddress.setState("OH");
CreditCard creditCard = new CreditCard();
creditCard.setBillingAddress(billingAddress);
creditCard.setCvv2(111);
creditCard.setExpireMonth(11);
creditCard.setExpireYear(2018);
creditCard.setFirstName("Joe");
creditCard.setLastName("Shopper");
creditCard.setNumber("5500005555555559");
creditCard.setType("mastercard");
Details details = new Details();
details.setShipping("1");
details.setSubtotal("5");
details.setTax("1");
Amount amount = new Amount();
amount.setCurrency("USD");
amount.setTotal("7");
amount.setDetails(details);
Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction
.setDescription("This is the payment transaction description.");
List<Transaction> transactions = new ArrayList<Transaction>();
transactions.add(transaction);
FundingInstrument fundingInstrument = new FundingInstrument();
fundingInstrument.setCreditCard(creditCard);
List<FundingInstrument> fundingInstrumentList = new ArrayList<FundingInstrument>();
fundingInstrumentList.add(fundingInstrument);
Payer payer = new Payer();
payer.setFundingInstruments(fundingInstrumentList);
payer.setPaymentMethod("credit_card");
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setTransactions(transactions);
Payment createdPayment = null;
try {
String accessToken = GenerateAccessToken.getAccessToken();
String realAccessToken = "A101.kPIsO7eGXhg420XIjnZmPboCS27CeDF6TZjVfGR31f6ja1IotK3e6U-E_k9MwOO5.....";
/*
* String requestId = Long.toString(System.nanoTime(); APIContext
* apiContext = new APIContext(accessToken, requestId ));
*/
createdPayment = payment.create(apiContext);
System.out.println("Created payment with id = " + createdPayment.getId()
+ " and status = " + createdPayment.getState());
} catch (PayPalRESTException e) {
System.out.println("Cannot make the payment from here: " + e.getMessage());
}
return createdPayment;
}
}
更新:- 我已经添加了 client_id
和 secret
身份验证凭据,现在我得到的是 400
错误,即 Validation Error
401 未授权。因此,首先您必须授权并创建授权令牌。 Here 是如何创建您的第一个电话的示例。
Make a /token call using your application's OAuth keys for the basic authentication values (the keys are the values of your client_id and secret). In the request body, set grant_type to client_credentials. When you run the command, PayPal generates and returns a new access token.
将您的代币添加到您要创建的付款中。
所以使用 Sandbox 和类似的东西:
Map<String, String> configurationMap = new HashMap<String, String>();
configurationMap.put("mode", "sandbox");
APIContext apiContext = new APIContext();
apiContext.setConfigurationMap(configurationMap);
tokeninfo = Tokeninfo.createFromAuthorizationCodeForFpp(apiContext, params);
tokeninfo.setAccessToken(tokeninfo.getTokenType() + " " + tokeninfo.getAccessToken());
之后您应该可以调用 tokeninfo.getAccessToken() 来创建付款。