Python中的条带未引发计费错误

Python中的条带未引发计费错误,python,stripe-payments,Python,Stripe Payments,我正在使用Python中的stripe库进行信用卡收费。我使用customerID是为了收费,而不是为了代币,因为我想在每次都不要求的情况下重复使用该卡。但是,如果我创建了一个错误条件,则不会抛出“except”。我正在使用无效的客户ID测试故障状态 服务器日志显示以下错误:“InvalidRequestError:Request req_8949iJfEmeX39p:无此类客户:22”,但这同样不会在try/except中处理 class ChargeCustomerCard(webapp2.

我正在使用Python中的stripe库进行信用卡收费。我使用customerID是为了收费,而不是为了代币,因为我想在每次都不要求的情况下重复使用该卡。但是,如果我创建了一个错误条件,则不会抛出“except”。我正在使用无效的客户ID测试故障状态

服务器日志显示以下错误:“InvalidRequestError:Request 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))
据介绍,您没有处理条带库提供的所有可能的错误/异常。正确处理财务数据应该更加谨慎,因此您确实需要至少做以下事情:

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

我认为官方文件可以提供更完整的样板文件。这就是我的结局:

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以记录一些有用的错误。

在特定的
条带.error.CardError
下面和
try
块的
else
之前,添加另一个
异常作为e
子句,看看这是否给出了适当的错误。如果是这样,您可能希望在提交问题,除非该问题不是特定于条带的异常/错误,或者可能在
stripe.Error
命名空间下存在另一个特定错误。您需要
except
是,这是正确的方法。谢谢,这是正确的。在本例中,将引发的异常是
InvalidRequestError
,因为客户ID无效。感谢这种方法!