【问题标题】:Try/Catch with Stripe.Net MVC Contoller使用 Stripe.Net MVC 控制器尝试/捕获
【发布时间】:2014-10-18 14:49:48
【问题描述】:

stripe.net 文档说您应该按如下方式处理错误:

任何服务上发生的任何错误都将引发 StripeException,并带有从 Stripe 返回的消息。在 try 中运行服务调用并捕获 StripeException 是个好主意。

您如何处理捕获错误并返回视图。

如果chargeService.Create失败并出现错误如何将对象stripeCharge返回给View: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);
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
            }

            return RedirectToAction("Index");
        }

        return View(stripeCharge);
    }

更新 经过进一步研究,这可能是solution

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

【问题讨论】:

    标签: asp.net-mvc try-catch stripe.net


    【解决方案1】:

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

    下面的代码 sn-p 应该是其documentaion page 上列出的一些各种错误的基本编程流程:

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

    【讨论】:

      【解决方案2】:

      尝试在内部捕获中使用 StripeException

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-11
        • 2016-03-14
        • 2015-10-14
        • 2020-09-25
        • 1970-01-01
        • 2019-05-04
        • 2016-01-05
        • 2023-03-07
        相关资源
        最近更新 更多