merchant

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

merchant_api_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c (7821B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023-2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Lesser General Public License as published by the Free Software
      7   Foundation; either version 2.1, 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 Lesser General Public License for more details.
     12 
     13   You should have received a copy of the GNU Lesser General Public License along with
     14   TALER; see the file COPYING.LGPL.  If not, see
     15   <http://www.gnu.org/licenses/>
     16 */
     17 /**
     18  * @file merchant_api_get-private-tokenfamilies-TOKEN_FAMILY_SLUG-new.c
     19  * @brief Implementation of the GET /private/tokenfamilies/$TOKEN_FAMILY_SLUG request
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/platform.h"
     23 #include <curl/curl.h>
     24 #include <jansson.h>
     25 #include <microhttpd.h> /* just for HTTP status codes */
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include \
     29   <taler/taler-merchant/get-private-tokenfamilies-TOKEN_FAMILY_SLUG.h>
     30 #include "merchant_api_curl_defaults.h"
     31 #include <taler/taler_json_lib.h>
     32 
     33 
     34 /**
     35  * Handle for a GET /private/tokenfamilies/$TOKEN_FAMILY_SLUG operation.
     36  */
     37 struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle
     38 {
     39   /**
     40    * Base URL of the merchant backend.
     41    */
     42   char *base_url;
     43 
     44   /**
     45    * The full URL for this request.
     46    */
     47   char *url;
     48 
     49   /**
     50    * Handle for the request.
     51    */
     52   struct GNUNET_CURL_Job *job;
     53 
     54   /**
     55    * Function to call with the result.
     56    */
     57   TALER_MERCHANT_GetPrivateTokenfamiliesCallback cb;
     58 
     59   /**
     60    * Closure for @a cb.
     61    */
     62   TALER_MERCHANT_GET_PRIVATE_TOKENFAMILIES_RESULT_CLOSURE *cb_cls;
     63 
     64   /**
     65    * Reference to the execution context.
     66    */
     67   struct GNUNET_CURL_Context *ctx;
     68 
     69   /**
     70    * Token family slug.
     71    */
     72   char *token_family_slug;
     73 };
     74 
     75 
     76 /**
     77  * Function called when we're done processing the
     78  * HTTP GET /private/tokenfamilies/$TOKEN_FAMILY_SLUG request.
     79  *
     80  * @param cls the `struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle`
     81  * @param response_code HTTP response code, 0 on error
     82  * @param response response body, NULL if not in JSON
     83  */
     84 static void
     85 handle_get_token_family_finished (void *cls,
     86                                   long response_code,
     87                                   const void *response)
     88 {
     89   struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle *gptf = cls;
     90   const json_t *json = response;
     91   struct TALER_MERCHANT_GetPrivateTokenfamiliesResponse tfgr = {
     92     .hr.http_status = (unsigned int) response_code,
     93     .hr.reply = json
     94   };
     95 
     96   gptf->job = NULL;
     97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     98               "Got /private/tokenfamilies/$SLUG response with status code %u\n",
     99               (unsigned int) response_code);
    100   switch (response_code)
    101   {
    102   case MHD_HTTP_OK:
    103     {
    104       struct GNUNET_JSON_Specification spec[] = {
    105         GNUNET_JSON_spec_string ("slug",
    106                                  &tfgr.details.ok.slug),
    107         GNUNET_JSON_spec_string ("name",
    108                                  &tfgr.details.ok.name),
    109         GNUNET_JSON_spec_string ("description",
    110                                  &tfgr.details.ok.description),
    111         GNUNET_JSON_spec_object_const ("description_i18n",
    112                                        &tfgr.details.ok.description_i18n),
    113         GNUNET_JSON_spec_mark_optional (
    114           GNUNET_JSON_spec_object_const ("extra_data",
    115                                          &tfgr.details.ok.extra_data),
    116           NULL),
    117         GNUNET_JSON_spec_timestamp ("valid_after",
    118                                     &tfgr.details.ok.valid_after),
    119         GNUNET_JSON_spec_timestamp ("valid_before",
    120                                     &tfgr.details.ok.valid_before),
    121         GNUNET_JSON_spec_relative_time ("duration",
    122                                         &tfgr.details.ok.duration),
    123         GNUNET_JSON_spec_relative_time ("validity_granularity",
    124                                         &tfgr.details.ok.validity_granularity),
    125         GNUNET_JSON_spec_relative_time ("start_offset",
    126                                         &tfgr.details.ok.start_offset),
    127         GNUNET_JSON_spec_string ("kind",
    128                                  &tfgr.details.ok.kind),
    129         GNUNET_JSON_spec_uint64 ("issued",
    130                                  &tfgr.details.ok.issued),
    131         GNUNET_JSON_spec_uint64 ("used",
    132                                  &tfgr.details.ok.used),
    133         GNUNET_JSON_spec_end ()
    134       };
    135 
    136       if (GNUNET_OK ==
    137           GNUNET_JSON_parse (json,
    138                              spec,
    139                              NULL, NULL))
    140       {
    141         gptf->cb (gptf->cb_cls,
    142                   &tfgr);
    143         GNUNET_JSON_parse_free (spec);
    144         TALER_MERCHANT_get_private_tokenfamilies_cancel (gptf);
    145         return;
    146       }
    147       tfgr.hr.http_status = 0;
    148       tfgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    149       break;
    150     }
    151   case MHD_HTTP_UNAUTHORIZED:
    152     tfgr.hr.ec = TALER_JSON_get_error_code (json);
    153     tfgr.hr.hint = TALER_JSON_get_error_hint (json);
    154     /* Nothing really to verify, merchant says we need to authenticate. */
    155     break;
    156   case MHD_HTTP_NOT_FOUND:
    157     tfgr.hr.ec = TALER_JSON_get_error_code (json);
    158     tfgr.hr.hint = TALER_JSON_get_error_hint (json);
    159     break;
    160   default:
    161     /* unexpected response code */
    162     tfgr.hr.ec = TALER_JSON_get_error_code (json);
    163     tfgr.hr.hint = TALER_JSON_get_error_hint (json);
    164     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    165                 "Unexpected response code %u/%d\n",
    166                 (unsigned int) response_code,
    167                 (int) tfgr.hr.ec);
    168     break;
    169   }
    170   gptf->cb (gptf->cb_cls,
    171             &tfgr);
    172   TALER_MERCHANT_get_private_tokenfamilies_cancel (gptf);
    173 }
    174 
    175 
    176 struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle *
    177 TALER_MERCHANT_get_private_tokenfamilies_create (
    178   struct GNUNET_CURL_Context *ctx,
    179   const char *url,
    180   const char *token_family_slug)
    181 {
    182   struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle *gptf;
    183 
    184   gptf = GNUNET_new (struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle);
    185   gptf->ctx = ctx;
    186   gptf->base_url = GNUNET_strdup (url);
    187   gptf->token_family_slug = GNUNET_strdup (token_family_slug);
    188   return gptf;
    189 }
    190 
    191 
    192 enum TALER_ErrorCode
    193 TALER_MERCHANT_get_private_tokenfamilies_start (
    194   struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle *gptf,
    195   TALER_MERCHANT_GetPrivateTokenfamiliesCallback cb,
    196   TALER_MERCHANT_GET_PRIVATE_TOKENFAMILIES_RESULT_CLOSURE *cb_cls)
    197 {
    198   CURL *eh;
    199 
    200   gptf->cb = cb;
    201   gptf->cb_cls = cb_cls;
    202   {
    203     char *path;
    204 
    205     GNUNET_asprintf (&path,
    206                      "private/tokenfamilies/%s",
    207                      gptf->token_family_slug);
    208     gptf->url = TALER_url_join (gptf->base_url,
    209                                 path,
    210                                 NULL);
    211     GNUNET_free (path);
    212   }
    213   if (NULL == gptf->url)
    214     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    215   eh = TALER_MERCHANT_curl_easy_get_ (gptf->url);
    216   if (NULL == eh)
    217     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    218   gptf->job = GNUNET_CURL_job_add (gptf->ctx,
    219                                    eh,
    220                                    &handle_get_token_family_finished,
    221                                    gptf);
    222   if (NULL == gptf->job)
    223     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    224   return TALER_EC_NONE;
    225 }
    226 
    227 
    228 void
    229 TALER_MERCHANT_get_private_tokenfamilies_cancel (
    230   struct TALER_MERCHANT_GetPrivateTokenfamiliesHandle *gptf)
    231 {
    232   if (NULL != gptf->job)
    233   {
    234     GNUNET_CURL_job_cancel (gptf->job);
    235     gptf->job = NULL;
    236   }
    237   GNUNET_free (gptf->url);
    238   GNUNET_free (gptf->base_url);
    239   GNUNET_free (gptf->token_family_slug);
    240   GNUNET_free (gptf);
    241 }
    242 
    243 
    244 /* end of merchant_api_get-private-tokenfamilies-TOKEN_FAMILY_SLUG-new.c */