paypal v2 orders api-ordersgetrequest和orderscapturerequest之间的差异

ioekq8ef  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(666)

我使用的是带有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/ ?

a11xaf1n

a11xaf1n1#

一个是获取订单的状态,另一个是捕获订单。
一旦你打过电话,就不应该进行捕获 actions.order.capture() 在客户端,并在这种情况下始终返回错误。在客户端创建订单时,它也可能返回错误( actions.order.create() )
正确的基于服务器的集成既不使用 actions.order.create() 也不是 actions.order.capture() 在客户端。这是非常重要的,不要这样做,因为交易将在您的服务器没有收到任何直接回应贝宝立即创建。
相反,在服务器上创建两个路由,一个用于“create order”,另一个用于“capture order”,如本文所述。这些路由应该返回json数据。在返回数据之前,捕获路由应该检查是否成功,并相应地发送电子邮件或您需要的任何其他业务操作。
与上述两条路线配对的审批流是https://developer.paypal.com/demo/checkout/#/pattern/server

相关问题