Parsing a PayPal REST Credit Card Transaction Response (JSON)

后端 未结 1 1196
别跟我提以往
别跟我提以往 2021-01-17 00:52

I am using the latest release of PayPal\'s REST API for PayPal and Credit Card transactions via C# ASP.NET 4.5 framework website. The transactions are working perfectly in t

1条回答
  •  执笔经年
    2021-01-17 01:02

    I had to do this tonight actually and it was a bit of work. Here is how to at least get a structured object back to utilize and write your own translation class/method.

    public class PaypalApiError
    {
      public string name { get; set; }
      public string message { get; set; }
      public List> details { get; set; }
      public string debug_id { get; set; }
      public string information_link { get; set; }
    }
    

    Then you can get a usable object back by modifying your code slightly.

    catch (PayPal.Exception.PayPalException ex)
    {
        string error = "";
        if (ex.InnerException is PayPal.Exception.ConnectionException)
        {
            var paypalError = JsonConvert.DeserializeObject(((PayPal.Exception.ConnectionException)ex.InnerException).Response);
            // method below would parse name/details and return a error message
            error = ParsePaypalError(paypalError);
        }
        else
        {
            error = ex.Message;
        }
    
        return new PaymentDataResult(){ 
                Success = false,
                Message = error
            };
    } 
    

    You would have to create the ParsePaypalError() method to return the errors based on what matters to you. I don't see an easy way to parse it and just display useful messages out of the gate when it will be the most common status'VALIDATION_ERROR'. Here is a link to the error status/messages.

    PayPal Rest API Errors

    0 讨论(0)
提交回复
热议问题