activate_account.sql (3565B)
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 DROP FUNCTION IF EXISTS merchant_do_activate_account; 18 CREATE FUNCTION merchant_do_activate_account( 19 IN in_h_wire BYTEA 20 ,IN in_salt BYTEA 21 ,IN in_full_payto TEXT 22 ,IN in_credit_facade_url TEXT -- can be NULL 23 ,IN in_credit_facade_credentials TEXT -- can be NULL 24 ,IN in_extra_wire_subject_metadata TEXT -- can be NULL 25 ,OUT out_h_wire BYTEA 26 ,OUT out_salt BYTEA 27 ,OUT out_not_found BOOL 28 ,OUT out_conflict BOOL 29 ) 30 LANGUAGE plpgsql 31 AS $$ 32 DECLARE my_active BOOL; 33 DECLARE my_cfu TEXT; 34 DECLARE my_cfc TEXT; 35 DECLARE my_ewsm TEXT; 36 DECLARE my_h_wire BYTEA; 37 DECLARE my_salt BYTEA; 38 BEGIN 39 out_not_found = FALSE; 40 out_conflict = FALSE; 41 out_h_wire = in_h_wire; 42 out_salt = in_salt; 43 44 INSERT INTO merchant_accounts 45 AS ma 46 (h_wire 47 ,salt 48 ,payto_uri 49 ,credit_facade_url 50 ,credit_facade_credentials 51 ,active 52 ,extra_wire_subject_metadata 53 ) VALUES ( 54 in_h_wire 55 ,in_salt 56 ,in_full_payto 57 ,in_credit_facade_url 58 ,in_credit_facade_credentials::JSONB 59 ,TRUE 60 ,in_extra_wire_subject_metadata 61 ) ON CONFLICT DO NOTHING; 62 IF FOUND 63 THEN 64 -- Notify taler-merchant-kyccheck about the change in 65 -- accounts. (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED) 66 NOTIFY XDQM4Z4N0D3GX0H9JEXH70EBC2T3KY7HC0TJB0Z60D2H781RXR6AG; 67 RETURN; 68 END IF; 69 70 SELECT h_wire 71 ,salt 72 ,active 73 ,credit_facade_url 74 ,credit_facade_credentials::TEXT 75 ,extra_wire_subject_metadata 76 INTO my_h_wire 77 ,my_salt 78 ,my_active 79 ,my_cfu 80 ,my_cfc 81 ,my_ewsm 82 FROM merchant_accounts 83 WHERE payto_uri=in_full_payto; 84 IF NOT FOUND 85 THEN 86 -- This should never happen (we had a conflict!) 87 -- Still, safe way is to return not found. 88 out_not_found = TRUE; 89 RETURN; 90 END IF; 91 92 -- Check for conflict 93 IF (my_active AND 94 (ROW (my_cfu 95 ,my_cfc 96 ,my_ewsm) 97 IS DISTINCT FROM 98 ROW (in_credit_facade_url 99 ,in_credit_facade_credentials 100 ,in_extra_wire_subject_metadata))) 101 THEN 102 -- Active conflicting account, refuse! 103 out_conflict = TRUE; 104 RETURN; 105 END IF; 106 107 -- Equivalent account exists, use its salt instead of the new salt 108 -- and just set it to active! 109 out_salt = my_salt; 110 out_h_wire = my_h_wire; 111 112 -- Now check if existing account is already active 113 IF my_active 114 THEN 115 -- nothing to do 116 RETURN; 117 END IF; 118 119 UPDATE merchant_accounts 120 SET active=TRUE 121 ,credit_facade_url=in_credit_facade_url 122 ,credit_facade_credentials=in_credit_facade_credentials::JSONB 123 ,extra_wire_subject_metadata=in_extra_wire_subject_metadata 124 WHERE h_wire=out_h_wire; 125 126 -- Notify taler-merchant-kyccheck about the change in (active) 127 -- accounts. (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED) 128 NOTIFY XDQM4Z4N0D3GX0H9JEXH70EBC2T3KY7HC0TJB0Z60D2H781RXR6AG; 129 130 END $$;