taler-merchant-httpd_private-get-incoming.c (7645B)
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 expected_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 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 json_t *ja = cls; 60 json_t *r; 61 62 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_allow_null ( 75 GNUNET_JSON_pack_timestamp ("execution_time", 76 execution_time)), 77 GNUNET_JSON_pack_bool ("validated", 78 validated), 79 GNUNET_JSON_pack_bool ("confirmed", 80 confirmed), 81 GNUNET_JSON_pack_uint64 ("last_http_status", 82 last_http_status), 83 GNUNET_JSON_pack_uint64 ("last_ec", 84 last_ec), 85 GNUNET_JSON_pack_allow_null ( 86 GNUNET_JSON_pack_string ("last_error_detail", 87 last_error_detail))); 88 GNUNET_assert (0 == 89 json_array_append_new (ja, 90 r)); 91 } 92 93 94 /** 95 * Manages a GET /private/incoming call. 96 * 97 * @param rh context of the handler 98 * @param connection the MHD connection to handle 99 * @param[in,out] hc context with further information about the request 100 * @return MHD result code 101 */ 102 MHD_RESULT 103 TMH_private_get_incoming (const struct TMH_RequestHandler *rh, 104 struct MHD_Connection *connection, 105 struct TMH_HandlerContext *hc) 106 { 107 struct TALER_FullPayto payto_uri = { 108 .full_payto = NULL 109 }; 110 struct GNUNET_TIME_Timestamp before = GNUNET_TIME_UNIT_FOREVER_TS; 111 struct GNUNET_TIME_Timestamp after = GNUNET_TIME_UNIT_ZERO_TS; 112 int64_t limit = -20; 113 uint64_t offset; 114 enum TALER_EXCHANGE_YesNoAll confirmed; 115 enum TALER_EXCHANGE_YesNoAll verified; 116 117 (void) rh; 118 TALER_MHD_parse_request_snumber (connection, 119 "limit", 120 &limit); 121 if (limit < 0) 122 offset = INT64_MAX; 123 else 124 offset = 0; 125 TALER_MHD_parse_request_number (connection, 126 "offset", 127 &offset); 128 TALER_MHD_parse_request_yna (connection, 129 "verified", 130 TALER_EXCHANGE_YNA_ALL, 131 &verified); 132 TALER_MHD_parse_request_yna (connection, 133 "confirmed", 134 TALER_EXCHANGE_YNA_ALL, 135 &confirmed); 136 TALER_MHD_parse_request_timestamp (connection, 137 "before", 138 &before); 139 TALER_MHD_parse_request_timestamp (connection, 140 "after", 141 &after); 142 { 143 const char *esc_payto; 144 145 esc_payto = MHD_lookup_connection_value (connection, 146 MHD_GET_ARGUMENT_KIND, 147 "payto_uri"); 148 if (NULL != esc_payto) 149 { 150 payto_uri.full_payto 151 = GNUNET_strdup (esc_payto); 152 (void) MHD_http_unescape (payto_uri.full_payto); 153 } 154 } 155 TMH_db->preflight (TMH_db->cls); 156 { 157 json_t *ja; 158 enum GNUNET_DB_QueryStatus qs; 159 160 ja = json_array (); 161 GNUNET_assert (NULL != ja); 162 qs = TMH_db->lookup_expected_transfers (TMH_db->cls, 163 hc->instance->settings.id, 164 payto_uri, 165 before, 166 after, 167 limit, 168 offset, 169 confirmed, 170 verified, 171 &incoming_cb, 172 ja); 173 GNUNET_free (payto_uri.full_payto); 174 if (0 > qs) 175 { 176 /* Simple select queries should not cause serialization issues */ 177 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); 178 /* Always report on hard error as well to enable diagnostics */ 179 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 180 return TALER_MHD_reply_with_error (connection, 181 MHD_HTTP_INTERNAL_SERVER_ERROR, 182 TALER_EC_GENERIC_DB_FETCH_FAILED, 183 "incoming"); 184 } 185 return TALER_MHD_REPLY_JSON_PACK ( 186 connection, 187 MHD_HTTP_OK, 188 GNUNET_JSON_pack_array_steal ("incoming", 189 ja)); 190 } 191 } 192 193 194 /* end of taler-merchant-httpd_private-get-incoming.c */