exchange

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

exchange_do_get_kyc_rules.sql (2291B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2023-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 DROP FUNCTION IF EXISTS exchange_do_get_kyc_rules;
     17 
     18 CREATE FUNCTION exchange_do_get_kyc_rules(
     19   IN in_h_payto BYTEA,
     20   IN in_now INT8,
     21   IN in_merchant_pub BYTEA, -- possibly NULL
     22   OUT out_target_pub BYTEA, -- possibly NULL
     23   OUT out_reserve_pub BYTEA, -- possibly NULL
     24   OUT out_jnew_rules JSONB -- possibly NULL
     25  )
     26 LANGUAGE plpgsql
     27 AS $$
     28 DECLARE
     29   my_found BOOL;
     30 BEGIN
     31   IF in_merchant_pub IS NOT NULL
     32   THEN
     33     PERFORM FROM reserves_in
     34       WHERE reserve_pub=in_merchant_pub
     35         AND wire_source_h_payto IN
     36          (SELECT wire_target_h_payto
     37             FROM wire_targets
     38            WHERE h_normalized_payto = in_h_payto);
     39     my_found = FOUND;
     40   ELSE
     41     my_found = FALSE;
     42   END IF;
     43   IF FOUND
     44   THEN
     45     -- The merchant_pub used by the client matches, use that
     46     out_reserve_pub = in_merchant_pub;
     47   ELSE
     48     -- If multiple reserves_in match, we pick the latest one
     49     SELECT reserve_pub
     50       INTO out_reserve_pub
     51       FROM reserves_in
     52      WHERE wire_source_h_payto IN
     53       (SELECT wire_target_h_payto
     54          FROM wire_targets
     55         WHERE h_normalized_payto = in_h_payto)
     56      ORDER BY execution_date DESC
     57      LIMIT 1;
     58   END IF;
     59 
     60   SELECT target_pub
     61     INTO out_target_pub
     62     FROM kyc_targets
     63    WHERE h_normalized_payto = in_h_payto;
     64 
     65   SELECT jnew_rules
     66     INTO out_jnew_rules
     67     FROM legitimization_outcomes
     68    WHERE h_payto = in_h_payto
     69      AND COALESCE(expiration_time >= $2, TRUE)
     70      AND COALESCE(is_active, TRUE)
     71    -- technically only one should ever be active, but we can be conservative
     72    ORDER BY expiration_time DESC
     73    LIMIT 1;
     74 
     75 
     76 END $$;