|
4 | 4 | import json |
5 | 5 | import logging |
6 | 6 |
|
7 | | -import requests |
8 | 7 | import stripe |
9 | 8 | from django.conf import settings |
10 | 9 | from django.http import HttpResponseServerError |
|
17 | 16 | from rest_framework.response import Response |
18 | 17 |
|
19 | 18 | from enterprise_access.apps.api import serializers |
20 | | -from enterprise_access.apps.api_client.lms_client import LmsApiClient |
21 | 19 | from enterprise_access.apps.core.constants import CUSTOMER_BILLING_CREATE_PORTAL_SESSION_PERMISSION |
22 | 20 | from enterprise_access.apps.customer_billing.api import ( |
23 | 21 | CreateCheckoutSessionValidationError, |
@@ -188,27 +186,52 @@ def create_checkout_session(self, request, *args, **kwargs): |
188 | 186 | methods=['get'], |
189 | 187 | url_path='create-portal-session', |
190 | 188 | ) |
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): |
194 | 196 | """ |
195 | 197 | Create a new Customer Portal Session. Response dict contains "url" key |
196 | 198 | that should be attached to a button that the customer clicks. |
197 | 199 |
|
198 | 200 | Response structure defined here: https://docs.stripe.com/api/customer_portal/sessions/create |
199 | 201 | """ |
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 | + |
202 | 222 | 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) |
212 | 235 |
|
213 | 236 | # TODO: pull out session fields actually needed, and structure a response. |
214 | 237 | return Response(customer_portal_session, status=status.HTTP_200_OK) |
|
0 commit comments