簡體   English   中英

解析PayPal REST信用卡交易響應(JSON)

[英]Parsing a PayPal REST Credit Card Transaction Response (JSON)

我正在通過C#ASP.NET 4.5框架網站使用最新版本的PayPal REST API進行PayPal和信用卡交易。 事務在沙盒中運行正常,響應顯示所有與事務相關的數據。

我要做的是使用標簽以更加用戶友好的方式顯示該信息。 如何將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; }
}

然后,您可以通過稍微修改代碼來獲得可用的對象。

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”時才顯示有用的消息。 這是錯誤狀態/消息的鏈接。

PayPal Rest API錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM