Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2014-2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Affero 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 taler-merchant-httpd_private-get-transfers.c
18 : * @brief implement API for obtaining a list of wire transfers
19 : * @author Marcello Stanisci
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <jansson.h>
24 : #include <taler/taler_json_lib.h>
25 : #include "taler-merchant-httpd_private-get-transfers.h"
26 :
27 :
28 : /**
29 : * Function called with information about a wire transfer.
30 : * Generate a response (array entry) based on the given arguments.
31 : *
32 : * @param cls closure with a `json_t *` array to build up the response
33 : * @param credit_amount how much was wired to the merchant (minus fees)
34 : * @param wtid wire transfer identifier
35 : * @param payto_uri target account that received the wire transfer
36 : * @param exchange_url base URL of the exchange that made the wire transfer
37 : * @param transfer_serial_id serial number identifying the transfer in the backend
38 : * @param execution_time when did the exchange make the transfer, #GNUNET_TIME_UNIT_FOREVER_ABS
39 : * if it did not yet happen
40 : * @param verified YES if we checked the exchange's answer and liked it,
41 : * NO if we checked the exchange's answer and it is problematic,
42 : * ALL if we did not yet check
43 : * @param confirmed true if the merchant acknowledged the wire transfer reception
44 : */
45 : static void
46 0 : transfer_cb (void *cls,
47 : const struct TALER_Amount *credit_amount,
48 : const struct TALER_WireTransferIdentifierRawP *wtid,
49 : const char *payto_uri,
50 : const char *exchange_url,
51 : uint64_t transfer_serial_id,
52 : struct GNUNET_TIME_Timestamp execution_time,
53 : bool verified,
54 : bool confirmed)
55 : {
56 0 : json_t *ja = cls;
57 : json_t *r;
58 :
59 0 : r = GNUNET_JSON_PACK (
60 : TALER_JSON_pack_amount ("credit_amount",
61 : credit_amount),
62 : GNUNET_JSON_pack_data_auto ("wtid",
63 : wtid),
64 : GNUNET_JSON_pack_string ("payto_uri",
65 : payto_uri),
66 : GNUNET_JSON_pack_string ("exchange_url",
67 : exchange_url),
68 : GNUNET_JSON_pack_uint64 ("transfer_serial_id",
69 : transfer_serial_id),
70 : GNUNET_JSON_pack_bool ("verified",
71 : verified),
72 : GNUNET_JSON_pack_bool ("confirmed",
73 : confirmed),
74 : GNUNET_JSON_pack_allow_null (
75 : GNUNET_JSON_pack_timestamp (
76 : "execution_time",
77 : GNUNET_TIME_absolute_is_never (execution_time.abs_time)
78 : ? GNUNET_TIME_UNIT_ZERO_TS /* => field omitted */
79 : : execution_time)) );
80 0 : GNUNET_assert (0 ==
81 : json_array_append_new (ja,
82 : r));
83 0 : }
84 :
85 :
86 : /**
87 : * Manages a GET /private/transfers call.
88 : *
89 : * @param rh context of the handler
90 : * @param connection the MHD connection to handle
91 : * @param[in,out] hc context with further information about the request
92 : * @return MHD result code
93 : */
94 : MHD_RESULT
95 0 : TMH_private_get_transfers (const struct TMH_RequestHandler *rh,
96 : struct MHD_Connection *connection,
97 : struct TMH_HandlerContext *hc)
98 : {
99 : const char *payto_uri;
100 0 : struct GNUNET_TIME_Timestamp before = GNUNET_TIME_UNIT_FOREVER_TS;
101 0 : struct GNUNET_TIME_Timestamp after = GNUNET_TIME_UNIT_ZERO_TS;
102 0 : int64_t limit = -20;
103 : uint64_t offset;
104 : enum TALER_EXCHANGE_YesNoAll verified;
105 :
106 : (void) rh;
107 0 : payto_uri = MHD_lookup_connection_value (connection,
108 : MHD_GET_ARGUMENT_KIND,
109 : "payto_uri");
110 : {
111 : const char *before_s;
112 :
113 0 : before_s = MHD_lookup_connection_value (connection,
114 : MHD_GET_ARGUMENT_KIND,
115 : "before");
116 0 : if ( (NULL != before_s) &&
117 : (GNUNET_OK !=
118 0 : GNUNET_STRINGS_fancy_time_to_timestamp (before_s,
119 : &before)) )
120 0 : return TALER_MHD_reply_with_error (connection,
121 : MHD_HTTP_BAD_REQUEST,
122 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
123 : "before");
124 : }
125 : {
126 : const char *after_s;
127 :
128 0 : after_s = MHD_lookup_connection_value (connection,
129 : MHD_GET_ARGUMENT_KIND,
130 : "after");
131 0 : if ( (NULL != after_s) &&
132 : (GNUNET_OK !=
133 0 : GNUNET_STRINGS_fancy_time_to_timestamp (after_s,
134 : &after)) )
135 0 : return TALER_MHD_reply_with_error (connection,
136 : MHD_HTTP_BAD_REQUEST,
137 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
138 : "after");
139 : }
140 : {
141 : const char *limit_s;
142 :
143 0 : limit_s = MHD_lookup_connection_value (connection,
144 : MHD_GET_ARGUMENT_KIND,
145 : "limit");
146 0 : if (NULL != limit_s)
147 : {
148 : char dummy[2];
149 : long long l;
150 :
151 0 : if (1 !=
152 0 : sscanf (limit_s,
153 : "%lld%1s",
154 : &l,
155 : dummy))
156 0 : return TALER_MHD_reply_with_error (connection,
157 : MHD_HTTP_BAD_REQUEST,
158 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
159 : "limit");
160 0 : limit = (int64_t) l;
161 : }
162 : }
163 : {
164 : const char *offset_s;
165 :
166 0 : offset_s = MHD_lookup_connection_value (connection,
167 : MHD_GET_ARGUMENT_KIND,
168 : "offset");
169 0 : if (NULL != offset_s)
170 : {
171 : char dummy[2];
172 : unsigned long long o;
173 :
174 0 : if (1 !=
175 0 : sscanf (offset_s,
176 : "%llu%1s",
177 : &o,
178 : dummy))
179 0 : return TALER_MHD_reply_with_error (connection,
180 : MHD_HTTP_BAD_REQUEST,
181 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
182 : "offset");
183 0 : offset = (uint64_t) o;
184 : }
185 : else
186 : {
187 0 : if (limit < 0)
188 0 : offset = INT64_MAX;
189 : else
190 0 : offset = 0;
191 : }
192 : }
193 0 : if (! (TALER_arg_to_yna (connection,
194 : "verified",
195 : TALER_EXCHANGE_YNA_ALL,
196 : &verified)) )
197 0 : return TALER_MHD_reply_with_error (connection,
198 : MHD_HTTP_BAD_REQUEST,
199 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
200 : "verified");
201 :
202 0 : TMH_db->preflight (TMH_db->cls);
203 : {
204 : json_t *ja;
205 : enum GNUNET_DB_QueryStatus qs;
206 :
207 0 : ja = json_array ();
208 0 : GNUNET_assert (NULL != ja);
209 0 : qs = TMH_db->lookup_transfers (TMH_db->cls,
210 0 : hc->instance->settings.id,
211 : payto_uri,
212 : before,
213 : after,
214 : limit,
215 : offset,
216 : verified,
217 : &transfer_cb,
218 : ja);
219 0 : if (0 > qs)
220 : {
221 : /* Simple select queries should not cause serialization issues */
222 0 : GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
223 : /* Always report on hard error as well to enable diagnostics */
224 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
225 0 : return TALER_MHD_reply_with_error (connection,
226 : MHD_HTTP_INTERNAL_SERVER_ERROR,
227 : TALER_EC_GENERIC_DB_FETCH_FAILED,
228 : "transfers");
229 : }
230 0 : return TALER_MHD_REPLY_JSON_PACK (
231 : connection,
232 : MHD_HTTP_OK,
233 : GNUNET_JSON_pack_array_steal ("transfers",
234 : ja));
235 : }
236 : }
237 :
238 :
239 : /* end of taler-merchant-httpd_track-transfer.c */
|