LCOV - code coverage report
Current view: top level - lib - merchant_api_post_templates.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 51 89 57.3 %
Date: 2025-06-23 16:22:09 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2022 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_templates.c
      21             :  * @brief Implementation of the POST /templates request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Priscilla HUANG
      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 /templates/$ID operation.
      39             :  */
      40             : struct TALER_MERCHANT_TemplatesPostHandle
      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_TemplatesPostCallback 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             :  * Function called when we're done processing the
      77             :  * HTTP POST /templates request.
      78             :  *
      79             :  * @param cls the `struct TALER_MERCHANT_TemplatesPostHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param response response body, NULL if not in JSON
      82             :  */
      83             : static void
      84          12 : handle_post_templates_finished (void *cls,
      85             :                                 long response_code,
      86             :                                 const void *response)
      87             : {
      88          12 :   struct TALER_MERCHANT_TemplatesPostHandle *tph = cls;
      89          12 :   const json_t *json = response;
      90          12 :   struct TALER_MERCHANT_HttpResponse hr = {
      91          12 :     .http_status = (unsigned int) response_code,
      92             :     .reply = json
      93             :   };
      94             : 
      95          12 :   tph->job = NULL;
      96          12 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      97             :               "POST /templates completed with response code %u\n",
      98             :               (unsigned int) response_code);
      99          12 :   switch (response_code)
     100             :   {
     101           0 :   case 0:
     102           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     103           0 :     break;
     104          10 :   case MHD_HTTP_NO_CONTENT:
     105          10 :     break;
     106           0 :   case MHD_HTTP_BAD_REQUEST:
     107           0 :     hr.ec = TALER_JSON_get_error_code (json);
     108           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     109             :     /* This should never happen, either us
     110             :      * or the merchant is buggy (or API version conflict);
     111             :      * just pass JSON reply to the application */
     112           0 :     break;
     113           0 :   case MHD_HTTP_UNAUTHORIZED:
     114           0 :     hr.ec = TALER_JSON_get_error_code (json);
     115           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     116             :     /* Nothing really to verify, merchant says we need to authenticate. */
     117           0 :     break;
     118           0 :   case MHD_HTTP_FORBIDDEN:
     119           0 :     hr.ec = TALER_JSON_get_error_code (json);
     120           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     121             :     /* Nothing really to verify, merchant says we tried to abort the payment
     122             :      * after it was successful. We should pass the JSON reply to the
     123             :      * application */
     124           0 :     break;
     125           0 :   case MHD_HTTP_NOT_FOUND:
     126           0 :     hr.ec = TALER_JSON_get_error_code (json);
     127           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     128             :     /* Nothing really to verify, this should never
     129             :        happen, we should pass the JSON reply to the
     130             :        application */
     131           0 :     break;
     132           2 :   case MHD_HTTP_CONFLICT:
     133           2 :     hr.ec = TALER_JSON_get_error_code (json);
     134           2 :     hr.hint = TALER_JSON_get_error_hint (json);
     135           2 :     break;
     136           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     137           0 :     hr.ec = TALER_JSON_get_error_code (json);
     138           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     139             :     /* Server had an internal issue; we should retry,
     140             :        but this API leaves this to the application */
     141           0 :     break;
     142           0 :   default:
     143           0 :     TALER_MERCHANT_parse_error_details_ (json,
     144             :                                          response_code,
     145             :                                          &hr);
     146             :     /* unexpected response code */
     147           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                 "Unexpected response code %u/%d\n",
     149             :                 (unsigned int) response_code,
     150             :                 (int) hr.ec);
     151           0 :     GNUNET_break_op (0);
     152           0 :     break;
     153             :   }
     154          12 :   tph->cb (tph->cb_cls,
     155             :            &hr);
     156          12 :   TALER_MERCHANT_templates_post_cancel (tph);
     157          12 : }
     158             : 
     159             : 
     160             : static bool
     161          12 : test_template_contract_valid (const json_t *template_contract)
     162             : {
     163             :   const char *summary;
     164          12 :   struct TALER_Amount amount = { .value = 0};
     165          12 :   uint32_t minimum_age = 0;
     166          12 :   struct GNUNET_TIME_Relative pay_duration = { 0 };
     167             :   struct GNUNET_JSON_Specification spec[] = {
     168          12 :     GNUNET_JSON_spec_mark_optional (
     169             :       GNUNET_JSON_spec_string ("summary",
     170             :                                &summary),
     171             :       NULL),
     172          12 :     GNUNET_JSON_spec_mark_optional (
     173             :       TALER_JSON_spec_amount_any ("amount",
     174             :                                   &amount),
     175             :       NULL),
     176          12 :     GNUNET_JSON_spec_uint32 ("minimum_age",
     177             :                              &minimum_age),
     178          12 :     GNUNET_JSON_spec_relative_time ("pay_duration",
     179             :                                     &pay_duration),
     180          12 :     GNUNET_JSON_spec_end ()
     181             :   };
     182             :   const char *ename;
     183             :   unsigned int eline;
     184             : 
     185          12 :   if (GNUNET_OK !=
     186          12 :       GNUNET_JSON_parse (template_contract,
     187             :                          spec,
     188             :                          &ename,
     189             :                          &eline))
     190             :   {
     191           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     192             :                 "Invalid template_contract for field %s\n",
     193             :                 ename);
     194           0 :     return false;
     195             :   }
     196          12 :   return true;
     197             : }
     198             : 
     199             : 
     200             : struct TALER_MERCHANT_TemplatesPostHandle *
     201          12 : TALER_MERCHANT_templates_post (
     202             :   struct GNUNET_CURL_Context *ctx,
     203             :   const char *backend_url,
     204             :   const char *template_id,
     205             :   const char *template_description,
     206             :   const char *otp_id,
     207             :   const json_t *template_contract,
     208             :   TALER_MERCHANT_TemplatesPostCallback cb,
     209             :   void *cb_cls)
     210             : {
     211             :   struct TALER_MERCHANT_TemplatesPostHandle *tph;
     212             :   json_t *req_obj;
     213             : 
     214          12 :   if (! test_template_contract_valid (template_contract))
     215             :   {
     216           0 :     GNUNET_break (0);
     217           0 :     return NULL;
     218             :   }
     219          12 :   req_obj = GNUNET_JSON_PACK (
     220             :     GNUNET_JSON_pack_string ("template_id",
     221             :                              template_id),
     222             :     GNUNET_JSON_pack_string ("template_description",
     223             :                              template_description),
     224             :     GNUNET_JSON_pack_allow_null (
     225             :       GNUNET_JSON_pack_string ("otp_id",
     226             :                                otp_id)),
     227             :     GNUNET_JSON_pack_object_incref ("template_contract",
     228             :                                     (json_t *) template_contract));
     229          12 :   tph = GNUNET_new (struct TALER_MERCHANT_TemplatesPostHandle);
     230          12 :   tph->ctx = ctx;
     231          12 :   tph->cb = cb;
     232          12 :   tph->cb_cls = cb_cls;
     233          12 :   tph->url = TALER_url_join (backend_url,
     234             :                              "private/templates",
     235             :                              NULL);
     236          12 :   if (NULL == tph->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 (tph);
     242           0 :     return NULL;
     243             :   }
     244             :   {
     245             :     CURL *eh;
     246             : 
     247          12 :     eh = TALER_MERCHANT_curl_easy_get_ (tph->url);
     248          12 :     GNUNET_assert (GNUNET_OK ==
     249             :                    TALER_curl_easy_post (&tph->post_ctx,
     250             :                                          eh,
     251             :                                          req_obj));
     252          12 :     json_decref (req_obj);
     253          24 :     tph->job = GNUNET_CURL_job_add2 (ctx,
     254             :                                      eh,
     255          12 :                                      tph->post_ctx.headers,
     256             :                                      &handle_post_templates_finished,
     257             :                                      tph);
     258          12 :     GNUNET_assert (NULL != tph->job);
     259             :   }
     260          12 :   return tph;
     261             : }
     262             : 
     263             : 
     264             : void
     265          12 : TALER_MERCHANT_templates_post_cancel (
     266             :   struct TALER_MERCHANT_TemplatesPostHandle *tph)
     267             : {
     268          12 :   if (NULL != tph->job)
     269             :   {
     270           0 :     GNUNET_CURL_job_cancel (tph->job);
     271           0 :     tph->job = NULL;
     272             :   }
     273          12 :   TALER_curl_easy_post_finished (&tph->post_ctx);
     274          12 :   GNUNET_free (tph->url);
     275          12 :   GNUNET_free (tph);
     276          12 : }
     277             : 
     278             : 
     279             : /* end of merchant_api_post_templates.c */

Generated by: LCOV version 1.16