Paypal Expected an order id to be passed

前端 未结 4 474
梦如初夏
梦如初夏 2021-01-20 16:48

Hi I have not been able to solve this problem for 3 days. I have integrated Paypal smart button to my page and it works. 3 days ago Do not pass Pay-xxx directly gave an erro

4条回答
  •  孤城傲影
    2021-01-20 17:10

    If you are using .net, then in your create payment api call, return an object that contains the order id instead of returning the BraintreeHttp.HttpResponse

     [HttpPost]
        public async Task Post()
        {
            try
            {
                var request = new OrdersCreateRequest();
                request.Prefer("return=representation");
                request.RequestBody(BuildRequestBody());
                //3. Call PayPal to set up a transaction
                var response = await PayPalClient.client().Execute(request);
                Order order = new Order();
                var result = response.Result();
    
    
                if (result?.Status?.Trim()?.ToLower() == "created")
                {
                    order.OrderId = result.Id;
                }
    
                return order;
            }
            catch (Exception ex)
            {
                string m = ex.Message;
                throw;
            }
        }
    

    then on my js

     paypal.Buttons({
            createOrder: function () {
    
    
              return fetch('/api/paypal', {
                method: 'post',
                headers: {
                  'content-type': 'application/json'
                  }
              }).then(function(res) {
                return res.json();
              }).then(function (data) {
                return data.orderId; // the data is the order object returned from the api call, its not the BrainTree.Response object
              });
            }
    

    Sorry for sloppy code, I was still in the process of trying to figure out the problem. Hope you get the gist of the solution

提交回复
热议问题