LCOV - code coverage report
Current view: top level - lib - merchant_api_post_order_claim.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 71 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2021 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, but
      11             :   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 Public
      16             :   License along with TALER; see the file COPYING.LGPL.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file merchant_api_post_order_claim.c
      21             :  * @brief Implementation of POST /orders/$ID/claim
      22             :  * @author Christian Grothoff
      23             :  * @author Marcello Stanisci
      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 <gnunet/gnunet_curl_lib.h>
      31             : #include "taler_merchant_service.h"
      32             : #include "merchant_api_curl_defaults.h"
      33             : #include <taler/taler_json_lib.h>
      34             : #include <taler/taler_signatures.h>
      35             : #include <taler/taler_curl_lib.h>
      36             : 
      37             : 
      38             : /**
      39             :  * Structure representing a POST /orders/$ID/claim operation.
      40             :  */
      41             : struct TALER_MERCHANT_OrderClaimHandle
      42             : {
      43             :   /**
      44             :    * Full URL, includes "/orders/$ID/claim".
      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_OrderClaimCallback 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             :  * POST /orders/$ID/claim request.
      78             :  *
      79             :  * @param cls the `struct TALER_MERCHANT_OrderClaimHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param response response body, should be NULL
      82             :  */
      83             : static void
      84           0 : handle_post_order_claim_finished (void *cls,
      85             :                                   long response_code,
      86             :                                   const void *response)
      87             : {
      88           0 :   struct TALER_MERCHANT_OrderClaimHandle *och = cls;
      89             :   json_t *contract_terms;
      90             :   struct TALER_MerchantSignatureP sig;
      91             :   struct TALER_PrivateContractHashP hash;
      92           0 :   const json_t *json = response;
      93             :   struct GNUNET_JSON_Specification spec[] = {
      94           0 :     GNUNET_JSON_spec_json ("contract_terms",
      95             :                            &contract_terms),
      96           0 :     GNUNET_JSON_spec_fixed_auto ("sig",
      97             :                                  &sig),
      98           0 :     GNUNET_JSON_spec_end ()
      99             :   };
     100           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     101           0 :     .http_status = (unsigned int) response_code,
     102             :     .reply = json
     103             :   };
     104             : 
     105           0 :   och->job = NULL;
     106           0 :   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     107             :               "Order claimed with status %u\n",
     108             :               (unsigned int) response_code);
     109             : 
     110           0 :   if (MHD_HTTP_OK != response_code)
     111             :   {
     112           0 :     hr.ec = TALER_JSON_get_error_code (json);
     113           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     114           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     115             :                 "Proposal lookup failed with HTTP status code %u/%d\n",
     116             :                 (unsigned int) response_code,
     117             :                 (int) hr.ec);
     118           0 :     och->cb (och->cb_cls,
     119             :              &hr,
     120             :              NULL,
     121             :              NULL,
     122             :              NULL);
     123           0 :     TALER_MERCHANT_order_claim_cancel (och);
     124           0 :     return;
     125             :   }
     126             : 
     127           0 :   if (GNUNET_OK !=
     128           0 :       GNUNET_JSON_parse (json,
     129             :                          spec,
     130             :                          NULL, NULL))
     131             :   {
     132           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     133             :                 "Claiming order failed: could not parse JSON response\n");
     134           0 :     GNUNET_break_op (0);
     135           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     136           0 :     hr.http_status = 0;
     137           0 :     och->cb (och->cb_cls,
     138             :              &hr,
     139             :              NULL,
     140             :              NULL,
     141             :              NULL);
     142           0 :     TALER_MERCHANT_order_claim_cancel (och);
     143           0 :     return;
     144             :   }
     145             : 
     146           0 :   if (GNUNET_OK !=
     147           0 :       TALER_JSON_contract_hash (contract_terms,
     148             :                                 &hash))
     149             :   {
     150           0 :     GNUNET_break (0);
     151           0 :     hr.ec = TALER_EC_MERCHANT_POST_ORDERS_ID_CLAIM_CLIENT_INTERNAL_FAILURE;
     152           0 :     hr.http_status = 0;
     153           0 :     GNUNET_JSON_parse_free (spec);
     154           0 :     och->cb (och->cb_cls,
     155             :              &hr,
     156             :              NULL,
     157             :              NULL,
     158             :              NULL);
     159           0 :     TALER_MERCHANT_order_claim_cancel (och);
     160           0 :     return;
     161             :   }
     162             : 
     163           0 :   och->cb (och->cb_cls,
     164             :            &hr,
     165             :            contract_terms,
     166             :            &sig,
     167             :            &hash);
     168           0 :   GNUNET_JSON_parse_free (spec);
     169           0 :   TALER_MERCHANT_order_claim_cancel (och);
     170             : }
     171             : 
     172             : 
     173             : struct TALER_MERCHANT_OrderClaimHandle *
     174           0 : TALER_MERCHANT_order_claim (struct GNUNET_CURL_Context *ctx,
     175             :                             const char *backend_url,
     176             :                             const char *order_id,
     177             :                             const struct GNUNET_CRYPTO_EddsaPublicKey *nonce,
     178             :                             const struct TALER_ClaimTokenP *claim_token,
     179             :                             TALER_MERCHANT_OrderClaimCallback cb,
     180             :                             void *cb_cls)
     181             : {
     182             :   struct TALER_MERCHANT_OrderClaimHandle *och;
     183             :   json_t *req_obj;
     184             : 
     185           0 :   if (NULL == order_id)
     186             :   {
     187           0 :     GNUNET_break (0);
     188           0 :     return NULL;
     189             :   }
     190           0 :   req_obj = GNUNET_JSON_PACK (
     191             :     GNUNET_JSON_pack_data_auto ("nonce",
     192             :                                 nonce),
     193             :     GNUNET_JSON_pack_allow_null (
     194             :       GNUNET_JSON_pack_data_auto ("token",
     195             :                                   claim_token)));
     196           0 :   och = GNUNET_new (struct TALER_MERCHANT_OrderClaimHandle);
     197           0 :   och->ctx = ctx;
     198           0 :   och->cb = cb;
     199           0 :   och->cb_cls = cb_cls;
     200             :   {
     201             :     char *path;
     202             : 
     203           0 :     GNUNET_asprintf (&path,
     204             :                      "orders/%s/claim",
     205             :                      order_id);
     206           0 :     och->url = TALER_url_join (backend_url,
     207             :                                path,
     208             :                                NULL);
     209           0 :     GNUNET_free (path);
     210             :   }
     211           0 :   if (NULL == och->url)
     212             :   {
     213           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     214             :                 "Could not construct request URL.\n");
     215           0 :     json_decref (req_obj);
     216           0 :     GNUNET_free (och);
     217           0 :     return NULL;
     218             :   }
     219           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     220             :               "Claiming order at %s\n",
     221             :               och->url);
     222             :   {
     223             :     CURL *eh;
     224             : 
     225           0 :     eh = TALER_MERCHANT_curl_easy_get_ (och->url);
     226           0 :     GNUNET_assert (GNUNET_OK ==
     227             :                    TALER_curl_easy_post (&och->post_ctx,
     228             :                                          eh,
     229             :                                          req_obj));
     230           0 :     json_decref (req_obj);
     231           0 :     och->job = GNUNET_CURL_job_add2 (ctx,
     232             :                                      eh,
     233           0 :                                      och->post_ctx.headers,
     234             :                                      &handle_post_order_claim_finished,
     235             :                                      och);
     236           0 :     GNUNET_assert (NULL != och->job);
     237             :   }
     238           0 :   return och;
     239             : }
     240             : 
     241             : 
     242             : void
     243           0 : TALER_MERCHANT_order_claim_cancel (struct TALER_MERCHANT_OrderClaimHandle *och)
     244             : {
     245           0 :   if (NULL != och->job)
     246             :   {
     247           0 :     GNUNET_CURL_job_cancel (och->job);
     248           0 :     och->job = NULL;
     249             :   }
     250           0 :   TALER_curl_easy_post_finished (&och->post_ctx);
     251           0 :   GNUNET_free (och->url);
     252           0 :   GNUNET_free (och);
     253           0 : }
     254             : 
     255             : 
     256             : /* end of merchant_api_post_order_claim.c */

Generated by: LCOV version 1.14