4

I am using the stripe library in Python for credit card charging. I use the customerID for purposes of charging rather than the token as I want to re-use the card without asking for it each time. The success process works just fine, however, if I create an error condition the "except" is never thrown. I am testing the failure state by using an invalid customer ID.

The server log shows the following error: "InvalidRequestError: Request req_8949iJfEmeX39p: No such customer: 22" but again this is not handled in try/except.

class ChargeCustomerCard(webapp2.RequestHandler):
def post(self):
    stripe.api_key = stripeApiKey
    customerID = self.request.get("cust")
    amount = self.request.get("amt")

    try:
        charge = stripe.Charge.create(amount=int(amount),currency="usd",customer=customerID,description=customerID)
    except stripe.error.CardError, e:
        output = {"result":e}
    else:
        output = {"result":"1"}

    self.response.out.write(json.dumps(output))
2
  • 1
    Add another except Exception as e clause below the specific stripe.error.CardError and before the else of your try block, and see if that gives you the appropriate error. If so, you may want to file an issue at github.com/stripe/stripe-python/issues unless it was intended not to be a stripe specific Exception/Error, or maybe there is another specific Error under stripe.error namespace you need to except Commented Mar 25, 2016 at 5:02
  • Yes, that is the correct approach. Thanks. Commented Mar 26, 2016 at 4:00

2 Answers 2

15

According to https://stripe.com/docs/api?lang=python#errors you're not handling all the possible errors/exceptions that the stripe library supplies. And dealing with financial data rightfully deserves even more caution so you really need to do at least something like:

class ChargeCustomerCard(webapp2.RequestHandler):
    def post(self):
        stripe.api_key = stripeApiKey
        customerID = self.request.get("cust")
        amount = self.request.get("amt")

        try:
            charge = stripe.Charge.create(amount=int(amount),currency="usd",customer=customerID,description=customerID)
        except stripe.error.CardError, e:
            output = {"result":e}
        except Exception as e:
            # handle this e, which could be stripe related, or more generic
            pass
        else:
            output = {"result":"1"}

        self.response.out.write(json.dumps(output))

Or even based on the official documentation, a more comprehensive one like:

try:
    # Use Stripe's library to make requests...
    pass
except stripe.error.CardError, e:
    # Since it's a decline, stripe.error.CardError will be caught
    body = e.json_body
    err  = body['error']

    print "Status is: %s" % e.http_status
    print "Type is: %s" % err['type']
    print "Code is: %s" % err['code']
    # param is '' in this case
    print "Param is: %s" % err['param']
    print "Message is: %s" % err['message']
except stripe.error.RateLimitError, e:
    # Too many requests made to the API too quickly
    pass
except stripe.error.InvalidRequestError, e:
    # Invalid parameters were supplied to Stripe's API
    pass
except stripe.error.AuthenticationError, e:
    # Authentication with Stripe's API failed
    # (maybe you changed API keys recently)
    pass
except stripe.error.APIConnectionError, e:
    # Network communication with Stripe failed
    pass
except stripe.error.StripeError, e:
    # Display a very generic error to the user, and maybe send
    # yourself an email
    pass
except Exception, e:
    # Something else happened, completely unrelated to Stripe
    pass
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct. In this instance, the exception that would be raised is InvalidRequestError as the customer ID is invalid.
Thanks for this approach!
2

I think the official documentation could provide a bit more complete boilerplate. This is what I ended up with:

except stripe.error.RateLimitError, e:
    # Too many requests made to the API too quickly
    err = e.json_body['error']
    lg.error("Stripe RateLimitError: %s" % (err))
    ...
except stripe.error.InvalidRequestError, e:
    # Invalid parameters were supplied to Stripe's API
    err = e.json_body['error']
    lg.error("Stripe InvalidRequestError: %s" % (err))
    ...

Which makes it a little more clear how you can deal with e to log some useful errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.