解析 PayPal REST 信用卡交易响应 (JSON) [英] Parsing a PayPal REST Credit Card Transaction Response (JSON)

查看:21
本文介绍了解析 PayPal REST 信用卡交易响应 (JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 C# ASP.NET 4.5 框架网站使用最新版本的 PayPal REST API 进行 PayPal 和信用卡交易.交易在沙箱中完美运行,响应显示与交易相关的所有数据.

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 the sandbox and the response is displaying all the data associated with the transaction.

我想要做的是使用标签以更加用户友好的方式显示该信息.如何将 JSON 响应解析为标签或文本框?

What I want to do is display that information in a more user friendly way using labels. How do I parse the JSON response into labels or textboxes?

这是显示不友好响应的当前代码.

This is the current code that displays unfriendly response.

try
            {
                APIContext apiContext = Configuration.GetAPIContext();
                Payment createdPayment = pymnt.Create(apiContext);
                CurrContext.Items.Add("ResponseJson", JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented));
            }

            catch (PayPal.Exception.PayPalException ex)
            {
                if (ex.InnerException is PayPal.Exception.ConnectionException)
                {
                    Label4.Text = (((PayPal.Exception.ConnectionException)ex.InnerException).Response);
                }

                else
                {
                    Label4.Text = (ex.Message);
                }

                CurrContext.Items.Add("Error", ex.Message);
            }
            CurrContext.Items.Add("RequestJson", JObject.Parse(pymnt.ConvertToJson()).ToString(Formatting.Indented));

推荐答案

实际上我今晚必须这样做,而且工作量很大.以下是如何至少恢复结构化对象以使用和编写您自己的翻译类/方法.

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<Dictionary<string, string>> 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<PaypalApiError>(((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
        };
} 

您必须创建 ParsePaypalError() 方法以根据对您重要的内容返回错误.我没有看到一种简单的方法来解析它,当它是最常见的状态VALIDATION_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 错误

这篇关于解析 PayPal REST 信用卡交易响应 (JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆