首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试/捕捉Stripe.Net MVC Contoller

尝试/捕捉Stripe.Net MVC Contoller
EN

Stack Overflow用户
提问于 2014-10-18 14:49:48
回答 2查看 527关注 0票数 0

stripe.net文档要求您按照以下方式处理错误:

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

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

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

代码语言:javascript
复制
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);
    }

经过进一步的研究,这可能是一个解决方案

代码语言:javascript
复制
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);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-03 20:02:06

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

下面的代码片段应该是文件页中列出的一些错误的基本编程流程:

代码语言:javascript
复制
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";
    }
票数 0
EN

Stack Overflow用户

发布于 2015-02-18 01:23:35

尝试在内部捕获中使用StripeException

这样,您就可以确定应该采取什么条带操作,而不是低级别的异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26441034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档