merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

do_insert_fountain.sql (2612B)


      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_fountain;
     18 CREATE FUNCTION merchant_do_insert_fountain(
     19    IN in_fountain_id TEXT
     20   ,IN in_h_fountain_secret BYTEA
     21   ,IN in_description TEXT
     22   ,IN in_poll_freq INT8
     23   ,IN in_token_family_slugs TEXT[]
     24   ,IN in_tokens_per_period_limits INT8[]
     25   ,IN in_tokens_per_period_stashes INT8[]
     26   ,IN in_key_window_sizes INT8[]
     27   ,OUT out_unknown_slug TEXT
     28   ,OUT out_conflict BOOL)
     29 LANGUAGE plpgsql
     30 AS $$
     31 DECLARE
     32   my_tf_serials INT8[];
     33   my_tf_serial INT8;
     34   my_fountain_serial INT8;
     35   i INT4;
     36 BEGIN
     37   out_unknown_slug = NULL;
     38   out_conflict = FALSE;
     39 
     40   -- Resolve all token family slugs before creating anything, so an
     41   -- unknown slug leaves no trace.
     42   my_tf_serials = ARRAY[]::INT8[];
     43   FOR i IN 1..COALESCE(array_length (in_token_family_slugs, 1), 0)
     44   LOOP
     45     SELECT token_family_serial
     46       INTO my_tf_serial
     47       FROM merchant_token_families
     48      WHERE slug = in_token_family_slugs[i];
     49     IF NOT FOUND
     50     THEN
     51       out_unknown_slug = in_token_family_slugs[i];
     52       RETURN;
     53     END IF;
     54     my_tf_serials = array_append (my_tf_serials, my_tf_serial);
     55   END LOOP;
     56 
     57   INSERT INTO merchant_fountains
     58     (fountain_id
     59     ,h_fountain_secret
     60     ,description
     61     ,poll_freq
     62     ) VALUES
     63     (in_fountain_id
     64     ,in_h_fountain_secret
     65     ,in_description
     66     ,in_poll_freq)
     67     ON CONFLICT DO NOTHING
     68     RETURNING fountain_serial
     69       INTO my_fountain_serial;
     70   IF NOT FOUND
     71   THEN
     72     out_conflict = TRUE;
     73     RETURN;
     74   END IF;
     75 
     76   FOR i IN 1..COALESCE(array_length (my_tf_serials, 1), 0)
     77   LOOP
     78     INSERT INTO merchant_fountain_grants
     79       (fountain_serial
     80       ,token_family_serial
     81       ,tokens_per_period_limit
     82       ,tokens_per_period_stash
     83       ,key_window_size
     84       ) VALUES
     85       (my_fountain_serial
     86       ,my_tf_serials[i]
     87       ,in_tokens_per_period_limits[i]
     88       ,in_tokens_per_period_stashes[i]
     89       ,in_key_window_sizes[i]);
     90   END LOOP;
     91 
     92 END $$;