88from helloasso_python .api .paiements_api import PaiementsApi
99from helloasso_python .api_client import ApiClient
1010from helloasso_python .configuration import Configuration
11+ from helloasso_python .exceptions import UnauthorizedException
1112from helloasso_python .models .hello_asso_api_v5_models_carts_checkout_payer import (
1213 HelloAssoApiV5ModelsCartsCheckoutPayer ,
1314)
@@ -187,8 +188,6 @@ async def init_checkout(
187188 ).model_dump (),
188189 )
189190
190- # TODO: if payment fail, we can retry
191- # then try without the payer infos
192191 response : HelloAssoApiV5ModelsCartsInitCheckoutResponse
193192 with ApiClient (configuration ) as api_client :
194193 checkout_api = CheckoutApi (api_client )
@@ -197,47 +196,60 @@ async def init_checkout(
197196 self ._helloasso_slug ,
198197 init_checkout_body ,
199198 )
200- except Exception :
199+ except UnauthorizedException :
201200 # We know that HelloAsso may refuse some payer infos, like using the firstname "test"
202201 # Even when prefilling the payer infos,the user will be able to edit them on the payment page,
203202 # so we can safely retry without the payer infos
204- payer_user_name = ""
205- if payer_user :
203+ if not payer_user :
204+ hyperion_error_logger .exception (
205+ f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } (no payer info provided)." ,
206+ )
207+ else :
206208 payer_user_name = f"{ payer_user .firstname } { payer_user .name } "
207- hyperion_error_logger .warning (
208- f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } . Retrying without payer { payer_user_name } infos" ,
209- )
209+ hyperion_error_logger .warning (
210+ f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } . Retrying without payer infos for { payer_user_name } " ,
211+ )
212+
213+ init_checkout_body .payer = None
214+ try :
215+ response = checkout_api .organizations_organization_slug_checkout_intents_post (
216+ self ._helloasso_slug ,
217+ init_checkout_body ,
218+ )
219+ except UnauthorizedException :
220+ # HelloAsso returned a 401 unauthorized again
221+ hyperion_error_logger .exception (
222+ f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } , with and without payer { payer_user_name } infos" ,
223+ )
224+
225+ if response and response .id :
226+ checkout_model = models_payment .Checkout (
227+ id = checkout_model_id ,
228+ module = module ,
229+ name = checkout_name ,
230+ amount = checkout_amount ,
231+ hello_asso_checkout_id = response .id ,
232+ secret = secret ,
233+ )
210234
211- init_checkout_body .payer = None
212- response = checkout_api .organizations_organization_slug_checkout_intents_post (
213- self ._helloasso_slug ,
214- init_checkout_body ,
215- )
235+ await cruds_payment .create_checkout (db = db , checkout = checkout_model )
216236
217- if response . id is None :
218- hyperion_error_logger . error (
219- f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } . No checkout id returned" ,
237+ return schemas_payment . Checkout (
238+ id = checkout_model_id ,
239+ payment_url = response . redirect_url ,
220240 )
221- raise MissingHelloAssoCheckoutIdError () # noqa: TRY301
222-
223- checkout_model = models_payment .Checkout (
224- id = checkout_model_id ,
225- module = module ,
226- name = checkout_name ,
227- amount = checkout_amount ,
228- hello_asso_checkout_id = response .id ,
229- secret = secret ,
241+ hyperion_error_logger .error (
242+ f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } . No checkout id returned" ,
230243 )
244+ raise MissingHelloAssoCheckoutIdError () # noqa: TRY301
231245
232- await cruds_payment .create_checkout (db = db , checkout = checkout_model )
233-
234- return schemas_payment .Checkout (
235- id = checkout_model_id ,
236- payment_url = response .redirect_url ,
237- )
238246 except Exception :
247+ # Different from a 401 unauthorized
248+ payer_user_name = ""
249+ if payer_user :
250+ payer_user_name = f"{ payer_user .firstname } { payer_user .name } "
239251 hyperion_error_logger .exception (
240- f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } " ,
252+ f"Payment: failed to init a checkout with HA for module { module } and name { checkout_name } with payer { payer_user_name } infos " ,
241253 )
242254 raise
243255
0 commit comments