【问题标题】:Stripe not Throwing Charging Error in PythonStripe 不会在 Python 中引发充电错误
【发布时间】:2016-07-12 20:21:45
【问题描述】:

我正在使用 Python 中的条带库进行信用卡收费。我将 customerID 用于收费而不是令牌,因为我想重复使用该卡而不是每次都要求它。成功过程工作得很好,但是,如果我创建错误条件,则永远不会抛出“except”。我正在使用无效的客户 ID 测试故障状态。

服务器日志显示以下错误:“InvalidRequestError: Request req_8949iJfEmeX39p: No such customer: 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))

【问题讨论】:

  • 在特定的stripe.error.CardError 下方和try 块的else 之前添加另一个except Exception as e 子句,看看这是否会给您带来适当的错误。如果是这样,您可能希望在 github.com/stripe/stripe-python/issues 提交问题,除非它不是特定于条带的异常/错误,或者在 stripe.error 命名空间下可能存在另一个特定错误,您需要 except
  • 是的,这是正确的方法。谢谢。

标签: python stripe-payments


【解决方案1】:

根据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

【讨论】:

  • 这是正确的。在这种情况下,将引发的异常是 InvalidRequestError,因为客户 ID 无效。
  • 感谢这种方法!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 2011-02-28
    • 1970-01-01
    • 2022-01-17
    • 2021-11-18
    • 2021-11-28
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多