2016-03-25 4 views
0

私はPythonでストライプライブラリを使用してクレジットカードの請求を行っています。私は毎回それを要求せずにカードを再利用したいので、トークンではなく請求の目的でcustomerIDを使用します。成功プロセスは正常に動作しますが、エラー条件を作成すると「例外」がスローされることはありません。無効な顧客IDを使用して障害状態をテストしています。Pythonでストライピングが発生しない充電エラー

サーバーログに「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)) 
+1

特定の 'stripe.error.CardError'の下と' try'ブロックの 'else'の前に' except Exception as e'句を追加し、それがあなたに適切なエラーを与えるかどうかを確認してください。そうであれば、ストライプ固有の例外/エラーでないことを意図していない限り、https://github.com/stripe/stripe-python/issuesで問題を提出することができます。または、ストライプ.error'あなたが 'except'に必要な名前空間 – woozyking

+0

はい、正しいアプローチです。ありがとう。 – C6Silver

答えて

4

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 
+0

これは正しいです。この例では、顧客IDが無効であるために発生する例外は 'InvalidRequestError'です。 – Ywain

+0

このアプローチに感謝します! – C6Silver

0

私、あなたが本当に好き、少なくとも何かをする必要があるので、財務データを扱うことは当然もっと注意に値します公式の文書がもう少し完全な定型文を提供できると思う。これは私が最後にしたものです:

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をどのように扱うことができるかをもう少し明確にしています。

関連する問題