Python中的Stripe不会引发计费错误

时间:2016-03-25 04:04:20

标签: python stripe-payments

我在Python中使用条带库进行信用卡收费。我使用customerID来充电而不是令牌,因为我想重新使用卡而不是每次都要求它。成功过程工作正常,但是,如果我创建一个错误条件"除了"永远不会抛出。我正在使用无效的客户ID测试故障状态。

服务器日志显示以下错误:" InvalidRequestError:请求req_8949iJfEmeX39p:没有这样的客户:22"但是再次这不是在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 个答案:

答案 0 :(得分:11)

根据https://stripe.com/docs/api?lang=python#errors,您没有处理条带库提供的所有可能的错误/异常。正确地处理财务数据应该更加谨慎,所以你真的需要做至少类似的事情:

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

甚至基于官方文档,更全面的文档如:

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

答案 1 :(得分:1)

我认为官方文档可以提供更完整的样板。这就是我最终的结果:

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

这让你更清楚如何处理e来记录一些有用的错误。