forked from stripe/stripe-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.py
More file actions
41 lines (29 loc) · 989 Bytes
/
Copy pathwebhooks.py
File metadata and controls
41 lines (29 loc) · 989 Bytes
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
40
41
from __future__ import absolute_import, division, print_function
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)))