spring - Spring Boot 贝宝 INVALID_RESOURCE_ID
问题描述
我正在尝试实现 PayPal 的 API,但由于这是我第一次使用它,所以我遇到了一些错误。
这是代码:
控制器
@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);
}
配置
@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;
}
这是错误:
响应代码:404 错误响应:{"name":"INVALID_RESOURCE_ID","message":"找不到请求的资源 ID。","information_link":"https://developer.paypal.com/docs/api/payments /#errors","debug_id":"c2dc1e86af7fe"}
结帐/贝宝没有给出任何错误,我认为它运行良好,它让我重定向到我的前端,在那里我提出第二个请求,但是当错误出现时......我真的不知道问题是什么......
解决方案
看来您正在集成旧v1/payments
API。您应该改为使用当前的v2/checkout/orders
.
请参阅https://developer.paypal.com/docs/checkout/reference/server-integration/上的指南
与之配对的最佳批准流程是https://developer.paypal.com/demo/checkout/#/pattern/server
使用,除非您有非常具体且明确定义的业务原因来添加干预授权步骤,否则请v2/checkout/orders
始终使用intent:capture
推荐阅读
- javascript - 防止点击底层div,反应js
- java - Spring Boot 安全性不会重定向配置(HttpSecurity http)方法上的 oauth/authorize 调用
- javascript - 每张幻灯片的水平 ScrollMagic 容器中的视差内容结结巴巴
- python - 如何对中间有浮点数的字符串列表进行排序?
- c - “typedef char A[max_size]”是什么意思?
- vue.js - Vue.js 和 Vuetify:单击项目时调用方法
- python-3.x - 如何在用户定义的函数中将列表作为参数传递?
- c# - 如何在 MVC Net Core 中编辑可枚举或列表视图
- python - 迭代期间与字符串的列表/元组不同
- tensorflow2.0 - Tensorflow 2.0:冻结图支持