0

The stripe.net documentation says you should handle errors as follows:

Any errors that occur on any of the services will throw a StripeException with the message returned from Stripe. It is a good idea to run your service calls in a try and catch StripeException.

How do you handle catching the errors and returning the view.

If the chargeService.Create fails with an error how return the object, stripeCharge, back to the 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);
    }

Update upon further research this this might be a 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);
    }
Community
  • 1
  • 1
Barry MSIH
  • 3,525
  • 5
  • 32
  • 53

2 Answers2

0

Try using StripeException in a inner catch

This way you are able to determine what STRIPE action should be taken instead of a the low level Exception

HoopSnake
  • 716
  • 1
  • 5
  • 17
0

I'm dealing with the same bit of code now. I believe you would want to handle errors differently, with the most likely being a 'card_error' and the most likely of those being 'declined' or 'incorrect_cvc'.

The code snippet below should be the basic programmatic flow for some of the various errors listed on their 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";
    }
Evan Morrison
  • 652
  • 6
  • 18