use*_*908 0 c# asp.net stripe-payments stripe.net
我设法使用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";
}
Run Code Online (Sandbox Code Playgroud)
它将捕获错误并让用户知道存在问题,但我是新的,以了解为什么它仍然显示错误,如果该过程工作.我知道它的问题与捕获的方式有关,但我不确定如何处理,我尝试过的所有内容都失败了.我想做的是让它重定向到另一个页面.有任何想法吗
++更新
在Olivier Jacot-Descombes的帮助下,我改变了我的代码
catch (StripeException ex)
{
lblerror.Text = (ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
并且能够获得更好的结果
不知道如果在上面的评论中完全回答了这个问题,但是这里有更多关于此的内容:(特别感谢@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; }
}
Run Code Online (Sandbox Code Playgroud)
所以你会想要这样的东西:
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..
Run Code Online (Sandbox Code Playgroud)
确保首先放置StripeException catch,否则会出现编译时错误.
在card_error案例中,您可能还希望根据发生的卡错误类型采取措施.有12个(看看上面的链接) - 像"card_declined"或"invalid_cvc"这样的东西