exchange

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

exchange_api_management_add_partner.c (6149B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023 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_management_add_partner.c
     19  * @brief functions to add an partner by an AML officer
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/platform.h"
     23 #include "taler/taler_json_lib.h"
     24 #include <gnunet/gnunet_curl_lib.h>
     25 #include <microhttpd.h>
     26 #include "taler/taler_exchange_service.h"
     27 #include "exchange_api_curl_defaults.h"
     28 #include "taler/taler_signatures.h"
     29 #include "taler/taler_curl_lib.h"
     30 #include "taler/taler_json_lib.h"
     31 
     32 
     33 struct TALER_EXCHANGE_ManagementAddPartner
     34 {
     35 
     36   /**
     37    * The url for this request.
     38    */
     39   char *url;
     40 
     41   /**
     42    * Minor context that holds body and headers.
     43    */
     44   struct TALER_CURL_PostContext post_ctx;
     45 
     46   /**
     47    * Handle for the request.
     48    */
     49   struct GNUNET_CURL_Job *job;
     50 
     51   /**
     52    * Function to call with the result.
     53    */
     54   TALER_EXCHANGE_ManagementAddPartnerCallback cb;
     55 
     56   /**
     57    * Closure for @a cb.
     58    */
     59   void *cb_cls;
     60 
     61   /**
     62    * Reference to the execution context.
     63    */
     64   struct GNUNET_CURL_Context *ctx;
     65 };
     66 
     67 
     68 /**
     69  * Function called when we're done processing the
     70  * HTTP POST /management/partners request.
     71  *
     72  * @param cls the `struct TALER_EXCHANGE_ManagementAddPartner *`
     73  * @param response_code HTTP response code, 0 on error
     74  * @param response response body, NULL if not in JSON
     75  */
     76 static void
     77 handle_add_partner_finished (void *cls,
     78                              long response_code,
     79                              const void *response)
     80 {
     81   struct TALER_EXCHANGE_ManagementAddPartner *wh = cls;
     82   const json_t *json = response;
     83   struct TALER_EXCHANGE_ManagementAddPartnerResponse apr = {
     84     .hr.http_status = (unsigned int) response_code,
     85     .hr.reply = json
     86   };
     87 
     88   wh->job = NULL;
     89   switch (response_code)
     90   {
     91   case 0:
     92     /* no reply */
     93     apr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     94     apr.hr.hint = "server offline?";
     95     break;
     96   case MHD_HTTP_NO_CONTENT:
     97     break;
     98   case MHD_HTTP_FORBIDDEN:
     99     apr.hr.ec = TALER_JSON_get_error_code (json);
    100     apr.hr.hint = TALER_JSON_get_error_hint (json);
    101     break;
    102   case MHD_HTTP_CONFLICT:
    103     apr.hr.ec = TALER_JSON_get_error_code (json);
    104     apr.hr.hint = TALER_JSON_get_error_hint (json);
    105     break;
    106   default:
    107     /* unexpected response code */
    108     GNUNET_break_op (0);
    109     apr.hr.ec = TALER_JSON_get_error_code (json);
    110     apr.hr.hint = TALER_JSON_get_error_hint (json);
    111     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    112                 "Unexpected response code %u/%d for adding exchange partner\n",
    113                 (unsigned int) response_code,
    114                 (int) apr.hr.ec);
    115     break;
    116   }
    117   if (NULL != wh->cb)
    118   {
    119     wh->cb (wh->cb_cls,
    120             &apr);
    121     wh->cb = NULL;
    122   }
    123   TALER_EXCHANGE_management_add_partner_cancel (wh);
    124 }
    125 
    126 
    127 struct TALER_EXCHANGE_ManagementAddPartner *
    128 TALER_EXCHANGE_management_add_partner (
    129   struct GNUNET_CURL_Context *ctx,
    130   const char *url,
    131   const struct TALER_MasterPublicKeyP *partner_pub,
    132   struct GNUNET_TIME_Timestamp start_date,
    133   struct GNUNET_TIME_Timestamp end_date,
    134   struct GNUNET_TIME_Relative wad_frequency,
    135   const struct TALER_Amount *wad_fee,
    136   const char *partner_base_url,
    137   const struct TALER_MasterSignatureP *master_sig,
    138   TALER_EXCHANGE_ManagementAddPartnerCallback cb,
    139   void *cb_cls)
    140 {
    141   struct TALER_EXCHANGE_ManagementAddPartner *wh;
    142   CURL *eh;
    143   json_t *body;
    144 
    145   wh = GNUNET_new (struct TALER_EXCHANGE_ManagementAddPartner);
    146   wh->cb = cb;
    147   wh->cb_cls = cb_cls;
    148   wh->ctx = ctx;
    149   wh->url = TALER_url_join (url,
    150                             "management/partners",
    151                             NULL);
    152   if (NULL == wh->url)
    153   {
    154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    155                 "Could not construct request URL.\n");
    156     GNUNET_free (wh);
    157     return NULL;
    158   }
    159   body = GNUNET_JSON_PACK (
    160     GNUNET_JSON_pack_string ("partner_base_url",
    161                              partner_base_url),
    162     GNUNET_JSON_pack_timestamp ("start_date",
    163                                 start_date),
    164     GNUNET_JSON_pack_timestamp ("end_date",
    165                                 end_date),
    166     GNUNET_JSON_pack_time_rel ("wad_frequency",
    167                                wad_frequency),
    168     GNUNET_JSON_pack_data_auto ("partner_pub",
    169                                 &partner_pub),
    170     GNUNET_JSON_pack_data_auto ("master_sig",
    171                                 &master_sig),
    172     TALER_JSON_pack_amount ("wad_fee",
    173                             wad_fee)
    174     );
    175   eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
    176   if ( (NULL == eh) ||
    177        (GNUNET_OK !=
    178         TALER_curl_easy_post (&wh->post_ctx,
    179                               eh,
    180                               body)) )
    181   {
    182     GNUNET_break (0);
    183     if (NULL != eh)
    184       curl_easy_cleanup (eh);
    185     json_decref (body);
    186     GNUNET_free (wh->url);
    187     return NULL;
    188   }
    189   json_decref (body);
    190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    191               "Requesting URL '%s'\n",
    192               wh->url);
    193   wh->job = GNUNET_CURL_job_add2 (ctx,
    194                                   eh,
    195                                   wh->post_ctx.headers,
    196                                   &handle_add_partner_finished,
    197                                   wh);
    198   if (NULL == wh->job)
    199   {
    200     TALER_EXCHANGE_management_add_partner_cancel (wh);
    201     return NULL;
    202   }
    203   return wh;
    204 }
    205 
    206 
    207 void
    208 TALER_EXCHANGE_management_add_partner_cancel (
    209   struct TALER_EXCHANGE_ManagementAddPartner *wh)
    210 {
    211   if (NULL != wh->job)
    212   {
    213     GNUNET_CURL_job_cancel (wh->job);
    214     wh->job = NULL;
    215   }
    216   TALER_curl_easy_post_finished (&wh->post_ctx);
    217   GNUNET_free (wh->url);
    218   GNUNET_free (wh);
    219 }