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 (4954B)


      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       TALER_MERCHANTDB_token_family_details_free (&details);
     81       return TALER_MHD_reply_with_error (connection,
     82                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     83                                          TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
     84                                          "invalid_token_family_kind");
     85     }
     86 
     87     result = TALER_MHD_REPLY_JSON_PACK (
     88       connection,
     89       MHD_HTTP_OK,
     90       GNUNET_JSON_pack_string ("slug",
     91                                details.slug),
     92       GNUNET_JSON_pack_string ("name",
     93                                details.name),
     94       GNUNET_JSON_pack_string ("description",
     95                                details.description),
     96       GNUNET_JSON_pack_object_steal ("description_i18n",
     97                                      details.description_i18n),
     98       GNUNET_JSON_pack_allow_null (
     99         GNUNET_JSON_pack_object_steal ("extra_data",
    100                                        details.extra_data)),
    101       GNUNET_JSON_pack_timestamp ("valid_after",
    102                                   details.valid_after),
    103       GNUNET_JSON_pack_timestamp ("valid_before",
    104                                   details.valid_before),
    105       GNUNET_JSON_pack_time_rel ("duration",
    106                                  details.duration),
    107       GNUNET_JSON_pack_time_rel ("validity_granularity",
    108                                  details.validity_granularity),
    109       GNUNET_JSON_pack_time_rel ("start_offset",
    110                                  details.start_offset),
    111       GNUNET_JSON_pack_string ("kind",
    112                                kind),
    113       GNUNET_JSON_pack_int64 ("issued",
    114                               details.issued),
    115       GNUNET_JSON_pack_int64 ("used",
    116                               details.used)
    117       );
    118 
    119     GNUNET_free (details.slug);
    120     GNUNET_free (details.name);
    121     GNUNET_free (details.description);
    122     GNUNET_free (details.cipher_spec);
    123     GNUNET_free (kind);
    124     return result;
    125   }
    126 }
    127 
    128 
    129 /* end of taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c */