gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] branch master updated: clean up refund API


From: gnunet
Subject: [taler-merchant] branch master updated: clean up refund API
Date: Sat, 07 Dec 2019 17:32:39 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository merchant.

The following commit(s) were added to refs/heads/master by this push:
     new effa994  clean up refund API
effa994 is described below

commit effa994fcf5dac8fb998ba352854d72b20340acb
Author: Christian Grothoff <address@hidden>
AuthorDate: Sat Dec 7 17:32:36 2019 +0100

    clean up refund API
---
 src/lib/Makefile.am                                |   1 +
 src/lib/merchant_api_refund.c                      | 259 +++------------------
 ...api_refund.c => merchant_api_refund_increase.c} | 210 ++---------------
 src/lib/testing_api_cmd_refund_increase.c          |  12 +-
 4 files changed, 59 insertions(+), 423 deletions(-)

diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 939959c..def3438 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 c48896d..9d8187f 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.c 
b/src/lib/merchant_api_refund_increase.c
similarity index 52%
copy from src/lib/merchant_api_refund.c
copy to src/lib/merchant_api_refund_increase.c
index c48896d..9cf5124 100644
--- a/src/lib/merchant_api_refund.c
+++ b/src/lib/merchant_api_refund_increase.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>
@@ -33,35 +32,6 @@
 #include <taler/taler_curl_lib.h>
 
 
-struct TALER_MERCHANT_RefundLookupOperation
-{
-  /**
-   * URL of the request, includes parameters
-   */
-  char *url;
-
-  /**
-   * Handle of the request
-   */
-  struct GNUNET_CURL_Job *job;
-
-  /**
-   * Function to call with the response
-   */
-  TALER_MERCHANT_RefundLookupCallback cb;
-
-  /**
-   * Closure for cb
-   */
-  void *cb_cls;
-
-  /**
-   * Reference to the execution context
-   */
-  struct GNUNET_CURL_Context *ctx;
-
-};
-
 struct TALER_MERCHANT_RefundIncreaseOperation
 {
   /**
@@ -109,22 +79,17 @@ handle_refund_increase_finished (void *cls,
                                  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.  */
+    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:
@@ -134,29 +99,12 @@ handle_refund_increase_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,
-          "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);
+    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);
 }
@@ -211,7 +159,9 @@ TALER_MERCHANT_refund_increase (struct GNUNET_CURL_Context 
*ctx,
   rio->ctx = ctx;
   rio->cb = cb;
   rio->cb_cls = cb_cls;
-  rio->url = TALER_url_join (backend_url, "refund", NULL);
+  rio->url = TALER_url_join (backend_url,
+                             "refund",
+                             NULL);
   if (NULL == rio->url)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -237,7 +187,6 @@ TALER_MERCHANT_refund_increase (struct GNUNET_CURL_Context 
*ctx,
     return NULL;
   }
   json_decref (req);
-
   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
                                                CURLOPT_URL,
                                                rio->url));
@@ -247,130 +196,11 @@ TALER_MERCHANT_refund_increase (struct 
GNUNET_CURL_Context *ctx,
                rio->post_ctx.headers,
                &handle_refund_increase_finished,
                rio);
-
-  return rio;
-}
-
-
-/**
- * Cancel a /refund lookup operation
- *
- * @param
- */
-void
-TALER_MERCHANT_refund_lookup_cancel (struct
-                                     TALER_MERCHANT_RefundLookupOperation *rlo)
-{
-  if (NULL != rlo->job)
-  {
-    GNUNET_CURL_job_cancel (rlo->job);
-    rlo->job = NULL;
-  }
-  GNUNET_free (rlo->url);
-  GNUNET_free (rlo);
-}
-
-
-/**
- * Process GET /refund response
- */
-static void
-handle_refund_lookup_finished (void *cls,
-                               long response_code,
-                               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)
+  if (NULL == rio->job)
   {
-  case 0:
-    /* Hard error */
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Backend didn't even return from GET /refund\n");
-    return;
-
-  case MHD_HTTP_OK:
-  case MHD_HTTP_NOT_FOUND:
-    rlo->cb (rlo->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))
-    {
-      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);
-  }
-
-  TALER_MERCHANT_refund_lookup_cancel (rlo);
-}
-
-
-/**
- * Does a GET /refund.
- *
- * @param ctx execution context
- * @param backend_url base URL of the merchant backend
- * @param order_id order id used to perform the lookup
- * @param cb callback which will work the response gotten from the backend
- * @param cb_cls closure to pass to the callback
- * @return handle for this operation, NULL upon errors
- */
-struct TALER_MERCHANT_RefundLookupOperation *
-TALER_MERCHANT_refund_lookup (struct GNUNET_CURL_Context *ctx,
-                              const char *backend_url,
-                              const char *order_id,
-                              TALER_MERCHANT_RefundLookupCallback cb,
-                              void *cb_cls)
-{
-  struct TALER_MERCHANT_RefundLookupOperation *rlo;
-  CURL *eh;
-
-  rlo = GNUNET_new (struct TALER_MERCHANT_RefundLookupOperation);
-  rlo->ctx = ctx;
-  rlo->cb = cb;
-  rlo->cb_cls = cb_cls;
-
-  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);
-    return NULL;
-  }
-
-  if (NULL == (rlo->job = GNUNET_CURL_job_add (ctx,
-                                               eh,
-                                               GNUNET_NO,
-                                               handle_refund_lookup_finished,
-                                               rlo)))
-  {
-    GNUNET_break (0);
+    GNUNET_free (rio->url);
+    GNUNET_free (rio);
     return NULL;
-
   }
-
-  return rlo;
+  return rio;
 }
diff --git a/src/lib/testing_api_cmd_refund_increase.c 
b/src/lib/testing_api_cmd_refund_increase.c
index 1852745..57f661b 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 ()
   };
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]