Skip to content

Commit fbe5488

Browse files
committed
Dynamic return URL
1 parent 15c8daf commit fbe5488

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

payments/stripewrapper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,11 @@ def make_stripe_event(
319319
stripe: stripeapi, payload: str, sig_header: str, endpoint_secret: str
320320
):
321321
return stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
322+
323+
324+
@_as_safe_operation
325+
def get_customer_portal_url(stripe: stripeapi, customer_id, return_url):
326+
session = stripe.billing_portal.Session.create(
327+
customer=customer_id, return_url=return_url
328+
)
329+
return session.url

payments/views.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
from django.contrib import messages
66
from django.contrib.auth.decorators import user_passes_test
7+
from django.urls import reverse
78
from django.utils import formats
89
from django.utils.decorators import method_decorator
9-
from django.views.generic import FormView, TemplateView
10+
from django.views.generic import FormView, TemplateView, RedirectView
1011
from django.views.decorators.csrf import csrf_exempt
1112
from django.http import HttpResponse, HttpResponseBadRequest
1213
from django.conf import settings
@@ -383,25 +384,28 @@ def get_method_table(
383384

384385

385386
@method_decorator(require_setup_completed, name="dispatch")
386-
class PaymentsView(PaymentsTableMixin, TemplateView):
387+
class PaymentsView(PaymentsTableMixin, RedirectView):
387388
template_name = "payments/view.html"
388389

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"""
392392
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
397393

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")
401397

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")
403407

404-
return context
408+
return url
405409

406410

407411
@csrf_exempt

0 commit comments

Comments
 (0)