merchant

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

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


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