@@ -109,23 +109,18 @@ async def lnurl_params(request: Request, device_id: str, switch_id: str):
109109async def lnurl_callback (
110110 request : Request ,
111111 paymentid : str ,
112- pr : str = Query (None ),
113- k1 : str = Query (None ),
112+ amount : int = Query (..., description = "Amount in millisatoshis" ),
114113):
115114 payment = await get_payment (paymentid )
116115 if not payment :
117- raise HTTPException (
118- status_code = HTTPStatus .NOT_FOUND , detail = "payment not found."
119- )
116+ return {"status" : "ERROR" , "reason" : "Payment not found." }
120117
121118 if payment .payhash == "used" :
122119 return {"status" : "ERROR" , "reason" : "Payment already used." }
123120
124121 device = await get_device (payment .deviceid )
125122 if not device :
126- raise HTTPException (
127- status_code = HTTPStatus .NOT_FOUND , detail = "device not found."
128- )
123+ return {"status" : "ERROR" , "reason" : "Device not found." }
129124
130125 switch = None
131126 for _switch in device .switches :
@@ -134,32 +129,37 @@ async def lnurl_callback(
134129 break
135130
136131 if not switch :
137- raise HTTPException (
138- status_code = HTTPStatus .NOT_FOUND , detail = "device switch not found."
132+ return {"status" : "ERROR" , "reason" : "Switch not found." }
133+
134+ # Validate amount matches expected
135+ if amount != payment .sats :
136+ return {"status" : "ERROR" , "reason" : f"Amount mismatch. Expected { payment .sats } msats." }
137+
138+ try :
139+ payment_hash , payment_request = await create_invoice (
140+ wallet_id = device .wallet ,
141+ amount = int (amount / 1000 ),
142+ memo = create_payment_memo (device , switch ),
143+ unhashed_description = json .dumps (
144+ [["text/plain" , create_payment_memo (device , switch )]]
145+ ).encode (),
146+ extra = {
147+ "tag" : "DeviceTimer" ,
148+ "Device" : device .id ,
149+ "Switch" : switch .id ,
150+ "amount" : switch .amount ,
151+ "currency" : device .currency ,
152+ "id" : paymentid ,
153+ "received" : False ,
154+ "fulfilled" : False ,
155+ },
139156 )
140157
141- payment_hash , payment_request = await create_invoice (
142- wallet_id = device .wallet ,
143- amount = int (payment .sats / 1000 ),
144- memo = create_payment_memo (device , switch ),
145- unhashed_description = json .dumps (
146- [["text/plain" , create_payment_memo (device , switch )]]
147- ).encode (),
148- extra = {
149- "tag" : "DeviceTimer" ,
150- "Device" : device .id ,
151- "Switch" : switch .id ,
152- "amount" : switch .amount ,
153- "currency" : device .currency ,
154- "id" : paymentid ,
155- "received" : False ,
156- "fulfilled" : False ,
157- },
158- )
159-
160- await update_payment (payment_id = paymentid , payhash = payment_hash )
158+ await update_payment (payment_id = paymentid , payhash = payment_hash )
161159
162- return {
163- "pr" : payment_request ,
164- "routes" : [],
165- }
160+ return {
161+ "pr" : payment_request ,
162+ "routes" : [],
163+ }
164+ except Exception as e :
165+ return {"status" : "ERROR" , "reason" : str (e )}
0 commit comments