Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2014-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 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 amount expected to be wired to the merchant (minus fees), NULL if unknown
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 expected true if the merchant acknowledged the wire transfer reception
41 : */
42 : static void
43 17 : transfer_cb (void *cls,
44 : const struct TALER_Amount *credit_amount,
45 : const struct TALER_WireTransferIdentifierRawP *wtid,
46 : struct TALER_FullPayto payto_uri,
47 : const char *exchange_url,
48 : uint64_t transfer_serial_id,
49 : struct GNUNET_TIME_Absolute execution_time,
50 : bool expected)
51 : {
52 17 : json_t *ja = cls;
53 : json_t *r;
54 :
55 17 : r = GNUNET_JSON_PACK (
56 : TALER_JSON_pack_amount ("credit_amount",
57 : credit_amount),
58 : GNUNET_JSON_pack_data_auto ("wtid",
59 : wtid),
60 : TALER_JSON_pack_full_payto ("payto_uri",
61 : payto_uri),
62 : GNUNET_JSON_pack_string ("exchange_url",
63 : exchange_url),
64 : GNUNET_JSON_pack_uint64 ("transfer_serial_id",
65 : transfer_serial_id),
66 : // FIXME: protocol breaking to remove...
67 : GNUNET_JSON_pack_bool ("verified",
68 : false),
69 : // FIXME: protocol breaking to remove...
70 : GNUNET_JSON_pack_bool ("confirmed",
71 : true),
72 : GNUNET_JSON_pack_bool ("expected",
73 : expected),
74 : GNUNET_TIME_absolute_is_zero (execution_time)
75 : ? GNUNET_JSON_pack_allow_null (
76 : GNUNET_JSON_pack_string ("dummy",
77 : NULL))
78 : : GNUNET_JSON_pack_timestamp (
79 : "execution_time",
80 : GNUNET_TIME_absolute_to_timestamp (execution_time)));
81 17 : GNUNET_assert (0 ==
82 : json_array_append_new (ja,
83 : r));
84 17 : }
85 :
86 :
87 : /**
88 : * Manages a GET /private/transfers call.
89 : *
90 : * @param rh context of the handler
91 : * @param connection the MHD connection to handle
92 : * @param[in,out] hc context with further information about the request
93 : * @return MHD result code
94 : */
95 : MHD_RESULT
96 12 : TMH_private_get_transfers (const struct TMH_RequestHandler *rh,
97 : struct MHD_Connection *connection,
98 : struct TMH_HandlerContext *hc)
99 : {
100 12 : struct TALER_FullPayto payto_uri = {
101 : .full_payto = NULL
102 : };
103 12 : struct GNUNET_TIME_Timestamp before = GNUNET_TIME_UNIT_FOREVER_TS;
104 12 : struct GNUNET_TIME_Timestamp after = GNUNET_TIME_UNIT_ZERO_TS;
105 12 : int64_t limit = -20;
106 : uint64_t offset;
107 : enum TALER_EXCHANGE_YesNoAll expected;
108 :
109 : (void) rh;
110 12 : TALER_MHD_parse_request_snumber (connection,
111 : "limit",
112 : &limit);
113 12 : if (limit < 0)
114 8 : offset = INT64_MAX;
115 : else
116 4 : offset = 0;
117 12 : TALER_MHD_parse_request_number (connection,
118 : "offset",
119 : &offset);
120 12 : TALER_MHD_parse_request_yna (connection,
121 : "expected",
122 : TALER_EXCHANGE_YNA_ALL,
123 : &expected);
124 12 : TALER_MHD_parse_request_timestamp (connection,
125 : "before",
126 : &before);
127 12 : TALER_MHD_parse_request_timestamp (connection,
128 : "after",
129 : &after);
130 : {
131 : const char *esc_payto;
132 :
133 12 : esc_payto = MHD_lookup_connection_value (connection,
134 : MHD_GET_ARGUMENT_KIND,
135 : "payto_uri");
136 12 : if (NULL != esc_payto)
137 : {
138 : payto_uri.full_payto
139 4 : = GNUNET_strdup (esc_payto);
140 4 : (void) MHD_http_unescape (payto_uri.full_payto);
141 : }
142 : }
143 12 : TMH_db->preflight (TMH_db->cls);
144 : {
145 : json_t *ja;
146 : enum GNUNET_DB_QueryStatus qs;
147 :
148 12 : ja = json_array ();
149 12 : GNUNET_assert (NULL != ja);
150 12 : qs = TMH_db->lookup_transfers (TMH_db->cls,
151 12 : hc->instance->settings.id,
152 : payto_uri,
153 : before,
154 : after,
155 : limit,
156 : offset,
157 : expected,
158 : &transfer_cb,
159 : ja);
160 12 : GNUNET_free (payto_uri.full_payto);
161 12 : if (0 > qs)
162 : {
163 : /* Simple select queries should not cause serialization issues */
164 0 : GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
165 : /* Always report on hard error as well to enable diagnostics */
166 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
167 0 : return TALER_MHD_reply_with_error (connection,
168 : MHD_HTTP_INTERNAL_SERVER_ERROR,
169 : TALER_EC_GENERIC_DB_FETCH_FAILED,
170 : "transfers");
171 : }
172 12 : return TALER_MHD_REPLY_JSON_PACK (
173 : connection,
174 : MHD_HTTP_OK,
175 : GNUNET_JSON_pack_array_steal ("transfers",
176 : ja));
177 : }
178 : }
179 :
180 :
181 : /* end of taler-merchant-httpd_track-transfer.c */
|