-
Notifications
You must be signed in to change notification settings - Fork 5
/
webhooks.py
61 lines (45 loc) · 1.48 KB
/
webhooks.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import time
import stripe
from django.core.mail import send_mail
from .models import Event, Customer
from .events import get_customer_from_event
stripe.api_key = os.environ.get("STRIPE_SECRET")
def invoice_payment_failed(event, link="test"):
print("processing invoice.payment_failed event...")
send_mail(
"Compute Studio",
"Payment failed",
"Compute Studio <hank@compute.studio>",
["hank@compute.studio"],
fail_silently=False,
)
def customer_created(event):
print("processing customer.created event...")
customer = Customer.objects.get(stripe_id=event.data["object"]["id"])
send_mail(
"Compute Studio",
"Your payment method was set successfully! Please write back if you have any questions.",
"Compute Studio <hank@compute.studio>",
[customer.user.email],
fail_silently=False,
)
webhook_map = {
"invoice.payment_failed": invoice_payment_failed,
"customer.created": customer_created,
}
def process_event(stripe_event):
start = time.time()
print("got event: ")
print(stripe_event)
customer = get_customer_from_event(stripe_event)
event = Event.construct(stripe_event, customer=customer)
func = webhook_map.get(event.kind, None)
if func is not None:
try:
func(event)
except Exception as e:
import traceback
traceback.print_exc()
total_time = time.time() - start
print(f"done {total_time}")