taler-merchant-httpd_get-private-transfers.c (7208B)
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 src/backend/taler-merchant-httpd_get-private-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_get-private-transfers.h" 26 #include "merchant-database/lookup_transfers.h" 27 #include "merchant-database/preflight.h" 28 29 30 /** 31 * Function called with information about a wire transfer. 32 * Generate a response (array entry) based on the given arguments. 33 * 34 * @param cls closure with a `json_t *` array to build up the response 35 * @param credit_amount amount expected to be wired to the merchant (minus fees), NULL if unknown 36 * @param wtid wire transfer identifier 37 * @param payto_uri target account that received the wire transfer 38 * @param exchange_url base URL of the exchange that made the wire transfer 39 * @param transfer_serial_id serial number identifying the transfer in the backend 40 * @param expected_transfer_serial_id serial number identifying the expected transfer in the backend, 0 if not @a expected 41 * @param execution_time when did the exchange make the transfer, #GNUNET_TIME_UNIT_FOREVER_ABS 42 * if it did not yet happen 43 * @param expected true if the merchant acknowledged the wire transfer reception 44 */ 45 static void 46 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 uint64_t expected_transfer_serial_id, 53 struct GNUNET_TIME_Absolute execution_time, 54 bool expected) 55 { 56 json_t *ja = cls; 57 json_t *r; 58 59 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_conditional ( 71 0 != expected_transfer_serial_id, 72 GNUNET_JSON_pack_uint64 ("expected_transfer_serial_id", 73 expected_transfer_serial_id)), 74 // FIXME: protocol breaking to remove... 75 GNUNET_JSON_pack_bool ("verified", 76 false), 77 // FIXME: protocol breaking to remove... 78 GNUNET_JSON_pack_bool ("confirmed", 79 true), 80 GNUNET_JSON_pack_bool ("expected", 81 expected), 82 GNUNET_JSON_pack_conditional ( 83 ! GNUNET_TIME_absolute_is_zero (execution_time), 84 GNUNET_JSON_pack_timestamp ( 85 "execution_time", 86 GNUNET_TIME_absolute_to_timestamp (execution_time)))); 87 GNUNET_assert (0 == 88 json_array_append_new (ja, 89 r)); 90 } 91 92 93 /** 94 * Manages a GET /private/transfers 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 enum MHD_Result 102 TMH_private_get_transfers (const struct TMH_RequestHandler *rh, 103 struct MHD_Connection *connection, 104 struct TMH_HandlerContext *hc) 105 { 106 struct TALER_FullPayto payto_uri = { 107 .full_payto = NULL 108 }; 109 struct GNUNET_TIME_Timestamp before = GNUNET_TIME_UNIT_FOREVER_TS; 110 struct GNUNET_TIME_Timestamp after = GNUNET_TIME_UNIT_ZERO_TS; 111 int64_t limit = -20; 112 uint64_t offset; 113 enum TALER_EXCHANGE_YesNoAll expected; 114 115 (void) rh; 116 TALER_MHD_parse_request_snumber (connection, 117 "limit", 118 &limit); 119 if (limit < 0) 120 offset = INT64_MAX; 121 else 122 offset = 0; 123 TALER_MHD_parse_request_number (connection, 124 "offset", 125 &offset); 126 TALER_MHD_parse_request_yna (connection, 127 "expected", 128 TALER_EXCHANGE_YNA_ALL, 129 &expected); 130 TALER_MHD_parse_request_timestamp (connection, 131 "before", 132 &before); 133 TALER_MHD_parse_request_timestamp (connection, 134 "after", 135 &after); 136 { 137 const char *esc_payto; 138 139 esc_payto = MHD_lookup_connection_value (connection, 140 MHD_GET_ARGUMENT_KIND, 141 "payto_uri"); 142 if (NULL != esc_payto) 143 { 144 payto_uri.full_payto 145 = GNUNET_strdup (esc_payto); 146 (void) MHD_http_unescape (payto_uri.full_payto); 147 } 148 } 149 TALER_MERCHANTDB_preflight (TMH_db); 150 { 151 json_t *ja; 152 enum GNUNET_DB_QueryStatus qs; 153 154 ja = json_array (); 155 GNUNET_assert (NULL != ja); 156 qs = TALER_MERCHANTDB_lookup_transfers (TMH_db, 157 hc->instance->settings.id, 158 payto_uri, 159 before, 160 after, 161 limit, 162 offset, 163 expected, 164 &transfer_cb, 165 ja); 166 GNUNET_free (payto_uri.full_payto); 167 if (0 > qs) 168 { 169 /* Simple select queries should not cause serialization issues */ 170 GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs); 171 /* Always report on hard error as well to enable diagnostics */ 172 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 173 return TALER_MHD_reply_with_error (connection, 174 MHD_HTTP_INTERNAL_SERVER_ERROR, 175 TALER_EC_GENERIC_DB_FETCH_FAILED, 176 "transfers"); 177 } 178 return TALER_MHD_REPLY_JSON_PACK ( 179 connection, 180 MHD_HTTP_OK, 181 GNUNET_JSON_pack_array_steal ("transfers", 182 ja)); 183 } 184 } 185 186 187 /* end of taler-merchant-httpd_track-transfer.c */