【问题标题】:Stripe C# error handlingStripe C# 错误处理
【发布时间】:2014-05-11 08:15:05
【问题描述】:

我设法使用 Strip.net dll 的一个版本来创建付款方式,但我在处理错误时遇到了问题。我已经做到了。

try
{
    StripeCustomer current = GetCustomer();
    // int? days = getaTraildays();
    //if (days != null)
    //{
    int chargetotal = 300; //Convert.ToInt32((3.33*Convert.ToInt32(days)*100));
    var mycharge = new StripeChargeCreateOptions();
    mycharge.AmountInCents = chargetotal;
    mycharge.Currency = "USD";
    mycharge.CustomerId = current.Id;
    string key = "sk_test_XXX";
    var chargeservice = new StripeChargeService(key);
    StripeCharge currentcharge = chargeservice.Create(mycharge);
    //}
}        
catch (StripeException)
{
    lblerror.Text = "Please check your card information and try again";
}

它将捕获错误并让用户知道存在问题,但我对此很陌生,以了解为什么如果进程正常,它仍然会显示错误。我知道它的写法有问题,但我不确定如何处理,我努力尝试的一切都失败了。我想做的是让它重定向到另一个页面。任何想法

++更新

在 Olivier Jacot-Descombes 的帮助下,我将代码更改为

catch (StripeException ex) 
{ 
lblerror.Text = (ex.Message); 
}

并且能够获得更好的结果

【问题讨论】:

  • “try”块中的代码是否可能引发不同的异常?否则(根据提供的小代码),应该捕获并抑制 StripeException。
  • 您的问题似乎与第 3 方 API 引发的异常有关。我建议你在那里查找错误。我们不是该 API 的开发者。
  • 在进入try 块之前,您是否清除了lblerror.Text
  • 异常被捕获(编辑)我的问题是,看起来所有消息都作为异常抛出,我想知道(如果我说得对)。如果它不是会停止代码将用户重定向到另一个页面的异常。这可能吗?
  • 尝试显示原始错误消息catch (StripeException ex) { lblerror.Text = ex.Message; }

标签: c# asp.net stripe-payments stripe.net


【解决方案1】:

不知道上面的 cmets 是否完全回答了这个问题,但这里有更多关于这个的内容:(特别感谢 @tnw 的绝对无用的评论)

Stripe Errors

您需要以不同的方式处理多种不同类型的错误。在上面的链接中可以看到,有api错误、无效请求错误和卡错误。您应该以不同的方式处理这三个问题,因为您可能不希望向用户显示 api 或内部错误。

一旦您进入异常范围,您需要的信息就在 exception.StripeError 对象中。有我不使用的 exception.HttpStatusCode 和看起来像这样的 exception.StripeError 对象:

public class StripeError
{
    [JsonProperty("type")]
    public string ErrorType { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("param")]
    public string Parameter { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("error_description")]
    public string ErrorSubscription { get; set; }

    [JsonProperty("charge")]
    public string ChargeId { get; set; }
}

所以你会想要这样的东西:

        catch (StripeException exception)
        {
            switch (exception.StripeError.ErrorType)
            {
                case "card_error":
                    //do some stuff, set your lblError or something like this
                    ModelState.AddModelError(exception.StripeError.Code, exception.StripeError.Message);

                    // or better yet, handle based on error code: exception.StripeError.Code

                    break;
                case "api_error":
                    //do some stuff
                    break;
                case "invalid_request_error":
                    //do some stuff
                    break;
                default:
                    throw;
            }
        }
        catch(Exception exception)
        { 
             etc...etc..

请确保您将 StripeException 捕获放在首位,否则您将收到编译时错误。

在 card_error 案例中,您可能还希望根据发生的卡错误类型采取措施。其中有 12 个(请看上面的链接)——诸如“card_declined”或“invalid_cvc”之类的东西

【讨论】:

    猜你喜欢
    • 2015-07-01
    • 2021-11-08
    • 2015-02-24
    • 1970-01-01
    • 2019-04-26
    • 2019-11-25
    • 1970-01-01
    • 2011-06-07
    • 2011-11-19
    相关资源
    最近更新 更多