【问题标题】:Spring boot Paypal checkout sdk - can not return HttpResponse<com.paypal.orders.Order>Spring Boot Paypal checkout sdk - 无法返回 HttpResponse<com.paypal.orders.Order>
【发布时间】:2021-02-08 13:21:17
【问题描述】:

您好,这是我创建订单的方法

public HttpResponse<com.paypal.orders.Order> createOrder() {
    OrdersCreateRequest request = new OrdersCreateRequest();
    request.header("prefer","return=representation");
    request.requestBody(buildRequestBodyX());
    try {
        HttpResponse<com.paypal.orders.Order> response = payPalClient.client().execute(request);
        return response;
    } catch (IOException e) {
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}

订单正在创建,但在控制器中,返回HttpResponse&lt;com.paypal.orders.Order&gt;后

我收到此错误

 Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.paypal.http.HttpResponse]

在将返回类型更改为 String 并仅返回其 id 后,它可以正常工作..

我也在问这在前端是否正确(Angular)

      paypal
        .Buttons({
          style: {
            color: 'blue',
            shape: 'pill',
            label: 'pay',
            height: 40
          },
          createOrder: function() {
            return fetch('http://localhost:8080/checkout/paypal/create', {
              method: 'post',
              headers: {
                'content-type': 'application/json'
              }
            }).then(function(res) {
              return res.json();
            }).then(function(data) {
              return data.id;
            });
          },
          onApprove: function(data) {
            return fetch('http://localhost:8080/checkout/paypal/capture?id=' + this.id + '&orderId=' + data.id, {
              headers: {
                'content-type': 'application/json'
              },

            }).then(function(res) {
              return res.json();
            }).then(function(details) {
              alert('Transaction funds captured from ' + details.payer_given_name);
            });
          },
          onError: err => {
          }
        })
        .render(this.paypalElement.nativeElement);
    }

【问题讨论】:

  • 查看是否可以调用该对象以获取 JSON 并让您的服务器返回此类。您需要将 JSON 返回给客户端。

标签: angular spring-boot paypal paypal-rest-sdk


【解决方案1】:

所以我刚刚更新了如下所示的角按钮功能,并且在后端仅返回订单 ID,并且可以正常工作..

     paypal
    .Buttons({
      style: {
        color:  'blue',
        shape:  'pill',
        label:  'pay',
        height: 40
      },
      createOrder: () => {
        return this.create_order_paypal(id).then(
          res=>{
            return res;
          }
        );
      },
      onApprove: async (data) => {
        return this.capture_order_paypal(id,data.orderID).then(
          res=>{
            this.apiService.successSnackBar("Successfully purchased!");
            this.paidFor = true;
            this.getOrder(id);
          }
        )
      },
      onError: err => {
        console.log(err)
      }
    })
    .render(this.paypalElement.nativeElement);

  async create_order_paypal(id) {
    return this.apiService.create_order_paypal(id).toPromise();
  }

  async capture_order_paypal(id,paypal_id) {
    return this.apiService.capture_order_paypal(id,paypal_id).toPromise();
  }

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 2021-09-07
    • 2015-05-27
    • 2019-08-13
    • 2012-10-22
    • 2010-12-03
    • 2023-04-04
    • 2018-10-28
    • 2017-01-08
    相关资源
    最近更新 更多