Asp.net mvc 使用Stripe.Net MVC控制器进行尝试/捕获

Asp.net mvc 使用Stripe.Net MVC控制器进行尝试/捕获,asp.net-mvc,try-catch,stripe.net,Asp.net Mvc,Try Catch,Stripe.net,文档中说,您应该按如下方式处理错误: 在任何服务上发生的任何错误都将抛出StripeeException,其中包含从Stripe返回的消息。在try-and-catch-StripeException中运行服务调用是一个好主意 如何处理捕获错误和返回视图的问题 如果chargeService.Create失败并出现错误,则如何将对象stripeCharge返回到视图:返回视图(stripeCharge) 更新 进一步研究后,这可能是一个 尝试在内部捕获中使用StripeException 通过这

文档中说,您应该按如下方式处理错误:

在任何服务上发生的任何错误都将抛出StripeeException,其中包含从Stripe返回的消息。在try-and-catch-StripeException中运行服务调用是一个好主意

如何处理捕获错误和返回视图的问题

如果chargeService.Create失败并出现错误,则如何将对象stripeCharge返回到视图:返回视图(stripeCharge)

更新 进一步研究后,这可能是一个


尝试在内部捕获中使用StripeException


通过这种方式,您可以确定应该采取什么条带操作,而不是低级异常

我现在正在处理相同的代码位。我相信您会希望以不同的方式处理错误,最有可能是“卡错误”,最有可能是“拒绝”或“不正确的cvc”

下面的代码段应该是其上列出的一些错误的基本编程流程:

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);
    }
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";
    }