PayPal v2 Orders API - OrdersGetRequest 和 OrdersCaptureRequest 之间的区别

PayPal v2 Orders API - difference between OrdersGetRequest and OrdersCaptureRequest

我正在使用带有 angular 和 spring 引导的 checkout-sdk。这是我在 angular 端

的代码
           paypal
            .Buttons({
              style: {
                color:  'blue',
                shape:  'pill',
                label:  'pay',
                height: 40
              },
              createOrder: (data, actions) => {
                return actions.order.create({
                  purchase_units: [
                    {
                      description: 'Order id: '+this.order.id,
                      amount: {
                        currency_code: 'EUR',
                        value: this.order.totalPrice
                      }
                    }
                  ]
                });
              },
              onApprove: async (data, actions) => {
                const order = await actions.order.capture();
                this.paidFor = true;
                this.checkoutPaypal(this.id,order.id)
              },
              onError: err => {
              }
            })
            .render(this.paypalElement.nativeElement);

此函数用于检索付款,并将详细信息保存到数据库..

 public String checkoutPaypal(Integer id, String orderId) {
        OrdersCaptureRequest request = new OrdersCaptureRequest(orderId);
        request.requestBody(buildRequestBody());
        HttpResponse<com.paypal.orders.Order> response;

        try {
            response = payPalClient.client().execute(request);
        } catch (IOException e) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
        }

        for (PurchaseUnit purchaseUnit : response.result().purchaseUnits()) {
            purchaseUnit.amountWithBreakdown();
            for (Capture capture : purchaseUnit.payments().captures()) {
                if (capture.status().equals("COMPLETED")) {
                    Order order = orderRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found!"));
                    order.setOrderState(OrderState.PAID);
                    order.setPaymentDetails("Charge id: " + capture.id() + "; Status: " + capture.status() + "; Time paid: " + capture.createTime() + " GMT");
                    order.addOrderStateChange(OrderState.PAID, false);

                    sendEmail(order, " paid successfully!", "Thanks for your purchase!<br>We will work as hard as we can, to deliver the order to you, as soon as possible!");
                    orderRepository.save(order);
                }
            }
    }
    return "Successfully paid!";
}

几天前还有效..但现在我收到了这个错误

{"name":"UNPROCESSABLE_ENTITY","details":[{"issue":"ORDER_ALREADY_CAPTURED","description":"Order already captured.If 'intent=CAPTURE' only one capture per order is allowed."}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"f058cb447ccbb","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_ALREADY_CAPTURED","rel":"information_link","method":"GET"}]}

但是替换后

 OrdersCaptureRequest request = new OrdersCaptureRequest(orderId);
 request.requestBody(buildRequestBody());
 HttpResponse<com.paypal.orders.Order> response;

 OrdersGetRequest request = new OrdersGetRequest(orderId);
 HttpResponse<com.paypal.orders.Order> response;

它正常工作。

所以我的问题是,两者之间有什么区别 https://developer.paypal.com/docs/checkout/reference/server-integration/capture-transaction/https://developer.paypal.com/docs/checkout/reference/server-integration/get-transaction/ ?

一个是获取订单状态,一个是抓取订单

在客户端调用 actions.order.capture() 后不应进行捕获,并且在这种情况下总是会 return 出错。当在客户端创建订单时,它也可能 return 错误 (actions.order.create())


正确的基于服务器的集成在客户端既不使用 actions.order.create() 也不使用 actions.order.capture()。不要这样做是非常重要的,因为交易将在您的服务器没有收到来自 PayPal 的任何即时直接响应的情况下创建。

相反,在您的服务器上创建两条路由,一条用于 'Create Order',一条用于 'Capture Order' 作为 documented here。这些路线应该 return JSON 数据。在 returning 数据之前,捕获路由应检查是否成功并相应地发送电子邮件或您需要的任何其他业务操作。

以上两条路线配对的审批流程为https://developer.paypal.com/demo/checkout/#/pattern/server