1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
import os
import stripe
from flask import Flask, request
stripe.api_key = os.environ.get("STRIPE_SECRET_KEY")
webhook_secret = os.environ.get("WEBHOOK_SECRET")
app = Flask(__name__)
@app.route("/webhooks", methods=["POST"])
def webhooks():
payload = request.data.decode("utf-8")
received_sig = request.headers.get("Stripe-Signature", None)
try:
event = stripe.Webhook.construct_event(
payload, received_sig, webhook_secret
)
except ValueError:
print("Error while decoding event!")
return "Bad payload", 400
except stripe.error.SignatureVerificationError:
print("Invalid signature!")
return "Bad signature", 400
print(
"Received event: id={id}, type={type}".format(
id=event.id, type=event.type
)
)
return "", 200
if __name__ == "__main__":
app.run(port=int(os.environ.get("PORT", 5000)))
|