-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
90 lines (70 loc) · 2.96 KB
/
views.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import stripe
from django.views import View, generic
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseServerError
from webapp.apps.users.models import Project
from . import webhooks
from .models import Customer
stripe.api_key = os.environ.get("STRIPE_SECRET")
wh_secret = os.environ.get("WEBHOOK_SECRET")
class Webhook(View):
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(Webhook, self).dispatch(*args, **kwargs)
def post(self, request):
payload = request.body
sig_header = request.META["HTTP_STRIPE_SIGNATURE"]
event = None
try:
event = stripe.Webhook.construct_event(payload, sig_header, wh_secret)
except ValueError as e:
# Invalid payload
return HttpResponse(status=400)
except stripe.error.SignatureVerificationError as e:
# Invalid signature
return HttpResponse(status=400)
webhooks.process_event(event)
return HttpResponse(status=200)
class UpdatePayment(View):
update_template = "billing/update_pmt_info.html"
add_template = "billing/add_pmt_info.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
if hasattr(request.user, "customer") and request.user.customer is not None:
return render(request, self.update_template)
else:
return render(request, self.add_template)
@method_decorator(login_required)
def post(self, request, *args, **kwargs):
# get stripe token and update web db and stripe db
stripe_token = request.POST["stripeToken"]
try:
if hasattr(request.user, "customer"):
request.user.customer.update_source(stripe_token)
else: # create customer.
stripe_customer = stripe.Customer.create(
email=request.user.email, source=stripe_token
)
customer = Customer.construct(stripe_customer, user=request.user)
if Project.objects.count() > 0:
customer.sync_subscriptions()
else:
print("No projects yet.")
return redirect("update_payment_done")
except Exception as e:
import traceback
traceback.print_exc()
msg = (
"Something has gone wrong. Contact us at admin@comp.com to "
"resolve this issue"
)
return HttpResponseServerError(msg)
class UpdatePaymentDone(generic.TemplateView):
template_name = "billing/update_pmt_info_done.html"
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)