merchant

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

taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c (4861B)


      1 /*
      2   This file is part of TALER
      3   (C) 2023, 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 src/backend/taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
     18  * @brief implement GET /tokenfamilies/$SLUG/
     19  * @author Christian Blättler
     20  */
     21 #include "platform.h"
     22 #include "taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.h"
     23 #include <gnunet/gnunet_json_lib.h>
     24 #include <taler/taler_json_lib.h>
     25 #include "merchant-database/lookup_token_family.h"
     26 
     27 
     28 /**
     29  * Handle a GET "/tokenfamilies/$SLUG" request.
     30  *
     31  * @param rh context of the handler
     32  * @param connection the MHD connection to handle
     33  * @param[in,out] hc context with further information about the request
     34  * @return MHD result code
     35  */
     36 enum MHD_Result
     37 TMH_private_get_tokenfamilies_SLUG (const struct TMH_RequestHandler *rh,
     38                                     struct MHD_Connection *connection,
     39                                     struct TMH_HandlerContext *hc)
     40 {
     41   struct TMH_MerchantInstance *mi = hc->instance;
     42   struct TALER_MERCHANTDB_TokenFamilyDetails details = { 0 };
     43   enum GNUNET_DB_QueryStatus status;
     44 
     45   GNUNET_assert (NULL != mi);
     46   status = TALER_MERCHANTDB_lookup_token_family (TMH_db,
     47                                                  mi->settings.id,
     48                                                  hc->infix,
     49                                                  &details);
     50   if (0 > status)
     51   {
     52     GNUNET_break (0);
     53     return TALER_MHD_reply_with_error (connection,
     54                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     55                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     56                                        "lookup_token_family");
     57   }
     58   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == status)
     59   {
     60     return TALER_MHD_reply_with_error (connection,
     61                                        MHD_HTTP_NOT_FOUND,
     62                                        TALER_EC_MERCHANT_GENERIC_TOKEN_FAMILY_UNKNOWN,
     63                                        hc->infix);
     64   }
     65   {
     66     char *kind = NULL;
     67     enum MHD_Result result;
     68 
     69     if (TALER_MERCHANTDB_TFK_Subscription == details.kind)
     70     {
     71       kind = GNUNET_strdup ("subscription");
     72     }
     73     else if (TALER_MERCHANTDB_TFK_Discount == details.kind)
     74     {
     75       kind = GNUNET_strdup ("discount");
     76     }
     77     else
     78     {
     79       GNUNET_break (0);
     80       return TALER_MHD_reply_with_error (connection,
     81                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     82                                          TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
     83                                          "invalid_token_family_kind");
     84     }
     85 
     86     result = TALER_MHD_REPLY_JSON_PACK (
     87       connection,
     88       MHD_HTTP_OK,
     89       GNUNET_JSON_pack_string ("slug",
     90                                details.slug),
     91       GNUNET_JSON_pack_string ("name",
     92                                details.name),
     93       GNUNET_JSON_pack_string ("description",
     94                                details.description),
     95       GNUNET_JSON_pack_object_steal ("description_i18n",
     96                                      details.description_i18n),
     97       GNUNET_JSON_pack_allow_null (
     98         GNUNET_JSON_pack_object_steal ("extra_data",
     99                                        details.extra_data)),
    100       GNUNET_JSON_pack_timestamp ("valid_after",
    101                                   details.valid_after),
    102       GNUNET_JSON_pack_timestamp ("valid_before",
    103                                   details.valid_before),
    104       GNUNET_JSON_pack_time_rel ("duration",
    105                                  details.duration),
    106       GNUNET_JSON_pack_time_rel ("validity_granularity",
    107                                  details.validity_granularity),
    108       GNUNET_JSON_pack_time_rel ("start_offset",
    109                                  details.start_offset),
    110       GNUNET_JSON_pack_string ("kind",
    111                                kind),
    112       GNUNET_JSON_pack_int64 ("issued",
    113                               details.issued),
    114       GNUNET_JSON_pack_int64 ("used",
    115                               details.used)
    116       );
    117 
    118     GNUNET_free (details.name);
    119     GNUNET_free (details.description);
    120     GNUNET_free (details.cipher_spec);
    121     GNUNET_free (kind);
    122     return result;
    123   }
    124 }
    125 
    126 
    127 /* end of taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c */