merchant

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

pg_activate_account.sql (3916B)


      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_activate_account;
     18 CREATE FUNCTION merchant_do_activate_account(
     19    IN in_instance_name TEXT
     20   ,IN in_h_wire BYTEA
     21   ,IN in_salt BYTEA
     22   ,IN in_full_payto TEXT
     23   ,IN in_credit_facade_url TEXT -- can be NULL
     24   ,IN in_credit_facade_credentials TEXT -- can be NULL
     25   ,IN in_extra_wire_subject_metadata TEXT -- can be NULL
     26   ,OUT out_h_wire BYTEA
     27   ,OUT out_salt BYTEA
     28   ,OUT out_not_found BOOL
     29   ,OUT out_conflict BOOL
     30 )
     31 LANGUAGE plpgsql
     32 AS $$
     33 DECLARE my_instance INT8;
     34 DECLARE my_active BOOL;
     35 DECLARE my_cfu TEXT;
     36 DECLARE my_cfc TEXT;
     37 DECLARE my_ewsm TEXT;
     38 DECLARE my_h_wire BYTEA;
     39 DECLARE my_salt BYTEA;
     40 BEGIN
     41   out_not_found = FALSE;
     42   out_conflict = FALSE;
     43   out_h_wire = in_h_wire;
     44   out_salt = in_salt;
     45 
     46   SELECT merchant_serial
     47     INTO my_instance
     48     FROM merchant_instances
     49    WHERE merchant_id=in_instance_name;
     50   IF NOT FOUND
     51   THEN
     52     out_not_found = TRUE;
     53     RETURN;
     54   END IF;
     55 
     56   INSERT INTO merchant_accounts
     57     AS ma
     58     (merchant_serial
     59     ,h_wire
     60     ,salt
     61     ,payto_uri
     62     ,credit_facade_url
     63     ,credit_facade_credentials
     64     ,active
     65     ,extra_wire_subject_metadata
     66     ) VALUES (
     67       my_instance
     68      ,in_h_wire
     69      ,in_salt
     70      ,in_full_payto
     71      ,in_credit_facade_url
     72      ,in_credit_facade_credentials::JSONB
     73      ,TRUE
     74      ,in_extra_wire_subject_metadata
     75    ) ON CONFLICT DO NOTHING;
     76   IF FOUND
     77   THEN
     78     -- Notify taler-merchant-kyccheck about the change in
     79     -- accounts. (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED)
     80     NOTIFY XDQM4Z4N0D3GX0H9JEXH70EBC2T3KY7HC0TJB0Z60D2H781RXR6AG;
     81     RETURN;
     82   END IF;
     83 
     84   SELECT h_wire
     85         ,salt
     86         ,active
     87         ,credit_facade_url
     88         ,credit_facade_credentials::TEXT
     89         ,extra_wire_subject_metadata
     90     INTO my_h_wire
     91         ,my_salt
     92         ,my_active
     93         ,my_cfu
     94         ,my_cfc
     95         ,my_ewsm
     96     FROM merchant_accounts
     97    WHERE merchant_serial=my_instance
     98      AND payto_uri=in_full_payto;
     99   IF NOT FOUND
    100   THEN
    101     -- This should never happen (we had a conflict!)
    102     -- Still, safe way is to return not found.
    103     out_not_found = TRUE;
    104     RETURN;
    105   END IF;
    106 
    107   -- Check for conflict
    108   IF (my_active AND
    109       (ROW (my_cfu
    110            ,my_cfc
    111            ,my_ewsm)
    112        IS DISTINCT FROM
    113        ROW (in_credit_facade_url
    114            ,in_credit_facade_credentials
    115            ,in_extra_wire_subject_metadata)))
    116   THEN
    117     -- Active conflicting account, refuse!
    118     out_conflict = TRUE;
    119     RETURN;
    120   END IF;
    121 
    122   -- Equivalent account exists, use its salt instead of the new salt
    123   -- and just set it to active!
    124   out_salt = my_salt;
    125   out_h_wire = my_h_wire;
    126 
    127   -- Now check if existing account is already active
    128   IF my_active
    129   THEN
    130     -- nothing to do
    131     RETURN;
    132   END IF;
    133 
    134   UPDATE merchant_accounts
    135      SET active=TRUE
    136         ,credit_facade_url=in_credit_facade_url
    137         ,credit_facade_credentials=in_credit_facade_credentials::JSONB
    138         ,extra_wire_subject_metadata=in_extra_wire_subject_metadata
    139    WHERE h_wire=out_h_wire
    140      AND merchant_serial=my_instance;
    141 
    142   -- Notify taler-merchant-kyccheck about the change in (active)
    143   -- accounts. (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED)
    144   NOTIFY XDQM4Z4N0D3GX0H9JEXH70EBC2T3KY7HC0TJB0Z60D2H781RXR6AG;
    145 
    146 END $$;