LCOV - code coverage report
Current view: top level - lib - merchant_api_post_transfers.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 104 0.0 %
Date: 2020-08-15 06:12:35 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, 2015, 2016, 2020 Taler Systems SA
       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_post_transfers.c
      19             :  * @brief Implementation of the POST /transfers request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include <taler/taler_curl_lib.h>
      30             : #include <taler/taler_json_lib.h>
      31             : 
      32             : 
      33             : /**
      34             :  * @brief A handle for POSTing transfer data.
      35             :  */
      36             : struct TALER_MERCHANT_PostTransfersHandle
      37             : {
      38             : 
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_PostTransfersCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             :   /**
      65             :    * Minor context that holds body and headers.
      66             :    */
      67             :   struct TALER_CURL_PostContext post_ctx;
      68             : 
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Function called when we're done processing the
      74             :  * HTTP POST /transfers request.
      75             :  *
      76             :  * @param cls the `struct TALER_MERCHANT_PostTransfersHandle`
      77             :  * @param response_code HTTP response code, 0 on error
      78             :  * @param json response body, NULL if not in JSON
      79             :  */
      80             : static void
      81           0 : handle_post_transfers_finished (void *cls,
      82             :                                 long response_code,
      83             :                                 const void *response)
      84             : {
      85           0 :   struct TALER_MERCHANT_PostTransfersHandle *pth = cls;
      86           0 :   const json_t *json = response;
      87           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      88           0 :     .http_status = (unsigned int) response_code,
      89             :     .reply = json
      90             :   };
      91             : 
      92           0 :   pth->job = NULL;
      93           0 :   switch (response_code)
      94             :   {
      95           0 :   case 0:
      96           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
      97           0 :     break;
      98           0 :   case MHD_HTTP_OK:
      99             :     {
     100             :       struct TALER_Amount total;
     101             :       struct TALER_Amount wire_fee;
     102             :       struct GNUNET_TIME_Absolute execution_time;
     103             :       json_t *deposit_sums;
     104             :       struct GNUNET_JSON_Specification spec[] = {
     105           0 :         TALER_JSON_spec_amount ("total",
     106             :                                 &total),
     107           0 :         TALER_JSON_spec_amount ("wire_fee",
     108             :                                 &wire_fee),
     109           0 :         TALER_JSON_spec_absolute_time ("execution_time",
     110             :                                        &execution_time),
     111           0 :         GNUNET_JSON_spec_json ("deposit_sums",
     112             :                                &deposit_sums),
     113           0 :         GNUNET_JSON_spec_end ()
     114             :       };
     115             : 
     116           0 :       if (GNUNET_OK !=
     117           0 :           GNUNET_JSON_parse (json,
     118             :                              spec,
     119             :                              NULL, NULL))
     120             :       {
     121           0 :         GNUNET_break_op (0);
     122           0 :         hr.http_status = 0;
     123           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     124           0 :         break;
     125             :       }
     126             :       else
     127             :       {
     128             :         size_t deposit_sums_length;
     129             :         struct TALER_MERCHANT_TrackTransferDetail *details;
     130             :         json_t *deposit_sum;
     131             :         unsigned int i;
     132             :         bool ok;
     133             : 
     134           0 :         if (! json_is_array (deposit_sums))
     135             :         {
     136           0 :           GNUNET_break_op (0);
     137           0 :           GNUNET_JSON_parse_free (spec);
     138           0 :           hr.http_status = 0;
     139           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     140           0 :           break;
     141             :         }
     142           0 :         deposit_sums_length = json_array_size (deposit_sums);
     143           0 :         details = GNUNET_new_array (deposit_sums_length,
     144             :                                     struct TALER_MERCHANT_TrackTransferDetail);
     145           0 :         ok = true;
     146           0 :         json_array_foreach (deposit_sums, i, deposit_sum) {
     147           0 :           struct TALER_MERCHANT_TrackTransferDetail *d = &details[i];
     148             :           struct GNUNET_JSON_Specification ispec[] = {
     149           0 :             GNUNET_JSON_spec_string ("order_id",
     150             :                                      &d->order_id),
     151           0 :             TALER_JSON_spec_amount ("deposit_value",
     152             :                                     &d->deposit_value),
     153           0 :             TALER_JSON_spec_amount ("deposit_fee",
     154             :                                     &d->deposit_fee),
     155           0 :             GNUNET_JSON_spec_end ()
     156             :           };
     157             : 
     158           0 :           if (GNUNET_OK !=
     159           0 :               GNUNET_JSON_parse (deposit_sum,
     160             :                                  ispec,
     161             :                                  NULL, NULL))
     162             :           {
     163           0 :             GNUNET_break_op (0);
     164           0 :             ok = false;
     165           0 :             break;
     166             :           }
     167             :         }
     168             : 
     169           0 :         if (! ok)
     170             :         {
     171           0 :           GNUNET_break_op (0);
     172           0 :           GNUNET_free (details);
     173           0 :           GNUNET_JSON_parse_free (spec);
     174           0 :           hr.http_status = 0;
     175           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     176           0 :           break;
     177             :         }
     178           0 :         pth->cb (pth->cb_cls,
     179             :                  &hr,
     180             :                  execution_time,
     181             :                  &total,
     182             :                  &wire_fee,
     183             :                  deposit_sums_length,
     184             :                  details);
     185           0 :         GNUNET_free (details);
     186           0 :         GNUNET_JSON_parse_free (spec);
     187           0 :         TALER_MERCHANT_transfers_post_cancel (pth);
     188           0 :         return;
     189             :       }
     190             :     }
     191           0 :   case MHD_HTTP_ACCEPTED:
     192           0 :     break;
     193           0 :   case MHD_HTTP_NOT_FOUND:
     194             :     /* Nothing really to verify, this should never
     195             :        happen, we should pass the JSON reply to the application */
     196           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     197             :                 "Did not find any data\n");
     198           0 :     hr.ec = TALER_JSON_get_error_code (json);
     199           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     200           0 :     break;
     201           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     202             :     /* Server had an internal issue; we should retry, but this API
     203             :        leaves this to the application */
     204           0 :     hr.ec = TALER_JSON_get_error_code (json);
     205           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     206           0 :     break;
     207           0 :   default:
     208             :     /* unexpected response code */
     209           0 :     GNUNET_break_op (0);
     210           0 :     TALER_MERCHANT_parse_error_details_ (json,
     211             :                                          response_code,
     212             :                                          &hr);
     213           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     214             :                 "Unexpected response code %u/%d\n",
     215             :                 (unsigned int) response_code,
     216             :                 (int) hr.ec);
     217           0 :     break;
     218             :   }
     219           0 :   pth->cb (pth->cb_cls,
     220             :            &hr,
     221             :            GNUNET_TIME_UNIT_FOREVER_ABS,
     222             :            NULL,
     223             :            NULL,
     224             :            0,
     225             :            NULL);
     226           0 :   TALER_MERCHANT_transfers_post_cancel (pth);
     227             : }
     228             : 
     229             : 
     230             : /**
     231             :  * Request backend to remember that we received the given
     232             :  * wire transfer and to request details about the aggregated
     233             :  * transactions from the exchange.
     234             :  *
     235             :  * @param ctx execution context
     236             :  * @param backend_url base URL of the backend
     237             :  * @param credit_amount how much money did we receive (without wire fee)
     238             :  * @param wtid base32 string indicating a wtid
     239             :  * @param payto_uri which account was credited by the wire transfer
     240             :  * @param exchange_url what is the URL of the exchange that made the transfer
     241             :  * @param cb the callback to call when a reply for this request is available
     242             :  * @param cb_cls closure for @a cb
     243             :  * @return a handle for this request
     244             :  */
     245             : struct TALER_MERCHANT_PostTransfersHandle *
     246           0 : TALER_MERCHANT_transfers_post (
     247             :   struct GNUNET_CURL_Context *ctx,
     248             :   const char *backend_url,
     249             :   const struct TALER_Amount *credit_amount,
     250             :   const struct TALER_WireTransferIdentifierRawP *wtid,
     251             :   const char *payto_uri,
     252             :   const char *exchange_url,
     253             :   TALER_MERCHANT_PostTransfersCallback cb,
     254             :   void *cb_cls)
     255             : {
     256             :   struct TALER_MERCHANT_PostTransfersHandle *pth;
     257             :   CURL *eh;
     258             :   json_t *req;
     259             : 
     260           0 :   pth = GNUNET_new (struct TALER_MERCHANT_PostTransfersHandle);
     261           0 :   pth->ctx = ctx;
     262           0 :   pth->cb = cb;
     263           0 :   pth->cb_cls = cb_cls;
     264           0 :   pth->url = TALER_url_join (backend_url,
     265             :                              "private/transfers",
     266             :                              NULL);
     267           0 :   if (NULL == pth->url)
     268             :   {
     269           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     270             :                 "Could not construct request URL.\n");
     271           0 :     GNUNET_free (pth);
     272           0 :     return NULL;
     273             :   }
     274           0 :   req = json_pack ("{s:o, s:o, s:s, s:s}",
     275             :                    "credit_amount", TALER_JSON_from_amount (credit_amount),
     276             :                    "wtid", GNUNET_JSON_from_data_auto (wtid),
     277             :                    "payto_uri", payto_uri,
     278             :                    "exchange_url", exchange_url);
     279           0 :   GNUNET_assert (NULL != req);
     280           0 :   eh = curl_easy_init ();
     281           0 :   GNUNET_assert (NULL != eh);
     282           0 :   if (GNUNET_OK != TALER_curl_easy_post (&pth->post_ctx,
     283             :                                          eh,
     284             :                                          req))
     285             :   {
     286           0 :     GNUNET_break (0);
     287           0 :     json_decref (req);
     288           0 :     GNUNET_free (pth);
     289           0 :     return NULL;
     290             :   }
     291           0 :   json_decref (req);
     292           0 :   GNUNET_assert (CURLE_OK ==
     293             :                  curl_easy_setopt (eh,
     294             :                                    CURLOPT_URL,
     295             :                                    pth->url));
     296           0 :   pth->job = GNUNET_CURL_job_add2 (ctx,
     297             :                                    eh,
     298           0 :                                    pth->post_ctx.headers,
     299             :                                    &handle_post_transfers_finished,
     300             :                                    pth);
     301           0 :   return pth;
     302             : }
     303             : 
     304             : 
     305             : /**
     306             :  * Cancel a POST /transfers request.  This function cannot be used
     307             :  * on a request handle if a response is already served for it.
     308             :  *
     309             :  * @param pth handle to the tracking operation being cancelled
     310             :  */
     311             : void
     312           0 : TALER_MERCHANT_transfers_post_cancel (
     313             :   struct TALER_MERCHANT_PostTransfersHandle *pth)
     314             : {
     315           0 :   if (NULL != pth->job)
     316             :   {
     317           0 :     GNUNET_CURL_job_cancel (pth->job);
     318           0 :     pth->job = NULL;
     319             :   }
     320           0 :   GNUNET_free (pth->url);
     321           0 :   TALER_curl_easy_post_finished (&pth->post_ctx);
     322           0 :   GNUNET_free (pth);
     323           0 : }
     324             : 
     325             : 
     326             : /* end of merchant_api_post_transfers.c */

Generated by: LCOV version 1.14