insert_deposit_confirmation.sql (4254B)
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 out_no_order=TRUE; 46 out_no_account=TRUE; 47 out_no_signkey=TRUE; 48 out_conflict=FALSE; 49 out_deposit_confirmation_serial=0; 50 51 SELECT account_serial 52 INTO my_account_serial 53 FROM merchant_accounts 54 WHERE h_wire=in_h_wire; 55 IF NOT FOUND 56 THEN 57 RETURN; 58 END IF; 59 out_no_account=FALSE; 60 61 SELECT signkey_serial 62 INTO my_signkey_serial 63 FROM merchant.merchant_exchange_signing_keys 64 WHERE exchange_pub=in_exchange_pub 65 ORDER BY start_date DESC 66 LIMIT 1; 67 IF NOT FOUND 68 THEN 69 RETURN; 70 END IF; 71 out_no_signkey=FALSE; 72 73 SELECT order_serial 74 INTO my_order_serial 75 FROM merchant_contract_terms 76 WHERE h_contract_terms=in_h_contract_terms; 77 IF NOT FOUND 78 THEN 79 RETURN; 80 END IF; 81 out_no_order=FALSE; 82 83 SELECT deposit_confirmation_serial 84 ,deposit_timestamp 85 ,exchange_url 86 ,total_without_fee 87 ,wire_fee 88 ,wire_transfer_deadline 89 ,account_serial 90 INTO my_record 91 FROM merchant_deposit_confirmations 92 WHERE order_serial=my_order_serial 93 AND exchange_url=in_exchange_url; 94 IF NOT FOUND 95 THEN 96 INSERT INTO merchant_deposit_confirmations 97 (order_serial 98 ,deposit_timestamp 99 ,exchange_url 100 ,total_without_fee 101 ,wire_fee 102 ,exchange_sig 103 ,wire_transfer_deadline 104 ,signkey_serial 105 ,account_serial 106 ) VALUES ( 107 my_order_serial 108 ,in_deposit_timestamp 109 ,in_exchange_url 110 ,in_total_without_fee 111 ,in_wire_fee 112 ,in_exchange_sig 113 ,in_wire_transfer_deadline 114 ,my_signkey_serial 115 ,my_account_serial 116 ) RETURNING deposit_confirmation_serial 117 INTO out_deposit_confirmation_serial; 118 ELSE 119 out_deposit_confirmation_serial = my_record.deposit_confirmation_serial; 120 IF (in_deposit_timestamp, 121 in_wire_transfer_deadline, 122 in_wire_fee, 123 my_account_serial) 124 IS DISTINCT FROM 125 (my_record.deposit_timestamp, 126 my_record.wire_transfer_deadline, 127 my_record.wire_fee, 128 my_record.account_serial) 129 THEN 130 out_conflict = TRUE; 131 RETURN; 132 END IF; 133 IF ( ((in_total_without_fee).val < (my_record.total_without_fee).val) OR 134 ( ((in_total_without_fee).val = (my_record.total_without_fee).val) AND 135 ((in_total_without_fee).frac <= (my_record.total_without_fee).frac) ) ) 136 THEN 137 -- new amount smaller or did not change, do NOT update. 138 RETURN; 139 END IF; 140 141 -- Same deposit, but total amount increased, store this! 142 UPDATE merchant_deposit_confirmations 143 SET total_without_fee = in_total_without_fee 144 ,exchange_sig = in_exchange_sig 145 ,signkey_serial = my_signkey_serial 146 WHERE deposit_confirmation_serial = my_record.deposit_confirmation_serial; 147 148 END IF; 149 150 -- Do notify on TALER_DBEVENT_MERCHANT_NEW_WIRE_DEADLINE 151 PERFORM pg_notify ('XBZ19D98AK2REYNX93F736A56MT14SCY2EEX7XNXQMNCQ01B121R0', 152 in_notify_arg_str); 153 154 END $$;