-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
478 lines (402 loc) · 17 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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 django.utils.safestring import mark_safe
from webapp.apps.users.models import Project
from . import webhooks
from .models import (
Customer,
Product,
Subscription,
SubscriptionItem,
UpdateStatus,
timestamp_to_datetime,
)
from .utils import update_payment, has_payment_method
stripe.api_key = os.environ.get("STRIPE_SECRET")
wh_secret = os.environ.get("WEBHOOK_SECRET")
white_listed_urls = set(
[
# next urls for pro plan
"/billing/upgrade/?selected_plan=pro",
"/billing/upgrade/monthly/?selected_plan=pro",
"/billing/upgrade/yearly/?selected_plan=pro",
"/billing/upgrade/monthly/aftertrial/",
"/billing/upgrade/yearly/aftertrial/",
"/billing/upgrade/monthly/aftertrial/?selected_plan=pro",
"/billing/upgrade/yearly/aftertrial/?selected_plan=pro",
]
)
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", "pro",):
upgrade_plan = None
selected_plan = request.GET.get("selected_plan", None)
if selected_plan is not None and selected_plan.lower() not in ("free", "pro",):
selected_plan = None
return upgrade_plan, selected_plan
class Plans(View):
template_name = "billing/upgrade_plan.html"
default_duration = "yearly"
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()
if current_plan["name"] == "free":
duration = self.default_duration
else:
duration = current_plan["plan_duration"]
return redirect(
reverse("upgrade_plan_duration", kwargs=dict(plan_duration=duration))
)
class AutoUpgradeAfterTrial(View):
template_name = "billing/auto_upgrade.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
banner_msg = None
plan_duration = kwargs["plan_duration"]
customer: Customer = getattr(request.user, "customer", None)
url_duration, selected_plan = parse_upgrade_params(request)
next_url = request.GET.get("next", None)
if next_url is not None:
request.session["post_upgrade_url"] = next_url
if customer is None:
return redirect(
reverse(
"upgrade_plan_duration",
kwargs=dict(plan_duration=url_duration or "monthly"),
),
)
card_info = customer.card_info()
si = customer.current_plan(as_dict=False)
if si is None:
return redirect(
reverse(
"upgrade_plan_duration",
kwargs=dict(plan_duration=url_duration or "monthly"),
),
)
current_plan = customer.current_plan(si=si)
sub: Subscription = si.subscription
if not sub.is_trial():
return redirect(
reverse(
"upgrade_plan_duration",
kwargs=dict(plan_duration=current_plan["plan_duration"]),
),
)
trial_end = sub.trial_end.date()
cancel_at = sub.cancel_at
# User is still on trial but has already opted in.
if sub.is_trial() and cancel_at is None:
banner_msg = (
f"You will continue to be on the {si.plan.nickname} after your trial "
f"ends on {sub.trial_end.date()}. Thanks for opting-in and enjoy the rest of "
f"your trial!"
)
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"selected_plan": selected_plan,
"next": next_url,
"banner_msg": banner_msg,
"trial_end": trial_end,
"has_payment_method": has_payment_method(request.user),
},
)
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"selected_plan": selected_plan,
"next": next_url,
"banner_msg": banner_msg,
"trial_end": trial_end,
"cancel_at": cancel_at.date() if cancel_at is not None else None,
"has_payment_method": has_payment_method(request.user),
},
)
class AutoUpgradeAfterTrialConfirm(View):
template_name = "billing/upgrade_plan.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
banner_msg = None
plan_duration = kwargs["plan_duration"]
customer = getattr(request.user, "customer", None)
next_url = request.GET.get("next", None)
# Redirect user if they do not have payment info.
if not has_payment_method(request.user):
pmt_url = reverse("update_payment")
pmt_url += (
f"?next=/billing/upgrade/{plan_duration}/aftertrial/?selected_plan=pro"
)
return redirect(pmt_url)
card_info = customer.card_info()
current_si = customer.current_plan(as_dict=False)
if (
current_si is None # no subscription to upgrade.
or not current_si.subscription.is_trial() # no trial for opt-in after ending.
or current_si.subscription.cancel_at is None # already opt-ed in.
):
return redirect(
reverse(
"upgrade_plan_duration", kwargs=dict(plan_duration=plan_duration)
)
)
sub: Subscription = current_si.subscription
# Get plan for Pro subscription corresponding to selected duration.
product = Product.objects.get(name="Compute Studio Subscription")
if plan_duration == "monthly":
new_plan = product.plans.get(nickname="Monthly Pro Plan")
else:
new_plan = product.plans.get(nickname="Yearly Pro Plan")
if current_si.plan != new_plan:
stripe.SubscriptionItem.modify(
current_si.stripe_id, plan=new_plan.stripe_id
)
current_si.plan = new_plan
current_si.save()
stripe_sub = stripe.Subscription.modify(
current_si.subscription.stripe_id,
cancel_at=None,
cancel_at_period_end=False,
)
current_si.subscription.update_from_stripe_obj(stripe_sub)
current_plan = customer.current_plan(si=current_si)
if request.session.get("post_upgrade_url"):
return redirect(request.session.pop("post_upgrade_url"))
banner_msg = (
f"You will continue to be on the {current_si.plan.nickname} after your trial "
f"ends on {sub.trial_end.date()}. Thanks for opting-in and enjoy the rest of "
f"your trial!"
)
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"selected_plan": None,
"next": next_url,
"banner_msg": banner_msg,
"trial_end": current_si.subscription.trial_end.date(),
"cancel_at": None,
"has_payment_method": has_payment_method(request.user),
},
)
class UpgradePlan(View):
template_name = "billing/upgrade_plan.html"
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
banner_msg = None
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"}
next_url = request.GET.get("next", None)
cancel_at_period_end = False
cancel_at = None
if next_url is not None:
request.session["post_upgrade_url"] = next_url
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 == "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)
current_si = customer.current_plan(as_dict=False)
current_plan = customer.current_plan(si=current_si)
if result != UpdateStatus.nochange and request.session.get(
"post_upgrade_url"
):
return redirect(request.session.pop("post_upgrade_url"))
elif result != UpdateStatus.nochange:
done_next_url = reverse(
"upgrade_plan_duration_done",
kwargs=dict(
plan_duration=current_plan["plan_duration"] or plan_duration
),
)
return redirect(done_next_url)
if current_si is not None:
sub: Subscription = current_si.subscription
if sub is not None and sub.cancel_at is not None and sub.is_trial():
banner_msg = mark_safe(
f"""
<p>Your free C/S Pro trial ends on {sub.trial_end.date()}.</p>
<p>
<a class="btn btn-primary" href="/billing/upgrade/yearly/aftertrial/">
<strong>Upgrade to C/S Pro after trial</strong>
</a>
</p>
"""
)
elif sub is not None and sub.cancel_at is not None:
banner_msg = (
f"Your {current_si.plan.nickname} will be downgraded "
f"to a Free account on {sub.current_period_end.date()}."
)
cancel_at = sub.cancel_at.date()
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"selected_plan": selected_plan,
"next": next_url,
"banner_msg": banner_msg,
"cancel_at_period_end": cancel_at_period_end,
"cancel_at": cancel_at,
"has_payment_method": has_payment_method(request.user),
},
)
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")
cancel_at_period_end = False
cancel_at = None
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 = "Free Plan"
sub = None
if current_si is not None:
plan_name = current_si.plan.nickname
sub = current_si.subscription
if sub is not None and sub.cancel_at_period_end:
banner_msg = (
f"Your {current_si.plan.nickname} will be downgraded "
f"to a Free account on {sub.current_period_end.date()}."
)
cancel_at_period_end = True
if sub.cancel_at is None:
sub.update_from_stripe_obj(
stripe.Subscription.retrieve(sub.stripe_id)
)
cancel_at = sub.cancel_at.date()
else:
banner_msg = f"You are now on the {plan_name}."
else:
card_info = {"last4": None, "brand": None}
current_plan = {"plan_duration": None, "name": "free"}
plan_name = "Free Plan"
banner_msg = f"You are now on the {plan_name}."
return render(
request,
self.template_name,
context={
"plan_duration": plan_duration,
"current_plan": current_plan,
"card_info": card_info,
"banner_msg": banner_msg,
"cancel_at_period_end": cancel_at_period_end,
"cancel_at": cancel_at,
"has_payment_method": has_payment_method(request.user),
},
)
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})