insert_transfer.sql (3193B)
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_insert_transfer; 18 CREATE FUNCTION merchant_do_insert_transfer ( 19 IN in_exchange_url TEXT, 20 IN in_wtid BYTEA, 21 IN in_credit_amount merchant.taler_amount_currency, 22 IN in_credited_account_payto TEXT, 23 IN in_bank_serial_id INT8, -- can be NULL if unknown 24 IN in_execution_time INT8, 25 OUT out_no_account BOOL, 26 OUT out_conflict BOOL) 27 LANGUAGE plpgsql 28 AS $$ 29 DECLARE 30 my_account_serial INT8; 31 my_record RECORD; 32 my_bank_serial_id INT8; 33 my_credit_amount merchant.taler_amount_currency; 34 BEGIN 35 36 out_conflict=FALSE; 37 38 -- NOTE: compare the payto URIs with any query parameters (such as 39 -- 'receiver-name') stripped. SPLIT_PART is used instead of REGEXP_REPLACE 40 -- because the previous regular expression ('\\?.*') matched an *optional* 41 -- backslash followed by anything, i.e. it matched at offset 0 and reduced 42 -- every URI to the empty string, making this condition always true. 43 SELECT account_serial 44 INTO my_account_serial 45 FROM merchant_accounts 46 WHERE SPLIT_PART(payto_uri,'?',1) 47 =SPLIT_PART(in_credited_account_payto,'?',1); 48 IF NOT FOUND 49 THEN 50 out_no_account=TRUE; 51 RETURN; 52 END IF; 53 out_no_account=FALSE; 54 55 SELECT bank_serial_id 56 ,credit_amount 57 INTO my_record 58 FROM merchant_transfers 59 WHERE wtid=in_wtid 60 AND account_serial=my_account_serial 61 AND exchange_url=in_exchange_url; 62 IF NOT FOUND 63 THEN 64 INSERT INTO merchant_transfers 65 (exchange_url 66 ,wtid 67 ,credit_amount 68 ,account_serial 69 ,bank_serial_id 70 ,execution_time 71 ) VALUES 72 (in_exchange_url 73 ,in_wtid 74 ,in_credit_amount 75 ,my_account_serial 76 ,in_bank_serial_id 77 ,in_execution_time); 78 -- Do notify on TALER_DBEVENT_MERCHANT_WIRE_TRANSFER_CONFIRMED 79 NOTIFY XJ5N652FA4TBS2WXGY3S1FMPMQYTD8KAZA9B7HW9JWJ4PZ2DB852G; 80 RETURN; 81 END IF; 82 83 my_bank_serial_id = my_record.bank_serial_id; 84 my_credit_amount = my_record.credit_amount; 85 86 IF ( (in_credit_amount.val != my_credit_amount.val) OR 87 (in_credit_amount.frac != my_credit_amount.frac) OR 88 (in_credit_amount.curr != my_credit_amount.curr) ) 89 THEN 90 out_conflict = TRUE; -- amounts differ, not OK! 91 RETURN; 92 END IF; 93 94 IF ( (my_bank_serial_id IS NULL) AND 95 (in_bank_serial_id IS NOT NULL) ) 96 THEN 97 -- We learned the bank_bank_serial_id, update that 98 UPDATE merchant_transfers 99 SET bank_serial_id=in_bank_serial_id 100 WHERE wtid=in_wtid 101 AND account_serial=my_account_serial 102 AND exchange_url=in_exchange_url; 103 RETURN; 104 END IF; 105 106 -- idempotent request, success. 107 108 END $$;