@@ -245,7 +245,9 @@ async def add_sub_event(
245245 data ["oid" ] = oid
246246 await self .subs .insert_one (data )
247247
248- def _get_sub_by_type_from_data (self , data : dict [str , object ]) -> Union [
248+ def _get_sub_by_type_from_data (
249+ self , data : dict [str , object ]
250+ ) -> Union [
249251 SubscriptionCreateOut ,
250252 SubscriptionImportOut ,
251253 SubscriptionUpdateOut ,
@@ -370,7 +372,7 @@ async def get_billing_portal_url(
370372 headers = {
371373 "Authorization" : "bearer " + external_subs_app_api_key
372374 },
373- json = req .dict (),
375+ json = req .model_dump (),
374376 raise_for_status = True ,
375377 ) as resp :
376378 json = await resp .json ()
@@ -394,41 +396,52 @@ async def get_execution_minutes_price(self, org: Organization):
394396 "GET" ,
395397 f"{ external_subs_app_api_url } /prices/additionalMinutes" ,
396398 headers = {
397- "Authorization" : "bearer " + external_subs_app_api_key
399+ "Authorization" : "bearer " + external_subs_app_api_key ,
398400 },
399401 raise_for_status = True ,
400402 ) as resp :
401- text = await resp .text ()
402- return AddonMinutesPricing . model_validate_json ( text )
403+ json = await resp .json ()
404+ return AddonMinutesPricing ( ** json )
403405 # pylint: disable=broad-exception-caught
404406 except Exception as exc :
405407 print ("Error fetching checkout url" , exc )
406408
407- async def get_checkout_url (self , org : Organization , minutes : int | None ):
409+ async def get_checkout_url (
410+ self ,
411+ org : Organization ,
412+ headers : dict [str , str ],
413+ minutes : int | None ,
414+ ):
408415 """Create checkout url for additional minutes"""
409416 if not org .subscription :
410417 raise HTTPException (
411418 status_code = 404 , detail = "Organization has no subscription"
412419 )
413420 subscription_id = org .subscription .subId
421+ return_url = f"{ get_origin (headers )} /orgs/{ org .slug } /settings/billing"
414422
415423 if external_subs_app_api_url :
416424 try :
417425 req = CheckoutAddonMinutesRequest (
418- orgId = str (org .id ), subId = subscription_id , minutes = minutes
426+ orgId = str (org .id ),
427+ subId = subscription_id ,
428+ minutes = minutes ,
429+ return_url = return_url ,
419430 )
420431 async with aiohttp .ClientSession () as session :
421432 async with session .request (
422433 "POST" ,
423434 f"{ external_subs_app_api_url } /checkout/additionalMinutes" ,
424435 headers = {
425- "Authorization" : "bearer " + external_subs_app_api_key
436+ "Authorization" : "bearer " + external_subs_app_api_key ,
437+ "Content-Type" : "application/json" ,
426438 },
427- json = req .model_dump_json (),
439+ json = req .model_dump (),
428440 raise_for_status = True ,
429441 ) as resp :
430- text = await resp .text ()
431- return CheckoutAddonMinutesResponse .model_validate_json (text )
442+ json = await resp .json ()
443+ print (f"get_checkout_url got response: { json } " )
444+ return CheckoutAddonMinutesResponse (** json )
432445 # pylint: disable=broad-exception-caught
433446 except Exception as exc :
434447 print ("Error fetching checkout url" , exc )
@@ -572,9 +585,10 @@ async def get_execution_minutes_price(
572585 response_model = CheckoutAddonMinutesResponse ,
573586 )
574587 async def get_execution_minutes_checkout_url (
588+ request : Request ,
575589 minutes : int | None = None ,
576590 org : Organization = Depends (org_ops .org_owner_dep ),
577591 ):
578- return await ops .get_checkout_url (org , minutes )
592+ return await ops .get_checkout_url (org , dict ( request . headers ), minutes )
579593
580594 return ops
0 commit comments