merchant

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

insert_kyc_status.sql (3166B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2024, 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 
     18 DROP FUNCTION IF EXISTS merchant_do_account_kyc_set_status;
     19 CREATE FUNCTION merchant_do_account_kyc_set_status (
     20   IN in_h_wire BYTEA,
     21   IN in_exchange_url TEXT,
     22   IN in_timestamp INT8,
     23   IN in_exchange_http_status INT4,
     24   IN in_exchange_ec_code INT4,
     25   IN in_access_token BYTEA, -- can be NULL
     26   IN in_jlimits JSONB,
     27   IN in_aml_active BOOL,
     28   IN in_kyc_ok BOOL,
     29   IN in_notify_str TEXT,
     30   IN in_notify2_str TEXT,
     31   IN in_rule_gen INT8,
     32   IN in_next_time INT8,
     33   IN in_kyc_backoff INT8,
     34   OUT out_no_account BOOL)
     35 LANGUAGE plpgsql
     36 AS $$
     37 DECLARE
     38   my_account_serial INT8;
     39   my_changed BOOL;
     40 BEGIN
     41 
     42 out_no_account=FALSE;
     43 
     44 SELECT account_serial
     45   INTO my_account_serial
     46   FROM merchant_accounts
     47  WHERE h_wire=in_h_wire;
     48 
     49 IF NOT FOUND
     50 THEN
     51   out_no_account=TRUE;
     52   RETURN;
     53 END IF;
     54 
     55 -- Serialize updates and compare status separately from polling bookkeeping.
     56 SELECT ROW(kyc_ok, jaccount_limits, aml_review, exchange_http_status,
     57            exchange_ec_code, access_token, last_rule_gen)
     58        IS DISTINCT FROM
     59        ROW(in_kyc_ok, in_jlimits, in_aml_active, in_exchange_http_status,
     60            in_exchange_ec_code, in_access_token, in_rule_gen)
     61   INTO my_changed
     62   FROM merchant_kyc
     63  WHERE account_serial=my_account_serial
     64    AND exchange_url=in_exchange_url
     65  FOR UPDATE;
     66 IF NOT FOUND
     67 THEN
     68   my_changed := TRUE;
     69 END IF;
     70 
     71 UPDATE merchant_kyc
     72    SET kyc_timestamp=in_timestamp
     73       ,kyc_ok=in_kyc_ok
     74       ,jaccount_limits=in_jlimits
     75       ,aml_review=in_aml_active
     76       ,exchange_http_status=in_exchange_http_status
     77       ,exchange_ec_code=in_exchange_ec_code
     78       ,access_token=in_access_token
     79       ,last_rule_gen=in_rule_gen
     80       ,next_kyc_poll=in_next_time
     81       ,kyc_backoff=in_kyc_backoff
     82  WHERE account_serial=my_account_serial
     83    AND exchange_url=in_exchange_url;
     84 
     85 IF NOT FOUND
     86 THEN
     87 
     88   INSERT INTO merchant_kyc
     89     (kyc_timestamp
     90     ,kyc_ok
     91     ,account_serial
     92     ,exchange_url
     93     ,jaccount_limits
     94     ,aml_review
     95     ,exchange_http_status
     96     ,exchange_ec_code
     97     ,access_token
     98     ,last_rule_gen
     99     ,next_kyc_poll
    100     ,kyc_backoff)
    101   VALUES
    102     (in_timestamp
    103     ,in_kyc_ok
    104     ,my_account_serial
    105     ,in_exchange_url
    106     ,in_jlimits
    107     ,in_aml_active
    108     ,in_exchange_http_status
    109     ,in_exchange_ec_code
    110     ,in_access_token
    111     ,in_rule_gen
    112     ,in_next_time
    113     ,in_kyc_backoff);
    114 END IF;
    115 
    116 IF my_changed
    117 THEN
    118   EXECUTE FORMAT ('NOTIFY %s', in_notify_str);
    119   EXECUTE FORMAT ('NOTIFY %s', in_notify2_str);
    120 END IF;
    121 
    122 
    123 -- Success!
    124 END $$;