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 (3007B)


      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_normalized_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   my_found = FALSE;
     32   IF in_merchant_pub IS NOT NULL
     33   THEN
     34     -- We were given an in_account_pub, but it did not match the
     35     -- target pub.
     36     -- Try to see if the in_account_pub appears in ANY reserve_in
     37     -- or kyc_auths_in for this account instead.
     38     PERFORM
     39        FROM reserves_in ri
     40        JOIN wire_targets wt
     41          ON (ri.wire_source_h_payto = wt.wire_target_h_payto)
     42       WHERE ri.reserve_pub=in_merchant_pub
     43         AND wt.h_normalized_payto=in_h_normalized_payto;
     44     IF FOUND
     45     THEN
     46       -- Found match in reserves, use it!
     47       out_reserve_pub = in_merchant_pub;
     48       my_found = TRUE;
     49     ELSE
     50       -- Check KYC Auths inputs
     51       PERFORM
     52          FROM kycauths_in ki
     53          JOIN wire_targets wt
     54            ON (ki.wire_source_h_payto = wt.wire_target_h_payto)
     55         WHERE ki.account_pub=in_merchant_pub
     56           AND wt.h_normalized_payto=in_h_normalized_payto;
     57       IF FOUND
     58       THEN
     59         out_reserve_pub = in_merchant_pub;
     60         my_found = TRUE;
     61       END IF;
     62     END IF;
     63   END IF;
     64 
     65   IF NOT my_found
     66   THEN
     67     -- Neither matches. Pick out_reserves_pub from reserves_in.
     68     -- If multiple reserves_in match, we pick the latest one
     69     SELECT reserve_pub
     70       INTO out_reserve_pub
     71       FROM reserves_in
     72      WHERE wire_source_h_payto IN
     73        (SELECT wire_target_h_payto
     74           FROM wire_targets
     75          WHERE h_normalized_payto = in_h_normalized_payto)
     76      ORDER BY execution_date DESC
     77      LIMIT 1;
     78   END IF;
     79 
     80   SELECT target_pub
     81     INTO out_target_pub
     82     FROM kyc_targets
     83    WHERE h_normalized_payto = in_h_normalized_payto;
     84 
     85   SELECT jnew_rules
     86     INTO out_jnew_rules
     87     FROM legitimization_outcomes
     88    WHERE h_payto = in_h_normalized_payto
     89      AND COALESCE(expiration_time >= $2, TRUE)
     90      AND COALESCE(is_active, TRUE)
     91    -- technically only one should ever be active, but we can be conservative
     92    ORDER BY expiration_time DESC
     93    LIMIT 1;
     94 
     95 END $$;