diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-06-23 21:47:37 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-06-23 21:47:37 +0200 |
commit | d52356b4ac66bc22f3c7195490ff713668f340b1 (patch) | |
tree | 006658be31f180bbfa5b9396a0da9aa9dd23e5b4 /src/merchant-tools | |
parent | 8f52ed6000b129ee0c9366b7d66e0d8237828924 (diff) |
implement taler-merchant-setup-reserve CLI tool
Diffstat (limited to 'src/merchant-tools')
-rw-r--r-- | src/merchant-tools/Makefile.am | 35 | ||||
-rw-r--r-- | src/merchant-tools/taler-merchant-setup-reserve.c | 238 |
2 files changed, 260 insertions, 13 deletions
diff --git a/src/merchant-tools/Makefile.am b/src/merchant-tools/Makefile.am index a78da09a..987a0897 100644 --- a/src/merchant-tools/Makefile.am +++ b/src/merchant-tools/Makefile.am @@ -7,23 +7,12 @@ if USE_COVERAGE endif bin_PROGRAMS = \ + taler-merchant-benchmark \ taler-merchant-dbinit \ - taler-merchant-benchmark - -taler_merchant_dbinit_SOURCES = \ - taler-merchant-dbinit.c - -taler_merchant_dbinit_LDADD = \ - $(LIBGCRYPT_LIBS) \ - $(top_builddir)/src/backenddb/libtalermerchantdb.la \ - -lgnunetutil \ - -ltalerutil \ - -ltalerpq \ - $(XLIB) + taler-merchant-setup-reserve taler_merchant_benchmark_SOURCES = \ taler-merchant-benchmark.c - taler_merchant_benchmark_LDADD = \ $(top_srcdir)/src/backenddb/libtalermerchantdb.la \ $(top_srcdir)/src/lib/libtalermerchant.la \ @@ -40,3 +29,23 @@ taler_merchant_benchmark_LDADD = \ -lgnunetutil \ -ljansson \ $(XLIB) + +taler_merchant_dbinit_SOURCES = \ + taler-merchant-dbinit.c +taler_merchant_dbinit_LDADD = \ + $(LIBGCRYPT_LIBS) \ + $(top_builddir)/src/backenddb/libtalermerchantdb.la \ + -ltalerutil \ + -ltalerpq \ + -lgnunetutil \ + $(XLIB) + +taler_merchant_setup_reserve_SOURCES = \ + taler-merchant-setup-reserve.c +taler_merchant_setup_reserve_LDADD = \ + $(LIBGCRYPT_LIBS) \ + $(top_builddir)/src/lib/libtalermerchant.la \ + -ltalerutil \ + -lgnunetcurl \ + -lgnunetutil \ + $(XLIB) diff --git a/src/merchant-tools/taler-merchant-setup-reserve.c b/src/merchant-tools/taler-merchant-setup-reserve.c new file mode 100644 index 00000000..5d5f3935 --- /dev/null +++ b/src/merchant-tools/taler-merchant-setup-reserve.c @@ -0,0 +1,238 @@ +/* + This file is part of TALER + Copyright (C) 2020 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-setup-reserve.c + * @brief Create reserve for tipping + * @author Christian Grothoff + */ +#include "platform.h" +#include <taler/taler_util.h> +#include <microhttpd.h> +#include <gnunet/gnunet_util_lib.h> +#include "taler_merchant_service.h" + + +/** + * Return value from main(). + */ +static int global_ret; + +/** + * Initial amount the reserve will be filled with. + */ +static struct TALER_Amount initial_amount; + +/** + * Base URL of the merchant (with instance) to create the reserve for. + */ +static char *merchant_base_url; + +/** + * Base URL of the exchange to create the reserve at. + */ +static char *exchange_base_url; + +/** + * Wire method to use. + */ +static char *wire_method; + +/** + * Operation handle. + */ +static struct TALER_MERCHANT_PostReservesHandle *prh; + +/** + * Our context for making HTTP requests. + */ +static struct GNUNET_CURL_Context *ctx; + +/** + * Reschedule context for #SH_ctx. + */ +static struct GNUNET_CURL_RescheduleContext *rc; + + +/** + * Shutdown task (invoked when the process is being terminated) + * + * @param cls NULL + */ +static void +do_shutdown (void *cls) +{ + if (NULL != ctx) + { + GNUNET_CURL_fini (ctx); + ctx = NULL; + } + if (NULL != rc) + { + GNUNET_CURL_gnunet_rc_destroy (rc); + rc = NULL; + } + if (NULL != prh) + { + TALER_MERCHANT_reserves_post_cancel (prh); + prh = NULL; + } +} + + +/** + * Callbacks of this type are used to work the result of submitting a + * POST /reserves request to a merchant + * + * @param cls closure + * @param hr HTTP response details + * @param reserve_pub public key of the created reserve, NULL on error + * @param payto_uri where to make the payment to for filling the reserve, NULL on error + */ +static void +result_cb (void *cls, + const struct TALER_MERCHANT_HttpResponse *hr, + const struct TALER_ReservePublicKeyP *reserve_pub, + const char *payto_uri) +{ + (void) cls; + prh = NULL; + switch (hr->http_status) + { + case MHD_HTTP_OK: + { + char res_str[sizeof (*reserve_pub) * 2 + 1]; + + GNUNET_STRINGS_data_to_string (reserve_pub, + sizeof (*reserve_pub), + res_str, + sizeof (res_str)); + fprintf (stdout, + "%s&subject=%s\n", + payto_uri, + res_str); + } + break; + default: + fprintf (stderr, + "Unexpected backend failure: %u/%d\n", + hr->http_status, + (int) hr->ec); + global_ret = 1; + break; + } + GNUNET_SCHEDULER_shutdown (); +} + + +/** + * 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 config configuration + */ +static void +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *config) +{ + /* setup HTTP client event loop */ + ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, + &rc); + rc = GNUNET_CURL_gnunet_rc_create (ctx); + /* setup termination logic */ + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + NULL); + /* run actual (async) operation */ + prh = TALER_MERCHANT_reserves_post (ctx, + merchant_base_url, + &initial_amount, + exchange_base_url, + wire_method, + &result_cb, + NULL); + if (NULL == prh) + { + fprintf (stderr, + "Failed to begin operation!\n"); + global_ret = 2; + GNUNET_SCHEDULER_shutdown (); + return; + } +} + + +/** + * The main function for setting up reserves for tipping. + * + * @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", + "amount to be transferred into the reserve", + &initial_amount)), + GNUNET_GETOPT_option_mandatory ( + GNUNET_GETOPT_option_string ('e', + "exchange-url", + "URL", + "base URL of the exchange to create the reserve at", + &exchange_base_url)), + GNUNET_GETOPT_option_mandatory ( + GNUNET_GETOPT_option_string ('m', + "merchant-url", + "URL", + "base URL of the merchant backend's REST API", + &merchant_base_url)), + GNUNET_GETOPT_option_mandatory ( + GNUNET_GETOPT_option_string ('w', + "wire-method", + "METHOD", + "wire method to use for the wire transfer (i.e. IBAN)", + &wire_method)), + 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-setup-reserve", + "INFO", + NULL)); + if (GNUNET_OK != + GNUNET_PROGRAM_run (argc, argv, + "taler-merchant-setup-reserve", + "Setup reserve for tipping", + options, + &run, NULL)) + return 3; + return global_ret; +} + + +/* end of taler-merchant-setup-reserve.c */ |