LCOV - code coverage report
Current view: top level - lib - merchant_api_post_order_claim.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 77 0.0 %
Date: 2021-04-12 06:04:58 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-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, 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 <taler/taler_json_lib.h>
      33             : #include <taler/taler_signatures.h>
      34             : #include <taler/taler_curl_lib.h>
      35             : 
      36             : 
      37             : /**
      38             :  * Structure representing a POST /orders/$ID/claim operation.
      39             :  */
      40             : struct TALER_MERCHANT_OrderClaimHandle
      41             : {
      42             :   /**
      43             :    * Full URL, includes "/orders/$ID/claim".
      44             :    */
      45             :   char *url;
      46             : 
      47             :   /**
      48             :    * Handle for the request.
      49             :    */
      50             :   struct GNUNET_CURL_Job *job;
      51             : 
      52             :   /**
      53             :    * Function to call with the result.
      54             :    */
      55             :   TALER_MERCHANT_OrderClaimCallback cb;
      56             : 
      57             :   /**
      58             :    * Closure for @a cb.
      59             :    */
      60             :   void *cb_cls;
      61             : 
      62             :   /**
      63             :    * Reference to the execution context.
      64             :    */
      65             :   struct GNUNET_CURL_Context *ctx;
      66             : 
      67             :   /**
      68             :    * Minor context that holds body and headers.
      69             :    */
      70             :   struct TALER_CURL_PostContext post_ctx;
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Function called when we're done processing the
      76             :  * POST /orders/$ID/claim request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_OrderClaimHandle`
      79             :  * @param response_code HTTP response code, 0 on error
      80             :  * @param response response body, should be NULL
      81             :  */
      82             : static void
      83           0 : handle_post_order_claim_finished (void *cls,
      84             :                                   long response_code,
      85             :                                   const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_OrderClaimHandle *och = cls;
      88             :   json_t *contract_terms;
      89             :   struct TALER_MerchantSignatureP sig;
      90             :   struct GNUNET_HashCode hash;
      91           0 :   const json_t *json = response;
      92             :   struct GNUNET_JSON_Specification spec[] = {
      93           0 :     GNUNET_JSON_spec_json ("contract_terms",
      94             :                            &contract_terms),
      95           0 :     GNUNET_JSON_spec_fixed_auto ("sig",
      96             :                                  &sig),
      97           0 :     GNUNET_JSON_spec_end ()
      98             :   };
      99           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     100           0 :     .http_status = (unsigned int) response_code,
     101             :     .reply = json
     102             :   };
     103             : 
     104           0 :   och->job = NULL;
     105           0 :   if (MHD_HTTP_OK != response_code)
     106             :   {
     107           0 :     hr.ec = TALER_JSON_get_error_code (json);
     108           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     109           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     110             :                 "Proposal lookup failed with HTTP status code %u/%d\n",
     111             :                 (unsigned int) response_code,
     112             :                 (int) hr.ec);
     113           0 :     och->cb (och->cb_cls,
     114             :              &hr,
     115             :              NULL,
     116             :              NULL,
     117             :              NULL);
     118           0 :     TALER_MERCHANT_order_claim_cancel (och);
     119           0 :     return;
     120             :   }
     121             : 
     122           0 :   if (GNUNET_OK !=
     123           0 :       GNUNET_JSON_parse (json,
     124             :                          spec,
     125             :                          NULL, NULL))
     126             :   {
     127           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     128             :                 "Claiming order failed: could not parse JSON response\n");
     129           0 :     GNUNET_break_op (0);
     130           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     131           0 :     hr.http_status = 0;
     132           0 :     och->cb (och->cb_cls,
     133             :              &hr,
     134             :              NULL,
     135             :              NULL,
     136             :              NULL);
     137           0 :     TALER_MERCHANT_order_claim_cancel (och);
     138           0 :     return;
     139             :   }
     140             : 
     141           0 :   if (GNUNET_OK !=
     142           0 :       TALER_JSON_contract_hash (contract_terms,
     143             :                                 &hash))
     144             :   {
     145           0 :     GNUNET_break (0);
     146           0 :     hr.ec = TALER_EC_MERCHANT_POST_ORDERS_ID_CLAIM_CLIENT_INTERNAL_FAILURE;
     147           0 :     hr.http_status = 0;
     148           0 :     GNUNET_JSON_parse_free (spec);
     149           0 :     och->cb (och->cb_cls,
     150             :              &hr,
     151             :              NULL,
     152             :              NULL,
     153             :              NULL);
     154           0 :     TALER_MERCHANT_order_claim_cancel (och);
     155           0 :     return;
     156             :   }
     157             : 
     158           0 :   och->cb (och->cb_cls,
     159             :            &hr,
     160             :            contract_terms,
     161             :            &sig,
     162             :            &hash);
     163           0 :   GNUNET_JSON_parse_free (spec);
     164           0 :   TALER_MERCHANT_order_claim_cancel (och);
     165             : }
     166             : 
     167             : 
     168             : struct TALER_MERCHANT_OrderClaimHandle *
     169           0 : TALER_MERCHANT_order_claim (struct GNUNET_CURL_Context *ctx,
     170             :                             const char *backend_url,
     171             :                             const char *order_id,
     172             :                             const struct GNUNET_CRYPTO_EddsaPublicKey *nonce,
     173             :                             const struct TALER_ClaimTokenP *claim_token,
     174             :                             TALER_MERCHANT_OrderClaimCallback cb,
     175             :                             void *cb_cls)
     176             : {
     177             :   struct TALER_MERCHANT_OrderClaimHandle *och;
     178             :   json_t *req_obj;
     179             : 
     180           0 :   if (NULL == order_id)
     181             :   {
     182           0 :     GNUNET_break (0);
     183           0 :     return NULL;
     184             :   }
     185           0 :   req_obj = json_pack ("{s:o}",
     186             :                        "nonce",
     187             :                        GNUNET_JSON_from_data_auto (nonce));
     188           0 :   if (NULL == req_obj)
     189             :   {
     190           0 :     GNUNET_break (0);
     191           0 :     return NULL;
     192             :   }
     193           0 :   if (NULL != claim_token)
     194             :   {
     195           0 :     GNUNET_assert (0 ==
     196             :                    json_object_set_new (req_obj,
     197             :                                         "token",
     198             :                                         GNUNET_JSON_from_data_auto (
     199             :                                           claim_token)));
     200             :   }
     201           0 :   och = GNUNET_new (struct TALER_MERCHANT_OrderClaimHandle);
     202           0 :   och->ctx = ctx;
     203           0 :   och->cb = cb;
     204           0 :   och->cb_cls = cb_cls;
     205             :   {
     206             :     char *path;
     207             : 
     208           0 :     GNUNET_asprintf (&path,
     209             :                      "orders/%s/claim",
     210             :                      order_id);
     211           0 :     och->url = TALER_url_join (backend_url,
     212             :                                path,
     213             :                                NULL);
     214           0 :     GNUNET_free (path);
     215             :   }
     216           0 :   if (NULL == och->url)
     217             :   {
     218           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     219             :                 "Could not construct request URL.\n");
     220           0 :     json_decref (req_obj);
     221           0 :     GNUNET_free (och);
     222           0 :     return NULL;
     223             :   }
     224           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     225             :               "Claiming order at %s\n",
     226             :               och->url);
     227             :   {
     228             :     CURL *eh;
     229             : 
     230           0 :     eh = curl_easy_init ();
     231           0 :     GNUNET_assert (NULL != eh);
     232           0 :     GNUNET_assert (GNUNET_OK ==
     233             :                    TALER_curl_easy_post (&och->post_ctx,
     234             :                                          eh,
     235             :                                          req_obj));
     236           0 :     json_decref (req_obj);
     237           0 :     GNUNET_assert (CURLE_OK ==
     238             :                    curl_easy_setopt (eh,
     239             :                                      CURLOPT_URL,
     240             :                                      och->url));
     241           0 :     och->job = GNUNET_CURL_job_add2 (ctx,
     242             :                                      eh,
     243           0 :                                      och->post_ctx.headers,
     244             :                                      &handle_post_order_claim_finished,
     245             :                                      och);
     246           0 :     GNUNET_assert (NULL != och->job);
     247             :   }
     248           0 :   return och;
     249             : }
     250             : 
     251             : 
     252             : void
     253           0 : TALER_MERCHANT_order_claim_cancel (struct TALER_MERCHANT_OrderClaimHandle *och)
     254             : {
     255           0 :   if (NULL != och->job)
     256             :   {
     257           0 :     GNUNET_CURL_job_cancel (och->job);
     258           0 :     och->job = NULL;
     259             :   }
     260           0 :   TALER_curl_easy_post_finished (&och->post_ctx);
     261           0 :   GNUNET_free (och->url);
     262           0 :   GNUNET_free (och);
     263           0 : }
     264             : 
     265             : 
     266             : /* end of merchant_api_post_order_claim.c */

Generated by: LCOV version 1.14