@@ -218,8 +218,6 @@ def create_enterprise_admin_portal_session(self, request, **kwargs):
218218
219219 Response structure defined here: https://docs.stripe.com/api/customer_portal/sessions/create
220220 """
221- customer_portal_session = None
222-
223221 enterprise_uuid = request .query_params .get ('enterprise_customer_uuid' )
224222 if not enterprise_uuid :
225223 msg = "enterprise_customer_uuid parameter is required."
@@ -230,31 +228,31 @@ def create_enterprise_admin_portal_session(self, request, **kwargs):
230228 origin_url = request .META .get ("HTTP_ORIGIN" )
231229
232230 if not checkout_intent :
231+ msg = f"No checkout intent for id, for enterprise_uuid: { enterprise_uuid } "
233232 logger .error (f"No checkout intent for id, for enterprise_uuid: { enterprise_uuid } " )
234- return Response (customer_portal_session , status = status .HTTP_404_NOT_FOUND )
233+ return Response (msg , status = status .HTTP_404_NOT_FOUND )
235234
236235 stripe_customer_id = checkout_intent .stripe_customer_id
237236 enterprise_slug = checkout_intent .enterprise_slug
238237
239238 if not (stripe_customer_id or enterprise_slug ):
240- logger .error (f"No stripe customer id or enterprise slug associated to enterprise_uuid:{ enterprise_uuid } " )
241- return Response (customer_portal_session , status = status .HTTP_404_NOT_FOUND )
239+ msg = f"No stripe customer id or enterprise slug associated to enterprise_uuid:{ enterprise_uuid } "
240+ logger .error (msg )
241+ return Response (msg , status = status .HTTP_404_NOT_FOUND )
242242
243243 try :
244244 customer_portal_session = stripe .billing_portal .Session .create (
245245 customer = stripe_customer_id ,
246246 return_url = f"{ origin_url } /billing-details/success" ,
247247 )
248248 except stripe .error .StripeError as e :
249- logger .exception (
250- f"StripeError creating billing portal session for CheckoutIntent { checkout_intent } : { e } " ,
251- )
252- return Response (customer_portal_session , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
249+ msg = f"StripeError creating billing portal session for CheckoutIntent { checkout_intent } : { e } "
250+ logger .exception (msg )
251+ return Response (msg , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
253252 except Exception as e : # pylint: disable=broad-except
254- logger .exception (
255- f"General exception creating billing portal session for CheckoutIntent { checkout_intent } : { e } " ,
256- )
257- return Response (customer_portal_session , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
253+ msg = f"General exception creating billing portal session for CheckoutIntent { checkout_intent } : { e } "
254+ logger .exception (msg )
255+ return Response (msg , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
258256
259257 # TODO: pull out session fields actually needed, and structure a response.
260258 return Response (
@@ -279,45 +277,46 @@ def create_checkout_portal_session(self, request, pk=None):
279277
280278 Response structure defined here: https://docs.stripe.com/api/customer_portal/sessions/create
281279 """
282- customer_portal_session = None
283280 origin_url = request .META .get ("HTTP_ORIGIN" )
284281 checkout_intent = CheckoutIntent .objects .filter (pk = int (pk )).first ()
285282
286283 if not checkout_intent :
287- logger .error (f"No checkout intent for id, for requesting user { request .user .id } " )
288- return Response (customer_portal_session , status = status .HTTP_404_NOT_FOUND )
284+ msg = f"No checkout intent for id, for requesting user { request .user .id } "
285+ logger .error (msg )
286+ return Response (msg , status = status .HTTP_404_NOT_FOUND )
289287
290288 stripe_customer_id = checkout_intent .stripe_customer_id
291289 if not stripe_customer_id :
292- logger .error (f"No stripe customer id associated to CheckoutIntent { checkout_intent } " )
293- return Response (customer_portal_session , status = status .HTTP_404_NOT_FOUND )
290+ msg = f"No stripe customer id associated to CheckoutIntent { checkout_intent } "
291+ logger .error (msg )
292+ return Response (msg , status = status .HTTP_404_NOT_FOUND )
294293
295294 if not checkout_intent :
295+ msg = f"No checkout intent for id { pk } "
296296 logger .error (f"No checkout intent for id { pk } " )
297- return Response (customer_portal_session , status = status .HTTP_404_NOT_FOUND )
297+ return Response (msg , status = status .HTTP_404_NOT_FOUND )
298298
299299 stripe_customer_id = checkout_intent .stripe_customer_id
300300 enterprise_slug = checkout_intent .enterprise_slug
301301
302302 if not (stripe_customer_id or enterprise_slug ):
303+ msg = f"No stripe customer id or enterprise slug associated to checkout_intent_id:{ pk } "
303304 logger .error (f"No stripe customer id or enterprise slug associated to checkout_intent_id:{ pk } " )
304- return Response (customer_portal_session , status = status .HTTP_404_NOT_FOUND )
305+ return Response (msg , status = status .HTTP_404_NOT_FOUND )
305306
306307 try :
307308 customer_portal_session = stripe .billing_portal .Session .create (
308309 customer = stripe_customer_id ,
309310 return_url = f"{ origin_url } /billing-details/success" ,
310311 )
311312 except stripe .error .StripeError as e :
312- logger .exception (
313- f"StripeError creating billing portal session for CheckoutIntent { checkout_intent } : { e } " ,
314- )
315- return Response (customer_portal_session , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
313+ msg = f"StripeError creating billing portal session for CheckoutIntent { checkout_intent } : { e } "
314+ logger .exception (msg )
315+ return Response (msg , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
316316 except Exception as e : # pylint: disable=broad-except
317- logger .exception (
318- f"General exception creating billing portal session for CheckoutIntent { checkout_intent } : { e } " ,
319- )
320- return Response (customer_portal_session , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
317+ msg = f"General exception creating billing portal session for CheckoutIntent { checkout_intent } : { e } "
318+ logger .exception (msg )
319+ return Response (msg , status = status .HTTP_422_UNPROCESSABLE_ENTITY )
321320
322321 # TODO: pull out session fields actually needed, and structure a response.
323322 return Response (
0 commit comments