merchant

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

insert_transfer.sql (2895B)


      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_transfer;
     18 CREATE FUNCTION merchant_do_insert_transfer (
     19   IN in_exchange_url TEXT,
     20   IN in_wtid BYTEA,
     21   IN in_credit_amount merchant.taler_amount_currency,
     22   IN in_credited_account_payto TEXT,
     23   IN in_bank_serial_id INT8, -- can be NULL if unknown
     24   IN in_execution_time INT8,
     25   OUT out_no_account BOOL,
     26   OUT out_conflict BOOL)
     27 LANGUAGE plpgsql
     28 AS $$
     29 DECLARE
     30   my_account_serial INT8;
     31   my_record RECORD;
     32   my_bank_serial_id INT8;
     33   my_credit_amount merchant.taler_amount_currency;
     34 BEGIN
     35 
     36 out_conflict=FALSE;
     37 
     38 SELECT account_serial
     39   INTO my_account_serial
     40   FROM merchant_accounts
     41  WHERE REGEXP_REPLACE(payto_uri,
     42                       '\\?.*','')
     43       =REGEXP_REPLACE(in_credited_account_payto,
     44                       '\\?.*','');
     45 IF NOT FOUND
     46 THEN
     47   out_no_account=TRUE;
     48   RETURN;
     49 END IF;
     50 out_no_account=FALSE;
     51 
     52 SELECT bank_serial_id
     53       ,credit_amount
     54   INTO my_record
     55   FROM merchant_transfers
     56  WHERE wtid=in_wtid
     57    AND account_serial=my_account_serial
     58    AND exchange_url=in_exchange_url;
     59 IF NOT FOUND
     60 THEN
     61   INSERT INTO merchant_transfers
     62     (exchange_url
     63     ,wtid
     64     ,credit_amount
     65     ,account_serial
     66     ,bank_serial_id
     67     ,execution_time
     68     ) VALUES
     69     (in_exchange_url
     70     ,in_wtid
     71     ,in_credit_amount
     72     ,my_account_serial
     73     ,in_bank_serial_id
     74     ,in_execution_time);
     75   -- Do notify on TALER_DBEVENT_MERCHANT_WIRE_TRANSFER_CONFIRMED
     76   NOTIFY XJ5N652FA4TBS2WXGY3S1FMPMQYTD8KAZA9B7HW9JWJ4PZ2DB852G;
     77   RETURN;
     78 END IF;
     79 
     80 my_bank_serial_id = my_record.bank_serial_id;
     81 my_credit_amount = my_record.credit_amount;
     82 
     83 IF ( (in_credit_amount.val  != my_credit_amount.val)  OR
     84      (in_credit_amount.frac != my_credit_amount.frac) OR
     85      (in_credit_amount.curr != my_credit_amount.curr) )
     86 THEN
     87   out_conflict = TRUE; -- amounts differ, not OK!
     88   RETURN;
     89 END IF;
     90 
     91 IF ( (my_bank_serial_id IS NULL) AND
     92      (in_bank_serial_id IS NOT NULL) )
     93 THEN
     94   -- We learned the bank_bank_serial_id, update that
     95   UPDATE merchant_transfers
     96      SET bank_serial_id=in_bank_serial_id
     97    WHERE wtid=in_wtid
     98      AND account_serial=my_account_serial
     99      AND exchange_url=in_exchange_url;
    100   RETURN;
    101 END IF;
    102 
    103 -- idempotent request, success.
    104 
    105 END $$;