exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_post-management-wire-fee.c (7319B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-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 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
     15   <http://www.gnu.org/licenses/>
     16 */
     17 /**
     18  * @file lib/exchange_api_post-management-wire-fee.c
     19  * @brief functions to set wire fees at an exchange
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/taler_json_lib.h"
     23 #include <gnunet/gnunet_curl_lib.h>
     24 #include <microhttpd.h>
     25 #include "taler/exchange/post-management-wire-fee.h"
     26 #include "exchange_api_curl_defaults.h"
     27 #include "taler/taler_curl_lib.h"
     28 
     29 
     30 struct TALER_EXCHANGE_PostManagementWireFeesHandle
     31 {
     32 
     33   /**
     34    * The base URL for this request.
     35    */
     36   char *base_url;
     37 
     38   /**
     39    * The full URL for this request, set during _start.
     40    */
     41   char *url;
     42 
     43   /**
     44    * Minor context that holds body and headers.
     45    */
     46   struct TALER_CURL_PostContext post_ctx;
     47 
     48   /**
     49    * Handle for the request.
     50    */
     51   struct GNUNET_CURL_Job *job;
     52 
     53   /**
     54    * Function to call with the result.
     55    */
     56   TALER_EXCHANGE_PostManagementWireFeesCallback cb;
     57 
     58   /**
     59    * Closure for @a cb.
     60    */
     61   TALER_EXCHANGE_POST_MANAGEMENT_WIRE_FEES_RESULT_CLOSURE *cb_cls;
     62 
     63   /**
     64    * Reference to the execution context.
     65    */
     66   struct GNUNET_CURL_Context *ctx;
     67 
     68   /**
     69    * Wire method these fees are for.
     70    */
     71   char *wire_method;
     72 
     73   /**
     74    * Start of validity period.
     75    */
     76   struct GNUNET_TIME_Timestamp validity_start;
     77 
     78   /**
     79    * End of validity period.
     80    */
     81   struct GNUNET_TIME_Timestamp validity_end;
     82 
     83   /**
     84    * Wire fees for this time period.
     85    */
     86   struct TALER_WireFeeSet fees;
     87 
     88   /**
     89    * Signature affirming the wire fees.
     90    */
     91   struct TALER_MasterSignatureP master_sig;
     92 
     93 };
     94 
     95 
     96 /**
     97  * Function called when we're done processing the
     98  * HTTP POST /management/wire-fees request.
     99  *
    100  * @param cls the `struct TALER_EXCHANGE_PostManagementWireFeesHandle`
    101  * @param response_code HTTP response code, 0 on error
    102  * @param response response body, NULL if not in JSON
    103  */
    104 static void
    105 handle_wire_fees_finished (void *cls,
    106                            long response_code,
    107                            const void *response)
    108 {
    109   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh = cls;
    110   const json_t *json = response;
    111   struct TALER_EXCHANGE_PostManagementWireFeesResponse res = {
    112     .hr.http_status = (unsigned int) response_code,
    113     .hr.reply = json
    114   };
    115 
    116   pmwfh->job = NULL;
    117   switch (response_code)
    118   {
    119   case MHD_HTTP_NO_CONTENT:
    120     break;
    121   case MHD_HTTP_FORBIDDEN:
    122     res.hr.ec = TALER_JSON_get_error_code (json);
    123     res.hr.hint = TALER_JSON_get_error_hint (json);
    124     break;
    125   case MHD_HTTP_CONFLICT:
    126     res.hr.ec = TALER_JSON_get_error_code (json);
    127     res.hr.hint = TALER_JSON_get_error_hint (json);
    128     break;
    129   default:
    130     /* unexpected response code */
    131     GNUNET_break_op (0);
    132     res.hr.ec = TALER_JSON_get_error_code (json);
    133     res.hr.hint = TALER_JSON_get_error_hint (json);
    134     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    135                 "Unexpected response code %u/%d for exchange management set wire fee\n",
    136                 (unsigned int) response_code,
    137                 (int) res.hr.ec);
    138     break;
    139   }
    140   if (NULL != pmwfh->cb)
    141   {
    142     pmwfh->cb (pmwfh->cb_cls,
    143                &res);
    144     pmwfh->cb = NULL;
    145   }
    146   TALER_EXCHANGE_post_management_wire_fees_cancel (pmwfh);
    147 }
    148 
    149 
    150 struct TALER_EXCHANGE_PostManagementWireFeesHandle *
    151 TALER_EXCHANGE_post_management_wire_fees_create (
    152   struct GNUNET_CURL_Context *ctx,
    153   const char *exchange_base_url,
    154   const char *wire_method,
    155   struct GNUNET_TIME_Timestamp validity_start,
    156   struct GNUNET_TIME_Timestamp validity_end,
    157   const struct TALER_WireFeeSet *fees,
    158   const struct TALER_MasterSignatureP *master_sig)
    159 {
    160   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh;
    161 
    162   pmwfh = GNUNET_new (struct TALER_EXCHANGE_PostManagementWireFeesHandle);
    163   pmwfh->ctx = ctx;
    164   pmwfh->base_url = GNUNET_strdup (exchange_base_url);
    165   pmwfh->wire_method = GNUNET_strdup (wire_method);
    166   pmwfh->validity_start = validity_start;
    167   pmwfh->validity_end = validity_end;
    168   pmwfh->fees = *fees;
    169   pmwfh->master_sig = *master_sig;
    170   return pmwfh;
    171 }
    172 
    173 
    174 enum TALER_ErrorCode
    175 TALER_EXCHANGE_post_management_wire_fees_start (
    176   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh,
    177   TALER_EXCHANGE_PostManagementWireFeesCallback cb,
    178   TALER_EXCHANGE_POST_MANAGEMENT_WIRE_FEES_RESULT_CLOSURE *cb_cls)
    179 {
    180   CURL *eh;
    181   json_t *body;
    182 
    183   pmwfh->cb = cb;
    184   pmwfh->cb_cls = cb_cls;
    185   pmwfh->url = TALER_url_join (pmwfh->base_url,
    186                                "management/wire-fee",
    187                                NULL);
    188   if (NULL == pmwfh->url)
    189   {
    190     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    191                 "Could not construct request URL.\n");
    192     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    193   }
    194   body = GNUNET_JSON_PACK (
    195     GNUNET_JSON_pack_string ("wire_method",
    196                              pmwfh->wire_method),
    197     GNUNET_JSON_pack_data_auto ("master_sig",
    198                                 &pmwfh->master_sig),
    199     GNUNET_JSON_pack_timestamp ("fee_start",
    200                                 pmwfh->validity_start),
    201     GNUNET_JSON_pack_timestamp ("fee_end",
    202                                 pmwfh->validity_end),
    203     TALER_JSON_pack_amount ("closing_fee",
    204                             &pmwfh->fees.closing),
    205     TALER_JSON_pack_amount ("wire_fee",
    206                             &pmwfh->fees.wire));
    207   eh = TALER_EXCHANGE_curl_easy_get_ (pmwfh->url);
    208   if ( (NULL == eh) ||
    209        (GNUNET_OK !=
    210         TALER_curl_easy_post (&pmwfh->post_ctx,
    211                               eh,
    212                               body)) )
    213   {
    214     GNUNET_break (0);
    215     if (NULL != eh)
    216       curl_easy_cleanup (eh);
    217     json_decref (body);
    218     GNUNET_free (pmwfh->url);
    219     pmwfh->url = NULL;
    220     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    221   }
    222   json_decref (body);
    223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    224               "Requesting URL '%s'\n",
    225               pmwfh->url);
    226   pmwfh->job = GNUNET_CURL_job_add2 (pmwfh->ctx,
    227                                      eh,
    228                                      pmwfh->post_ctx.headers,
    229                                      &handle_wire_fees_finished,
    230                                      pmwfh);
    231   if (NULL == pmwfh->job)
    232   {
    233     TALER_curl_easy_post_finished (&pmwfh->post_ctx);
    234     GNUNET_free (pmwfh->url);
    235     pmwfh->url = NULL;
    236     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    237   }
    238   return TALER_EC_NONE;
    239 }
    240 
    241 
    242 void
    243 TALER_EXCHANGE_post_management_wire_fees_cancel (
    244   struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh)
    245 {
    246   if (NULL != pmwfh->job)
    247   {
    248     GNUNET_CURL_job_cancel (pmwfh->job);
    249     pmwfh->job = NULL;
    250   }
    251   TALER_curl_easy_post_finished (&pmwfh->post_ctx);
    252   GNUNET_free (pmwfh->wire_method);
    253   GNUNET_free (pmwfh->url);
    254   GNUNET_free (pmwfh->base_url);
    255   GNUNET_free (pmwfh);
    256 }
    257 
    258 
    259 /* end of exchange_api_post-management-wire-fee.c */