merchant

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

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


      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 keys keys of the exchange, NULL if we do not have any yet
     38  * @param next_download when will be the next download
     39  * @param keys_expiration when does the current ``/keys`` response expire
     40  * @param http_status last HTTP status from ``/keys``
     41  * @param ec last error code from fetching ``/keys``
     42  */
     43 static void
     44 add_exchange (
     45   void *cls,
     46   const char *exchange_url,
     47   const json_t *keys,
     48   struct GNUNET_TIME_Absolute next_download,
     49   struct GNUNET_TIME_Absolute keys_expiration,
     50   unsigned int http_status,
     51   enum TALER_ErrorCode ec)
     52 {
     53   json_t *xa = cls;
     54   json_t *xi;
     55   json_t *debit_restrictions = NULL;
     56 
     57   if (NULL != keys)
     58   {
     59     json_t *accounts;
     60     json_t *account;
     61     size_t i;
     62 
     63     accounts = json_object_get (keys,
     64                                 "accounts");
     65     debit_restrictions = json_object ();
     66     json_array_foreach (accounts, i, account)
     67     {
     68       json_t *dr = json_object_get (account,
     69                                     "debit_restrictions");
     70       const char *payto;
     71 
     72       payto = json_string_value (json_object_get (account,
     73                                                   "payto_uri"));
     74 
     75       if (NULL != dr)
     76       {
     77         GNUNET_break (0 ==
     78                       json_object_set (debit_restrictions,
     79                                        payto,
     80                                        dr));
     81       }
     82       else
     83       {
     84         GNUNET_break (0 ==
     85                       json_object_set (debit_restrictions,
     86                                        payto,
     87                                        json_null ()));
     88       }
     89     }
     90   }
     91 
     92   xi = GNUNET_JSON_PACK (
     93     GNUNET_JSON_pack_string ("exchange_url",
     94                              exchange_url),
     95     GNUNET_JSON_pack_timestamp ("next_download",
     96                                 GNUNET_TIME_absolute_to_timestamp (next_download
     97                                                                    )),
     98     GNUNET_TIME_absolute_is_zero (keys_expiration)
     99     ? GNUNET_JSON_pack_allow_null (
    100       GNUNET_JSON_pack_string ("dummy",
    101                                NULL))
    102     : GNUNET_JSON_pack_timestamp ("keys_expiration",
    103                                   GNUNET_TIME_absolute_to_timestamp (
    104                                     keys_expiration)),
    105     GNUNET_JSON_pack_allow_null (
    106       GNUNET_JSON_pack_object_steal ("debit_restrictions",
    107                                      debit_restrictions)),
    108     GNUNET_JSON_pack_uint64 ("keys_http_status",
    109                              http_status),
    110     GNUNET_JSON_pack_uint64 ("keys_ec",
    111                              ec),
    112     GNUNET_JSON_pack_string ("keys_hint",
    113                              TALER_ErrorCode_get_hint (ec)));
    114   GNUNET_assert (NULL != xi);
    115   GNUNET_assert (0 ==
    116                  json_array_append_new (xa,
    117                                         xi));
    118 }
    119 
    120 
    121 enum MHD_Result
    122 MH_handler_exchanges (const struct TMH_RequestHandler *rh,
    123                       struct MHD_Connection *connection,
    124                       struct TMH_HandlerContext *hc)
    125 {
    126   json_t *exchanges;
    127   enum GNUNET_DB_QueryStatus qs;
    128 
    129   exchanges = json_array ();
    130   GNUNET_assert (NULL != exchanges);
    131   qs = TALER_MERCHANTDB_select_exchanges (TMH_db,
    132                                           &add_exchange,
    133                                           exchanges);
    134   if (0 > qs)
    135   {
    136     GNUNET_break (0);
    137     json_decref (exchanges);
    138     return TALER_MHD_reply_with_error (connection,
    139                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    140                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    141                                        "select_exchanges");
    142   }
    143   return TALER_MHD_REPLY_JSON_PACK (
    144     connection,
    145     MHD_HTTP_OK,
    146     GNUNET_JSON_pack_array_steal ("exchanges",
    147                                   exchanges));
    148 }
    149 
    150 
    151 /* end of taler-merchant-httpd_get-exchanges.c */