merchant

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

taler-merchant-httpd_private-get-instances.c (4201B)


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