LCOV - code coverage report
Current view: top level - lib - merchant_api_lock_product.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 81 54.3 %
Date: 2025-06-23 16:22:09 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Lesser General Public License as
       7             :   published by the Free Software Foundation; either version 2.1,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful,
      11             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU Lesser General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU Lesser General
      16             :   Public License along with TALER; see the file COPYING.LGPL.
      17             :   If not, see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file merchant_api_lock_product.c
      21             :  * @brief Implementation of the POST /products/$ID/lock request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include <curl/curl.h>
      27             : #include <jansson.h>
      28             : #include <microhttpd.h> /* just for HTTP status codes */
      29             : #include <gnunet/gnunet_util_lib.h>
      30             : #include "taler_merchant_service.h"
      31             : #include "merchant_api_common.h"
      32             : #include "merchant_api_curl_defaults.h"
      33             : #include <taler/taler_json_lib.h>
      34             : #include <taler/taler_curl_lib.h>
      35             : 
      36             : 
      37             : /**
      38             :  * Handle for a POST /products/$ID/lock operation.
      39             :  */
      40             : struct TALER_MERCHANT_ProductLockHandle
      41             : {
      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_ProductLockCallback 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             :    * Minor context that holds body and headers.
      70             :    */
      71             :   struct TALER_CURL_PostContext post_ctx;
      72             : 
      73             : };
      74             : 
      75             : 
      76             : /**
      77             :  * Function called when we're done processing the
      78             :  * HTTP POST /products/$ID/lock request.
      79             :  *
      80             :  * @param cls the `struct TALER_MERCHANT_ProductLockHandle`
      81             :  * @param response_code HTTP response code, 0 on error
      82             :  * @param response response body, NULL if not in JSON
      83             :  */
      84             : static void
      85           8 : handle_lock_product_finished (void *cls,
      86             :                               long response_code,
      87             :                               const void *response)
      88             : {
      89           8 :   struct TALER_MERCHANT_ProductLockHandle *plh = cls;
      90           8 :   const json_t *json = response;
      91           8 :   struct TALER_MERCHANT_HttpResponse hr = {
      92           8 :     .http_status = (unsigned int) response_code,
      93             :     .reply = json
      94             :   };
      95             : 
      96           8 :   plh->job = NULL;
      97           8 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      98             :               "LOCK /products/$ID completed with response code %u\n",
      99             :               (unsigned int) response_code);
     100           8 :   switch (response_code)
     101             :   {
     102           0 :   case 0:
     103           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     104           0 :     break;
     105           4 :   case MHD_HTTP_NO_CONTENT:
     106           4 :     break;
     107           0 :   case MHD_HTTP_UNAUTHORIZED:
     108           0 :     hr.ec = TALER_JSON_get_error_code (json);
     109           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110             :     /* Nothing really to verify, merchant says we need to authenticate. */
     111           0 :     break;
     112           0 :   case MHD_HTTP_BAD_REQUEST:
     113           0 :     hr.ec = TALER_JSON_get_error_code (json);
     114           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     115           0 :     GNUNET_break_op (0);
     116             :     /* This should never happen, either us
     117             :      * or the merchant is buggy (or API version conflict);
     118             :      * just pass JSON reply to the application */
     119           0 :     break;
     120           0 :   case MHD_HTTP_FORBIDDEN:
     121           0 :     hr.ec = TALER_JSON_get_error_code (json);
     122           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     123             :     /* Nothing really to verify, merchant says we tried to abort the payment
     124             :      * after it was successful. We should pass the JSON reply to the
     125             :      * application */
     126           0 :     break;
     127           2 :   case MHD_HTTP_NOT_FOUND:
     128           2 :     hr.ec = TALER_JSON_get_error_code (json);
     129           2 :     hr.hint = TALER_JSON_get_error_hint (json);
     130           2 :     break;
     131           2 :   case MHD_HTTP_GONE:
     132           2 :     hr.ec = TALER_JSON_get_error_code (json);
     133           2 :     hr.hint = TALER_JSON_get_error_hint (json);
     134           2 :     break;
     135           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     136           0 :     hr.ec = TALER_JSON_get_error_code (json);
     137           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     138             :     /* Server had an internal issue; we should retry,
     139             :        but this API leaves this to the application */
     140           0 :     break;
     141           0 :   default:
     142           0 :     TALER_MERCHANT_parse_error_details_ (json,
     143             :                                          response_code,
     144             :                                          &hr);
     145             :     /* unexpected response code */
     146           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147             :                 "Unexpected response code %u/%d\n",
     148             :                 (unsigned int) response_code,
     149             :                 (int) hr.ec);
     150           0 :     GNUNET_break_op (0);
     151           0 :     break;
     152             :   }
     153           8 :   plh->cb (plh->cb_cls,
     154             :            &hr);
     155           8 :   TALER_MERCHANT_product_lock_cancel (plh);
     156           8 : }
     157             : 
     158             : 
     159             : struct TALER_MERCHANT_ProductLockHandle *
     160           8 : TALER_MERCHANT_product_lock (
     161             :   struct GNUNET_CURL_Context *ctx,
     162             :   const char *backend_url,
     163             :   const char *product_id,
     164             :   const char *uuid,
     165             :   struct GNUNET_TIME_Relative duration,
     166             :   uint32_t quantity,
     167             :   TALER_MERCHANT_ProductLockCallback cb,
     168             :   void *cb_cls)
     169             : {
     170             :   struct TALER_MERCHANT_ProductLockHandle *plh;
     171             :   json_t *req_obj;
     172             : 
     173           8 :   req_obj = GNUNET_JSON_PACK (
     174             :     GNUNET_JSON_pack_string ("lock_uuid",
     175             :                              uuid),
     176             :     GNUNET_JSON_pack_time_rel ("duration",
     177             :                                duration),
     178             :     GNUNET_JSON_pack_uint64 ("quantity",
     179             :                              quantity));
     180           8 :   plh = GNUNET_new (struct TALER_MERCHANT_ProductLockHandle);
     181           8 :   plh->ctx = ctx;
     182           8 :   plh->cb = cb;
     183           8 :   plh->cb_cls = cb_cls;
     184             :   {
     185             :     char *path;
     186             : 
     187           8 :     GNUNET_asprintf (&path,
     188             :                      "private/products/%s/lock",
     189             :                      product_id);
     190           8 :     plh->url = TALER_url_join (backend_url,
     191             :                                path,
     192             :                                NULL);
     193           8 :     GNUNET_free (path);
     194             :   }
     195           8 :   if (NULL == plh->url)
     196             :   {
     197           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                 "Could not construct request URL.\n");
     199           0 :     json_decref (req_obj);
     200           0 :     GNUNET_free (plh);
     201           0 :     return NULL;
     202             :   }
     203             :   {
     204             :     CURL *eh;
     205             : 
     206           8 :     eh = TALER_MERCHANT_curl_easy_get_ (plh->url);
     207           8 :     if (GNUNET_OK !=
     208           8 :         TALER_curl_easy_post (&plh->post_ctx,
     209             :                               eh,
     210             :                               req_obj))
     211             :     {
     212           0 :       GNUNET_break (0);
     213           0 :       curl_easy_cleanup (eh);
     214           0 :       json_decref (req_obj);
     215           0 :       GNUNET_free (plh->url);
     216           0 :       GNUNET_free (plh);
     217           0 :       return NULL;
     218             :     }
     219           8 :     json_decref (req_obj);
     220          16 :     plh->job = GNUNET_CURL_job_add2 (ctx,
     221             :                                      eh,
     222           8 :                                      plh->post_ctx.headers,
     223             :                                      &handle_lock_product_finished,
     224             :                                      plh);
     225             :   }
     226           8 :   return plh;
     227             : }
     228             : 
     229             : 
     230             : void
     231           8 : TALER_MERCHANT_product_lock_cancel (
     232             :   struct TALER_MERCHANT_ProductLockHandle *plh)
     233             : {
     234           8 :   if (NULL != plh->job)
     235             :   {
     236           0 :     GNUNET_CURL_job_cancel (plh->job);
     237           0 :     plh->job = NULL;
     238             :   }
     239           8 :   TALER_curl_easy_post_finished (&plh->post_ctx);
     240           8 :   GNUNET_free (plh->url);
     241           8 :   GNUNET_free (plh);
     242           8 : }
     243             : 
     244             : 
     245             : /* end of merchant_api_lock_product.c */

Generated by: LCOV version 1.16