Skip to content

Commit 9a05167

Browse files
authored
Fix(MyECLPay): catch on retry checkout init (#821)
### Description * Retry only if relevant (user info was provided) * Catch a specific 401 exception * More expressive logs
1 parent 021d5e5 commit 9a05167

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

app/core/payment/payment_tool.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from helloasso_python.api.paiements_api import PaiementsApi
99
from helloasso_python.api_client import ApiClient
1010
from helloasso_python.configuration import Configuration
11+
from helloasso_python.exceptions import UnauthorizedException
1112
from 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

tests/test_payment.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import pytest_asyncio
66
from fastapi.testclient import TestClient
7+
from helloasso_python.exceptions import UnauthorizedException
78
from helloasso_python.models.hello_asso_api_v5_models_carts_init_checkout_body import (
89
HelloAssoApiV5ModelsCartsInitCheckoutBody,
910
)
@@ -496,7 +497,7 @@ def init_a_checkout_side_effect(
496497
if init_checkout_body.payer is not None:
497498
r = Response()
498499
r.status_code = 400
499-
raise Exception # noqa: TRY002
500+
raise UnauthorizedException
500501
return HelloAssoApiV5ModelsCartsInitCheckoutResponse(
501502
id=7,
502503
redirect_url=redirect_url,

0 commit comments

Comments
 (0)