LCOV - code coverage report
Current view: top level - lib - merchant_api_track_transaction.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 25 40 62.5 %
Date: 2018-07-14 06:17:23 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V. and INRIA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/merchant_api_track_transaction.c
      19             :  * @brief Implementation of the /track/transaction request of the
      20             :  * merchant's HTTP API
      21             :  * @author Marcello Stanisci
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include <curl/curl.h>
      26             : #include <jansson.h>
      27             : #include <microhttpd.h> /* just for HTTP status codes */
      28             : #include <gnunet/gnunet_util_lib.h>
      29             : #include <gnunet/gnunet_curl_lib.h>
      30             : #include "taler_merchant_service.h"
      31             : #include <taler/taler_json_lib.h>
      32             : #include <taler/taler_signatures.h>
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A handle for tracking transactions.
      37             :  */
      38             : struct TALER_MERCHANT_TrackTransactionHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_MERCHANT_TrackTransactionCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Function called when we're done processing the
      70             :  * HTTP /track/transaction request.
      71             :  *
      72             :  * @param cls the `struct TALER_MERCHANT_TrackTransactionHandle`
      73             :  * @param response_code HTTP response code, 0 on error
      74             :  * @param json response body, NULL if not in JSON
      75             :  */
      76             : static void
      77           6 : handle_track_transaction_finished (void *cls,
      78             :                                    long response_code,
      79             :                                    const json_t *json)
      80             : {
      81           6 :   struct TALER_MERCHANT_TrackTransactionHandle *tdo = cls;
      82             : 
      83           6 :   tdo->job = NULL;
      84           6 :   switch (response_code)
      85             :   {
      86             :   case 0:
      87           0 :     break;
      88             :   case MHD_HTTP_OK:
      89           6 :     tdo->cb (tdo->cb_cls,
      90             :              MHD_HTTP_OK,
      91             :              TALER_EC_NONE,
      92             :              json);
      93           6 :     TALER_MERCHANT_track_transaction_cancel (tdo);
      94           6 :     return;
      95             :   case MHD_HTTP_ACCEPTED:
      96             :     {
      97             :       /* Expect time stamp of when the transfer is supposed to happen */
      98             :     }
      99           0 :     break;
     100             :   case MHD_HTTP_FAILED_DEPENDENCY:
     101           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     102             :                 "Exchange gave inconsistent response\n");
     103           0 :     break;
     104             :   case MHD_HTTP_NOT_FOUND:
     105             :     /* Nothing really to verify, this should never
     106             :        happen, we should pass the JSON reply to the application */
     107           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     108             :                 "Did not find any data\n");
     109           0 :     break;
     110             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     111             :     /* Server had an internal issue; we should retry, but this API
     112             :        leaves this to the application */
     113           0 :     break;
     114             :   default:
     115             :     /* unexpected response code */
     116           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     117             :                 "Unexpected response code %u\n",
     118             :                 (unsigned int) response_code);
     119           0 :     GNUNET_break (0);
     120           0 :     response_code = 0;
     121           0 :     break;
     122             :   }
     123           0 :   tdo->cb (tdo->cb_cls,
     124             :            response_code,
     125             :            TALER_JSON_get_error_code (json),
     126             :            json);
     127           0 :   TALER_MERCHANT_track_transaction_cancel (tdo);
     128             : }
     129             : 
     130             : 
     131             : /**
     132             :  * Request backend to return transactions associated with a given wtid.
     133             :  *
     134             :  * @param ctx execution context
     135             :  * @param backend_url base URL of the backend
     136             :  * @param instance which merchant instance is going to be tracked
     137             :  * @param order_id order id pointing to the transaction being tracked
     138             :  * @param track_transaction_cb the callback to call when a reply for this request is available
     139             :  * @param track_transaction_cb_cls closure for @a track_transaction_cb
     140             :  * @return a handle for this request
     141             :  */
     142             : struct TALER_MERCHANT_TrackTransactionHandle *
     143           6 : TALER_MERCHANT_track_transaction (struct GNUNET_CURL_Context *ctx,
     144             :                                   const char *backend_url,
     145             :                                   const char *instance,
     146             :                                   const char *order_id,
     147             :                                   TALER_MERCHANT_TrackTransactionCallback track_transaction_cb,
     148             :                                   void *track_transaction_cb_cls)
     149             : {
     150             :   struct TALER_MERCHANT_TrackTransactionHandle *tdo;
     151             :   CURL *eh;
     152             :   char *base;
     153             : 
     154           6 :   tdo = GNUNET_new (struct TALER_MERCHANT_TrackTransactionHandle);
     155           6 :   tdo->ctx = ctx;
     156           6 :   tdo->cb = track_transaction_cb;
     157           6 :   tdo->cb_cls = track_transaction_cb_cls;
     158           6 :   base = TALER_url_join (backend_url, "/track/transaction", NULL);
     159           6 :   GNUNET_asprintf (&tdo->url,
     160             :                    "%s?order_id=%s&instance=%s",
     161             :                    base,
     162             :                    order_id,
     163             :                    instance);
     164           6 :   GNUNET_free (base);
     165           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     166             :               "Requesting URL '%s'\n",
     167             :               tdo->url);
     168           6 :   eh = curl_easy_init ();
     169           6 :   GNUNET_assert (CURLE_OK ==
     170             :                  curl_easy_setopt (eh,
     171             :                                    CURLOPT_URL,
     172             :                                    tdo->url));
     173           6 :   tdo->job = GNUNET_CURL_job_add (ctx,
     174             :                                   eh,
     175             :                                   GNUNET_YES,
     176             :                                   &handle_track_transaction_finished,
     177             :                                   tdo);
     178           6 :   return tdo;
     179             : }
     180             : 
     181             : 
     182             : /**
     183             :  * Cancel a /track/transaction request.  This function cannot be used
     184             :  * on a request handle if a response is already served for it.
     185             :  *
     186             :  * @param tdo handle to the tracking operation being cancelled
     187             :  */
     188             : void
     189           6 : TALER_MERCHANT_track_transaction_cancel (struct TALER_MERCHANT_TrackTransactionHandle *tdo)
     190             : {
     191           6 :   if (NULL != tdo->job)
     192             :   {
     193           0 :     GNUNET_CURL_job_cancel (tdo->job);
     194           0 :     tdo->job = NULL;
     195             :   }
     196           6 :   GNUNET_free (tdo->url);
     197           6 :   GNUNET_free (tdo);
     198           6 : }
     199             : 
     200             : /* end of merchant_api_track_transaction.c */

Generated by: LCOV version 1.13