Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2014-2024 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 17 : transfer_cb (void *cls,
47 : const struct TALER_Amount *credit_amount,
48 : const struct TALER_WireTransferIdentifierRawP *wtid,
49 : struct TALER_FullPayto 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 17 : json_t *ja = cls;
57 : json_t *r;
58 :
59 17 : r = GNUNET_JSON_PACK (
60 : TALER_JSON_pack_amount ("credit_amount",
61 : credit_amount),
62 : GNUNET_JSON_pack_data_auto ("wtid",
63 : wtid),
64 : TALER_JSON_pack_full_payto ("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 17 : GNUNET_assert (0 ==
81 : json_array_append_new (ja,
82 : r));
83 17 : }
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 12 : TMH_private_get_transfers (const struct TMH_RequestHandler *rh,
96 : struct MHD_Connection *connection,
97 : struct TMH_HandlerContext *hc)
98 : {
99 12 : struct TALER_FullPayto payto_uri = {
100 : .full_payto = NULL
101 : };
102 12 : struct GNUNET_TIME_Timestamp before = GNUNET_TIME_UNIT_FOREVER_TS;
103 12 : struct GNUNET_TIME_Timestamp after = GNUNET_TIME_UNIT_ZERO_TS;
104 12 : int64_t limit = -20;
105 : uint64_t offset;
106 : enum TALER_EXCHANGE_YesNoAll verified;
107 :
108 : (void) rh;
109 : {
110 : const char *esc_payto;
111 :
112 12 : esc_payto = MHD_lookup_connection_value (connection,
113 : MHD_GET_ARGUMENT_KIND,
114 : "payto_uri");
115 12 : if (NULL != esc_payto)
116 : {
117 : payto_uri.full_payto
118 4 : = GNUNET_strdup (esc_payto);
119 4 : (void) MHD_http_unescape (payto_uri.full_payto);
120 : }
121 : }
122 : {
123 : const char *before_s;
124 :
125 12 : before_s = MHD_lookup_connection_value (connection,
126 : MHD_GET_ARGUMENT_KIND,
127 : "before");
128 12 : if ( (NULL != before_s) &&
129 : (GNUNET_OK !=
130 0 : GNUNET_STRINGS_fancy_time_to_timestamp (before_s,
131 : &before)) )
132 : {
133 0 : GNUNET_break_op (0);
134 0 : GNUNET_free (payto_uri.full_payto);
135 0 : return TALER_MHD_reply_with_error (connection,
136 : MHD_HTTP_BAD_REQUEST,
137 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
138 : "before");
139 : }
140 : }
141 : {
142 : const char *after_s;
143 :
144 12 : after_s = MHD_lookup_connection_value (connection,
145 : MHD_GET_ARGUMENT_KIND,
146 : "after");
147 12 : if ( (NULL != after_s) &&
148 : (GNUNET_OK !=
149 0 : GNUNET_STRINGS_fancy_time_to_timestamp (after_s,
150 : &after)) )
151 : {
152 0 : GNUNET_break_op (0);
153 0 : GNUNET_free (payto_uri.full_payto);
154 0 : return TALER_MHD_reply_with_error (connection,
155 : MHD_HTTP_BAD_REQUEST,
156 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
157 : "after");
158 : }
159 : }
160 12 : TALER_MHD_parse_request_snumber (connection,
161 : "limit",
162 : &limit);
163 12 : if (limit < 0)
164 8 : offset = INT64_MAX;
165 : else
166 4 : offset = 0;
167 12 : TALER_MHD_parse_request_number (connection,
168 : "offset",
169 : &offset);
170 12 : if (! (TALER_MHD_arg_to_yna (connection,
171 : "verified",
172 : TALER_EXCHANGE_YNA_ALL,
173 : &verified)) )
174 : {
175 0 : GNUNET_break_op (0);
176 0 : GNUNET_free (payto_uri.full_payto);
177 0 : return TALER_MHD_reply_with_error (connection,
178 : MHD_HTTP_BAD_REQUEST,
179 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
180 : "verified");
181 : }
182 12 : TMH_db->preflight (TMH_db->cls);
183 : {
184 : json_t *ja;
185 : enum GNUNET_DB_QueryStatus qs;
186 :
187 12 : ja = json_array ();
188 12 : GNUNET_assert (NULL != ja);
189 12 : qs = TMH_db->lookup_transfers (TMH_db->cls,
190 12 : hc->instance->settings.id,
191 : payto_uri,
192 : before,
193 : after,
194 : limit,
195 : offset,
196 : verified,
197 : &transfer_cb,
198 : ja);
199 12 : GNUNET_free (payto_uri.full_payto);
200 12 : if (0 > qs)
201 : {
202 : /* Simple select queries should not cause serialization issues */
203 0 : GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
204 : /* Always report on hard error as well to enable diagnostics */
205 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
206 0 : return TALER_MHD_reply_with_error (connection,
207 : MHD_HTTP_INTERNAL_SERVER_ERROR,
208 : TALER_EC_GENERIC_DB_FETCH_FAILED,
209 : "transfers");
210 : }
211 12 : return TALER_MHD_REPLY_JSON_PACK (
212 : connection,
213 : MHD_HTTP_OK,
214 : GNUNET_JSON_pack_array_steal ("transfers",
215 : ja));
216 : }
217 : }
218 :
219 :
220 : /* end of taler-merchant-httpd_track-transfer.c */
|