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