C6Silver
C6Silver

Reputation: 3357

Stripe not Throwing Charging Error in Python

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

Upvotes: 4

Views: 4676

Answers (2)

time e. less
time e. less

Reputation: 487

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.

Upvotes: 2

woozyking
woozyking

Reputation: 5220

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

Upvotes: 15

Related Questions