LCOV - code coverage report
Current view: top level - lib - merchant_api_post_transfers.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 126 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 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 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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_curl_lib.h>
      31             : #include <taler/taler_json_lib.h>
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A handle for POSTing transfer data.
      36             :  */
      37             : struct TALER_MERCHANT_PostTransfersHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_MERCHANT_PostTransfersCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : 
      65             :   /**
      66             :    * Minor context that holds body and headers.
      67             :    */
      68             :   struct TALER_CURL_PostContext post_ctx;
      69             : 
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Function called when we're done processing the
      75             :  * HTTP POST /transfers request.
      76             :  *
      77             :  * @param cls the `struct TALER_MERCHANT_PostTransfersHandle`
      78             :  * @param response_code HTTP response code, 0 on error
      79             :  * @param response response body, NULL if not in JSON
      80             :  */
      81             : static void
      82           0 : handle_post_transfers_finished (void *cls,
      83             :                                 long response_code,
      84             :                                 const void *response)
      85             : {
      86           0 :   struct TALER_MERCHANT_PostTransfersHandle *pth = cls;
      87           0 :   const json_t *json = response;
      88           0 :   json_t *deposit_sum = NULL;
      89           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      90           0 :     .http_status = (unsigned int) response_code,
      91             :     .reply = json
      92             :   };
      93             : 
      94           0 :   pth->job = NULL;
      95           0 :   switch (response_code)
      96             :   {
      97           0 :   case 0:
      98           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      99           0 :     break;
     100           0 :   case MHD_HTTP_OK:
     101             :     {
     102             :       struct TALER_Amount total;
     103             :       struct TALER_Amount wire_fee;
     104             :       struct GNUNET_TIME_Timestamp execution_time;
     105             :       json_t *deposit_sums;
     106             :       struct GNUNET_JSON_Specification spec[] = {
     107           0 :         TALER_JSON_spec_amount_any ("total",
     108             :                                     &total),
     109           0 :         TALER_JSON_spec_amount_any ("wire_fee",
     110             :                                     &wire_fee),
     111           0 :         GNUNET_JSON_spec_timestamp ("execution_time",
     112             :                                     &execution_time),
     113           0 :         GNUNET_JSON_spec_json ("deposit_sums",
     114             :                                &deposit_sums),
     115           0 :         GNUNET_JSON_spec_end ()
     116             :       };
     117             : 
     118           0 :       if (GNUNET_OK !=
     119           0 :           GNUNET_JSON_parse (json,
     120             :                              spec,
     121             :                              NULL, NULL))
     122             :       {
     123           0 :         GNUNET_break_op (0);
     124           0 :         hr.http_status = 0;
     125           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     126           0 :         break;
     127             :       }
     128             :       else
     129             :       {
     130             :         size_t deposit_sums_length;
     131             :         struct TALER_MERCHANT_TrackTransferDetail *details;
     132             :         unsigned int i;
     133             :         bool ok;
     134             : 
     135           0 :         if (! json_is_array (deposit_sums))
     136             :         {
     137           0 :           GNUNET_break_op (0);
     138           0 :           GNUNET_JSON_parse_free (spec);
     139           0 :           hr.http_status = 0;
     140           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     141           0 :           break;
     142             :         }
     143           0 :         deposit_sums_length = json_array_size (deposit_sums);
     144           0 :         details = GNUNET_new_array (deposit_sums_length,
     145             :                                     struct TALER_MERCHANT_TrackTransferDetail);
     146           0 :         ok = true;
     147           0 :         json_array_foreach (deposit_sums, i, deposit_sum) {
     148           0 :           struct TALER_MERCHANT_TrackTransferDetail *d = &details[i];
     149             :           struct GNUNET_JSON_Specification ispec[] = {
     150           0 :             GNUNET_JSON_spec_string ("order_id",
     151             :                                      &d->order_id),
     152           0 :             TALER_JSON_spec_amount_any ("deposit_value",
     153             :                                         &d->deposit_value),
     154           0 :             TALER_JSON_spec_amount_any ("deposit_fee",
     155             :                                         &d->deposit_fee),
     156           0 :             GNUNET_JSON_spec_end ()
     157             :           };
     158             : 
     159           0 :           if (GNUNET_OK !=
     160           0 :               GNUNET_JSON_parse (deposit_sum,
     161             :                                  ispec,
     162             :                                  NULL, NULL))
     163             :           {
     164           0 :             GNUNET_break_op (0);
     165           0 :             ok = false;
     166           0 :             break;
     167             :           }
     168             :         }
     169             : 
     170           0 :         if (! ok)
     171             :         {
     172           0 :           GNUNET_break_op (0);
     173           0 :           GNUNET_free (details);
     174           0 :           GNUNET_JSON_parse_free (spec);
     175           0 :           hr.http_status = 0;
     176           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     177           0 :           break;
     178             :         }
     179           0 :         pth->cb (pth->cb_cls,
     180             :                  &hr,
     181             :                  execution_time,
     182             :                  &total,
     183             :                  &wire_fee,
     184             :                  deposit_sums_length,
     185             :                  details);
     186           0 :         GNUNET_free (details);
     187           0 :         GNUNET_JSON_parse_free (spec);
     188           0 :         TALER_MERCHANT_transfers_post_cancel (pth);
     189           0 :         return;
     190             :       }
     191             :     }
     192           0 :   case MHD_HTTP_ACCEPTED:
     193           0 :     break;
     194           0 :   case MHD_HTTP_UNAUTHORIZED:
     195           0 :     hr.ec = TALER_JSON_get_error_code (json);
     196           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     197             :     /* Nothing really to verify, merchant says we need to authenticate. */
     198           0 :     break;
     199           0 :   case MHD_HTTP_NOT_FOUND:
     200             :     /* Nothing really to verify, this should never
     201             :        happen, we should pass the JSON reply to the application */
     202           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     203             :                 "Did not find any data\n");
     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 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     208             :     /* Server had an internal issue; we should retry, but this API
     209             :        leaves this to the application */
     210           0 :     hr.ec = TALER_JSON_get_error_code (json);
     211           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     212           0 :     break;
     213           0 :   case MHD_HTTP_BAD_GATEWAY:
     214             :     /* Exchange had an issue; we should retry, but this API
     215             :        leaves this to the application */
     216           0 :     hr.ec = TALER_JSON_get_error_code (json);
     217           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     218             :     {
     219             :       uint32_t eec;
     220             :       uint32_t ehc;
     221             :       struct GNUNET_JSON_Specification ispec[] = {
     222           0 :         GNUNET_JSON_spec_uint32 ("exchange_code",
     223             :                                  &eec),
     224           0 :         GNUNET_JSON_spec_uint32 ("exchange_http_status",
     225             :                                  &ehc),
     226           0 :         GNUNET_JSON_spec_end ()
     227             :       };
     228             : 
     229           0 :       if (GNUNET_OK !=
     230           0 :           GNUNET_JSON_parse (deposit_sum,
     231             :                              ispec,
     232             :                              NULL, NULL))
     233             :       {
     234           0 :         GNUNET_break_op (0);
     235           0 :         break;
     236             :       }
     237             :       else
     238             :       {
     239           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     240             :                     "Exchange returned %u/%u\n",
     241             :                     (unsigned int) eec,
     242             :                     (unsigned int) ehc);
     243             :       }
     244             :     }
     245           0 :     break;
     246           0 :   case MHD_HTTP_GATEWAY_TIMEOUT:
     247             :     /* Server had an internal issue; we should retry, but this API
     248             :        leaves this to the application */
     249           0 :     hr.ec = TALER_JSON_get_error_code (json);
     250           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     251           0 :     break;
     252           0 :   default:
     253             :     /* unexpected response code */
     254           0 :     GNUNET_break_op (0);
     255           0 :     TALER_MERCHANT_parse_error_details_ (json,
     256             :                                          response_code,
     257             :                                          &hr);
     258           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     259             :                 "Unexpected response code %u/%d\n",
     260             :                 (unsigned int) response_code,
     261             :                 (int) hr.ec);
     262           0 :     break;
     263             :   }
     264           0 :   pth->cb (pth->cb_cls,
     265             :            &hr,
     266           0 :            GNUNET_TIME_UNIT_FOREVER_TS,
     267             :            NULL,
     268             :            NULL,
     269             :            0,
     270             :            NULL);
     271           0 :   TALER_MERCHANT_transfers_post_cancel (pth);
     272             : }
     273             : 
     274             : 
     275             : struct TALER_MERCHANT_PostTransfersHandle *
     276           0 : TALER_MERCHANT_transfers_post (
     277             :   struct GNUNET_CURL_Context *ctx,
     278             :   const char *backend_url,
     279             :   const struct TALER_Amount *credit_amount,
     280             :   const struct TALER_WireTransferIdentifierRawP *wtid,
     281             :   const char *payto_uri,
     282             :   const char *exchange_url,
     283             :   TALER_MERCHANT_PostTransfersCallback cb,
     284             :   void *cb_cls)
     285             : {
     286             :   struct TALER_MERCHANT_PostTransfersHandle *pth;
     287             :   CURL *eh;
     288             :   json_t *req;
     289             : 
     290           0 :   pth = GNUNET_new (struct TALER_MERCHANT_PostTransfersHandle);
     291           0 :   pth->ctx = ctx;
     292           0 :   pth->cb = cb;
     293           0 :   pth->cb_cls = cb_cls;
     294           0 :   pth->url = TALER_url_join (backend_url,
     295             :                              "private/transfers",
     296             :                              NULL);
     297           0 :   if (NULL == pth->url)
     298             :   {
     299           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     300             :                 "Could not construct request URL.\n");
     301           0 :     GNUNET_free (pth);
     302           0 :     return NULL;
     303             :   }
     304           0 :   req = GNUNET_JSON_PACK (
     305             :     TALER_JSON_pack_amount ("credit_amount",
     306             :                             credit_amount),
     307             :     GNUNET_JSON_pack_data_auto ("wtid",
     308             :                                 wtid),
     309             :     GNUNET_JSON_pack_string ("payto_uri",
     310             :                              payto_uri),
     311             :     GNUNET_JSON_pack_string ("exchange_url",
     312             :                              exchange_url));
     313           0 :   eh = TALER_MERCHANT_curl_easy_get_ (pth->url);
     314           0 :   if (GNUNET_OK !=
     315           0 :       TALER_curl_easy_post (&pth->post_ctx,
     316             :                             eh,
     317             :                             req))
     318             :   {
     319           0 :     GNUNET_break (0);
     320           0 :     curl_easy_cleanup (eh);
     321           0 :     json_decref (req);
     322           0 :     GNUNET_free (pth->url);
     323           0 :     GNUNET_free (pth);
     324           0 :     return NULL;
     325             :   }
     326           0 :   json_decref (req);
     327           0 :   pth->job = GNUNET_CURL_job_add2 (ctx,
     328             :                                    eh,
     329           0 :                                    pth->post_ctx.headers,
     330             :                                    &handle_post_transfers_finished,
     331             :                                    pth);
     332           0 :   return pth;
     333             : }
     334             : 
     335             : 
     336             : void
     337           0 : TALER_MERCHANT_transfers_post_cancel (
     338             :   struct TALER_MERCHANT_PostTransfersHandle *pth)
     339             : {
     340           0 :   if (NULL != pth->job)
     341             :   {
     342           0 :     GNUNET_CURL_job_cancel (pth->job);
     343           0 :     pth->job = NULL;
     344             :   }
     345           0 :   GNUNET_free (pth->url);
     346           0 :   TALER_curl_easy_post_finished (&pth->post_ctx);
     347           0 :   GNUNET_free (pth);
     348           0 : }
     349             : 
     350             : 
     351             : /* end of merchant_api_post_transfers.c */

Generated by: LCOV version 1.14