[英]Parsing a PayPal REST Credit Card Transaction Response (JSON)
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. 我正在通过C#ASP.NET 4.5框架网站使用最新版本的PayPal REST API进行PayPal和信用卡交易。 The transactions are working perfectly in the sandbox and the response is displaying all the data associated with the transaction. 事务在沙盒中运行正常,响应显示所有与事务相关的数据。
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? 如何将JSON响应解析为标签或文本框?
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
};
}
You would have to create the ParsePaypalError() method to return the errors based on what matters to you. 您将必须创建ParsePaypalError()方法来根据对您而言重要的错误返回错误。 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'. 我没有一种简单的方法来解析它,而只是在最常见的状态为“ VALIDATION_ERROR”时才显示有用的消息。 Here is a link to the error status/messages. 这是错误状态/消息的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.