exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_do_reserves_in_insert.sql (3702B)


      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 
     18 DROP FUNCTION IF EXISTS exchange_do_array_reserves_insert;
     19 CREATE FUNCTION exchange_do_array_reserves_insert(
     20   IN in_gc_date INT8,
     21   IN in_reserve_expiration INT8,
     22   IN ina_reserve_pub BYTEA[],
     23   IN ina_wire_ref INT8[],
     24   IN ina_credit taler_amount[],
     25   IN ina_exchange_account_name TEXT[],
     26   IN ina_execution_date INT8[],
     27   IN ina_wire_source_h_payto BYTEA[],
     28   IN ina_h_normalized_payto BYTEA[],
     29   IN ina_payto_uri TEXT[],
     30   IN ina_notify TEXT[])
     31 RETURNS SETOF exchange_do_array_reserve_insert_return_type
     32 LANGUAGE plpgsql
     33 AS $$
     34 DECLARE
     35   conflict BOOL;
     36   dup BOOL;
     37   uuid INT8;
     38   i INT4;
     39   my_is_wallet BOOL;
     40   ini_reserve_pub BYTEA;
     41   ini_wire_ref INT8;
     42   ini_credit taler_amount;
     43   ini_exchange_account_name TEXT;
     44   ini_execution_date INT8;
     45   ini_wire_source_h_payto BYTEA;
     46   ini_h_normalized_payto BYTEA;
     47   ini_payto_uri TEXT;
     48   ini_notify TEXT;
     49 BEGIN
     50 
     51   FOR i IN 1..array_length(ina_reserve_pub,1)
     52   LOOP
     53     ini_reserve_pub = ina_reserve_pub[i];
     54     ini_wire_ref = ina_wire_ref[i];
     55     ini_credit = ina_credit[i];
     56     ini_exchange_account_name = ina_exchange_account_name[i];
     57     ini_execution_date = ina_execution_date[i];
     58     ini_wire_source_h_payto = ina_wire_source_h_payto[i];
     59     ini_h_normalized_payto = ina_h_normalized_payto[i];
     60     ini_payto_uri = ina_payto_uri[i];
     61     ini_notify = ina_notify[i];
     62 
     63 --    RAISE WARNING 'Starting loop on %', ini_notify;
     64 
     65     my_is_wallet
     66       = (LOWER (SUBSTRING (ini_payto_uri, 0, 23)) =
     67          'payto://taler-reserve/') OR
     68         (LOWER (SUBSTRING (ini_payto_uri, 0, 28)) =
     69          'payto://taler-reserve-http/');
     70     INSERT INTO kyc_targets
     71       (h_normalized_payto
     72       ,is_wallet
     73       ) VALUES (
     74        ini_h_normalized_payto
     75       ,my_is_wallet
     76       )
     77       ON CONFLICT DO NOTHING;
     78     INSERT INTO wire_targets
     79       (wire_target_h_payto
     80       ,h_normalized_payto
     81       ,payto_uri
     82       ) VALUES (
     83         ini_wire_source_h_payto
     84        ,ini_h_normalized_payto
     85        ,ini_payto_uri
     86       )
     87     ON CONFLICT DO NOTHING;
     88 
     89     INSERT INTO reserves
     90       (reserve_pub
     91       ,current_balance
     92       ,expiration_date
     93       ,gc_date
     94     ) VALUES (
     95       ini_reserve_pub
     96      ,ini_credit
     97      ,in_reserve_expiration
     98      ,in_gc_date
     99     )
    100     ON CONFLICT DO NOTHING
    101     RETURNING reserve_uuid
    102       INTO uuid;
    103     conflict = NOT FOUND;
    104 
    105     INSERT INTO reserves_in
    106       (reserve_pub
    107       ,wire_reference
    108       ,credit
    109       ,exchange_account_section
    110       ,wire_source_h_payto
    111       ,execution_date
    112     ) VALUES (
    113       ini_reserve_pub
    114      ,ini_wire_ref
    115      ,ini_credit
    116      ,ini_exchange_account_name
    117      ,ini_wire_source_h_payto
    118      ,ini_execution_date
    119     )
    120     ON CONFLICT DO NOTHING;
    121 
    122     IF NOT FOUND
    123     THEN
    124       IF conflict
    125       THEN
    126         dup = TRUE;
    127       else
    128         dup = FALSE;
    129       END IF;
    130     ELSE
    131       IF NOT conflict
    132       THEN
    133         EXECUTE FORMAT (
    134           'NOTIFY %s'
    135           ,ini_notify);
    136       END IF;
    137       dup = FALSE;
    138     END IF;
    139     RETURN NEXT (dup,uuid);
    140   END LOOP;
    141   RETURN;
    142 END $$;