exchange_do_trigger_kyc_rule_for_account.sql (4005B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2014--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 DROP FUNCTION IF EXISTS exchange_do_trigger_kyc_rule_for_account; 18 19 CREATE FUNCTION exchange_do_trigger_kyc_rule_for_account( 20 IN in_h_normalized_payto BYTEA, 21 IN in_account_pub BYTEA, -- can be NULL, if given, should be SET 22 IN in_merchant_pub BYTEA, -- can be NULL 23 IN in_payto_uri TEXT, -- can be NULL 24 IN in_h_full_payto BYTEA, 25 IN in_now INT8, 26 IN in_jmeasures JSONB, 27 IN in_display_priority INT4, 28 IN in_notify_s TEXT, 29 OUT out_legitimization_measure_serial_id INT8, 30 OUT out_bad_kyc_auth BOOL) 31 LANGUAGE plpgsql 32 AS $$ 33 DECLARE 34 my_rec RECORD; 35 my_is_wallet BOOL; 36 my_access_token BYTEA; 37 my_account_pub BYTEA; 38 my_reserve_pub BYTEA; 39 BEGIN 40 -- Note: in_payto_uri is allowed to be NULL *if* 41 -- in_h_normalized_payto is already in wire_targets 42 43 SELECT access_token 44 ,target_pub 45 INTO my_rec 46 FROM kyc_targets 47 WHERE h_normalized_payto=in_h_normalized_payto; 48 49 IF FOUND 50 THEN 51 -- Extract details, determine if KYC auth matches. 52 my_access_token = my_rec.access_token; 53 my_account_pub = my_rec.target_pub; 54 out_bad_kyc_auth = COALESCE ((my_account_pub != in_merchant_pub), TRUE); 55 ELSE 56 -- No constraint on merchant_pub, just create 57 -- the wire_target. 58 my_is_wallet 59 = (LOWER (SUBSTRING (in_payto_uri, 0, 23)) = 60 'payto://taler-reserve/') OR 61 (LOWER (SUBSTRING (in_payto_uri, 0, 28)) = 62 'payto://taler-reserve-http/'); 63 INSERT INTO kyc_targets 64 (h_normalized_payto 65 ,is_wallet 66 ,target_pub 67 ) VALUES ( 68 in_h_normalized_payto 69 ,my_is_wallet 70 ,in_account_pub 71 ) 72 RETURNING access_token 73 INTO my_access_token; 74 INSERT INTO wire_targets 75 (payto_uri 76 ,wire_target_h_payto 77 ,h_normalized_payto 78 ) VALUES ( 79 in_payto_uri 80 ,in_h_full_payto 81 ,in_h_normalized_payto 82 ); 83 out_bad_kyc_auth=TRUE; 84 END IF; 85 86 IF out_bad_kyc_auth 87 THEN 88 -- Check reserve_in wire transfers, we also 89 -- allow those reserve public keys for authentication! 90 PERFORM FROM reserves_in 91 WHERE wire_source_h_payto IN ( 92 SELECT wire_target_h_payto 93 FROM wire_targets 94 WHERE h_normalized_payto=in_h_normalized_payto 95 ) 96 AND reserve_pub = in_merchant_pub 97 ORDER BY execution_date DESC; 98 IF FOUND 99 THEN 100 out_bad_kyc_auth = FALSE; 101 END IF; 102 END IF; 103 104 -- First check if a perfectly equivalent legi measure 105 -- already exists, to avoid creating tons of duplicates. 106 UPDATE legitimization_measures 107 SET display_priority=GREATEST(in_display_priority,display_priority) 108 WHERE access_token=my_access_token 109 AND jmeasures=in_jmeasures 110 AND NOT is_finished 111 RETURNING legitimization_measure_serial_id 112 INTO out_legitimization_measure_serial_id; 113 114 IF NOT FOUND 115 THEN 116 INSERT INTO legitimization_measures 117 (access_token 118 ,start_time 119 ,jmeasures 120 ,display_priority 121 ) VALUES ( 122 my_access_token 123 ,in_now 124 ,in_jmeasures 125 ,in_display_priority) 126 RETURNING legitimization_measure_serial_id 127 INTO out_legitimization_measure_serial_id; 128 129 -- mark all other active measures finished! 130 UPDATE legitimization_measures 131 SET is_finished=TRUE 132 WHERE access_token=my_access_token 133 AND NOT is_finished 134 AND legitimization_measure_serial_id != out_legitimization_measure_serial_id; 135 END IF; 136 137 EXECUTE FORMAT ( 138 'NOTIFY %s' 139 ,in_notify_s); 140 141 142 END $$;