insert_deposit_to_transfer.sql (4610B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2024, 2025 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 18 DROP FUNCTION IF EXISTS merchant_insert_deposit_to_transfer; 19 CREATE FUNCTION merchant_insert_deposit_to_transfer ( 20 IN in_deposit_serial INT8, 21 IN in_coin_contribution merchant.taler_amount_currency, 22 IN in_execution_time INT8, 23 IN in_exchange_url TEXT, 24 IN in_h_wire BYTEA, 25 IN in_exchange_sig BYTEA, 26 IN in_exchange_pub BYTEA, 27 IN in_wtid BYTEA, 28 -- Exchange signing key unknown: transient, we will try again. 29 OUT out_no_signkey BOOL, 30 -- Target account unknown: permanent failure, the money went 31 -- somewhere we do not recognize. 32 OUT out_no_account BOOL) 33 LANGUAGE plpgsql 34 AS $$ 35 DECLARE 36 my_signkey_serial INT8; 37 my_account_serial INT8; 38 my_decose INT8; 39 my_expected_credit_serial INT8; 40 my_wire_pending_cleared BOOL; 41 BEGIN 42 out_no_signkey=FALSE; 43 out_no_account=FALSE; 44 45 -- Find exchange sign key 46 SELECT signkey_serial 47 INTO my_signkey_serial 48 FROM merchant.merchant_exchange_signing_keys 49 WHERE exchange_pub=in_exchange_pub 50 ORDER BY start_date DESC 51 LIMIT 1; 52 53 IF NOT FOUND 54 THEN 55 -- Maybe 'keys' is outdated, try again in 8 hours. 56 UPDATE merchant_deposits 57 SET settlement_last_ec=2029 -- MERCHANT_EXCHANGE_SIGN_PUB_UNKNOWN 58 ,settlement_last_http_status=200 59 ,settlement_last_detail=ENCODE(in_exchange_pub, 'hex') 60 ,settlement_wtid=in_wtid 61 ,settlement_retry_needed=TRUE 62 ,settlement_retry_time=(EXTRACT(epoch FROM (CURRENT_TIMESTAMP + interval '8 hours')) * 1000000)::INT8 63 WHERE deposit_serial=in_deposit_serial; 64 out_no_signkey=TRUE; 65 RETURN; 66 END IF; 67 68 -- Find deposit confirmation 69 SELECT deposit_confirmation_serial 70 INTO my_decose 71 FROM merchant_deposits 72 WHERE deposit_serial=in_deposit_serial; 73 74 -- Find merchant account 75 SELECT account_serial 76 INTO my_account_serial 77 FROM merchant_deposit_confirmations mdc 78 JOIN merchant_accounts ma 79 USING (account_serial) 80 WHERE mdc.deposit_confirmation_serial=my_decose 81 AND ma.h_wire=in_h_wire; 82 83 IF NOT FOUND 84 THEN 85 -- Merchant account referenced in exchange response is unknown to us. 86 -- Remember fatal error and do not try again. Note that 87 -- settlement_last_ec stays non-zero, which is what keeps this 88 -- deposit from counting as settled in 89 -- merchant_do_insert_transfer_details. 90 UPDATE merchant_deposits 91 SET settlement_last_ec=2558 -- MERCHANT_EXCHANGE_TRANSFERS_TARGET_ACCOUNT_UNKNOWN 92 ,settlement_last_http_status=200 93 ,settlement_last_detail=ENCODE(in_h_wire, 'hex') 94 ,settlement_wtid=in_wtid 95 ,settlement_retry_needed=FALSE 96 ,settlement_coin_contribution=in_coin_contribution 97 ,signkey_serial=my_signkey_serial 98 ,settlement_exchange_sig=in_exchange_sig 99 WHERE deposit_serial=in_deposit_serial; 100 out_no_account=TRUE; 101 RETURN; 102 END IF; 103 104 105 -- Make sure wire transfer is expected. 106 SELECT expected_credit_serial 107 INTO my_expected_credit_serial 108 FROM merchant_expected_transfers 109 WHERE wtid=in_wtid 110 AND exchange_url=in_exchange_url 111 AND account_serial=my_account_serial; 112 113 IF NOT FOUND 114 THEN 115 INSERT INTO merchant_expected_transfers 116 (exchange_url 117 ,wtid 118 ,account_serial 119 ,expected_time) 120 VALUES 121 (in_exchange_url 122 ,in_wtid 123 ,my_account_serial 124 ,in_execution_time) 125 RETURNING expected_credit_serial 126 INTO my_expected_credit_serial; 127 END IF; 128 129 -- Finally, update merchant_deposits so we do not try again. 130 UPDATE merchant_deposits 131 SET settlement_last_ec=0 132 ,settlement_last_http_status=200 133 ,settlement_last_detail=NULL 134 ,settlement_wtid=in_wtid 135 ,settlement_retry_needed=FALSE 136 ,settlement_coin_contribution=in_coin_contribution 137 ,settlement_expected_credit_serial=my_expected_credit_serial 138 ,signkey_serial=my_signkey_serial 139 ,settlement_exchange_sig=in_exchange_sig 140 WHERE deposit_serial=in_deposit_serial; 141 142 -- MERCHANT_WIRE_TRANSFER_EXPECTED 143 NOTIFY XR6849FMRD2AJFY1E2YY0GWA8GN0YT407Z66WHJB0SAKJWF8G2Q60; 144 145 END $$;