0

I managed to use a version of the Strip.net dll to create a payment method but im having issues dealing with errors. I got as far as this.

try
{
    StripeCustomer current = GetCustomer();
    // int? days = getaTraildays();
    //if (days != null)
    //{
    int chargetotal = 300; //Convert.ToInt32((3.33*Convert.ToInt32(days)*100));
    var mycharge = new StripeChargeCreateOptions();
    mycharge.AmountInCents = chargetotal;
    mycharge.Currency = "USD";
    mycharge.CustomerId = current.Id;
    string key = "sk_test_XXX";
    var chargeservice = new StripeChargeService(key);
    StripeCharge currentcharge = chargeservice.Create(mycharge);
    //}
}        
catch (StripeException)
{
    lblerror.Text = "Please check your card information and try again";
}

it will catch the errors and let the user know that there was an issue but im to new at this to understand why it still displays the error if the process works. i know its an issues with the way the catch is written but im unsure as to how to process and everything i have tried so hard has failed. what i would like to do is have it redirect to another page. Any ideas

++Update

with some help from Olivier Jacot-Descombes i changed my code to

catch (StripeException ex) 
{ 
lblerror.Text = (ex.Message); 
}

and was able to get even better results

user3266908
  • 45
  • 1
  • 4
  • 11
  • Is it possible the code in your 'try' block is throwing a different exception? Otherwise (from the little code provided), a StripeException should be caught and suppressed. – jsirr13 Mar 31 '14 at 19:55
  • Your question appears to be about exceptions being thrown by a 3rd party API. I suggest you look up the error there. We are not the developers of that API. – tnw Mar 31 '14 at 19:56
  • 1
    Do you clear `lblerror.Text` before you enter the `try` block? – heijp06 Mar 31 '14 at 19:57
  • the Exception is being catch(ed) my problem is that it looks like all messages are thrown as Exceptions and i was wondering (if im saying this right). that if its not an Exception that would halt the code redirect the user to another page. is that possible ? – user3266908 Mar 31 '14 at 20:01
  • Try displaying the original error message `catch (StripeException ex) { lblerror.Text = ex.Message; }` – Olivier Jacot-Descombes Mar 31 '14 at 20:02
  • @ Olivier Jacot-Descombes, thanks... that works perfectly. i changed it to [ catch (StripeException ex) { lblerror.Text = (ex.Message); } ] and its a far better result than what i had in mind. thanks a million – user3266908 Mar 31 '14 at 20:08

1 Answers1

8

dunno if this is fully answered in the above comments but here's a little more about this: (special thanks @tnw for your absolutely useless comment)

Stripe Errors

There are several different kinds of errors that you'll want to handle differently. As you can see in the link above, there are api errors, invalid request errors, and card errors. You should handle all three differently since you probably don't wish to display api or internal errors to your users.

Once you are into the exception scope, the info you'll need is in the exception.StripeError object. There is exception.HttpStatusCode which I don't use and the exception.StripeError object which looks like this:

public class StripeError
{
    [JsonProperty("type")]
    public string ErrorType { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("param")]
    public string Parameter { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("error_description")]
    public string ErrorSubscription { get; set; }

    [JsonProperty("charge")]
    public string ChargeId { get; set; }
}

So you'll want something like this:

        catch (StripeException exception)
        {
            switch (exception.StripeError.ErrorType)
            {
                case "card_error":
                    //do some stuff, set your lblError or something like this
                    ModelState.AddModelError(exception.StripeError.Code, exception.StripeError.Message);

                    // or better yet, handle based on error code: exception.StripeError.Code

                    break;
                case "api_error":
                    //do some stuff
                    break;
                case "invalid_request_error":
                    //do some stuff
                    break;
                default:
                    throw;
            }
        }
        catch(Exception exception)
        { 
             etc...etc..

Make sure you put your StripeException catch first or you will get compile-time errors.

Inside the card_error case, you may also wish to take action based on the type of card error that occurred. There are like 12 of these (look at the link above) - things like "card_declined" or "invalid_cvc"

Marian Simonca
  • 1,472
  • 1
  • 16
  • 29
J Benjamin
  • 4,722
  • 6
  • 29
  • 39