From 4605116bd3fc3714452e8d5dcdd516da486ddf05 Mon Sep 17 00:00:00 2001 From: 9qeklajc <9qeklajc> Date: Fri, 25 Jul 2025 13:49:32 +0200 Subject: [PATCH] fix get refund when request failed --- router/payment/x_cashu.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/router/payment/x_cashu.py b/router/payment/x_cashu.py index 1b69c0b0..d6901a35 100644 --- a/router/payment/x_cashu.py +++ b/router/payment/x_cashu.py @@ -55,6 +55,24 @@ async def forward_to_upstream( stream=True, ) + if response.status_code != 200: + refund_token = await send_refund(amount, unit) + response = Response( + content=json.dumps( + { + "error": { + "message": "Error forwarding request to upstream", + "type": "upstream_error", + "code": response.status_code, + "refund_token": refund_token, + } + } + ), + status_code=response.status_code, + media_type="application/json", + ) + response.headers["X-Cashu"] = refund_token + return response if path.endswith("chat/completions"): result = await handle_x_cashu_chat_completion(response, amount, unit) background_tasks = BackgroundTasks()