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_refunds_detailed.c
18 : * @brief Implementation of the iterate_refunds_detailed 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_refunds_detailed.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #lookup_refunds_detailed_cb().
28 : */
29 : struct LookupRefundsDetailedContext
30 : {
31 : /**
32 : * Function to call for each refund.
33 : */
34 : TALER_MERCHANTDB_RefundDetailCallback rc;
35 :
36 : /**
37 : * Closure for @e rc.
38 : */
39 : void *rc_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_MERCHANTDB_PostgresContext *pg;
45 :
46 : /**
47 : * Transaction result.
48 : */
49 : enum GNUNET_DB_QueryStatus qs;
50 : };
51 :
52 :
53 : /**
54 : * Function to be called with the results of a SELECT statement
55 : * that has returned @a num_results results.
56 : *
57 : * @param cls of type `struct GetRefundsContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 87 : lookup_refunds_detailed_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 87 : struct LookupRefundsDetailedContext *lrdc = cls;
67 :
68 129 : for (unsigned int i = 0; i<num_results; i++)
69 : {
70 : uint64_t refund_serial;
71 : struct GNUNET_TIME_Timestamp timestamp;
72 : struct TALER_CoinSpendPublicKeyP coin_pub;
73 : uint64_t rtransaction_id;
74 : struct TALER_Amount refund_amount;
75 : char *reason;
76 : char *exchange_url;
77 : uint8_t pending8;
78 42 : struct GNUNET_PQ_ResultSpec rs[] = {
79 42 : GNUNET_PQ_result_spec_uint64 ("refund_serial",
80 : &refund_serial),
81 42 : GNUNET_PQ_result_spec_timestamp ("refund_timestamp",
82 : ×tamp),
83 42 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
84 : &coin_pub),
85 42 : GNUNET_PQ_result_spec_string ("exchange_url",
86 : &exchange_url),
87 42 : GNUNET_PQ_result_spec_uint64 ("rtransaction_id",
88 : &rtransaction_id),
89 42 : GNUNET_PQ_result_spec_string ("reason",
90 : &reason),
91 42 : TALER_PQ_result_spec_amount_with_currency ("refund_amount",
92 : &refund_amount),
93 42 : GNUNET_PQ_result_spec_auto_from_type ("pending",
94 : &pending8),
95 : GNUNET_PQ_result_spec_end
96 : };
97 :
98 42 : if (GNUNET_OK !=
99 42 : GNUNET_PQ_extract_result (result,
100 : rs,
101 : i))
102 : {
103 0 : GNUNET_break (0);
104 0 : lrdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
105 0 : return;
106 : }
107 42 : lrdc->rc (lrdc->rc_cls,
108 : refund_serial,
109 : timestamp,
110 : &coin_pub,
111 : exchange_url,
112 : rtransaction_id,
113 : reason,
114 : &refund_amount,
115 : 0 != pending8);
116 42 : GNUNET_PQ_cleanup_result (rs);
117 : }
118 87 : lrdc->qs = num_results;
119 : }
120 :
121 :
122 : enum GNUNET_DB_QueryStatus
123 87 : TALER_MERCHANTDB_iterate_refunds_detailed (
124 : struct TALER_MERCHANTDB_PostgresContext *pg,
125 : const char *instance_id,
126 : const struct TALER_PrivateContractHashP *
127 : h_contract_terms,
128 : TALER_MERCHANTDB_RefundDetailCallback rc,
129 : void *rc_cls)
130 : {
131 87 : struct GNUNET_PQ_QueryParam params[] = {
132 87 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
133 : GNUNET_PQ_query_param_end
134 : };
135 87 : struct LookupRefundsDetailedContext lrdc = {
136 : .rc = rc,
137 : .rc_cls = rc_cls,
138 : .pg = pg
139 : };
140 : enum GNUNET_DB_QueryStatus qs;
141 :
142 87 : GNUNET_assert (NULL != pg->current_merchant_id);
143 87 : GNUNET_assert (0 == strcmp (instance_id,
144 : pg->current_merchant_id));
145 : /* no preflight check here, run in transaction by caller! */
146 87 : TALER_LOG_DEBUG ("Looking for refund %s + %s\n",
147 : GNUNET_h2s (&h_contract_terms->hash),
148 : instance_id);
149 :
150 87 : TMH_PQ_prepare_anon (pg,
151 : "SELECT"
152 : " ref.refund_serial"
153 : ",ref.refund_timestamp"
154 : ",dep.coin_pub"
155 : ",mcon.exchange_url"
156 : ",ref.rtransaction_id"
157 : ",ref.reason"
158 : ",ref.refund_amount"
159 : ",merchant_refund_proofs.exchange_sig IS NULL AS pending"
160 : " FROM merchant_deposit_confirmations mcon"
161 : " JOIN merchant_deposits dep"
162 : " USING (deposit_confirmation_serial)"
163 : " JOIN merchant_refunds ref"
164 : " USING (order_serial, coin_pub)"
165 : " LEFT JOIN merchant_refund_proofs"
166 : " USING (refund_serial)"
167 : " WHERE mcon.order_serial="
168 : " (SELECT order_serial"
169 : " FROM merchant_contract_terms"
170 : " WHERE h_contract_terms=$1)");
171 :
172 87 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
173 : "",
174 : params,
175 : &lookup_refunds_detailed_cb,
176 : &lrdc);
177 87 : if (0 >= qs)
178 60 : return qs;
179 27 : return lrdc.qs;
180 : }
|