merchant

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

taler-merchant-httpd_get-private-donau.c (4903B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 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-private-donau.c
     18  * @brief implementation of GET /donau
     19  * @author Bohdan Potuzhnyi
     20  * @author Vlada Svirsh
     21  */
     22 #include "taler/platform.h"
     23 #include <jansson.h>
     24 #include <taler/taler_json_lib.h>
     25 #include <taler/taler_dbevents.h>
     26 #include "taler/taler_merchant_service.h"
     27 #include "taler-merchant-httpd_get-private-donau.h"
     28 
     29 
     30 /**
     31  * Add details about a Donau instance to the JSON array.
     32  *
     33  * @param cls json array to which the Donau instance details will be added
     34  * @param donau_instance_serial the serial number of the Donau instance
     35  * @param donau_url the URL of the Donau instance
     36  * @param charity_name the name of the charity
     37  * @param charity_pub_key the public key of the charity
     38  * @param charity_id the ID of the charity
     39  * @param charity_max_per_year the maximum donation amount per year
     40  * @param charity_receipts_to_date the total donations received so far this year
     41  * @param current_year the current year being tracked for donations
     42  * @param donau_keys_json JSON object with key information specific to the Donau instance, NULL if not (yet) available.
     43  */
     44 static void
     45 add_donau_instance (void *cls,
     46                     uint64_t donau_instance_serial,
     47                     const char *donau_url,
     48                     const char *charity_name,
     49                     const struct DONAU_CharityPublicKeyP *charity_pub_key,
     50                     uint64_t charity_id,
     51                     const struct TALER_Amount *charity_max_per_year,
     52                     const struct TALER_Amount *charity_receipts_to_date,
     53                     int64_t current_year,
     54                     const json_t *donau_keys_json)
     55 {
     56   json_t *json_instances = cls;
     57 
     58   GNUNET_assert (
     59     0 == json_array_append_new (
     60       json_instances,
     61       GNUNET_JSON_PACK (
     62         GNUNET_JSON_pack_uint64 ("donau_instance_serial",
     63                                  donau_instance_serial),
     64         GNUNET_JSON_pack_string ("donau_url",
     65                                  donau_url),
     66         GNUNET_JSON_pack_string ("charity_name",
     67                                  charity_name),
     68         GNUNET_JSON_pack_data_auto ("charity_pub_key",
     69                                     charity_pub_key),
     70         GNUNET_JSON_pack_uint64 ("charity_id",
     71                                  charity_id),
     72         TALER_JSON_pack_amount ("charity_max_per_year",
     73                                 charity_max_per_year),
     74         TALER_JSON_pack_amount ("charity_receipts_to_date",
     75                                 charity_receipts_to_date),
     76         GNUNET_JSON_pack_int64 ("current_year",
     77                                 current_year),
     78         GNUNET_JSON_pack_allow_null (
     79           GNUNET_JSON_pack_object_incref ("donau_keys_json",
     80                                           (json_t *) donau_keys_json))
     81         )));
     82 }
     83 
     84 
     85 /**
     86  * Handle a GET "/donau" request.
     87  *
     88  * @param rh context of the handler
     89  * @param connection the MHD connection to handle
     90  * @param[in,out] hc context with further information about the request
     91  * @return MHD result code
     92  */
     93 MHD_RESULT
     94 TMH_private_get_donau_instances (const struct TMH_RequestHandler *rh,
     95                                  struct MHD_Connection *connection,
     96                                  struct TMH_HandlerContext *hc)
     97 {
     98   json_t *json_donau_instances = json_array ();
     99   enum GNUNET_DB_QueryStatus qs;
    100 
    101   TMH_db->preflight (TMH_db->cls);
    102   qs = TMH_db->select_donau_instances (TMH_db->cls,
    103                                        hc->instance->settings.id,
    104                                        &add_donau_instance,
    105                                        json_donau_instances);
    106   if (0 > qs)
    107   {
    108     GNUNET_break (0);
    109     json_decref (json_donau_instances);
    110     return TALER_MHD_reply_with_error (connection,
    111                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    112                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    113                                        NULL);
    114   }
    115 
    116   return TALER_MHD_REPLY_JSON_PACK (connection,
    117                                     MHD_HTTP_OK,
    118                                     GNUNET_JSON_pack_array_steal (
    119                                       "donau_instances",
    120                                       json_donau_instances));
    121 }