如何解决春季启动贝宝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错误响应:{“名称”:“ INVALID_RESOURCE_ID”,“消息”:“未找到请求的资源ID。”,“ information_link”:“ https://developer.paypal.com/docs/ api / payments /#errors“,” debug_id“:” c2dc1e86af7fe“}
结帐/ PayPal没有给出任何错误,并且我认为效果很好,它使我重定向到我的前端,我在这里进行第二次请求,但是有错误发生的时候。 我真的不知道是什么问题。
解决方法
似乎您正在集成旧的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
,除非您有非常明确的业务原因要添加中间的授权步骤
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。