merchant

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

taler-merchant-httpd_get-management-instances.c (4231B)


      1 /*
      2   This file is part of TALER
      3   (C) 2019-2023 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-management-instances.c
     18  * @brief implement GET /instances
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "taler-merchant-httpd_get-management-instances.h"
     23 
     24 
     25 /**
     26  * Add merchant instance to our JSON array.
     27  *
     28  * @param cls a `json_t *` JSON array to build
     29  * @param key unused
     30  * @param value a `struct TMH_MerchantInstance *`
     31  * @return #GNUNET_OK (continue to iterate)
     32  */
     33 static enum GNUNET_GenericReturnValue
     34 add_instance (void *cls,
     35               const struct GNUNET_HashCode *key,
     36               void *value)
     37 {
     38   json_t *ja = cls;
     39   struct TMH_MerchantInstance *mi = value;
     40   json_t *pta;
     41 
     42   (void) key;
     43   /* Compile array of all unique wire methods supported by this
     44      instance */
     45   pta = json_array ();
     46   GNUNET_assert (NULL != pta);
     47   for (struct TMH_WireMethod *wm = mi->wm_head;
     48        NULL != wm;
     49        wm = wm->next)
     50   {
     51     bool duplicate = false;
     52 
     53     if (! wm->active)
     54       continue;
     55     /* Yes, O(n^2), but really how many bank accounts can an
     56        instance realistically have for this to matter? */
     57     for (struct TMH_WireMethod *pm = mi->wm_head;
     58          pm != wm;
     59          pm = pm->next)
     60       if (0 == strcasecmp (pm->wire_method,
     61                            wm->wire_method))
     62       {
     63         duplicate = true;
     64         break;
     65       }
     66     if (duplicate)
     67       continue;
     68     GNUNET_assert (0 ==
     69                    json_array_append_new (pta,
     70                                           json_string (wm->wire_method)));
     71   }
     72   GNUNET_assert (0 ==
     73                  json_array_append_new (
     74                    ja,
     75                    GNUNET_JSON_PACK (
     76                      GNUNET_JSON_pack_string ("name",
     77                                               mi->settings.name),
     78                      GNUNET_JSON_pack_allow_null (
     79                        GNUNET_JSON_pack_string ("website",
     80                                                 mi->settings.website)),
     81                      GNUNET_JSON_pack_allow_null (
     82                        GNUNET_JSON_pack_string ("logo",
     83                                                 mi->settings.logo)),
     84                      GNUNET_JSON_pack_string ("id",
     85                                               mi->settings.id),
     86                      GNUNET_JSON_pack_data_auto ("merchant_pub",
     87                                                  &mi->merchant_pub),
     88                      GNUNET_JSON_pack_array_steal ("payment_targets",
     89                                                    pta),
     90                      GNUNET_JSON_pack_bool ("deleted",
     91                                             mi->deleted))));
     92   return GNUNET_OK;
     93 }
     94 
     95 
     96 /**
     97  * Handle a GET "/instances" request.
     98  *
     99  * @param rh context of the handler
    100  * @param connection the MHD connection to handle
    101  * @param[in,out] hc context with further information about the request
    102  * @return MHD result code
    103  */
    104 enum MHD_Result
    105 TMH_private_get_instances (const struct TMH_RequestHandler *rh,
    106                            struct MHD_Connection *connection,
    107                            struct TMH_HandlerContext *hc)
    108 {
    109   json_t *ia;
    110 
    111   (void) rh;
    112   (void) hc;
    113   ia = json_array ();
    114   GNUNET_assert (NULL != ia);
    115   GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map,
    116                                          &add_instance,
    117                                          ia);
    118   return TALER_MHD_REPLY_JSON_PACK (
    119     connection,
    120     MHD_HTTP_OK,
    121     GNUNET_JSON_pack_array_steal ("instances",
    122                                   ia));
    123 }
    124 
    125 
    126 /* end of taler-merchant-httpd_get-management-instances.c */