-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
277 lines (228 loc) · 9.21 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import os
import stripe
from django.views import View, generic
from django.shortcuts import render, redirect, reverse
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 django.urls import resolve, Resolver404
from webapp.apps.users.models import Project
from . import webhooks
from .email import send_teams_interest_mail
from .models import (
Customer,
Product,
Subscription,
SubscriptionItem,
UpdateStatus,
timestamp_to_datetime,
)
stripe.api_key = os.environ.get("STRIPE_SECRET")
wh_secret = os.environ.get("WEBHOOK_SECRET")
white_listed_urls = (
# next urls for plus plan
"/billing/upgrade/?selected_plan=plus",
"/billing/upgrade/monthly/?selected_plan=plus",
"/billing/upgrade/yearly/?selected_plan=plus",
# next urls for pro plan
"/billing/upgrade/?selected_plan=pro",
"/billing/upgrade/monthly/?selected_plan=pro",
"/billing/upgrade/yearly/?selected_plan=pro",
)
def update_payment(user, stripe_token):
if hasattr(user, "customer"):
user.customer.update_source(stripe_token)
else: # create customer.
stripe_customer = stripe.Customer.create(email=user.email, source=stripe_token)
customer = Customer.construct(stripe_customer, user=user)
if Project.objects.count() > 0:
customer.sync_subscriptions()
else:
print("No projects yet.")
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:
update_payment(request.user, stripe_token)
next_url = "update_payment_done"
to = request.GET.get("next", None)
if to is not None:
# Try to resolve the url.
try:
next_url = resolve(to).url_name
except Resolver404:
if to in white_listed_urls:
next_url = to
return redirect(next_url)
except Exception as e:
import traceback
traceback.print_exc()
msg = (
"Something has gone wrong. Contact us at admin@compute.studio 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)
def parse_upgrade_params(request):
upgrade_plan = request.GET.get("upgrade_plan", None)
if upgrade_plan is not None and upgrade_plan.lower() not in (
"free",
"plus",
"pro",
"team",
):
upgrade_plan = None
selected_plan = request.GET.get("selected_plan", None)
if selected_plan is not None and selected_plan.lower() not in (
"free",
"plus",
"pro",
"team",
):
selected_plan = None
return upgrade_plan, selected_plan
class Plans(View):
template_name = "billing/upgrade_plan.html"
default_duration = "monthly"
def get(self, request, *args, **kwargs):
current_plan = {"plan_duration": None, "name": "free"}
if getattr(request.user, "customer", None) is not None:
current_plan = request.user.customer.current_plan()
return redirect(
reverse(
"upgrade_plan_duration",
kwargs=dict(
plan_duration=current_plan["plan_duration"] or self.default_duration
),
)
)
class UpgradePlan(View):
template_name = "billing/upgrade_plan.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
plan_duration = kwargs.get("plan_duration")
upgrade_plan, selected_plan = parse_upgrade_params(request)
customer = getattr(request.user, "customer", None)
card_info = {"last4": None, "brand": None}
current_plan = {"plan_duration": None, "name": "free"}
if customer is not None:
card_info = customer.card_info()
product = Product.objects.get(name="Compute Studio Subscription")
current_plan = customer.current_plan()
result = UpdateStatus.nochange
new_plan = None
if upgrade_plan == "free":
new_plan = product.plans.get(nickname="Free Plan")
result = customer.update_plan(new_plan)
elif upgrade_plan == "plus":
if plan_duration == "monthly":
new_plan = product.plans.get(nickname="Monthly Plus Plan")
else:
new_plan = product.plans.get(nickname="Yearly Plus Plan")
result = customer.update_plan(new_plan)
elif upgrade_plan == "pro":
if plan_duration == "monthly":
new_plan = product.plans.get(nickname="Monthly Pro Plan")
else:
new_plan = product.plans.get(nickname="Yearly Pro Plan")
result = customer.update_plan(new_plan)
elif upgrade_plan == "team":
send_teams_interest_mail(customer.user)
current_plan = customer.current_plan()
if result != UpdateStatus.nochange:
next_url = reverse(
"upgrade_plan_duration_done",
kwargs=dict(
plan_duration=current_plan["plan_duration"] or plan_duration
),
)
return redirect(next_url)
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"selected_plan": selected_plan,
},
)
class UpgradePlanDone(View):
template_name = "billing/upgrade_plan.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
# plan_duration optionally given in url: /billing/upgrade/[plan_duration]/
plan_duration = kwargs.get("plan_duration", "monthly")
customer = getattr(request.user, "customer", None)
if customer is not None:
card_info = customer.card_info()
current_si = customer.current_plan(as_dict=False)
current_plan = customer.current_plan(si=current_si, as_dict=True)
plan_name = current_si.plan.nickname
else:
card_info = {"last4": None, "brand": None}
current_plan = {"plan_duration": None, "name": "free"}
plan_name = "Free Plan"
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"update_completed": True,
"update_completed_msg": f"You are now on the {plan_name}.",
},
)
class ListInvoices(View):
template_name = "billing/invoices.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
invoices = []
if getattr(request.user, "customer", None) is not None:
for invoice in request.user.customer.invoices():
invoices.append(
{
"hosted_invoice_url": invoice.hosted_invoice_url,
"created": timestamp_to_datetime(invoice.created).strftime(
"%Y-%m-%d"
),
"amount": "{0:.2f}".format(invoice.total / 100),
"invoice_pdf": invoice.invoice_pdf,
"status": invoice.status,
}
)
return render(request, self.template_name, context={"invoices": invoices})