fix(webhooks): return 5xx on YooKassa processing errors

Replace 200-on-error with 500 so the payment provider retries transient
failures instead of treating them as successfully processed.
This commit is contained in:
3252a8
2026-04-27 08:43:44 +03:00
parent 604f0d9656
commit f1113eb80a
+12 -16
View File
@@ -486,10 +486,9 @@ async def yookassa_webhook_route(request: web.Request):
lknpd_service: Optional[LknpdService] = request.app.get('lknpd_service') lknpd_service: Optional[LknpdService] = request.app.get('lknpd_service')
async_session_factory: sessionmaker = request.app[ async_session_factory: sessionmaker = request.app[
'async_session_factory'] 'async_session_factory']
except KeyError as e_app_ctx: except KeyError:
logging.error( logging.exception(
f"KeyError accessing app context in yookassa_webhook_route: {e_app_ctx}.", "KeyError accessing app context in yookassa_webhook_route.")
exc_info=True)
return web.Response( return web.Response(
status=500, status=500,
text="Internal Server Error: Missing app context component") text="Internal Server Error: Missing app context component")
@@ -673,23 +672,20 @@ async def yookassa_webhook_route(request: web.Request):
logging.exception("Failed to cancel bind-only payment auth") logging.exception("Failed to cancel bind-only payment auth")
except Exception: except Exception:
logging.exception("Failed to handle bind-only waiting_for_capture webhook") logging.exception("Failed to handle bind-only waiting_for_capture webhook")
except Exception as e_webhook_db_processing: except Exception:
await session.rollback() await session.rollback()
logging.error( logging.exception(
f"Error processing YooKassa webhook event '{notification_object.event}' " "Error processing YooKassa webhook event '%s' for YK Payment ID %s in DB transaction.",
f"for YK Payment ID {payment_dict_for_processing.get('id')} in DB transaction: {e_webhook_db_processing}", notification_object.event,
exc_info=True) payment_dict_for_processing.get('id'))
return web.Response( return web.Response(
status=200, text="ok_internal_processing_error_logged") status=500, text="internal_processing_error")
return web.Response(status=200, text="ok") return web.Response(status=200, text="ok")
except json.JSONDecodeError: except json.JSONDecodeError:
logging.error("YooKassa Webhook: Invalid JSON received.") logging.error("YooKassa Webhook: Invalid JSON received.")
return web.Response(status=400, text="bad_request_invalid_json") return web.Response(status=400, text="bad_request_invalid_json")
except Exception as e_general_webhook: except Exception:
logging.error( logging.exception("YooKassa Webhook general processing error.")
f"YooKassa Webhook general processing error: {e_general_webhook}", return web.Response(status=500, text="internal_error")
exc_info=True)
return web.Response(status=200,
text="ok_general_internal_error_logged")