Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析PayPal REST信用卡交易响应(JSON)_C#_Json_Rest_Paypal - Fatal编程技术网

C# 解析PayPal REST信用卡交易响应(JSON)

C# 解析PayPal REST信用卡交易响应(JSON),c#,json,rest,paypal,C#,Json,Rest,Paypal,我正在通过C#ASP.NET 4.5框架网站为PayPal和信用卡交易使用最新版本的PayPal REST API。事务在沙箱中工作正常,响应显示与事务相关的所有数据 我想做的是使用标签以更用户友好的方式显示这些信息。如何将JSON响应解析为标签或文本框 这是显示不友好响应的当前代码 try { APIContext apiContext = Configuration.GetAPIContext(); Pa

我正在通过C#ASP.NET 4.5框架网站为PayPal和信用卡交易使用最新版本的PayPal REST API。事务在沙箱中工作正常,响应显示与事务相关的所有数据

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

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

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));

事实上,我今晚必须做这件事,这是一项艰巨的工作。下面是如何至少获取一个结构化对象,以便使用和编写您自己的翻译类/方法

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; }
}
公共类PaypalApiError
{
公共字符串名称{get;set;}
公共字符串消息{get;set;}
公共列表详细信息{get;set;}
公共字符串调试_id{get;set;}
公共字符串信息_链接{get;set;}
}
然后,您可以通过稍微修改代码来获得一个可用的对象

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
        };
} 
catch(PayPal.Exception.PayPalException-ex)
{
字符串错误=”;
if(例如InnerException是PayPal.Exception.ConnectionException)
{
var paypalError=JsonConvert.DeserializeObject(((PayPal.Exception.ConnectionException)ex.InnerException.Response);
//下面的方法将解析名称/详细信息并返回错误消息
error=ParsePaypalError(paypalError);
}
其他的
{
错误=例如消息;
}
返回新的PaymentDataResult(){
成功=错误,
消息=错误
};
} 
您必须创建ParsePaypalError()方法,根据对您重要的内容返回错误。我看不到一个简单的方法来解析它,当它是最常见的状态“VALIDATION_ERROR”时,只在大门外显示有用的消息。这里是指向错误状态/消息的链接


很抱歉这么晚才返回此帖子。你是说我必须解析返回的错误代码,以便用支付信息填充标签或文本框吗?如果你想得到详细的错误消息,可以。不过,我还是使用经典SDK进行信用卡支付。这比在WebForms中实现REST要容易得多。不幸的是,我在剩下的事情上浪费了一天。Classic SDK就在这里,有很好的例子:我只是尝试获取事务细节,比如令牌等。获取错误代码不是问题。我以前使用Classic,但现在随着新PayPal Advance解决方案的实施,您必须使用REST。所以我的问题仍然存在,如何获取交易详情?在您的示例中,createdPayment.id将具有交易id。createdPayment.state将创建/批准/失败/取消/过期。在pymnt.Create()调用之后(如果未引发异常),会自动填充这些值。