Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 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 : * @file src/backenddb/iterate_deposits_by_contract_and_coin.c
18 : * @brief Implementation of the iterate_deposits_by_contract_and_coin function for Postgres
19 : * @author Iván Ávalos
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_deposits_by_contract_and_coin.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #lookup_deposits_by_contract_and_coin_cb().
28 : */
29 : struct LookupDepositsByCnCContext
30 : {
31 : /**
32 : * Function to call for each deposit.
33 : */
34 : TALER_MERCHANTDB_CoinDepositCallback cb;
35 :
36 : /**
37 : * Closure for @e cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_MERCHANTDB_PostgresContext *pg;
45 :
46 : /**
47 : * Total amount refunded on this coin and contract.
48 : */
49 : struct TALER_Amount refund_total;
50 :
51 : /**
52 : * Transaction result.
53 : */
54 : enum GNUNET_DB_QueryStatus qs;
55 : };
56 :
57 :
58 : /**
59 : * Function to be called with the results of a SELECT statement
60 : * that has returned @a num_results results.
61 : *
62 : * @param cls of type `struct LookupDepositsByCnCContext *`
63 : * @param result the postgres result
64 : * @param num_results the number of results in @a result
65 : */
66 : static void
67 9 : lookup_refunds_cb (void *cls,
68 : PGresult *result,
69 : unsigned int num_results)
70 : {
71 9 : struct LookupDepositsByCnCContext *ldcc = cls;
72 :
73 9 : for (unsigned int i = 0; i<num_results; i++)
74 : {
75 : struct TALER_Amount refund_amount;
76 0 : struct GNUNET_PQ_ResultSpec rs[] = {
77 0 : TALER_PQ_result_spec_amount_with_currency ("refund_amount",
78 : &refund_amount),
79 : GNUNET_PQ_result_spec_end
80 : };
81 :
82 0 : if (GNUNET_OK !=
83 0 : GNUNET_PQ_extract_result (result,
84 : rs,
85 : i))
86 : {
87 0 : GNUNET_break (0);
88 0 : ldcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
89 0 : return;
90 : }
91 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
92 : "Coin had refund of %s\n",
93 : TALER_amount2s (&refund_amount));
94 0 : if (0 == i)
95 0 : ldcc->refund_total = refund_amount;
96 : else
97 0 : GNUNET_assert (0 <=
98 : TALER_amount_add (&ldcc->refund_total,
99 : &ldcc->refund_total,
100 : &refund_amount));
101 0 : GNUNET_PQ_cleanup_result (rs); /* technically useless here */
102 : }
103 : }
104 :
105 :
106 : /**
107 : * Function to be called with the results of a SELECT statement
108 : * that has returned @a num_results results.
109 : *
110 : * @param cls of type `struct LookupDepositsByCnCContext *`
111 : * @param result the postgres result
112 : * @param num_results the number of results in @a result
113 : */
114 : static void
115 9 : lookup_deposits_by_contract_and_coin_cb (
116 : void *cls,
117 : PGresult *result,
118 : unsigned int num_results)
119 : {
120 9 : struct LookupDepositsByCnCContext *ldcc = cls;
121 :
122 18 : for (unsigned int i = 0; i<num_results; i++)
123 : {
124 : char *exchange_url;
125 : struct TALER_Amount amount_with_fee;
126 : struct TALER_Amount deposit_fee;
127 : struct TALER_Amount refund_fee;
128 : struct TALER_Amount wire_fee;
129 : struct TALER_MerchantWireHashP h_wire;
130 : struct GNUNET_TIME_Timestamp deposit_timestamp;
131 : struct GNUNET_TIME_Timestamp refund_deadline;
132 : struct TALER_ExchangeSignatureP exchange_sig;
133 : struct TALER_ExchangePublicKeyP exchange_pub;
134 9 : struct GNUNET_PQ_ResultSpec rs[] = {
135 9 : GNUNET_PQ_result_spec_string ("exchange_url",
136 : &exchange_url),
137 9 : TALER_PQ_result_spec_amount_with_currency ("amount_with_fee",
138 : &amount_with_fee),
139 9 : TALER_PQ_result_spec_amount_with_currency ("deposit_fee",
140 : &deposit_fee),
141 9 : TALER_PQ_result_spec_amount_with_currency ("refund_fee",
142 : &refund_fee),
143 9 : TALER_PQ_result_spec_amount_with_currency ("wire_fee",
144 : &wire_fee),
145 9 : GNUNET_PQ_result_spec_auto_from_type ("h_wire",
146 : &h_wire),
147 9 : GNUNET_PQ_result_spec_timestamp ("deposit_timestamp",
148 : &deposit_timestamp),
149 9 : GNUNET_PQ_result_spec_timestamp ("refund_deadline",
150 : &refund_deadline),
151 9 : GNUNET_PQ_result_spec_auto_from_type ("exchange_sig",
152 : &exchange_sig),
153 9 : GNUNET_PQ_result_spec_auto_from_type ("exchange_pub",
154 : &exchange_pub),
155 : GNUNET_PQ_result_spec_end
156 : };
157 :
158 9 : if (GNUNET_OK !=
159 9 : GNUNET_PQ_extract_result (result,
160 : rs,
161 : i))
162 : {
163 0 : GNUNET_break (0);
164 0 : ldcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
165 0 : return;
166 : }
167 9 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168 : "Coin original deposit value is %s\n",
169 : TALER_amount2s (&amount_with_fee));
170 9 : if (TALER_amount_is_valid (&ldcc->refund_total))
171 : {
172 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
173 : "Coin had total refunds of %s\n",
174 : TALER_amount2s (&ldcc->refund_total));
175 0 : if (1 ==
176 0 : TALER_amount_cmp (&ldcc->refund_total,
177 : &amount_with_fee))
178 : {
179 : /* Refunds exceeded total deposit? not OK! */
180 0 : GNUNET_break (0);
181 0 : ldcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
182 0 : return;
183 : }
184 0 : if (0 ==
185 0 : TALER_amount_cmp (&ldcc->refund_total,
186 : &amount_with_fee))
187 : {
188 : /* refund_total == amount_with_fee;
189 : in this case, the total contributed to the
190 : wire transfer is zero (as are fees) */
191 0 : GNUNET_assert (GNUNET_OK ==
192 : TALER_amount_set_zero (ldcc->refund_total.currency,
193 : &amount_with_fee));
194 0 : GNUNET_assert (GNUNET_OK ==
195 : TALER_amount_set_zero (ldcc->refund_total.currency,
196 : &deposit_fee));
197 :
198 : }
199 : else
200 : {
201 : /* Compute deposit value by subtracting refunds */
202 0 : GNUNET_assert (0 <
203 : TALER_amount_subtract (&amount_with_fee,
204 : &amount_with_fee,
205 : &ldcc->refund_total));
206 0 : if (-1 ==
207 0 : TALER_amount_cmp (&amount_with_fee,
208 : &deposit_fee))
209 : {
210 : /* amount_with_fee < deposit_fee, so after refunds less than
211 : the deposit fee remains; reduce deposit fee to
212 : the remaining value of the coin */
213 0 : deposit_fee = amount_with_fee;
214 : }
215 : }
216 : }
217 9 : ldcc->cb (ldcc->cb_cls,
218 : exchange_url,
219 : &amount_with_fee,
220 : &deposit_fee,
221 : &refund_fee,
222 : &wire_fee,
223 : &h_wire,
224 : deposit_timestamp,
225 : refund_deadline,
226 : &exchange_sig,
227 : &exchange_pub);
228 9 : GNUNET_PQ_cleanup_result (rs);
229 : }
230 9 : ldcc->qs = num_results;
231 : }
232 :
233 :
234 : enum GNUNET_DB_QueryStatus
235 9 : TALER_MERCHANTDB_iterate_deposits_by_contract_and_coin (
236 : struct TALER_MERCHANTDB_PostgresContext *pg,
237 : const char *instance_id,
238 : const struct TALER_PrivateContractHashP *
239 : h_contract_terms,
240 : const struct TALER_CoinSpendPublicKeyP *
241 : coin_pub,
242 : TALER_MERCHANTDB_CoinDepositCallback cb,
243 : void *cb_cls)
244 : {
245 9 : struct GNUNET_PQ_QueryParam params[] = {
246 9 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
247 9 : GNUNET_PQ_query_param_auto_from_type (coin_pub),
248 : GNUNET_PQ_query_param_end
249 : };
250 9 : struct LookupDepositsByCnCContext ldcc = {
251 : .cb = cb,
252 : .cb_cls = cb_cls,
253 : .pg = pg
254 : };
255 : enum GNUNET_DB_QueryStatus qs;
256 :
257 9 : GNUNET_assert (NULL != pg->current_merchant_id);
258 9 : GNUNET_assert (0 == strcmp (instance_id,
259 : pg->current_merchant_id));
260 : /* no preflight check here, run in transaction by caller! */
261 9 : TALER_LOG_DEBUG ("Looking for refund of h_contract_terms %s at `%s'\n",
262 : GNUNET_h2s (&h_contract_terms->hash),
263 : instance_id);
264 9 : TMH_PQ_prepare_anon (pg,
265 : "SELECT"
266 : " refund_amount"
267 : " FROM merchant_refunds"
268 : /* Join to filter by refunds that actually
269 : did work, not only those we approved */
270 : " JOIN merchant_refund_proofs"
271 : " USING (refund_serial)"
272 : " WHERE coin_pub=$2"
273 : " AND order_serial="
274 : " (SELECT order_serial"
275 : " FROM merchant_contract_terms"
276 : " WHERE h_contract_terms=$1)");
277 9 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
278 : "",
279 : params,
280 : &lookup_refunds_cb,
281 : &ldcc);
282 9 : if (0 > qs)
283 0 : return qs;
284 :
285 9 : TMH_PQ_prepare_anon (pg,
286 : "SELECT"
287 : " mcon.exchange_url"
288 : ",dep.amount_with_fee"
289 : ",dep.deposit_fee"
290 : ",dep.refund_fee"
291 : ",mcon.wire_fee"
292 : ",acc.h_wire"
293 : ",mcon.deposit_timestamp"
294 : ",mct.refund_deadline"
295 : ",mcon.exchange_sig"
296 : ",msig.exchange_pub"
297 : " FROM merchant_contract_terms mct"
298 : " JOIN merchant_deposit_confirmations mcon"
299 : " USING (order_serial)"
300 : " JOIN merchant_deposits dep"
301 : " USING (deposit_confirmation_serial)"
302 : " JOIN merchant.merchant_exchange_signing_keys msig"
303 : " ON (mcon.signkey_serial=msig.signkey_serial)"
304 : " JOIN merchant_accounts acc"
305 : " USING (account_serial)"
306 : " WHERE h_contract_terms=$1"
307 : " AND dep.coin_pub=$2");
308 :
309 9 : qs = GNUNET_PQ_eval_prepared_multi_select (
310 : pg->conn,
311 : "",
312 : params,
313 : &lookup_deposits_by_contract_and_coin_cb,
314 : &ldcc);
315 9 : if (0 >= qs)
316 0 : return qs;
317 9 : return ldcc.qs;
318 : }
|