@@ -214,6 +214,39 @@ def create_membership(access_type, org, user_id, **kwargs):
214214 # Add the user to account_holders group
215215 KeycloakService .join_account_holders_group ()
216216
217+ @staticmethod
218+ def _get_payment_method_descriptions (current_payment_method : str , new_payment_method : str ) -> str :
219+ """Get payment method descriptions for activity logging."""
220+ valid_payment_methods = [item .value for item in PaymentMethod ]
221+ new_method_description = (
222+ PaymentMethod (new_payment_method ).name if new_payment_method in valid_payment_methods else ""
223+ )
224+ if not current_payment_method :
225+ return new_method_description
226+ current_method_description = (
227+ PaymentMethod (current_payment_method ).name if current_payment_method in valid_payment_methods else ""
228+ )
229+ return f"{ current_method_description } |{ new_method_description } "
230+
231+ @staticmethod
232+ def _handle_pay_http_error_raise_business_exception (http_error : HTTPError ) -> None :
233+ """Handle HTTP error by extracting error info and raising BusinessException."""
234+ error_payload = http_error .response .json ()
235+ error_code = next (
236+ (error_payload [key ] for key in ["error" , "code" ] if key in error_payload ),
237+ Error .PAYMENT_ACCOUNT_UPSERT_FAILED ,
238+ )
239+ error_details = next (
240+ (
241+ error_payload [key ]
242+ for key in ["error_description" , "message" , "description" , "type" ]
243+ if key in error_payload
244+ ),
245+ "" ,
246+ )
247+ current_app .logger .error (f"Account create payment Error: { http_error } " )
248+ raise BusinessException (error_code , error_details ) from http_error
249+
217250 @staticmethod
218251 @user_context
219252 def _create_payment_settings (
@@ -229,19 +262,23 @@ def _create_payment_settings(
229262 pay_url = current_app .config .get ("PAY_API_URL" )
230263 pay_request = Org ._build_payment_request (org_model , payment_info , payment_method , mailing_address , ** kwargs )
231264 error_code = None
265+ current_payment_method = None
266+ token = RestService .get_service_account_token ()
232267
233268 if is_new_org :
234269 response = RestService .post (
235270 endpoint = f"{ pay_url } /accounts" ,
236271 data = pay_request ,
237- token = RestService . get_service_account_token () ,
272+ token = token ,
238273 raise_for_status = True ,
239274 )
240275 else :
276+ response = RestService .get (endpoint = f"{ pay_url } /accounts/{ org_model .id } " , token = token )
277+ current_payment_method = response .json ().get ("paymentMethod" )
241278 response = RestService .put (
242279 endpoint = f"{ pay_url } /accounts/{ org_model .id } " ,
243280 data = pay_request ,
244- token = RestService . get_service_account_token () ,
281+ token = token ,
245282 raise_for_status = True ,
246283 )
247284
@@ -252,42 +289,25 @@ def _create_payment_settings(
252289 payment_account_status = PaymentAccountStatus .PENDING
253290 case _:
254291 payment_account_status = PaymentAccountStatus .FAILED
255- error_payload = getattr (response , "json" , lambda : {})()
256- error_code = error_payload .get ("error" , "UNKNOWN_ERROR" )
292+ error_code = getattr (response , "json" , lambda : {})().get ("error" , "UNKNOWN_ERROR" )
257293 current_app .logger .error (f"Account create payment Error: { response .text } " )
258294
259295 if payment_account_status != PaymentAccountStatus .FAILED and payment_method :
260- payment_method_description = (
261- PaymentMethod (payment_method ).name
262- if payment_method in [item .value for item in PaymentMethod ]
263- else ""
296+ payment_method_descriptions = Org ._get_payment_method_descriptions (
297+ current_payment_method , payment_method
264298 )
265299 ActivityLogPublisher .publish_activity (
266300 Activity (
267301 org_model .id ,
268302 ActivityAction .PAYMENT_INFO_CHANGE .value ,
269303 name = org_model .name ,
270- value = payment_method_description ,
304+ value = payment_method_descriptions ,
271305 )
272306 )
273307 return payment_account_status , error_code
274308
275309 except HTTPError as http_error :
276- error_payload = http_error .response .json ()
277- error_code = next (
278- (error_payload [key ] for key in ["error" , "code" ] if key in error_payload ),
279- Error .PAYMENT_ACCOUNT_UPSERT_FAILED ,
280- )
281- error_details = next (
282- (
283- error_payload [key ]
284- for key in ["error_description" , "message" , "description" , "type" ]
285- if key in error_payload
286- ),
287- "" ,
288- )
289- current_app .logger .error (f"Account create payment Error: { http_error } " )
290- raise BusinessException (error_code , error_details ) from http_error
310+ return Org ._handle_pay_http_error_raise_business_exception (http_error )
291311
292312 @staticmethod
293313 def _build_payment_request (org_model : OrgModel , payment_info : dict , payment_method : str , mailing_address , ** kwargs ):
0 commit comments