Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 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 src/backend/taler-merchant-httpd_get-private-incoming.c
18 : * @brief implement API for obtaining a list of expected incoming wire transfers
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <jansson.h>
23 : #include <taler/taler_json_lib.h>
24 : #include "taler-merchant-httpd_get-private-incoming.h"
25 : #include "merchant-database/iterate_expected_transfers.h"
26 : #include "merchant-database/preflight.h"
27 :
28 :
29 : /**
30 : * Function called with information about a wire transfer.
31 : * Generate a response (array entry) based on the given arguments.
32 : *
33 : * @param cls closure with a `json_t *` array to build up the response
34 : * @param expected_credit_amount amount expected to be wired to the merchant (minus fees), NULL if unknown
35 : * @param wtid wire transfer identifier
36 : * @param payto_uri target account that received the wire transfer
37 : * @param exchange_url base URL of the exchange that made the wire transfer
38 : * @param expected_transfer_serial_id serial number identifying the transfer in the backend
39 : * @param execution_time when did the exchange make the transfer, #GNUNET_TIME_UNIT_FOREVER_ABS
40 : * if it did not yet happen
41 : * @param confirmed true if the merchant acknowledged the wire transfer reception
42 : * @param validated true if the reconciliation succeeded
43 : * @param last_http_status HTTP status of our last request to the exchange for this transfer
44 : * @param last_ec last error code we got back (otherwise #TALER_EC_NONE)
45 : * @param last_error_detail last detail we got back (or NULL for none)
46 : */
47 : static void
48 0 : incoming_cb (void *cls,
49 : const struct TALER_Amount *expected_credit_amount,
50 : const struct TALER_WireTransferIdentifierRawP *wtid,
51 : struct TALER_FullPayto payto_uri,
52 : const char *exchange_url,
53 : uint64_t expected_transfer_serial_id,
54 : struct GNUNET_TIME_Timestamp execution_time,
55 : bool confirmed,
56 : bool validated,
57 : unsigned int last_http_status,
58 : enum TALER_ErrorCode last_ec,
59 : const char *last_error_detail)
60 : {
61 0 : json_t *ja = cls;
62 : json_t *r;
63 :
64 0 : r = GNUNET_JSON_PACK (
65 : GNUNET_JSON_pack_allow_null (
66 : TALER_JSON_pack_amount ("expected_credit_amount",
67 : expected_credit_amount)),
68 : GNUNET_JSON_pack_data_auto ("wtid",
69 : wtid),
70 : TALER_JSON_pack_full_payto ("payto_uri",
71 : payto_uri),
72 : GNUNET_JSON_pack_string ("exchange_url",
73 : exchange_url),
74 : GNUNET_JSON_pack_uint64 ("expected_transfer_serial_id",
75 : expected_transfer_serial_id),
76 : GNUNET_JSON_pack_allow_null (
77 : GNUNET_JSON_pack_timestamp ("execution_time",
78 : execution_time)),
79 : GNUNET_JSON_pack_bool ("validated",
80 : validated),
81 : GNUNET_JSON_pack_bool ("confirmed",
82 : confirmed),
83 : GNUNET_JSON_pack_uint64 ("last_http_status",
84 : last_http_status),
85 : GNUNET_JSON_pack_uint64 ("last_ec",
86 : last_ec),
87 : GNUNET_JSON_pack_allow_null (
88 : GNUNET_JSON_pack_string ("last_error_detail",
89 : last_error_detail)));
90 0 : GNUNET_assert (0 ==
91 : json_array_append_new (ja,
92 : r));
93 0 : }
94 :
95 :
96 : /**
97 : * Manages a GET /private/incoming call.
98 : *
99 : * @param rh context of the handler
100 : * @param connection the MHD connection to handle
101 : * @param[in,out] hc context with further information about the request
102 : * @return MHD result code
103 : */
104 : enum MHD_Result
105 0 : TMH_private_get_incoming (const struct TMH_RequestHandler *rh,
106 : struct MHD_Connection *connection,
107 : struct TMH_HandlerContext *hc)
108 : {
109 0 : struct TALER_FullPayto payto_uri = {
110 : .full_payto = NULL
111 : };
112 0 : struct GNUNET_TIME_Timestamp before = GNUNET_TIME_UNIT_FOREVER_TS;
113 0 : struct GNUNET_TIME_Timestamp after = GNUNET_TIME_UNIT_ZERO_TS;
114 0 : int64_t limit = -20;
115 : uint64_t offset;
116 : enum TALER_EXCHANGE_YesNoAll confirmed;
117 : enum TALER_EXCHANGE_YesNoAll verified;
118 :
119 : (void) rh;
120 0 : TALER_MHD_parse_request_snumber (connection,
121 : "limit",
122 : &limit);
123 0 : if (limit < 0)
124 0 : offset = INT64_MAX;
125 : else
126 0 : offset = 0;
127 0 : TALER_MHD_parse_request_number (connection,
128 : "offset",
129 : &offset);
130 0 : TALER_MHD_parse_request_yna (connection,
131 : "verified",
132 : TALER_EXCHANGE_YNA_ALL,
133 : &verified);
134 0 : TALER_MHD_parse_request_yna (connection,
135 : "confirmed",
136 : TALER_EXCHANGE_YNA_ALL,
137 : &confirmed);
138 0 : TALER_MHD_parse_request_timestamp (connection,
139 : "before",
140 : &before);
141 0 : TALER_MHD_parse_request_timestamp (connection,
142 : "after",
143 : &after);
144 : {
145 : const char *esc_payto;
146 :
147 0 : esc_payto = MHD_lookup_connection_value (connection,
148 : MHD_GET_ARGUMENT_KIND,
149 : "payto_uri");
150 0 : if (NULL != esc_payto)
151 : {
152 : payto_uri.full_payto
153 0 : = GNUNET_strdup (esc_payto);
154 0 : (void) MHD_http_unescape (payto_uri.full_payto);
155 : }
156 : }
157 0 : TALER_MERCHANTDB_preflight (TMH_db);
158 : {
159 : json_t *ja;
160 : enum GNUNET_DB_QueryStatus qs;
161 :
162 0 : ja = json_array ();
163 0 : GNUNET_assert (NULL != ja);
164 0 : qs = TALER_MERCHANTDB_iterate_expected_transfers (TMH_db,
165 0 : hc->instance->settings.id,
166 : payto_uri,
167 : before,
168 : after,
169 : limit,
170 : offset,
171 : confirmed,
172 : verified,
173 : &incoming_cb,
174 : ja);
175 0 : GNUNET_free (payto_uri.full_payto);
176 0 : if (0 > qs)
177 : {
178 : /* Simple select queries should not cause serialization issues */
179 0 : GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
180 : /* Always report on hard error as well to enable diagnostics */
181 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
182 0 : return TALER_MHD_reply_with_error (connection,
183 : MHD_HTTP_INTERNAL_SERVER_ERROR,
184 : TALER_EC_GENERIC_DB_FETCH_FAILED,
185 : "iterate_expected_transfers");
186 : }
187 0 : return TALER_MHD_REPLY_JSON_PACK (
188 : connection,
189 : MHD_HTTP_OK,
190 : GNUNET_JSON_pack_array_steal ("incoming",
191 : ja));
192 : }
193 : }
194 :
195 :
196 : /* end of taler-merchant-httpd_get-private-incoming.c */
|