merchant

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

insert_deposit_confirmation.sql (4386B)


      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_deposit_confirmation;
     18 CREATE FUNCTION merchant_do_insert_deposit_confirmation (
     19   IN in_h_contract_terms BYTEA,
     20   IN in_deposit_timestamp INT8,
     21   IN in_exchange_url TEXT,
     22   IN in_total_without_fee merchant.taler_amount_currency,
     23   IN in_wire_fee merchant.taler_amount_currency,
     24   IN in_h_wire BYTEA,
     25   IN in_exchange_sig BYTEA,
     26   IN in_exchange_pub BYTEA,
     27   IN in_wire_transfer_deadline INT8,
     28   IN in_notify_arg_str TEXT,
     29   OUT out_no_order BOOL,
     30   OUT out_no_account BOOL,
     31   OUT out_no_signkey BOOL,
     32   OUT out_conflict BOOL,
     33   OUT out_deposit_confirmation_serial INT8)
     34 LANGUAGE plpgsql
     35 AS $$
     36 DECLARE
     37   my_order_serial INT8;
     38   my_account_serial INT8;
     39   my_signkey_serial INT8;
     40   my_record RECORD;
     41   my_bank_serial_id INT8;
     42   my_credit_amount merchant.taler_amount_currency;
     43 BEGIN
     44 
     45 -- Note: exactly one of the out_no_* flags is set on the failure
     46 -- paths below, the caller distinguishes the causes by them.
     47 out_no_order=FALSE;
     48 out_no_account=FALSE;
     49 out_no_signkey=FALSE;
     50 out_conflict=FALSE;
     51 out_deposit_confirmation_serial=0;
     52 
     53 SELECT account_serial
     54   INTO my_account_serial
     55   FROM merchant_accounts
     56  WHERE h_wire=in_h_wire;
     57 IF NOT FOUND
     58 THEN
     59   out_no_account=TRUE;
     60   RETURN;
     61 END IF;
     62 
     63 SELECT signkey_serial
     64   INTO my_signkey_serial
     65   FROM merchant.merchant_exchange_signing_keys
     66  WHERE exchange_pub=in_exchange_pub
     67  ORDER BY start_date DESC
     68  LIMIT 1;
     69 IF NOT FOUND
     70 THEN
     71   out_no_signkey=TRUE;
     72   RETURN;
     73 END IF;
     74 
     75 SELECT order_serial
     76   INTO my_order_serial
     77   FROM merchant_contract_terms
     78  WHERE h_contract_terms=in_h_contract_terms;
     79 IF NOT FOUND
     80 THEN
     81   out_no_order=TRUE;
     82   RETURN;
     83 END IF;
     84 
     85 SELECT deposit_confirmation_serial
     86    ,deposit_timestamp
     87    ,exchange_url
     88    ,total_without_fee
     89    ,wire_fee
     90    ,wire_transfer_deadline
     91    ,account_serial
     92   INTO my_record
     93   FROM merchant_deposit_confirmations
     94  WHERE order_serial=my_order_serial
     95    AND exchange_url=in_exchange_url;
     96 IF NOT FOUND
     97 THEN
     98   INSERT INTO merchant_deposit_confirmations
     99     (order_serial
    100     ,deposit_timestamp
    101     ,exchange_url
    102     ,total_without_fee
    103     ,wire_fee
    104     ,exchange_sig
    105     ,wire_transfer_deadline
    106     ,signkey_serial
    107     ,account_serial
    108   ) VALUES (
    109      my_order_serial
    110     ,in_deposit_timestamp
    111     ,in_exchange_url
    112     ,in_total_without_fee
    113     ,in_wire_fee
    114     ,in_exchange_sig
    115     ,in_wire_transfer_deadline
    116     ,my_signkey_serial
    117     ,my_account_serial
    118   ) RETURNING deposit_confirmation_serial
    119      INTO out_deposit_confirmation_serial;
    120 ELSE
    121   out_deposit_confirmation_serial = my_record.deposit_confirmation_serial;
    122   IF (in_deposit_timestamp,
    123       in_wire_transfer_deadline,
    124       in_wire_fee,
    125       my_account_serial)
    126   IS DISTINCT FROM
    127      (my_record.deposit_timestamp,
    128       my_record.wire_transfer_deadline,
    129       my_record.wire_fee,
    130       my_record.account_serial)
    131   THEN
    132     out_conflict = TRUE;
    133     RETURN;
    134   END IF;
    135   IF ( ((in_total_without_fee).val < (my_record.total_without_fee).val) OR
    136        ( ((in_total_without_fee).val = (my_record.total_without_fee).val) AND
    137          ((in_total_without_fee).frac <= (my_record.total_without_fee).frac) ) )
    138   THEN
    139     -- new amount smaller or did not change, do NOT update.
    140     RETURN;
    141   END IF;
    142 
    143   -- Same deposit, but total amount increased, store this!
    144   UPDATE merchant_deposit_confirmations
    145      SET total_without_fee = in_total_without_fee
    146         ,exchange_sig = in_exchange_sig
    147         ,signkey_serial = my_signkey_serial
    148    WHERE deposit_confirmation_serial = my_record.deposit_confirmation_serial;
    149 
    150 END IF;
    151 
    152 -- Do notify on TALER_DBEVENT_MERCHANT_NEW_WIRE_DEADLINE
    153 PERFORM pg_notify ('XBZ19D98AK2REYNX93F736A56MT14SCY2EEX7XNXQMNCQ01B121R0',
    154                    in_notify_arg_str);
    155 
    156 END $$;