LCOV - code coverage report
Current view: top level - lib - merchant_api_post_products.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 82 51.2 %
Date: 2025-06-23 16:22:09 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020-2024 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_post_products.c
      21             :  * @brief Implementation of the POST /products 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_curl_defaults.h"
      32             : #include "merchant_api_common.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 operation.
      39             :  */
      40             : struct TALER_MERCHANT_ProductsPostHandle
      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_ProductsPostCallback 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 request.
      79             :  *
      80             :  * @param cls the `struct TALER_MERCHANT_ProductsPostHandle`
      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          12 : handle_post_products_finished (void *cls,
      86             :                                long response_code,
      87             :                                const void *response)
      88             : {
      89          12 :   struct TALER_MERCHANT_ProductsPostHandle *pph = cls;
      90          12 :   const json_t *json = response;
      91          12 :   struct TALER_MERCHANT_HttpResponse hr = {
      92          12 :     .http_status = (unsigned int) response_code,
      93             :     .reply = json
      94             :   };
      95             : 
      96          12 :   pph->job = NULL;
      97          12 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      98             :               "POST /products completed with response code %u\n",
      99             :               (unsigned int) response_code);
     100          12 :   switch (response_code)
     101             :   {
     102           0 :   case 0:
     103           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     104           0 :     break;
     105          10 :   case MHD_HTTP_NO_CONTENT:
     106          10 :     break;
     107           0 :   case MHD_HTTP_BAD_REQUEST:
     108           0 :     hr.ec = TALER_JSON_get_error_code (json);
     109           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110             :     /* This should never happen, either us
     111             :      * or the merchant is buggy (or API version conflict);
     112             :      * just pass JSON reply to the application */
     113           0 :     break;
     114           0 :   case MHD_HTTP_UNAUTHORIZED:
     115           0 :     hr.ec = TALER_JSON_get_error_code (json);
     116           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     117             :     /* Nothing really to verify, merchant says we need to authenticate. */
     118           0 :     break;
     119           0 :   case MHD_HTTP_FORBIDDEN:
     120           0 :     hr.ec = TALER_JSON_get_error_code (json);
     121           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     122             :     /* Nothing really to verify, merchant says we tried to abort the payment
     123             :      * after it was successful. We should pass the JSON reply to the
     124             :      * application */
     125           0 :     break;
     126           0 :   case MHD_HTTP_NOT_FOUND:
     127           0 :     hr.ec = TALER_JSON_get_error_code (json);
     128           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     129             :     /* Nothing really to verify, this should never
     130             :        happen, we should pass the JSON reply to the
     131             :        application */
     132           0 :     break;
     133           2 :   case MHD_HTTP_CONFLICT:
     134           2 :     hr.ec = TALER_JSON_get_error_code (json);
     135           2 :     hr.hint = TALER_JSON_get_error_hint (json);
     136           2 :     break;
     137           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     138           0 :     hr.ec = TALER_JSON_get_error_code (json);
     139           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     140             :     /* Server had an internal issue; we should retry,
     141             :        but this API leaves this to the application */
     142           0 :     break;
     143           0 :   default:
     144           0 :     TALER_MERCHANT_parse_error_details_ (json,
     145             :                                          response_code,
     146             :                                          &hr);
     147             :     /* unexpected response code */
     148           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     149             :                 "Unexpected response code %u/%d\n",
     150             :                 (unsigned int) response_code,
     151             :                 (int) hr.ec);
     152           0 :     GNUNET_break_op (0);
     153           0 :     break;
     154             :   }
     155          12 :   pph->cb (pph->cb_cls,
     156             :            &hr);
     157          12 :   TALER_MERCHANT_products_post_cancel (pph);
     158          12 : }
     159             : 
     160             : 
     161             : struct TALER_MERCHANT_ProductsPostHandle *
     162          12 : TALER_MERCHANT_products_post3 (
     163             :   struct GNUNET_CURL_Context *ctx,
     164             :   const char *backend_url,
     165             :   const char *product_id,
     166             :   const char *description,
     167             :   const json_t *description_i18n,
     168             :   const char *unit,
     169             :   const struct TALER_Amount *price,
     170             :   const char *image,
     171             :   const json_t *taxes,
     172             :   int64_t total_stock,
     173             :   const json_t *address,
     174             :   struct GNUNET_TIME_Timestamp next_restock,
     175             :   uint32_t minimum_age,
     176             :   unsigned int num_cats,
     177             :   const uint64_t *cats,
     178             :   TALER_MERCHANT_ProductsPostCallback cb,
     179             :   void *cb_cls)
     180             : {
     181             :   struct TALER_MERCHANT_ProductsPostHandle *pph;
     182             :   json_t *req_obj;
     183             :   json_t *categories;
     184             : 
     185          12 :   if (0 == num_cats)
     186             :   {
     187          12 :     categories = NULL;
     188             :   }
     189             :   else
     190             :   {
     191           0 :     categories = json_array ();
     192           0 :     GNUNET_assert (NULL != categories);
     193           0 :     for (unsigned int i = 0; i<num_cats; i++)
     194           0 :       GNUNET_assert (0 ==
     195             :                      json_array_append_new (categories,
     196             :                                             json_integer (cats[i])));
     197             :   }
     198          12 :   req_obj = GNUNET_JSON_PACK (
     199             :     GNUNET_JSON_pack_string ("product_id",
     200             :                              product_id),
     201             :     GNUNET_JSON_pack_string ("description",
     202             :                              description),
     203             :     GNUNET_JSON_pack_allow_null (
     204             :       GNUNET_JSON_pack_object_incref ("description_i18n",
     205             :                                       (json_t *) description_i18n)),
     206             :     GNUNET_JSON_pack_allow_null (
     207             :       GNUNET_JSON_pack_array_steal ("categories",
     208             :                                     categories)),
     209             :     GNUNET_JSON_pack_string ("unit",
     210             :                              unit),
     211             :     TALER_JSON_pack_amount ("price",
     212             :                             price),
     213             :     GNUNET_JSON_pack_string ("image",
     214             :                              image),
     215             :     GNUNET_JSON_pack_allow_null (
     216             :       GNUNET_JSON_pack_array_incref ("taxes",
     217             :                                      (json_t *) taxes)),
     218             :     GNUNET_JSON_pack_uint64 ("total_stock",
     219             :                              total_stock),
     220             :     GNUNET_JSON_pack_allow_null (
     221             :       GNUNET_JSON_pack_uint64 ("minimum_age",
     222             :                                minimum_age)),
     223             :     GNUNET_JSON_pack_allow_null (
     224             :       GNUNET_JSON_pack_object_incref ("address",
     225             :                                       (json_t *) address)),
     226             :     GNUNET_JSON_pack_allow_null (
     227             :       GNUNET_JSON_pack_timestamp ("next_restock",
     228             :                                   next_restock)));
     229          12 :   pph = GNUNET_new (struct TALER_MERCHANT_ProductsPostHandle);
     230          12 :   pph->ctx = ctx;
     231          12 :   pph->cb = cb;
     232          12 :   pph->cb_cls = cb_cls;
     233          12 :   pph->url = TALER_url_join (backend_url,
     234             :                              "private/products",
     235             :                              NULL);
     236          12 :   if (NULL == pph->url)
     237             :   {
     238           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     239             :                 "Could not construct request URL.\n");
     240           0 :     json_decref (req_obj);
     241           0 :     GNUNET_free (pph);
     242           0 :     return NULL;
     243             :   }
     244             :   {
     245             :     CURL *eh;
     246             : 
     247          12 :     eh = TALER_MERCHANT_curl_easy_get_ (pph->url);
     248          12 :     GNUNET_assert (GNUNET_OK ==
     249             :                    TALER_curl_easy_post (&pph->post_ctx,
     250             :                                          eh,
     251             :                                          req_obj));
     252          12 :     json_decref (req_obj);
     253          24 :     pph->job = GNUNET_CURL_job_add2 (ctx,
     254             :                                      eh,
     255          12 :                                      pph->post_ctx.headers,
     256             :                                      &handle_post_products_finished,
     257             :                                      pph);
     258          12 :     GNUNET_assert (NULL != pph->job);
     259             :   }
     260          12 :   return pph;
     261             : }
     262             : 
     263             : 
     264             : struct TALER_MERCHANT_ProductsPostHandle *
     265          12 : TALER_MERCHANT_products_post2 (
     266             :   struct GNUNET_CURL_Context *ctx,
     267             :   const char *backend_url,
     268             :   const char *product_id,
     269             :   const char *description,
     270             :   const json_t *description_i18n,
     271             :   const char *unit,
     272             :   const struct TALER_Amount *price,
     273             :   const char *image,
     274             :   const json_t *taxes,
     275             :   int64_t total_stock,
     276             :   const json_t *address,
     277             :   struct GNUNET_TIME_Timestamp next_restock,
     278             :   uint32_t minimum_age,
     279             :   TALER_MERCHANT_ProductsPostCallback cb,
     280             :   void *cb_cls)
     281             : {
     282          12 :   return TALER_MERCHANT_products_post3 (ctx,
     283             :                                         backend_url,
     284             :                                         product_id,
     285             :                                         description,
     286             :                                         description_i18n,
     287             :                                         unit,
     288             :                                         price,
     289             :                                         image,
     290             :                                         taxes,
     291             :                                         total_stock,
     292             :                                         address,
     293             :                                         next_restock,
     294             :                                         minimum_age,
     295             :                                         0,
     296             :                                         NULL,
     297             :                                         cb,
     298             :                                         cb_cls);
     299             : }
     300             : 
     301             : 
     302             : struct TALER_MERCHANT_ProductsPostHandle *
     303           0 : TALER_MERCHANT_products_post (
     304             :   struct GNUNET_CURL_Context *ctx,
     305             :   const char *backend_url,
     306             :   const char *product_id,
     307             :   const char *description,
     308             :   const json_t *description_i18n,
     309             :   const char *unit,
     310             :   const struct TALER_Amount *price,
     311             :   const char *image,
     312             :   const json_t *taxes,
     313             :   int64_t total_stock,
     314             :   const json_t *address,
     315             :   struct GNUNET_TIME_Timestamp next_restock,
     316             :   TALER_MERCHANT_ProductsPostCallback cb,
     317             :   void *cb_cls)
     318             : {
     319           0 :   return TALER_MERCHANT_products_post2 (ctx,
     320             :                                         backend_url,
     321             :                                         product_id,
     322             :                                         description,
     323             :                                         description_i18n,
     324             :                                         unit,
     325             :                                         price,
     326             :                                         image,
     327             :                                         taxes,
     328             :                                         total_stock,
     329             :                                         address,
     330             :                                         next_restock,
     331             :                                         0,
     332             :                                         cb,
     333             :                                         cb_cls);
     334             : }
     335             : 
     336             : 
     337             : void
     338          12 : TALER_MERCHANT_products_post_cancel (
     339             :   struct TALER_MERCHANT_ProductsPostHandle *pph)
     340             : {
     341          12 :   if (NULL != pph->job)
     342             :   {
     343           0 :     GNUNET_CURL_job_cancel (pph->job);
     344           0 :     pph->job = NULL;
     345             :   }
     346          12 :   TALER_curl_easy_post_finished (&pph->post_ctx);
     347          12 :   GNUNET_free (pph->url);
     348          12 :   GNUNET_free (pph);
     349          12 : }
     350             : 
     351             : 
     352             : /* end of merchant_api_post_products.c */

Generated by: LCOV version 1.16