libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

libeufin-conversion-setup.sql (2856B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2024-2025 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 BEGIN;
     17 SET search_path TO libeufin_bank;
     18 
     19 DROP TRIGGER IF EXISTS cashin_link ON libeufin_nexus.talerable_incoming_transactions;
     20 DROP FUNCTION IF EXISTS cashin_link;
     21 
     22 CREATE OR REPLACE FUNCTION cashin_link() 
     23 RETURNS trigger 
     24 LANGUAGE plpgsql AS $$
     25   DECLARE
     26     now_date INT8;
     27     local_amount libeufin_bank.taler_amount;
     28     local_subject TEXT;
     29     too_small BOOLEAN;
     30     balance_insufficient BOOLEAN;
     31     no_account BOOLEAN;
     32   BEGIN
     33     -- Only reserve transaction triggers cashin
     34     IF NEW.type != 'reserve' THEN
     35       RETURN NEW;
     36     END IF;
     37 
     38     SELECT (amount).val, (amount).frac, subject, execution_time
     39       INTO local_amount.val, local_amount.frac, local_subject, now_date
     40       FROM libeufin_nexus.incoming_transactions
     41       WHERE incoming_transaction_id = NEW.incoming_transaction_id;
     42     SET search_path TO libeufin_bank;
     43     SELECT out_too_small, out_balance_insufficient, out_no_account
     44       INTO too_small, balance_insufficient, no_account
     45       FROM libeufin_bank.cashin(now_date, NEW.metadata, local_amount, local_subject);
     46     SET search_path TO libeufin_nexus;
     47 
     48     -- Bounce on soft failures
     49     IF too_small THEN
     50       -- TODO bounce fees ?
     51       PERFORM bounce_incoming(
     52          NEW.incoming_transaction_id
     53         ,((local_amount).val, (local_amount).frac)::taler_amount
     54         -- use gen_random_uuid to get some randomness
     55         -- remove all - characters as they are not random
     56         -- capitalise the UUID as some bank may still be case sensitive
     57         -- end with 34 random chars which is valid for EBICS (max 35 chars)
     58         ,upper(replace(gen_random_uuid()::text, '-', ''))
     59         ,now_date
     60         ,'amount too small to be converted'
     61       );
     62       RETURN NULL;
     63     END IF;
     64 
     65     -- Error on hard failures
     66     IF no_account THEN
     67       RAISE EXCEPTION 'cashin failed: missing exchange account';
     68     END IF;
     69     IF balance_insufficient THEN
     70       RAISE EXCEPTION 'cashin failed: admin balance insufficient';
     71     END IF;
     72 
     73     RETURN NEW;
     74   END;
     75 $$;
     76 
     77 CREATE OR REPLACE TRIGGER cashin_link BEFORE INSERT ON libeufin_nexus.talerable_incoming_transactions
     78     FOR EACH ROW EXECUTE FUNCTION cashin_link();
     79 
     80 COMMIT;