diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-12-07 17:32:36 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-12-07 17:32:36 +0100 |
commit | effa994fcf5dac8fb998ba352854d72b20340acb (patch) | |
tree | 2136c4b74343eb54a203c915b0e825434aaee892 /src/lib | |
parent | 230c0202e3dc1879e8e9272bac1bab1dc496c620 (diff) | |
download | merchant-effa994fcf5dac8fb998ba352854d72b20340acb.tar.gz merchant-effa994fcf5dac8fb998ba352854d72b20340acb.tar.bz2 merchant-effa994fcf5dac8fb998ba352854d72b20340acb.zip |
clean up refund API
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/merchant_api_refund.c | 259 | ||||
-rw-r--r-- | src/lib/merchant_api_refund_increase.c | 206 | ||||
-rw-r--r-- | src/lib/testing_api_cmd_refund_increase.c | 12 |
4 files changed, 245 insertions, 233 deletions
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 939959c3..def34387 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -25,6 +25,7 @@ libtalermerchant_la_SOURCES = \ merchant_api_pay.c \ merchant_api_poll_payment.c \ merchant_api_refund.c \ + merchant_api_refund_increase.c \ merchant_api_tip_authorize.c \ merchant_api_tip_pickup.c \ merchant_api_tip_query.c \ diff --git a/src/lib/merchant_api_refund.c b/src/lib/merchant_api_refund.c index c48896d7..9d8187f7 100644 --- a/src/lib/merchant_api_refund.c +++ b/src/lib/merchant_api_refund.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2014, 2015, 2016, 2017 GNUnet e.V. and INRIA + Copyright (C) 2014, 2015, 2016, 2017, 2019 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software @@ -15,12 +15,11 @@ <http://www.gnu.org/licenses/> */ /** - * @file lib/merchant_api_proposal.c + * @file lib/merchant_api_refund.c * @brief Implementation of the /refund POST and GET * @author Christian Grothoff * @author Marcello Stanisci */ - #include "platform.h" #include <curl/curl.h> #include <jansson.h> @@ -62,195 +61,6 @@ struct TALER_MERCHANT_RefundLookupOperation }; -struct TALER_MERCHANT_RefundIncreaseOperation -{ - /** - * Complete URL where the backend offers /refund - */ - char *url; - - /** - * Minor context that holds body and headers. - */ - struct TEAH_PostContext post_ctx; - - /** - * The CURL context to connect to the backend - */ - struct GNUNET_CURL_Context *ctx; - - /** - * The callback to pass the backend response to - */ - TALER_MERCHANT_RefundIncreaseCallback cb; - - /** - * Clasure to pass to the callback - */ - void *cb_cls; - - /** - * Handle for the request - */ - struct GNUNET_CURL_Job *job; -}; - - -/** - * Callback to process POST /refund response - * - * @param cls the `struct TALER_MERCHANT_RefundIncreaseOperation` - * @param response_code HTTP response code, 0 on error - * @param json response body, NULL if not JSON - */ -static void -handle_refund_increase_finished (void *cls, - long response_code, - const void *response) -{ - struct TALER_MERCHANT_RefundIncreaseOperation *rio = cls; - char *error; - char *hint; - enum TALER_ErrorCode code; - const json_t *json = response; - - rio->job = NULL; - switch (response_code) - { - case 0: - /* Hard error */ - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Backend didn't even return from POST /refund\n"); - return; - - /* Good to hand everything to the callback, as all - * the logic is actually there. */ - case MHD_HTTP_OK: - case MHD_HTTP_BAD_REQUEST: - case MHD_HTTP_NOT_FOUND: - rio->cb (rio->cb_cls, - response_code, - TALER_JSON_get_error_code (json), - json); - break; - default: - /** - * The backend gave response, but it's error, log it. - * NOTE that json must be a Taler-specific error object (FIXME, - * need a link to error objects at docs) - */if (-1 == json_unpack - ((json_t *) json, - "{s:s, s:I, s:s}", - "error", &error, - "code", &code, - "hint", &hint)) - - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "/refund failed (HTTP code: %lu), backend did " - "not give a valid error object\n", response_code); - break; - } - - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "/refund, error: %s, code: %d, hint: %s\n", - error, - code, - hint); - } - TALER_MERCHANT_refund_increase_cancel (rio); -} - - -/** - * Cancel a POST /refund request. - * - * @param rio the refund increasing operation to cancel - */ -void -TALER_MERCHANT_refund_increase_cancel (struct - TALER_MERCHANT_RefundIncreaseOperation * - rio) -{ - if (NULL != rio->job) - { - GNUNET_CURL_job_cancel (rio->job); - rio->job = NULL; - } - TALER_curl_easy_post_finished (&rio->post_ctx); - GNUNET_free (rio->url); - GNUNET_free (rio); -} - - -/** - * Increase the refund associated to a order - * - * @param ctx the CURL context used to connect to the backend - * @param backend_url backend's base URL, including final "/" - * @param order_id id of the order whose refund is to be increased - * @param refund amount to which increase the refund - * @param reason human-readable reason justifying the refund - * @param cb callback processing the response from /refund - * @param cb_cls closure for cb - */ -struct TALER_MERCHANT_RefundIncreaseOperation * -TALER_MERCHANT_refund_increase (struct GNUNET_CURL_Context *ctx, - const char *backend_url, - const char *order_id, - const struct TALER_Amount *refund, - const char *reason, - TALER_MERCHANT_RefundIncreaseCallback cb, - void *cb_cls) -{ - struct TALER_MERCHANT_RefundIncreaseOperation *rio; - json_t *req; - CURL *eh; - - rio = GNUNET_new (struct TALER_MERCHANT_RefundIncreaseOperation); - rio->ctx = ctx; - rio->cb = cb; - rio->cb_cls = cb_cls; - rio->url = TALER_url_join (backend_url, "refund", NULL); - if (NULL == rio->url) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Could not construct request URL.\n"); - GNUNET_free (rio); - return NULL; - } - - req = json_pack ("{s:o, s:s, s:s}", - "refund", TALER_JSON_from_amount (refund), - "order_id", order_id, - "reason", reason); - eh = curl_easy_init (); - - if (GNUNET_OK != TALER_curl_easy_post (&rio->post_ctx, - eh, - req)) - { - GNUNET_break (0); - json_decref (req); - GNUNET_free (rio->url); - GNUNET_free (rio); - return NULL; - } - json_decref (req); - - GNUNET_assert (CURLE_OK == curl_easy_setopt (eh, - CURLOPT_URL, - rio->url)); - rio->job = GNUNET_CURL_job_add2 - (ctx, - eh, - rio->post_ctx.headers, - &handle_refund_increase_finished, - rio); - - return rio; -} - /** * Cancel a /refund lookup operation @@ -273,6 +83,10 @@ TALER_MERCHANT_refund_lookup_cancel (struct /** * Process GET /refund response + * + * @param cls a `struct TALER_MERCHANT_RefundLookupOperation *` + * @param response_code HTTP status, 0 for HTTP failure + * @param response a `const json_t *` with the JSON of the HTTP body */ static void handle_refund_lookup_finished (void *cls, @@ -280,19 +94,19 @@ handle_refund_lookup_finished (void *cls, const void *response) { struct TALER_MERCHANT_RefundLookupOperation *rlo = cls; - char *error; - enum TALER_ErrorCode code; const json_t *json = response; rlo->job = NULL; switch (response_code) { case 0: - /* Hard error */ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Backend didn't even return from GET /refund\n"); - return; - + rlo->cb (rlo->cb_cls, + 0, + TALER_EC_INVALID_RESPONSE, + NULL); + break; case MHD_HTTP_OK: case MHD_HTTP_NOT_FOUND: rlo->cb (rlo->cb_cls, @@ -301,27 +115,13 @@ handle_refund_lookup_finished (void *cls, json); break; default: - /** - * The backend gave response, but it's error, log it. - * NOTE that json must be a Taler-specific error object (FIXME, - * need a link to error objects at docs) - */if (-1 == json_unpack ((json_t *) json, - "{s:s, s:I, s:s}", - "error", &error, - "code", &code)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed GET /refund, error: %s, code: %d\n", - error, - code); - break; - } - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed /refund lookup, backend did not give" - " a valid error object, HTTP code was %lu\n", - response_code); + GNUNET_break_op (0); /* unexpected status code */ + rlo->cb (rlo->cb_cls, + response_code, + TALER_JSON_get_error_code (json), + json); + break; } - TALER_MERCHANT_refund_lookup_cancel (rlo); } @@ -350,27 +150,32 @@ TALER_MERCHANT_refund_lookup (struct GNUNET_CURL_Context *ctx, rlo->ctx = ctx; rlo->cb = cb; rlo->cb_cls = cb_cls; - - rlo->url = TALER_url_join (backend_url, "refund", "order_id", order_id, NULL); + rlo->url = TALER_url_join (backend_url, + "refund", + "order_id", + order_id, + NULL); eh = curl_easy_init (); if (CURLE_OK != curl_easy_setopt (eh, CURLOPT_URL, rlo->url)) { GNUNET_break (0); + GNUNET_free (rlo->url); + GNUNET_free (rlo); return NULL; } - - if (NULL == (rlo->job = GNUNET_CURL_job_add (ctx, - eh, - GNUNET_NO, - handle_refund_lookup_finished, - rlo))) + rlo->job = GNUNET_CURL_job_add (ctx, + eh, + GNUNET_NO, + handle_refund_lookup_finished, + rlo); + if (NULL == rlo->job) { + GNUNET_free (rlo->url); + GNUNET_free (rlo); GNUNET_break (0); return NULL; - } - return rlo; } diff --git a/src/lib/merchant_api_refund_increase.c b/src/lib/merchant_api_refund_increase.c new file mode 100644 index 00000000..9cf5124c --- /dev/null +++ b/src/lib/merchant_api_refund_increase.c @@ -0,0 +1,206 @@ +/* + This file is part of TALER + Copyright (C) 2014, 2015, 2016, 2017, 2019 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file lib/merchant_api_refund.c + * @brief Implementation of the /refund POST and GET + * @author Christian Grothoff + * @author Marcello Stanisci + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> +#include <taler/taler_curl_lib.h> + + +struct TALER_MERCHANT_RefundIncreaseOperation +{ + /** + * Complete URL where the backend offers /refund + */ + char *url; + + /** + * Minor context that holds body and headers. + */ + struct TEAH_PostContext post_ctx; + + /** + * The CURL context to connect to the backend + */ + struct GNUNET_CURL_Context *ctx; + + /** + * The callback to pass the backend response to + */ + TALER_MERCHANT_RefundIncreaseCallback cb; + + /** + * Clasure to pass to the callback + */ + void *cb_cls; + + /** + * Handle for the request + */ + struct GNUNET_CURL_Job *job; +}; + + +/** + * Callback to process POST /refund response + * + * @param cls the `struct TALER_MERCHANT_RefundIncreaseOperation` + * @param response_code HTTP response code, 0 on error + * @param json response body, NULL if not JSON + */ +static void +handle_refund_increase_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_RefundIncreaseOperation *rio = cls; + const json_t *json = response; + + rio->job = NULL; + switch (response_code) + { + case 0: + rio->cb (rio->cb_cls, + 0, + TALER_EC_INVALID_RESPONSE, + NULL); + break; + case MHD_HTTP_OK: + case MHD_HTTP_BAD_REQUEST: + case MHD_HTTP_NOT_FOUND: + rio->cb (rio->cb_cls, + response_code, + TALER_JSON_get_error_code (json), + json); + break; + default: + GNUNET_break_op (0); /* unexpected status code */ + rio->cb (rio->cb_cls, + response_code, + TALER_JSON_get_error_code (json), + json); + break; + } + TALER_MERCHANT_refund_increase_cancel (rio); +} + + +/** + * Cancel a POST /refund request. + * + * @param rio the refund increasing operation to cancel + */ +void +TALER_MERCHANT_refund_increase_cancel (struct + TALER_MERCHANT_RefundIncreaseOperation * + rio) +{ + if (NULL != rio->job) + { + GNUNET_CURL_job_cancel (rio->job); + rio->job = NULL; + } + TALER_curl_easy_post_finished (&rio->post_ctx); + GNUNET_free (rio->url); + GNUNET_free (rio); +} + + +/** + * Increase the refund associated to a order + * + * @param ctx the CURL context used to connect to the backend + * @param backend_url backend's base URL, including final "/" + * @param order_id id of the order whose refund is to be increased + * @param refund amount to which increase the refund + * @param reason human-readable reason justifying the refund + * @param cb callback processing the response from /refund + * @param cb_cls closure for cb + */ +struct TALER_MERCHANT_RefundIncreaseOperation * +TALER_MERCHANT_refund_increase (struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *order_id, + const struct TALER_Amount *refund, + const char *reason, + TALER_MERCHANT_RefundIncreaseCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_RefundIncreaseOperation *rio; + json_t *req; + CURL *eh; + + rio = GNUNET_new (struct TALER_MERCHANT_RefundIncreaseOperation); + rio->ctx = ctx; + rio->cb = cb; + rio->cb_cls = cb_cls; + rio->url = TALER_url_join (backend_url, + "refund", + NULL); + if (NULL == rio->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (rio); + return NULL; + } + + req = json_pack ("{s:o, s:s, s:s}", + "refund", TALER_JSON_from_amount (refund), + "order_id", order_id, + "reason", reason); + eh = curl_easy_init (); + + if (GNUNET_OK != TALER_curl_easy_post (&rio->post_ctx, + eh, + req)) + { + GNUNET_break (0); + json_decref (req); + GNUNET_free (rio->url); + GNUNET_free (rio); + return NULL; + } + json_decref (req); + GNUNET_assert (CURLE_OK == curl_easy_setopt (eh, + CURLOPT_URL, + rio->url)); + rio->job = GNUNET_CURL_job_add2 + (ctx, + eh, + rio->post_ctx.headers, + &handle_refund_increase_finished, + rio); + if (NULL == rio->job) + { + GNUNET_free (rio->url); + GNUNET_free (rio); + return NULL; + } + return rio; +} diff --git a/src/lib/testing_api_cmd_refund_increase.c b/src/lib/testing_api_cmd_refund_increase.c index 18527450..57f661b5 100644 --- a/src/lib/testing_api_cmd_refund_increase.c +++ b/src/lib/testing_api_cmd_refund_increase.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2014-2018 Taler Systems SA + Copyright (C) 2014-2019 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 @@ -16,13 +16,12 @@ License along with TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ - /** * @file lib/testing_api_cmd_refund_increase.c * @brief command to test refunds. * @author Marcello Stanisci + * @author Christian Grothoff */ - #include "platform.h" #include <taler/taler_exchange_service.h> #include <taler/taler_testing_lib.h> @@ -120,7 +119,6 @@ refund_increase_cb (void *cls, ris->rio = NULL; if (ris->http_code != http_status) TALER_TESTING_FAIL (ris->is); - TALER_TESTING_interpreter_next (ris->is); } @@ -151,7 +149,8 @@ refund_increase_run (void *cls, ris->reason, &refund_increase_cb, ris); - GNUNET_assert (NULL != ris->rio); + if (NULL == ris->rio) + TALER_TESTING_FAIL (is); } @@ -173,7 +172,8 @@ refund_increase_traits (void *cls, { struct RefundIncreaseState *ris = cls; struct TALER_TESTING_Trait traits[] = { - TALER_TESTING_make_trait_amount (0, ris->refund_amount), + TALER_TESTING_make_trait_amount (0, + ris->refund_amount), TALER_TESTING_trait_end () }; |