insert_kyc_failure.sql (2321B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2024 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_account_kyc_set_failed; 19 CREATE FUNCTION merchant_do_account_kyc_set_failed ( 20 IN in_h_wire BYTEA, 21 IN in_exchange_url TEXT, 22 IN in_timestamp INT8, 23 IN in_exchange_http_status INT4, 24 IN in_kyc_ok BOOL, 25 IN in_notify_str TEXT, 26 IN in_notify2_str TEXT, 27 OUT out_no_account BOOL) 28 LANGUAGE plpgsql 29 AS $$ 30 DECLARE 31 my_account_serial INT8; 32 my_changed BOOL; 33 BEGIN 34 35 out_no_account=FALSE; 36 37 SELECT account_serial 38 INTO my_account_serial 39 FROM merchant_accounts 40 WHERE h_wire=in_h_wire; 41 42 IF NOT FOUND 43 THEN 44 out_no_account=TRUE; 45 RETURN; 46 END IF; 47 48 -- Serialize updates and compare status separately from polling bookkeeping. 49 SELECT ROW(kyc_ok, exchange_http_status, exchange_ec_code) 50 IS DISTINCT FROM 51 ROW(in_kyc_ok, in_exchange_http_status, 0) 52 INTO my_changed 53 FROM merchant_kyc 54 WHERE account_serial=my_account_serial 55 AND exchange_url=in_exchange_url 56 FOR UPDATE; 57 IF NOT FOUND 58 THEN 59 my_changed := TRUE; 60 END IF; 61 62 UPDATE merchant_kyc 63 SET kyc_timestamp=in_timestamp 64 ,kyc_ok=in_kyc_ok 65 ,exchange_http_status=in_exchange_http_status 66 ,exchange_ec_code=0 67 WHERE account_serial=my_account_serial 68 AND exchange_url=in_exchange_url; 69 70 IF NOT FOUND 71 THEN 72 73 INSERT INTO merchant_kyc 74 (kyc_timestamp 75 ,kyc_ok 76 ,account_serial 77 ,exchange_url 78 ,exchange_http_status) 79 VALUES 80 (in_timestamp 81 ,in_kyc_ok 82 ,my_account_serial 83 ,in_exchange_url 84 ,in_exchange_http_status); 85 END IF; 86 87 IF my_changed 88 THEN 89 EXECUTE FORMAT ('NOTIFY %s', in_notify_str); 90 EXECUTE FORMAT ('NOTIFY %s', in_notify2_str); 91 END IF; 92 93 94 -- Success! 95 END $$;