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

            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              :     /* FIXME: once we move to the new-style API,
     202              :        allow applications to set the product name properly! */
     203              :     GNUNET_JSON_pack_string ("product_name",
     204              :                              description),
     205              :     GNUNET_JSON_pack_string ("description",
     206              :                              description),
     207              :     GNUNET_JSON_pack_allow_null (
     208              :       GNUNET_JSON_pack_object_incref ("description_i18n",
     209              :                                       (json_t *) description_i18n)),
     210              :     GNUNET_JSON_pack_allow_null (
     211              :       GNUNET_JSON_pack_array_steal ("categories",
     212              :                                     categories)),
     213              :     GNUNET_JSON_pack_string ("unit",
     214              :                              unit),
     215              :     TALER_JSON_pack_amount ("price",
     216              :                             price),
     217              :     GNUNET_JSON_pack_string ("image",
     218              :                              image),
     219              :     GNUNET_JSON_pack_allow_null (
     220              :       GNUNET_JSON_pack_array_incref ("taxes",
     221              :                                      (json_t *) taxes)),
     222              :     GNUNET_JSON_pack_uint64 ("total_stock",
     223              :                              total_stock),
     224              :     GNUNET_JSON_pack_allow_null (
     225              :       GNUNET_JSON_pack_uint64 ("minimum_age",
     226              :                                minimum_age)),
     227              :     GNUNET_JSON_pack_allow_null (
     228              :       GNUNET_JSON_pack_object_incref ("address",
     229              :                                       (json_t *) address)),
     230              :     GNUNET_JSON_pack_allow_null (
     231              :       GNUNET_JSON_pack_timestamp ("next_restock",
     232              :                                   next_restock)));
     233           12 :   pph = GNUNET_new (struct TALER_MERCHANT_ProductsPostHandle);
     234           12 :   pph->ctx = ctx;
     235           12 :   pph->cb = cb;
     236           12 :   pph->cb_cls = cb_cls;
     237           12 :   pph->url = TALER_url_join (backend_url,
     238              :                              "private/products",
     239              :                              NULL);
     240           12 :   if (NULL == pph->url)
     241              :   {
     242            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     243              :                 "Could not construct request URL.\n");
     244            0 :     json_decref (req_obj);
     245            0 :     GNUNET_free (pph);
     246            0 :     return NULL;
     247              :   }
     248              :   {
     249              :     CURL *eh;
     250              : 
     251           12 :     eh = TALER_MERCHANT_curl_easy_get_ (pph->url);
     252           12 :     GNUNET_assert (GNUNET_OK ==
     253              :                    TALER_curl_easy_post (&pph->post_ctx,
     254              :                                          eh,
     255              :                                          req_obj));
     256           12 :     json_decref (req_obj);
     257           24 :     pph->job = GNUNET_CURL_job_add2 (ctx,
     258              :                                      eh,
     259           12 :                                      pph->post_ctx.headers,
     260              :                                      &handle_post_products_finished,
     261              :                                      pph);
     262           12 :     GNUNET_assert (NULL != pph->job);
     263              :   }
     264           12 :   return pph;
     265              : }
     266              : 
     267              : 
     268              : struct TALER_MERCHANT_ProductsPostHandle *
     269           12 : TALER_MERCHANT_products_post2 (
     270              :   struct GNUNET_CURL_Context *ctx,
     271              :   const char *backend_url,
     272              :   const char *product_id,
     273              :   const char *description,
     274              :   const json_t *description_i18n,
     275              :   const char *unit,
     276              :   const struct TALER_Amount *price,
     277              :   const char *image,
     278              :   const json_t *taxes,
     279              :   int64_t total_stock,
     280              :   const json_t *address,
     281              :   struct GNUNET_TIME_Timestamp next_restock,
     282              :   uint32_t minimum_age,
     283              :   TALER_MERCHANT_ProductsPostCallback cb,
     284              :   void *cb_cls)
     285              : {
     286           12 :   return TALER_MERCHANT_products_post3 (ctx,
     287              :                                         backend_url,
     288              :                                         product_id,
     289              :                                         description,
     290              :                                         description_i18n,
     291              :                                         unit,
     292              :                                         price,
     293              :                                         image,
     294              :                                         taxes,
     295              :                                         total_stock,
     296              :                                         address,
     297              :                                         next_restock,
     298              :                                         minimum_age,
     299              :                                         0,
     300              :                                         NULL,
     301              :                                         cb,
     302              :                                         cb_cls);
     303              : }
     304              : 
     305              : 
     306              : struct TALER_MERCHANT_ProductsPostHandle *
     307            0 : TALER_MERCHANT_products_post (
     308              :   struct GNUNET_CURL_Context *ctx,
     309              :   const char *backend_url,
     310              :   const char *product_id,
     311              :   const char *description,
     312              :   const json_t *description_i18n,
     313              :   const char *unit,
     314              :   const struct TALER_Amount *price,
     315              :   const char *image,
     316              :   const json_t *taxes,
     317              :   int64_t total_stock,
     318              :   const json_t *address,
     319              :   struct GNUNET_TIME_Timestamp next_restock,
     320              :   TALER_MERCHANT_ProductsPostCallback cb,
     321              :   void *cb_cls)
     322              : {
     323            0 :   return TALER_MERCHANT_products_post2 (ctx,
     324              :                                         backend_url,
     325              :                                         product_id,
     326              :                                         description,
     327              :                                         description_i18n,
     328              :                                         unit,
     329              :                                         price,
     330              :                                         image,
     331              :                                         taxes,
     332              :                                         total_stock,
     333              :                                         address,
     334              :                                         next_restock,
     335              :                                         0,
     336              :                                         cb,
     337              :                                         cb_cls);
     338              : }
     339              : 
     340              : 
     341              : void
     342           12 : TALER_MERCHANT_products_post_cancel (
     343              :   struct TALER_MERCHANT_ProductsPostHandle *pph)
     344              : {
     345           12 :   if (NULL != pph->job)
     346              :   {
     347            0 :     GNUNET_CURL_job_cancel (pph->job);
     348            0 :     pph->job = NULL;
     349              :   }
     350           12 :   TALER_curl_easy_post_finished (&pph->post_ctx);
     351           12 :   GNUNET_free (pph->url);
     352           12 :   GNUNET_free (pph);
     353           12 : }
     354              : 
     355              : 
     356              : /* end of merchant_api_post_products.c */
        

Generated by: LCOV version 2.0-1