merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-httpd_get-exchanges.c (3889B)


      1 /*
      2   This file is part of TALER
      3   (C) 2026 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-exchanges.c
     18  * @brief implement API for querying exchange status
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <jansson.h>
     23 #include <taler/taler_util.h>
     24 #include <taler/taler_json_lib.h>
     25 #include "taler-merchant-httpd.h"
     26 #include "taler-merchant-httpd_get-exchanges.h"
     27 #include "taler-merchant-httpd_mhd.h"
     28 #include "taler-merchant-httpd_get-exchanges.h"
     29 #include "merchant-database/select_exchanges.h"
     30 
     31 
     32 /**
     33  * Callback on an exchange known to us.
     34  *
     35  * @param cls closure with `json_t *` array to expand
     36  * @param exchange_url base URL of the exchange
     37  * @param next_download when will be the next download
     38  * @param keys_expiration when does the current ``/keys`` response expire
     39  * @param http_status last HTTP status from ``/keys``
     40  * @param ec last error code from fetching ``/keys``
     41  */
     42 static void
     43 add_exchange (
     44   void *cls,
     45   const char *exchange_url,
     46   struct GNUNET_TIME_Absolute next_download,
     47   struct GNUNET_TIME_Absolute keys_expiration,
     48   unsigned int http_status,
     49   enum TALER_ErrorCode ec)
     50 {
     51   json_t *xa = cls;
     52   json_t *xi;
     53 
     54   xi = GNUNET_JSON_PACK (
     55     GNUNET_JSON_pack_string ("exchange_url",
     56                              exchange_url),
     57     GNUNET_JSON_pack_timestamp ("next_download",
     58                                 GNUNET_TIME_absolute_to_timestamp (next_download
     59                                                                    )),
     60     GNUNET_TIME_absolute_is_zero (keys_expiration)
     61     ? GNUNET_JSON_pack_allow_null (
     62       GNUNET_JSON_pack_string ("dummy",
     63                                NULL))
     64     : GNUNET_JSON_pack_timestamp ("keys_expiration",
     65                                   GNUNET_TIME_absolute_to_timestamp (
     66                                     keys_expiration)),
     67     GNUNET_JSON_pack_uint64 ("keys_http_status",
     68                              http_status),
     69     GNUNET_JSON_pack_uint64 ("keys_ec",
     70                              ec),
     71     GNUNET_JSON_pack_string ("keys_hint",
     72                              TALER_ErrorCode_get_hint (ec)));
     73   GNUNET_assert (NULL != xi);
     74   GNUNET_assert (0 ==
     75                  json_array_append_new (xa,
     76                                         xi));
     77 }
     78 
     79 
     80 enum MHD_Result
     81 MH_handler_exchanges (const struct TMH_RequestHandler *rh,
     82                       struct MHD_Connection *connection,
     83                       struct TMH_HandlerContext *hc)
     84 {
     85   json_t *exchanges;
     86   enum GNUNET_DB_QueryStatus qs;
     87 
     88   exchanges = json_array ();
     89   GNUNET_assert (NULL != exchanges);
     90   qs = TALER_MERCHANTDB_select_exchanges (TMH_db,
     91                                           &add_exchange,
     92                                           exchanges);
     93   if (0 > qs)
     94   {
     95     GNUNET_break (0);
     96     json_decref (exchanges);
     97     return TALER_MHD_reply_with_error (connection,
     98                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     99                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    100                                        "select_exchanges");
    101   }
    102   return TALER_MHD_REPLY_JSON_PACK (
    103     connection,
    104     MHD_HTTP_OK,
    105     GNUNET_JSON_pack_array_steal ("exchanges",
    106                                   exchanges));
    107 }
    108 
    109 
    110 /* end of taler-merchant-httpd_get-exchanges.c */