首页 > 解决方案 > 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"}

结帐/贝宝没有给出任何错误,我认为它运行良好,它让我重定向到我的前端,在那里我提出第二个请求,但是当错误出现时......我真的不知道问题是什么......

标签: springspring-bootpaypal

解决方案


看来您正在集成旧v1/paymentsAPI。您应该改为使用当前的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


推荐阅读