diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-11-04 11:51:30 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-11-04 11:51:30 +0100 |
commit | 65d6a4851e9544fded2b776fb955900598744f96 (patch) | |
tree | 7919d1024347d49da0719bbd64ce7ab13f482356 /src | |
parent | dc03bcf42d48ba06cdc26fd8f31dae72324be387 (diff) | |
download | merchant-65d6a4851e9544fded2b776fb955900598744f96.tar.gz merchant-65d6a4851e9544fded2b776fb955900598744f96.tar.bz2 merchant-65d6a4851e9544fded2b776fb955900598744f96.zip |
add taler-merchant-tip-enable, including docs
Diffstat (limited to 'src')
-rw-r--r-- | src/merchant-tools/Makefile.am | 14 | ||||
-rw-r--r-- | src/merchant-tools/taler-merchant-tip-enable.c | 277 |
2 files changed, 289 insertions, 2 deletions
diff --git a/src/merchant-tools/Makefile.am b/src/merchant-tools/Makefile.am index fb950e1d..df271a2e 100644 --- a/src/merchant-tools/Makefile.am +++ b/src/merchant-tools/Makefile.am @@ -3,7 +3,8 @@ AM_CPPFLAGS = -I$(top_srcdir)/src/include bin_PROGRAMS = \ taler-merchant-dbinit \ - taler-merchant-generate-payments + taler-merchant-generate-payments \ + taler-merchant-tip-enable taler_merchant_dbinit_SOURCES = \ taler-merchant-dbinit.c @@ -15,6 +16,16 @@ taler_merchant_dbinit_LDADD = \ -ltalerutil \ -ltalerpq +taler_merchant_tip_enable_SOURCES = \ + taler-merchant-tip-enable.c + +taler_merchant_tip_enable_LDADD = \ + $(LIBGCRYPT_LIBS) \ + $(top_builddir)/src/lib/libtalermerchant.la \ + -lgnunetcurl \ + -lgnunetutil \ + -ltalerutil + taler_merchant_generate_payments_SOURCES = \ taler-merchant-generate-payments.c @@ -30,4 +41,3 @@ taler_merchant_generate_payments_LDADD = \ -lgnunetcurl \ -lgnunetutil \ -ljansson - diff --git a/src/merchant-tools/taler-merchant-tip-enable.c b/src/merchant-tools/taler-merchant-tip-enable.c new file mode 100644 index 00000000..776d10a5 --- /dev/null +++ b/src/merchant-tools/taler-merchant-tip-enable.c @@ -0,0 +1,277 @@ +/* + This file is part of TALER + Copyright (C) 2017 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file merchant-tools/taler-merchant-tip-enable.c + * @brief enable tips by telling the backend that a reserve was charged + * @author Christian Grothoff + */ +#include "platform.h" +#include <taler/taler_util.h> +#include <gnunet/gnunet_util_lib.h> +#include "taler_merchant_service.h" +#include <microhttpd.h> /* just for HTTP status code, no need to link against */ + +/** + * Return value from main(). + */ +static int global_ret; + +/** + * -a option: how much money was deposited into the reserve + */ +static struct TALER_Amount amount; + +/** + * For which instance did we charge the reserve? + */ +static char *instance; + +/** + * Under which URI does the backend run? + */ +static char *backend_uri; + +/** + * UUID of the wire transfer. + */ +static char *credit_uuid; + +/** + * Expiration time for the reserve. + */ +static struct GNUNET_TIME_Absolute expiration; + +/** + * Main execution context for the main loop of the exchange. + */ +static struct GNUNET_CURL_Context *ctx; + +/** + * Context for running the #ctx's event loop. + */ +static struct GNUNET_CURL_RescheduleContext *rc; + +/** + * Handle for the /tip-enable operation. + */ +static struct TALER_MERCHANT_TipEnableOperation *teo; + + +/** + * Function run when the test terminates (good or bad). + * Cleans up our state. + * + * @param cls the interpreter state. + */ +static void +do_shutdown (void *cls) +{ + if (NULL != teo) + { + TALER_MERCHANT_tip_enable_cancel (teo); + teo = NULL; + } + if (NULL != ctx) + { + GNUNET_CURL_fini (ctx); + ctx = NULL; + } + if (NULL != rc) + { + GNUNET_CURL_gnunet_rc_destroy (rc); + rc = NULL; + } +} + + +/** + * Callback for a /tip-enable request. Returns the result of + * the operation. + * + * @param cls closure + * @param http_status HTTP status returned by the merchant backend + * @param ec taler-specific error code + */ +static void +enable_cb (void *cls, + unsigned int http_status, + enum TALER_ErrorCode ec) +{ + teo = NULL; + GNUNET_SCHEDULER_shutdown (); + if ( (MHD_HTTP_OK == http_status) && + (TALER_EC_NONE == ec) ) + { + global_ret = 0; + return; + } + fprintf (stderr, + "Failed with HTTP status %u and error code %u\n", + http_status, + ec); + global_ret = 3; +} + + +/** + * Main function that will be run. + * + * @param cls closure + * @param args remaining command-line arguments + * @param cfgfile name of the configuration file used (for saving, can be NULL!) + * @param cfg configuration + */ +static void +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + struct TALER_ReservePrivateKeyP reserve_priv; + char *section; + char *res_str; + struct GNUNET_HashCode hcredit_uuid; + struct GNUNET_CURL_Context *ctx; + + GNUNET_asprintf (§ion, + "merchant-instance-%s", + instance); + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_filename (cfg, + section, + "TIP_RESERVE_PRIV", + &res_str)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, + section, + "TIP_RESERVE_PRIV"); + GNUNET_free (section); + global_ret = 1; + return; + } + if (GNUNET_OK != + GNUNET_STRINGS_string_to_data (res_str, + strlen (res_str), + &reserve_priv, + sizeof (struct TALER_ReservePrivateKeyP))) + { + GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, + section, + "TIP_RESERVE_PRIV", + "Must decode to private EdDSA key"); + GNUNET_free (section); + GNUNET_free (res_str); + global_ret = 1; + return; + } + GNUNET_free (res_str); + GNUNET_free (section); + + GNUNET_CRYPTO_hash (credit_uuid, + strlen (credit_uuid), + &hcredit_uuid); + + ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, + &rc); + if (NULL == ctx) + { + GNUNET_break (0); + global_ret = 1; + return; + } + rc = GNUNET_CURL_gnunet_rc_create (ctx); + teo = TALER_MERCHANT_tip_enable (ctx, + backend_uri, + &amount, + expiration, + &reserve_priv, + &hcredit_uuid, + &enable_cb, + NULL); + GNUNET_assert (NULL != teo); + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + NULL); +} + + +/** + * The main function of the database initialization tool. + * Used to initialize the Taler Exchange's database. + * + * @param argc number of arguments from the command line + * @param argv command line arguments + * @return 0 ok, 1 on error + */ +int +main (int argc, + char *const *argv) +{ + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_option_mandatory + (TALER_getopt_get_amount ('a', + "amount", + "VALUE", + "value that was added to the reserve", + &amount)), + GNUNET_GETOPT_option_mandatory + (GNUNET_GETOPT_option_string ('b', + "backend", + "URI", + "URI of the backend to use", + &backend_uri)), + GNUNET_GETOPT_option_mandatory + (GNUNET_GETOPT_option_string ('C', + "credit-uuid", + "UUID", + "unique identifier of the wire transfer (to detect duplicate invocations)", + &credit_uuid)), + GNUNET_GETOPT_option_mandatory + (GNUNET_GETOPT_option_absolute_time ('e', + "expiration", + "TIMESTAMP", + "when does the reserve expire", + &expiration)), + GNUNET_GETOPT_option_mandatory + (GNUNET_GETOPT_option_string ('i', + "instance", + "NAME", + "name of the instance of which the reserve was charged", + &instance)), + GNUNET_GETOPT_OPTION_END + }; + + /* force linker to link against libtalerutil; if we do + not do this, the linker may "optimize" libtalerutil + away and skip #TALER_OS_init(), which we do need */ + (void) TALER_project_data_default (); + GNUNET_assert (GNUNET_OK == + GNUNET_log_setup ("taler-merchant-tip-enable", + "INFO", + NULL)); + global_ret = 2; + if (GNUNET_OK != + GNUNET_PROGRAM_run (argc, argv, + "taler-merchant-tip-enable", + "Enable tipping by telling the backend that a reserve was charged", + options, + &run, + NULL)) + return 1; + return global_ret; +} + + +/* end of taler-exchange-tip-enable.c */ |