LCOV - code coverage report
Current view: top level - lib - merchant_api_get_orders.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 85 127 66.9 %
Date: 2025-06-23 16:22:09 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2023 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file merchant_api_get_orders.c
      19             :  * @brief Implementation of the GET /orders request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : /**
      34             :  * Maximum number of orders we return.
      35             :  */
      36             : #define MAX_ORDERS 1024
      37             : 
      38             : /**
      39             :  * Handle for a GET /orders operation.
      40             :  */
      41             : struct TALER_MERCHANT_OrdersGetHandle
      42             : {
      43             :   /**
      44             :    * The url for this request.
      45             :    */
      46             :   char *url;
      47             : 
      48             :   /**
      49             :    * Handle for the request.
      50             :    */
      51             :   struct GNUNET_CURL_Job *job;
      52             : 
      53             :   /**
      54             :    * Function to call with the result.
      55             :    */
      56             :   TALER_MERCHANT_OrdersGetCallback cb;
      57             : 
      58             :   /**
      59             :    * Closure for @a cb.
      60             :    */
      61             :   void *cb_cls;
      62             : 
      63             :   /**
      64             :    * Reference to the execution context.
      65             :    */
      66             :   struct GNUNET_CURL_Context *ctx;
      67             : 
      68             : };
      69             : 
      70             : 
      71             : /**
      72             :  * Parse order information from @a ia.
      73             :  *
      74             :  * @param ia JSON array (or NULL!) with order data
      75             :  * @param[in] ogr response to fill
      76             :  * @param ogh operation handle
      77             :  * @return #GNUNET_OK on success
      78             :  */
      79             : static enum GNUNET_GenericReturnValue
      80           9 : parse_orders (const json_t *ia,
      81             :               struct TALER_MERCHANT_OrdersGetResponse *ogr,
      82             :               struct TALER_MERCHANT_OrdersGetHandle *ogh)
      83             : {
      84           9 :   unsigned int oes_len = (unsigned int) json_array_size (ia);
      85             : 
      86           9 :   if ( (json_array_size (ia) != (size_t)  oes_len) ||
      87             :        (oes_len > MAX_ORDERS) )
      88             :   {
      89           0 :     GNUNET_break (0);
      90           0 :     return GNUNET_SYSERR;
      91             :   }
      92           9 :   {
      93           9 :     struct TALER_MERCHANT_OrderEntry oes[GNUNET_NZL (oes_len)];
      94             :     size_t index;
      95             :     json_t *value;
      96             : 
      97          17 :     json_array_foreach (ia, index, value) {
      98           8 :       struct TALER_MERCHANT_OrderEntry *ie = &oes[index];
      99             :       struct GNUNET_JSON_Specification spec[] = {
     100           8 :         GNUNET_JSON_spec_string ("order_id",
     101             :                                  &ie->order_id),
     102           8 :         GNUNET_JSON_spec_timestamp ("timestamp",
     103             :                                     &ie->timestamp),
     104           8 :         GNUNET_JSON_spec_uint64 ("row_id",
     105             :                                  &ie->order_serial),
     106           8 :         TALER_JSON_spec_amount_any ("amount",
     107             :                                     &ie->amount),
     108           8 :         GNUNET_JSON_spec_string ("summary",
     109             :                                  &ie->summary),
     110           8 :         GNUNET_JSON_spec_bool ("refundable",
     111             :                                &ie->refundable),
     112           8 :         GNUNET_JSON_spec_bool ("paid",
     113             :                                &ie->paid),
     114           8 :         GNUNET_JSON_spec_end ()
     115             :       };
     116             : 
     117           8 :       if (GNUNET_OK !=
     118           8 :           GNUNET_JSON_parse (value,
     119             :                              spec,
     120             :                              NULL, NULL))
     121             :       {
     122           0 :         GNUNET_break_op (0);
     123           0 :         return GNUNET_SYSERR;
     124             :       }
     125             :     }
     126           9 :     ogr->details.ok.orders_length = oes_len;
     127           9 :     ogr->details.ok.orders = oes;
     128           9 :     ogh->cb (ogh->cb_cls,
     129             :              ogr);
     130           9 :     ogh->cb = NULL; /* just to be sure */
     131             :   }
     132           9 :   return GNUNET_OK;
     133             : }
     134             : 
     135             : 
     136             : /**
     137             :  * Function called when we're done processing the
     138             :  * HTTP /orders request.
     139             :  *
     140             :  * @param cls the `struct TALER_MERCHANT_OrdersGetHandle`
     141             :  * @param response_code HTTP response code, 0 on error
     142             :  * @param response response body, NULL if not in JSON
     143             :  */
     144             : static void
     145           9 : handle_get_orders_finished (void *cls,
     146             :                             long response_code,
     147             :                             const void *response)
     148             : {
     149           9 :   struct TALER_MERCHANT_OrdersGetHandle *ogh = cls;
     150           9 :   const json_t *json = response;
     151           9 :   struct TALER_MERCHANT_OrdersGetResponse ogr = {
     152           9 :     .hr.http_status = (unsigned int) response_code,
     153             :     .hr.reply = json
     154             :   };
     155             : 
     156           9 :   ogh->job = NULL;
     157           9 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     158             :               "Got /orders response with status code %u\n",
     159             :               (unsigned int) response_code);
     160           9 :   switch (response_code)
     161             :   {
     162           9 :   case MHD_HTTP_OK:
     163             :     {
     164             :       const json_t *orders;
     165             :       struct GNUNET_JSON_Specification spec[] = {
     166           9 :         GNUNET_JSON_spec_array_const ("orders",
     167             :                                       &orders),
     168           9 :         GNUNET_JSON_spec_end ()
     169             :       };
     170             : 
     171           9 :       if (GNUNET_OK !=
     172           9 :           GNUNET_JSON_parse (json,
     173             :                              spec,
     174             :                              NULL, NULL))
     175             :       {
     176           0 :         ogr.hr.http_status = 0;
     177           0 :         ogr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     178           0 :         break;
     179             :       }
     180           9 :       if (GNUNET_OK ==
     181           9 :           parse_orders (orders,
     182             :                         &ogr,
     183             :                         ogh))
     184             :       {
     185           9 :         TALER_MERCHANT_orders_get_cancel (ogh);
     186           9 :         return;
     187             :       }
     188           0 :       ogr.hr.http_status = 0;
     189           0 :       ogr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     190           0 :       break;
     191             :     }
     192           0 :   case MHD_HTTP_UNAUTHORIZED:
     193           0 :     ogr.hr.ec = TALER_JSON_get_error_code (json);
     194           0 :     ogr.hr.hint = TALER_JSON_get_error_hint (json);
     195             :     /* Nothing really to verify, merchant says we need to authenticate. */
     196           0 :     break;
     197           0 :   case MHD_HTTP_NOT_FOUND:
     198           0 :     ogr.hr.ec = TALER_JSON_get_error_code (json);
     199           0 :     ogr.hr.hint = TALER_JSON_get_error_hint (json);
     200           0 :     break;
     201           0 :   default:
     202             :     /* unexpected response code */
     203           0 :     ogr.hr.ec = TALER_JSON_get_error_code (json);
     204           0 :     ogr.hr.hint = TALER_JSON_get_error_hint (json);
     205           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     206             :                 "Unexpected response code %u/%d\n",
     207             :                 (unsigned int) response_code,
     208             :                 (int) ogr.hr.ec);
     209           0 :     break;
     210             :   }
     211           0 :   ogh->cb (ogh->cb_cls,
     212             :            &ogr);
     213           0 :   TALER_MERCHANT_orders_get_cancel (ogh);
     214             : }
     215             : 
     216             : 
     217             : struct TALER_MERCHANT_OrdersGetHandle *
     218           7 : TALER_MERCHANT_orders_get (
     219             :   struct GNUNET_CURL_Context *ctx,
     220             :   const char *backend_url,
     221             :   TALER_MERCHANT_OrdersGetCallback cb,
     222             :   void *cb_cls)
     223             : {
     224          14 :   return TALER_MERCHANT_orders_get2 (ctx,
     225             :                                      backend_url,
     226             :                                      TALER_EXCHANGE_YNA_ALL,
     227             :                                      TALER_EXCHANGE_YNA_ALL,
     228             :                                      TALER_EXCHANGE_YNA_ALL,
     229           7 :                                      GNUNET_TIME_UNIT_FOREVER_TS,
     230             :                                      UINT64_MAX,
     231             :                                      -20, /* default is most recent 20 entries */
     232           7 :                                      GNUNET_TIME_UNIT_ZERO,
     233             :                                      cb,
     234             :                                      cb_cls);
     235             : }
     236             : 
     237             : 
     238             : struct TALER_MERCHANT_OrdersGetHandle *
     239           9 : TALER_MERCHANT_orders_get2 (
     240             :   struct GNUNET_CURL_Context *ctx,
     241             :   const char *backend_url,
     242             :   enum TALER_EXCHANGE_YesNoAll paid,
     243             :   enum TALER_EXCHANGE_YesNoAll refunded,
     244             :   enum TALER_EXCHANGE_YesNoAll wired,
     245             :   struct GNUNET_TIME_Timestamp date,
     246             :   uint64_t start_row,
     247             :   int64_t delta,
     248             :   struct GNUNET_TIME_Relative timeout,
     249             :   TALER_MERCHANT_OrdersGetCallback cb,
     250             :   void *cb_cls)
     251             : {
     252           9 :   return TALER_MERCHANT_orders_get3 (
     253             :     ctx,
     254             :     backend_url,
     255             :     paid,
     256             :     refunded,
     257             :     wired,
     258             :     NULL,
     259             :     NULL,
     260             :     date,
     261             :     start_row,
     262             :     delta,
     263             :     timeout,
     264             :     cb,
     265             :     cb_cls);
     266             : }
     267             : 
     268             : 
     269             : struct TALER_MERCHANT_OrdersGetHandle *
     270           9 : TALER_MERCHANT_orders_get3 (
     271             :   struct GNUNET_CURL_Context *ctx,
     272             :   const char *backend_url,
     273             :   enum TALER_EXCHANGE_YesNoAll paid,
     274             :   enum TALER_EXCHANGE_YesNoAll refunded,
     275             :   enum TALER_EXCHANGE_YesNoAll wired,
     276             :   const char *session_id,
     277             :   const char *fulfillment_url,
     278             :   struct GNUNET_TIME_Timestamp date,
     279             :   uint64_t start_row,
     280             :   int64_t delta,
     281             :   struct GNUNET_TIME_Relative timeout,
     282             :   TALER_MERCHANT_OrdersGetCallback cb,
     283             :   void *cb_cls)
     284             : {
     285             :   struct TALER_MERCHANT_OrdersGetHandle *ogh;
     286             :   CURL *eh;
     287          18 :   unsigned int tms = timeout.rel_value_us
     288           9 :                      / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
     289             : 
     290           9 :   GNUNET_assert (NULL != backend_url);
     291           9 :   if ( (delta > MAX_ORDERS) ||
     292             :        (delta < -MAX_ORDERS) )
     293             :   {
     294           0 :     GNUNET_break (0);
     295           0 :     return NULL;
     296             :   }
     297           9 :   if (0 == delta)
     298             :   {
     299           0 :     GNUNET_break (0);
     300           0 :     return NULL;
     301             :   }
     302           9 :   ogh = GNUNET_new (struct TALER_MERCHANT_OrdersGetHandle);
     303           9 :   ogh->ctx = ctx;
     304           9 :   ogh->cb = cb;
     305           9 :   ogh->cb_cls = cb_cls;
     306             : 
     307             :   /* build ogh->url with the various optional arguments */
     308             :   {
     309             :     char dstr[30];
     310           9 :     char *fec = NULL;
     311           9 :     char *sid = NULL;
     312             :     bool have_date;
     313             :     bool have_srow;
     314             :     char cbuf[30];
     315             :     char dbuf[30];
     316             :     char tbuf[30];
     317             : 
     318           9 :     GNUNET_snprintf (tbuf,
     319             :                      sizeof (tbuf),
     320             :                      "%u",
     321             :                      tms);
     322           9 :     GNUNET_snprintf (dbuf,
     323             :                      sizeof (dbuf),
     324             :                      "%lld",
     325             :                      (long long) delta);
     326           9 :     GNUNET_snprintf (cbuf,
     327             :                      sizeof (cbuf),
     328             :                      "%llu",
     329             :                      (unsigned long long) start_row);
     330           9 :     if (NULL != session_id)
     331           0 :       (void) GNUNET_STRINGS_urlencode (strlen (session_id),
     332             :                                        session_id,
     333             :                                        &sid);
     334           9 :     if (NULL != fulfillment_url)
     335           0 :       (void) GNUNET_STRINGS_urlencode (strlen (fulfillment_url),
     336             :                                        fulfillment_url,
     337             :                                        &fec);
     338           9 :     GNUNET_snprintf (dstr,
     339             :                      sizeof (dstr),
     340             :                      "%llu",
     341           9 :                      (unsigned long long) GNUNET_TIME_timestamp_to_s (date));
     342           9 :     if (delta > 0)
     343             :     {
     344           2 :       have_date = ! GNUNET_TIME_absolute_is_zero (date.abs_time);
     345           2 :       have_srow = (0 != start_row);
     346             :     }
     347             :     else
     348             :     {
     349           7 :       have_date = ! GNUNET_TIME_absolute_is_never (date.abs_time);
     350           7 :       have_srow = (UINT64_MAX != start_row);
     351             :     }
     352           9 :     ogh->url = TALER_url_join (backend_url,
     353             :                                "private/orders",
     354             :                                "paid",
     355             :                                (TALER_EXCHANGE_YNA_ALL != paid)
     356           0 :                                ? TALER_yna_to_string (paid)
     357             :                                : NULL,
     358             :                                "refunded",
     359             :                                (TALER_EXCHANGE_YNA_ALL != refunded)
     360           0 :                                ? TALER_yna_to_string (refunded)
     361             :                                : NULL,
     362             :                                "wired",
     363             :                                (TALER_EXCHANGE_YNA_ALL != wired)
     364           0 :                                ? TALER_yna_to_string (wired)
     365             :                                : NULL,
     366             :                                "date_s",
     367             :                                (have_date)
     368             :                                ? dstr
     369             :                                : NULL,
     370             :                                "start",
     371             :                                (have_srow)
     372             :                                ? cbuf
     373             :                                : NULL,
     374             :                                "delta",
     375             :                                (-20 != delta)
     376             :                                ? dbuf
     377             :                                : NULL,
     378             :                                "timeout_ms",
     379             :                                (0 != tms)
     380             :                                ? tbuf
     381             :                                : NULL,
     382             :                                "session_id",
     383             :                                sid,
     384             :                                "fulfillment_url",
     385             :                                fec,
     386             :                                NULL);
     387           9 :     GNUNET_free (sid);
     388           9 :     GNUNET_free (fec);
     389             :   }
     390           9 :   if (NULL == ogh->url)
     391             :   {
     392           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     393             :                 "Could not construct request URL.\n");
     394           0 :     GNUNET_free (ogh);
     395           0 :     return NULL;
     396             :   }
     397           9 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     398             :               "Requesting URL '%s'\n",
     399             :               ogh->url);
     400           9 :   eh = TALER_MERCHANT_curl_easy_get_ (ogh->url);
     401           9 :   if (NULL == eh)
     402             :   {
     403           0 :     GNUNET_break (0);
     404           0 :     GNUNET_free (ogh->url);
     405           0 :     GNUNET_free (ogh);
     406           0 :     return NULL;
     407             :   }
     408           9 :   if (0 != tms)
     409             :   {
     410           2 :     GNUNET_break (CURLE_OK ==
     411             :                   curl_easy_setopt (eh,
     412             :                                     CURLOPT_TIMEOUT_MS,
     413             :                                     (long) (tms + 100L)));
     414             :   }
     415           9 :   ogh->job = GNUNET_CURL_job_add (ctx,
     416             :                                   eh,
     417             :                                   &handle_get_orders_finished,
     418             :                                   ogh);
     419           9 :   return ogh;
     420             : }
     421             : 
     422             : 
     423             : void
     424           9 : TALER_MERCHANT_orders_get_cancel (
     425             :   struct TALER_MERCHANT_OrdersGetHandle *ogh)
     426             : {
     427           9 :   if (NULL != ogh->job)
     428           0 :     GNUNET_CURL_job_cancel (ogh->job);
     429           9 :   GNUNET_free (ogh->url);
     430           9 :   GNUNET_free (ogh);
     431           9 : }

Generated by: LCOV version 1.16