stripe.net文档要求您按照以下方式处理错误:
在任何服务上发生的任何错误都将引发带有从Stripe返回的消息的StripeException。尝试并捕获StripeException中运行服务调用是个好主意。
如何处理捕获错误并返回视图。
如果chargeService.Create失败并出现错误,那么如何返回对象stripeCharge,返回到视图:返回视图(StripeCharge);
public ActionResult Create(StripeCharge stripeCharge)
{
if (ModelState.IsValid)
{
var myPlan = new StripeChargeCreateOptions();
myPlan.Amount = stripeCharge.Amount;
try
{
var chargeService = new StripeChargeService();
StripeCharge response = chargeService.Create(myPlan);
}
catch (Exception e)
{
errorMessage = e.Message;
}
return RedirectToAction("Index");
}
return View(stripeCharge);
}经过进一步的研究,这可能是一个解决方案
public ActionResult Create(StripeCharge stripeCharge)
{
if (ModelState.IsValid)
{
var myPlan = new StripeChargeCreateOptions();
myPlan.Amount = stripeCharge.Amount;
try
{
var chargeService = new StripeChargeService();
StripeCharge response = chargeService.Create(myPlan);
return RedirectToAction("Index");
}
catch (Exception e)
{
errorMessage = e.Message;
return View(stripeCharge);
}
}
return View(stripeCharge);
}发布于 2015-07-03 20:02:06
我现在处理的是同样的代码。我相信您会希望以不同的方式处理错误,其中最有可能是“card_error”,最有可能是“拒绝”或“不正确的_cvc”。
下面的代码片段应该是文件页中列出的一些错误的基本编程流程:
try
{
var stripeCharge = chargeService.Create(myPlan);
return stripeCharge.Id;
}
catch (StripeException e)
{
switch (e.StripeError.ErrorType)
{
case "card_error":
switch (e.StripeError.Code)
{
case "incorrect_cvc":
// example error logger
ErrorLog.Enter(e.Message);
ErrorLog.Enter(e.HttpStatusCode);
ErrorLog.Enter(e.StripeError.ChargeId);
return "Incorrect CVC code";
case "card_declined":
// todo
return "";
case "processing_error":
// todo
return "";
}
return "Other Card Error";
case "api_error":
// todo
return "";
case "invalid_request_error":
// todo
return "";
}
return "Unknown Error";
}发布于 2015-02-18 01:23:35
尝试在内部捕获中使用StripeException
这样,您就可以确定应该采取什么条带操作,而不是低级别的异常。
https://stackoverflow.com/questions/26441034
复制相似问题