0

我设法使用 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); 
}

并且能够得到更好的结果

4

1 回答 1

8

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

条带错误

您需要以不同的方式处理几种不同类型的错误。在上面的链接中可以看到,有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”之类的东西

于 2014-06-27T21:41:54.850 回答