Skip to content

Commit 322882f

Browse files
committed
feat: update create billing portal session api
1 parent e87874e commit 322882f

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

enterprise_access/apps/api/v1/views/customer_billing.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import json
55
import logging
66

7-
import requests
87
import stripe
98
from django.conf import settings
109
from django.http import HttpResponseServerError
@@ -17,7 +16,6 @@
1716
from rest_framework.response import Response
1817

1918
from enterprise_access.apps.api import serializers
20-
from enterprise_access.apps.api_client.lms_client import LmsApiClient
2119
from enterprise_access.apps.core.constants import CUSTOMER_BILLING_CREATE_PORTAL_SESSION_PERMISSION
2220
from enterprise_access.apps.customer_billing.api import (
2321
CreateCheckoutSessionValidationError,
@@ -188,27 +186,52 @@ def create_checkout_session(self, request, *args, **kwargs):
188186
methods=['get'],
189187
url_path='create-portal-session',
190188
)
191-
# UUID in path is used as the "permission object" for role-based auth.
192-
@permission_required(CUSTOMER_BILLING_CREATE_PORTAL_SESSION_PERMISSION, fn=lambda request, pk: pk)
193-
def create_portal_session(self, request, pk=None, **kwargs):
189+
# TODO: determine permission on this API, must be authenticated at the very least
190+
# # UUID in path is used as the "permission object" for role-based auth.
191+
# @permission_required(
192+
# CUSTOMER_BILLING_CREATE_PORTAL_SESSION_PERMISSION,
193+
# fn=lambda request, pk: pk
194+
# )
195+
def create_portal_session(self, request, **kwargs):
194196
"""
195197
Create a new Customer Portal Session. Response dict contains "url" key
196198
that should be attached to a button that the customer clicks.
197199
198200
Response structure defined here: https://docs.stripe.com/api/customer_portal/sessions/create
199201
"""
200-
lms_client = LmsApiClient()
201-
# First, fetch the enterprise customer data.
202+
customer_portal_session = None
203+
checkout_intent_id = str(kwargs['pk'])
204+
205+
if not checkout_intent_id:
206+
logger.error("No checkout intent id provided to create portal session")
207+
return Response(customer_portal_session, status=status.HTTP_400_BAD_REQUEST)
208+
209+
checkout_intent = CheckoutIntent.objects.get(id=checkout_intent_id)
210+
211+
if not checkout_intent:
212+
logger.error(f"No checkout intent for id {checkout_intent_id}")
213+
return Response(customer_portal_session, status=status.HTTP_404_NOT_FOUND)
214+
215+
stripe_customer_id = checkout_intent.stripe_customer_id
216+
enterprise_slug = checkout_intent.enterprise_slug
217+
218+
if not (stripe_customer_id or enterprise_slug):
219+
logger.error(f"No stripe customer id or enterprise slug associated to checkout_intent_id:{checkout_intent_id}")
220+
return Response(customer_portal_session, status=status.HTTP_404_NOT_FOUND)
221+
202222
try:
203-
enterprise_customer_data = lms_client.get_enterprise_customer_data(pk)
204-
except requests.exceptions.HTTPError:
205-
return Response(None, status=status.HTTP_404_NOT_FOUND)
206-
207-
# Next, create a stripe customer portal session.
208-
customer_portal_session = stripe.billing_portal.Session.create(
209-
customer=enterprise_customer_data['stripe_customer_id'],
210-
return_url=f"https://portal.edx.org/{enterprise_customer_data['slug']}",
211-
)
223+
customer_portal_session = stripe.billing_portal.Session.create(
224+
customer=stripe_customer_id,
225+
return_url=f"https://portal.edx.org/{enterprise_slug}",
226+
)
227+
except stripe.error.StripeError as e:
228+
# Generic catch-all for other Stripe errors
229+
logger.error(f"StripeError: {e.user_message}")
230+
Response(customer_portal_session, status=status.HTTP_502_BAD_GATEWAY)
231+
except Exception as e:
232+
# Any other unexpected error
233+
logger.error(f"Unexpected error creating billing portal session, Error:{e}")
234+
Response(customer_portal_session, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
212235

213236
# TODO: pull out session fields actually needed, and structure a response.
214237
return Response(customer_portal_session, status=status.HTTP_200_OK)

0 commit comments

Comments
 (0)