|
4 | 4 |
|
5 | 5 | from django.contrib import messages |
6 | 6 | from django.contrib.auth.decorators import user_passes_test |
| 7 | +from django.urls import reverse |
7 | 8 | from django.utils import formats |
8 | 9 | from django.utils.decorators import method_decorator |
9 | | -from django.views.generic import FormView, TemplateView |
| 10 | +from django.views.generic import FormView, TemplateView, RedirectView |
10 | 11 | from django.views.decorators.csrf import csrf_exempt |
11 | 12 | from django.http import HttpResponse, HttpResponseBadRequest |
12 | 13 | from django.conf import settings |
@@ -383,25 +384,28 @@ def get_method_table( |
383 | 384 |
|
384 | 385 |
|
385 | 386 | @method_decorator(require_setup_completed, name="dispatch") |
386 | | -class PaymentsView(PaymentsTableMixin, TemplateView): |
| 387 | +class PaymentsView(PaymentsTableMixin, RedirectView): |
387 | 388 | template_name = "payments/view.html" |
388 | 389 |
|
389 | | - def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: |
390 | | - context = super().get_context_data(**kwargs) |
391 | | - |
| 390 | + def get_redirect_url(self, *args: Any, **kwargs: Any) -> str: |
| 391 | + """Redirects to the Stripe customer portal for the user""" |
392 | 392 | customer = self.request.user.alumni.membership.customer |
393 | | - context["user"] = self.request.user |
394 | | - |
395 | | - invoices, error = self.__class__.get_invoice_table(customer) |
396 | | - context["invoices"] = invoices |
397 | 393 |
|
398 | | - if error is None: |
399 | | - methods, error = self.__class__.get_method_table(customer) |
400 | | - context["methods"] = methods |
| 394 | + # if the user is not a member, redirect to the signup page |
| 395 | + if customer is None: |
| 396 | + return self.reverse("setup_signup") |
401 | 397 |
|
402 | | - context["error"] = error |
| 398 | + # otherwise, redirect to the stripe customer portal |
| 399 | + portal_url = self.request.build_absolute_uri(reverse("portal")) |
| 400 | + url, err = stripewrapper.get_customer_portal_url(customer, portal_url) |
| 401 | + if err is not None: |
| 402 | + messages.error( |
| 403 | + self.request, |
| 404 | + "Something went wrong when trying to redirect you to the payment portal. Please try again later or contact support. ", |
| 405 | + ) |
| 406 | + return self.reverse("payments_view") |
403 | 407 |
|
404 | | - return context |
| 408 | + return url |
405 | 409 |
|
406 | 410 |
|
407 | 411 | @csrf_exempt |
|
0 commit comments