insert_unclaim_signature.sql (2788B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2026 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 18 DROP FUNCTION IF EXISTS merchant_do_insert_unclaim_signature; 19 CREATE FUNCTION merchant_do_insert_unclaim_signature ( 20 IN in_order_id TEXT, 21 IN in_nonce_str TEXT, 22 IN in_notify_str TEXT, 23 IN in_h_contract_terms BYTEA, 24 IN in_nonce_sig BYTEA, 25 OUT out_found BOOL) 26 LANGUAGE plpgsql 27 AS $$ 28 DECLARE 29 my_expiration_time INT8; 30 my_order_serial INT8; 31 BEGIN 32 33 SELECT pay_deadline 34 ,order_serial 35 INTO my_expiration_time 36 ,my_order_serial 37 FROM merchant_contract_terms AS mct 38 WHERE mct.order_id=in_order_id 39 AND mct.contract_terms->>'nonce' = in_nonce_str 40 AND mct.h_contract_terms=in_h_contract_terms 41 AND NOT mct.paid 42 AND NOT EXISTS 43 (SELECT 1 44 FROM merchant_deposit_confirmations AS mdc 45 WHERE mdc.order_serial=mct.order_serial) 46 AND NOT EXISTS 47 (SELECT 1 48 FROM merchant_refunds AS mr 49 WHERE mr.order_serial=mct.order_serial) 50 AND NOT EXISTS 51 (SELECT 1 52 FROM merchant_order_token_blinded_sigs AS mots 53 WHERE mots.order_serial=mct.order_serial) 54 FOR UPDATE; 55 56 IF NOT FOUND 57 THEN 58 -- A repeated request after the contract row was removed is successful, 59 -- but an old proof must not release a contract claimed with a new nonce. 60 PERFORM FROM merchant_contract_terms 61 WHERE order_id = in_order_id; 62 IF FOUND 63 THEN 64 out_found = FALSE; 65 RETURN; 66 END IF; 67 PERFORM FROM merchant_unclaim_signatures 68 WHERE h_contract_terms = in_h_contract_terms 69 AND unclaim_sig = in_nonce_sig; 70 out_found = FOUND; 71 RETURN; 72 END IF; 73 74 INSERT INTO merchant_unclaim_signatures 75 (h_contract_terms 76 ,unclaim_sig 77 ,expiration_time) 78 VALUES 79 (in_h_contract_terms 80 ,in_nonce_sig 81 ,my_expiration_time) 82 ON CONFLICT DO NOTHING; 83 84 DELETE FROM merchant_contract_terms 85 WHERE order_serial=my_order_serial; 86 IF NOT FOUND 87 THEN 88 out_found = FALSE; 89 RETURN; 90 END IF; 91 92 out_found = TRUE; 93 94 -- order status change notification 95 EXECUTE FORMAT ( 96 'NOTIFY %s' 97 ,in_notify_str); 98 99 END $$;