【问题标题】:PayPal Order Request never gets back贝宝订单请求永远不会回来
【发布时间】:2021-02-09 06:59:56
【问题描述】:

亲爱的大家, 我从payPal开始, 我已经尝试实现 SDK 提供的标准示例(c#,FW 4.6.1)

在我的服务器端方法下面

    public async static Task<PayPalHttp.HttpResponse> CreateOrder()
    {
        OrdersCreateRequest oRequest = new OrdersCreateRequest();
        oRequest.Prefer("return=representation");
        //System.Net.ServicePointManager.Expect100Continue = true;
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        oRequest.RequestBody(BuildRequestBodyWithMinimumFields());
        //3. Call PayPal to set up a transaction
        PayPalHttp.HttpClient oClient = PayPalClient.client();
        oClient.SetConnectTimeout(new TimeSpan(0, 0, 0, 10, 0));
        var oResponse = await oClient.Execute(oRequest);
        var result = oResponse.Result<Order>();

        return oResponse;
}

在这里 jquery 调用

paypal.Buttons({
    style: {
        shape: 'rect',
        color: 'blue',
        layout: 'vertical',
        label: 'pay',

    },
    createOrder: function () {
        return fetch('/shop/paypal_test.aspx/CreateOrder', {
            method: 'post',
            headers: {
                'content-type': 'application/json'
            }
        }).then(function (res) {
            return res.json();
        }).then(function (data) {
            return data.orderID; // Use the same key name for order ID on the client and server
        });
    }
}).render('#paypal-button-container');

问题是对 oClient.Execute 的响应永远不会返回。

PayPalClient 已完全按照 SDK 示例构建。

查看 PayPal API 调用日志,API 被正确调用,并标有绿旗。

你有什么想法吗?

提前谢谢你

【问题讨论】:

    标签: c# paypal paypal-sandbox


    【解决方案1】:

    “永远不会回来”是什么意思?你说有一个“绿旗”,这是否意味着有一个 HTTP 2xx 成功响应?响应正文中是否有id

    如果您将响应正文转发到前端,则前端不应该寻找 data.orderID 键,因为除非您提供它,否则不会有这样的键。这是用于前端的更好示例,其内容为 data.id : https://developer.paypal.com/demo/checkout/#/pattern/server

    请注意,该示例还包括 onApprove 函数中的提取。您需要实现这样的路由来捕获您的订单,否则将不会创建交易。

    【讨论】:

      【解决方案2】:

      亲爱的, 我终于找到了解决办法。

      如前所述,API 调用后,在 developer.paypal 网站上,相关调用已被标记为绿色标志,表示调用已正确进行。

      然后,我研究了服务器代码,以及如何管理任务。

      异步调用嵌入在私有方法中,并由“等待”响应的同步方法调用。

      这样一切正常

      这里是允许例程工作而不被停止到“client.Execute(oRequest)”的sn-p代码。

      [WebMethod(EnableSession = true)]
      public static PayPalCheckoutSdk.Orders.Order SetupTransaction()
      {
          data.tessutigenovataddei.com.Order oOrder = null;
          //save order in database.
          oOrder = CreateOrderWithPaypal();
      
          OrdersCreateRequest oRequest = new OrdersCreateRequest();
          oRequest.Prefer("return=representation");
      
          System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
          oRequest.RequestBody(BuildRequestBodyWithMinimumFields(oOrder));
      
          PayPalHttp.HttpClient oClient = PayPalClient.client();
          oClient.SetConnectTimeout(new TimeSpan(0, 0, 0, 10, 0));
          var oResponseTask = SetupTransactionAsync(oClient, oRequest);
      
          oResponseTask.Wait();
      
          return oResponseTask.Result.Result<PayPalCheckoutSdk.Orders.Order>();
      }
      
      private async static Task<PayPalHttp.HttpResponse> SetupTransactionAsync(PayPalHttp.HttpClient client, OrdersCreateRequest request)
      {
          SynchronizationContext.SetSynchronizationContext(null);
          PayPalHttp.HttpResponse oResponse = await client.Execute(request);
      
          return oResponse;
      }
      

      感谢您的支持

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-16
        • 2012-04-15
        • 2014-06-21
        • 2011-12-26
        • 2023-03-08
        • 2023-03-12
        • 2014-06-17
        • 1970-01-01
        相关资源
        最近更新 更多