merchant

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

pg_insert_deposit_confirmation.sql (4665B)


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