From f406f96129766c144c1531dc853969664f410d8c Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 3 Mar 2017 20:31:29 +0100 Subject: implement #4929: add wire transfer fee to /wire (but not yet charged by aggregator) --- src/exchange/taler-exchange-httpd_validation.c | 50 ++++++++++++++---- src/exchange/taler-exchange-httpd_wire.c | 73 +++++++++++++++++++++++++- src/exchange/taler-exchange-httpd_wire.h | 10 ++++ 3 files changed, 122 insertions(+), 11 deletions(-) (limited to 'src/exchange') diff --git a/src/exchange/taler-exchange-httpd_validation.c b/src/exchange/taler-exchange-httpd_validation.c index 14c1476b8..f8a1f7cba 100644 --- a/src/exchange/taler-exchange-httpd_validation.c +++ b/src/exchange/taler-exchange-httpd_validation.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2016 GNUnet e.V. + Copyright (C) 2016, 2017 GNUnet e.V. TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software @@ -23,6 +23,7 @@ #include #include "taler-exchange-httpd.h" #include "taler-exchange-httpd_validation.h" +#include "taler-exchange-httpd_wire.h" #include "taler_wire_lib.h" @@ -77,6 +78,7 @@ load_plugin (void *cls, { int *ret = cls; struct Plugin *p; + json_t *fees; p = GNUNET_new (struct Plugin); p->type = GNUNET_strdup (name); @@ -84,13 +86,26 @@ load_plugin (void *cls, name); if (NULL == p->plugin) { - GNUNET_free (p); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to load plugin %s\n", name); + GNUNET_free (p->type); + GNUNET_free (p); + *ret = GNUNET_SYSERR; + return; + } + fees = TEH_WIRE_get_fees (name); + if (NULL == fees) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Disabling method `%s' as wire transfer fees are not given correctly\n", + name); + GNUNET_free (p->type); + GNUNET_free (p); *ret = GNUNET_SYSERR; return; } + json_decref (fees); GNUNET_CONTAINER_DLL_insert (wire_head, wire_tail, p); @@ -114,9 +129,8 @@ TEH_VALIDATION_init (const struct GNUNET_CONFIGURATION_Handle *cfg) &ret); if (NULL == wire_head) { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "exchange", - "wireformat"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to find properly configured wire transfer method\n"); ret = GNUNET_SYSERR; } if (GNUNET_OK != ret) @@ -201,17 +215,17 @@ json_t * TEH_VALIDATION_get_wire_methods (const char *prefix) { json_t *methods; - json_t *method; - struct Plugin *p; - struct TALER_WIRE_Plugin *plugin; char *account_name; char *emsg; enum TALER_ErrorCode ec; methods = json_object (); - for (p=wire_head;NULL != p;p = p->next) + for (struct Plugin *p=wire_head;NULL != p;p = p->next) { - plugin = p->plugin; + struct TALER_WIRE_Plugin *plugin = p->plugin; + json_t *method; + json_t *fees; + GNUNET_asprintf (&account_name, "%s-%s", prefix, @@ -233,6 +247,22 @@ TEH_VALIDATION_get_wire_methods (const char *prefix) json_decref (method); method = NULL; } + fees = TEH_WIRE_get_fees (p->type); + if (NULL == fees) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Disabling method `%s' as wire transfer fees are not given correctly\n", + p->type); + json_decref (method); + method = NULL; + } + else + { + json_object_set_new (method, + "fees", + fees); + } + if (NULL != method) json_object_set_new (methods, p->type, diff --git a/src/exchange/taler-exchange-httpd_wire.c b/src/exchange/taler-exchange-httpd_wire.c index 7857a5bc1..9c8b2ff13 100644 --- a/src/exchange/taler-exchange-httpd_wire.c +++ b/src/exchange/taler-exchange-httpd_wire.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2015, 2016 GNUnet e.V. and INRIA + Copyright (C) 2015-2017 GNUnet e.V. and INRIA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software @@ -24,6 +24,7 @@ #include "taler-exchange-httpd_responses.h" #include "taler-exchange-httpd_validation.h" #include "taler-exchange-httpd_wire.h" +#include "taler_json_lib.h" #include /** @@ -32,6 +33,76 @@ static json_t *wire_methods; +/** + * Convert fee structure to JSON result to be returned + * as part of a /wire response. + * + * @param af fee structure to convert + * @return NULL on error, otherwise json data structure for /wire. + */ +static json_t * +fees_to_json (struct TALER_EXCHANGEDB_AggregateFees *af) +{ + json_t *a; + + a = json_array (); + while (NULL != af) + { + if ( (GNUNET_NO == GNUNET_TIME_round_abs (&af->start_date)) || + (GNUNET_NO == GNUNET_TIME_round_abs (&af->end_date)) ) + { + json_decref (a); + return NULL; + } + json_array_append_new (a, + json_pack ("{s:o, s:o, s:o, s:o}", + "wire_fee", TALER_JSON_from_amount (&af->wire_fee), + "start_date", GNUNET_JSON_from_time_abs (af->start_date), + "end_date", GNUNET_JSON_from_time_abs (af->end_date), + "sig", GNUNET_JSON_from_data_auto (&af->master_sig))); + af = af->next; + } + return a; +} + + +/** + * Obtain fee structure for @a wire_plugin_name wire transfers. + * + * @param wire_plugin_name name of the plugin to load fees for + * @return JSON object (to be freed by caller) with fee structure + */ +json_t * +TEH_WIRE_get_fees (const char *wire_plugin_name) +{ + struct TALER_EXCHANGEDB_AggregateFees *af; + json_t *j; + struct GNUNET_TIME_Absolute now; + + af = TALER_EXCHANGEDB_fees_read (cfg, + wire_plugin_name); + now = GNUNET_TIME_absolute_get (); + while ( (NULL != af) && + (af->end_date.abs_value_us < now.abs_value_us) ) + { + struct TALER_EXCHANGEDB_AggregateFees *n = af->next; + + GNUNET_free (af); + af = n; + } + if (NULL == af) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to find current wire transfer fees for `%s'\n", + wire_plugin_name); + return NULL; + } + j = fees_to_json (af); + TALER_EXCHANGEDB_fees_free (af); + return j; +} + + /** * Handle a "/wire" request. * diff --git a/src/exchange/taler-exchange-httpd_wire.h b/src/exchange/taler-exchange-httpd_wire.h index d67c16a82..a85fde696 100644 --- a/src/exchange/taler-exchange-httpd_wire.h +++ b/src/exchange/taler-exchange-httpd_wire.h @@ -26,6 +26,16 @@ #include "taler-exchange-httpd.h" +/** + * Obtain fee structure for @a wire_plugin_name wire transfers. + * + * @param wire_plugin_name name of the plugin to load fees for + * @return JSON object (to be freed by caller) with fee structure + */ +json_t * +TEH_WIRE_get_fees (const char *wire_plugin_name); + + /** * Handle a "/wire" request. * -- cgit v1.2.3