Spring boot paypal无效的\u资源\u ID
我正在尝试实现PayPal的API,但由于这是我第一次使用它,我得到了一些错误 代码如下: 控制器Spring boot paypal无效的\u资源\u ID,spring,spring-boot,paypal,Spring,Spring Boot,Paypal,我正在尝试实现PayPal的API,但由于这是我第一次使用它,我得到了一些错误 代码如下: 控制器 @PostMapping("/checkout/paypal") public String checkout_paypal(@RequestParam(value = "id") Integer id) throws Exception { Order order = orderServices.findOrderById(i
@PostMapping("/checkout/paypal")
public String checkout_paypal(@RequestParam(value = "id") Integer id) throws Exception {
Order order = orderServices.findOrderById(id);
try {
Payment payment = service.createPayment(order.getTotalPrice().doubleValue(), "EUR", "PAYPAL",
"ORDER", "Order id:"+order.getId(), "http://localhost:4200/checkout?id="+order.getId(),
"http://localhost:4200/checkout?id="+order.getId());
for(Links link:payment.getLinks()) {
if(link.getRel().equals("approval_url")) {
return "redirect:"+link.getHref();
}
}
} catch (PayPalRESTException e) {
e.printStackTrace();
}
System.out.println("Success");
return "Success";
}
@GetMapping("/successPaymentPaypal")
public String successPayment(@RequestParam(value = "id") Integer id,@RequestParam(value = "paymentId") String paymentId,@RequestParam(value = "PayerID") String PayerID) throws Exception {
System.out.println(id+" "+paymentId+" "+PayerID);
try {
Payment payment = service.executePayment(paymentId, PayerID);
if(payment.getState().equals("approved")){
Order order = orderServices.findOrderById(id);
order.setOrderState(OrderState.PAID);
orderServices.saveOrder(order);
return "success";
}
} catch (PayPalRESTException e) {
throw new Exception("Error occured while processing payment!");
}
return "Done";
}
服务
@Autowired
private APIContext apiContext;
public Payment createPayment(
Double total,
String currency,
String method,
String intent,
String description,
String cancelUrl,
String successUrl) throws PayPalRESTException {
Amount amount = new Amount();
amount.setCurrency(currency);
total = new BigDecimal(total).setScale(2, RoundingMode.HALF_UP).doubleValue();
amount.setTotal(String.format("%.2f", total));
Transaction transaction = new Transaction();
transaction.setDescription(description);
transaction.setAmount(amount);
List<Transaction> transactions = new ArrayList<>();
transactions.add(transaction);
Payer payer = new Payer();
payer.setPaymentMethod(method.toString());
Payment payment = new Payment();
payment.setIntent(intent.toString());
payment.setPayer(payer);
payment.setTransactions(transactions);
RedirectUrls redirectUrls = new RedirectUrls();
redirectUrls.setCancelUrl(cancelUrl);
redirectUrls.setReturnUrl(successUrl);
payment.setRedirectUrls(redirectUrls);
return payment.create(apiContext);
}
public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
Payment payment = new Payment();
payment.setId(paymentId);
PaymentExecution paymentExecute = new PaymentExecution();
paymentExecute.setPayerId(payerId);
return payment.execute(apiContext, paymentExecute);
}
@Autowired
私有APIContext-APIContext;
公共支付(
双倍总数,
字符串货币,
字符串方法,
字符串意图,
字符串描述,
字符串取消URL,
字符串successUrl)引发PayPalRESTException{
金额=新金额();
金额。设置货币(币种);
总计=新的BigDecimal(总计)。设置刻度(2,舍入模式。向上舍入半个)。doubleValue();
amount.setTotal(String.format(“%.2f”,total));
事务=新事务();
事务.setDescription(描述);
交易。设置金额(金额);
列表事务=新建ArrayList();
交易。添加(交易);
付款人付款人=新付款人();
payer.setPaymentMethod(method.toString());
付款=新付款();
payment.setIntent(intent.toString());
付款。设置付款人(付款人);
支付。设置交易(交易);
RedirectUrls RedirectUrls=新的重定向URL();
重定向URL.setCancelUrl(取消URL);
重定向URL.setReturnUrl(成功URL);
支付。设置重定向URL(重定向URL);
返回付款。创建(apiContext);
}
public Payment executePayment(字符串paymentId,字符串PayRid)引发PayPalRESTException{
付款=新付款();
payment.setId(paymentId);
PaymentExecution paymentExecute=新的PaymentExecution();
paymentExecute.setPayerId(payerId);
返回payment.execute(apiContext,paymentExecute);
}
配置
@Value("${paypal.client.id}")
private String clientId;
@Value("${paypal.mode}")
private String paypalMode;
@Value("${paypal.client.secret}")
private String clientSecret;
@Bean
public Map<String, String> paypalSdkConfig(){
Map<String, String> sdkConfig = new HashMap<>();
sdkConfig.put("mode", paypalMode);
return sdkConfig;
}
@Bean
public OAuthTokenCredential authTokenCredential(){
return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig());
}
@Bean
public APIContext apiContext() throws PayPalRESTException{
APIContext apiContext = new APIContext(authTokenCredential().getAccessToken());
apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext;
}
@Value(${paypal.client.id})
私有字符串clientId;
@值(${paypal.mode}”)
私有字符串paypolmode;
@值(${paypal.client.secret}”)
私有字符串clientSecret;
@豆子
公共地图paypalSdkConfig(){
Map sdkConfig=new HashMap();
sdkConfig.put(“模式”,PayPalmMode);
返回sdkConfig;
}
@豆子
公共OAuthTokenCredential authTokenCredential(){
返回新的OAuthTokenCredential(clientId、clientSecret、paypalSdkConfig());
}
@豆子
公共APIContext APIContext()引发PayPalRESTException{
APIContext APIContext=新的APIContext(authTokenCredential().getAccessToken());
setConfigurationMap(paypalSdkConfig());
返回apiContext;
}
这就是错误:
响应代码:404错误响应:{“名称”:“无效的资源ID”,“消息”:“未找到请求的资源ID”,“信息链接”:”https://developer.paypal.com/docs/api/payments/#errors,“调试id”:“c2dc1e86af7fe”}
收银台/贝宝没有给出任何错误,而且工作得很好,我想,它让我重定向到我的前端,在那里我提出第二个请求,但当错误出现时。。
我真的不知道问题出在哪里。看来您正在集成旧的
v1/payments
API。您应该使用当前的v2/checkout/orders
进行集成
请参阅
与之匹配的最佳审批流是
对于v2/checkout/orders
,请始终使用intent:capture
,除非您有非常具体和明确的业务原因来添加中间授权步骤