LCOV - code coverage report
Current view: top level - lib - merchant_api_post_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 87 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) 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,
      11             :   but 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
      16             :   Public License along with TALER; see the file COPYING.LGPL.
      17             :   If not, see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file lib/merchant_api_post_instances.c
      21             :  * @brief Implementation of the POST /instances request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Christian Grothoff
      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 "taler_merchant_service.h"
      31             : #include <taler/taler_json_lib.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a POST /instances/$ID operation.
      37             :  */
      38             : struct TALER_MERCHANT_InstancesPostHandle
      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_InstancesPostCallback 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             :    * Minor context that holds body and headers.
      68             :    */
      69             :   struct TALER_CURL_PostContext post_ctx;
      70             : 
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Function called when we're done processing the
      76             :  * HTTP POST /instances request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_InstancesPostHandle`
      79             :  * @param response_code HTTP response code, 0 on error
      80             :  * @param json response body, NULL if not in JSON
      81             :  */
      82             : static void
      83           0 : handle_post_instances_finished (void *cls,
      84             :                                 long response_code,
      85             :                                 const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_InstancesPostHandle *iph = cls;
      88           0 :   const json_t *json = response;
      89           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      90           0 :     .http_status = (unsigned int) response_code,
      91             :     .reply = json
      92             :   };
      93             : 
      94           0 :   iph->job = NULL;
      95           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      96             :               "POST /instances completed with response code %u\n",
      97             :               (unsigned int) response_code);
      98           0 :   switch (response_code)
      99             :   {
     100           0 :   case 0:
     101           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     102           0 :     break;
     103           0 :   case MHD_HTTP_NO_CONTENT:
     104           0 :     break;
     105           0 :   case MHD_HTTP_BAD_REQUEST:
     106           0 :     hr.ec = TALER_JSON_get_error_code (json);
     107           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     108             :     /* This should never happen, either us
     109             :      * or the merchant is buggy (or API version conflict);
     110             :      * just pass JSON reply to the application */
     111           0 :     break;
     112           0 :   case MHD_HTTP_FORBIDDEN:
     113           0 :     hr.ec = TALER_JSON_get_error_code (json);
     114           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     115             :     /* Nothing really to verify, merchant says we tried to abort the payment
     116             :      * after it was successful. We should pass the JSON reply to the
     117             :      * application */
     118           0 :     break;
     119           0 :   case MHD_HTTP_NOT_FOUND:
     120           0 :     hr.ec = TALER_JSON_get_error_code (json);
     121           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     122             :     /* Nothing really to verify, this should never
     123             :        happen, we should pass the JSON reply to the
     124             :        application */
     125           0 :     break;
     126           0 :   case MHD_HTTP_CONFLICT:
     127           0 :     hr.ec = TALER_JSON_get_error_code (json);
     128           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     129           0 :     break;
     130           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     131           0 :     hr.ec = TALER_JSON_get_error_code (json);
     132           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     133             :     /* Server had an internal issue; we should retry,
     134             :        but this API leaves this to the application */
     135           0 :     break;
     136           0 :   default:
     137           0 :     TALER_MERCHANT_parse_error_details_ (json,
     138             :                                          response_code,
     139             :                                          &hr);
     140             :     /* unexpected response code */
     141           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     142             :                 "Unexpected response code %u/%d\n",
     143             :                 (unsigned int) response_code,
     144             :                 (int) hr.ec);
     145           0 :     GNUNET_break_op (0);
     146           0 :     break;
     147             :   }
     148           0 :   iph->cb (iph->cb_cls,
     149             :            &hr);
     150           0 :   TALER_MERCHANT_instances_post_cancel (iph);
     151           0 : }
     152             : 
     153             : 
     154             : /**
     155             :  * Setup an new instance in the backend.
     156             :  *
     157             :  * @param ctx the context
     158             :  * @param backend_url HTTP base URL for the backend
     159             :  * @param instance_id identity of the instance to get information about
     160             :  * @param payto_uris_length length of the @a accounts array
     161             :  * @param payto_uris URIs of the bank accounts of the merchant instance
     162             :  * @param name name of the merchant instance
     163             :  * @param address physical address of the merchant instance
     164             :  * @param jurisdiction jurisdiction of the merchant instance
     165             :  * @param default_max_wire_fee default maximum wire fee merchant is willing to fully pay
     166             :  * @param default_wire_fee_amortization default amortization factor for excess wire fees
     167             :  * @param default_max_deposit_fee default maximum deposit fee merchant is willing to pay
     168             :  * @param default_wire_transfer_delay default wire transfer delay merchant will ask for
     169             :  * @param default_pay_delay default validity period for offers merchant makes
     170             :  * @param cb function to call with the
     171             :  *        backend's instances information
     172             :  * @param cb_cls closure for @a config_cb
     173             :  * @return the instances handle; NULL upon error
     174             :  */
     175             : struct TALER_MERCHANT_InstancesPostHandle *
     176           0 : TALER_MERCHANT_instances_post (
     177             :   struct GNUNET_CURL_Context *ctx,
     178             :   const char *backend_url,
     179             :   const char *instance_id,
     180             :   unsigned int accounts_length,
     181             :   const char *payto_uris[],
     182             :   const char *name,
     183             :   const json_t *address,
     184             :   const json_t *jurisdiction,
     185             :   const struct TALER_Amount *default_max_wire_fee,
     186             :   uint32_t default_wire_fee_amortization,
     187             :   const struct TALER_Amount *default_max_deposit_fee,
     188             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     189             :   struct GNUNET_TIME_Relative default_pay_delay,
     190             :   TALER_MERCHANT_InstancesPostCallback cb,
     191             :   void *cb_cls)
     192             : {
     193             :   struct TALER_MERCHANT_InstancesPostHandle *iph;
     194             :   json_t *jpayto_uris;
     195             :   json_t *req_obj;
     196             : 
     197           0 :   jpayto_uris = json_array ();
     198           0 :   if (NULL == jpayto_uris)
     199             :   {
     200           0 :     GNUNET_break (0);
     201           0 :     return NULL;
     202             :   }
     203           0 :   for (unsigned int i = 0; i<accounts_length; i++)
     204             :   {
     205           0 :     if (0 !=
     206           0 :         json_array_append_new (jpayto_uris,
     207           0 :                                json_string (payto_uris[i])))
     208             :     {
     209           0 :       GNUNET_break (0);
     210           0 :       json_decref (jpayto_uris);
     211           0 :       return NULL;
     212             :     }
     213             :   }
     214           0 :   req_obj = json_pack ("{s:o, s:s, s:s, s:O, s:O"
     215             :                        " s:o, s:I: s:o, s:o, s:o}",
     216             :                        "payto_uris",
     217             :                        jpayto_uris,
     218             :                        "id",
     219             :                        instance_id,
     220             :                        "name",
     221             :                        name,
     222             :                        "address",
     223             :                        address,
     224             :                        "jurisdiction",
     225             :                        jurisdiction,
     226             :                        /* end of group of 5 */
     227             :                        "default_max_wire_fee",
     228             :                        TALER_JSON_from_amount (default_max_wire_fee),
     229             :                        "default_wire_fee_amortization",
     230             :                        (json_int_t) default_wire_fee_amortization,
     231             :                        "default_max_deposit_fee",
     232             :                        TALER_JSON_from_amount (default_max_deposit_fee),
     233             :                        "default_wire_transfer_delay",
     234             :                        GNUNET_JSON_from_time_rel (default_wire_transfer_delay),
     235             :                        "default_pay_delay",
     236             :                        GNUNET_JSON_from_time_rel (default_pay_delay));
     237           0 :   if (NULL == req_obj)
     238             :   {
     239           0 :     GNUNET_break (0);
     240           0 :     return NULL;
     241             :   }
     242           0 :   iph = GNUNET_new (struct TALER_MERCHANT_InstancesPostHandle);
     243           0 :   iph->ctx = ctx;
     244           0 :   iph->cb = cb;
     245           0 :   iph->cb_cls = cb_cls;
     246           0 :   iph->url = TALER_url_join (backend_url,
     247             :                              "private/instances",
     248             :                              NULL);
     249           0 :   if (NULL == iph->url)
     250             :   {
     251           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     252             :                 "Could not construct request URL.\n");
     253           0 :     json_decref (req_obj);
     254           0 :     GNUNET_free (iph);
     255           0 :     return NULL;
     256             :   }
     257             :   {
     258             :     CURL *eh;
     259             : 
     260           0 :     eh = curl_easy_init ();
     261           0 :     if (GNUNET_OK !=
     262           0 :         TALER_curl_easy_post (&iph->post_ctx,
     263             :                               eh,
     264             :                               req_obj))
     265             :     {
     266           0 :       GNUNET_break (0);
     267           0 :       json_decref (req_obj);
     268           0 :       GNUNET_free (iph);
     269           0 :       return NULL;
     270             :     }
     271             : 
     272           0 :     json_decref (req_obj);
     273           0 :     GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     274             :                                                  CURLOPT_URL,
     275             :                                                  iph->url));
     276           0 :     iph->job = GNUNET_CURL_job_add2 (ctx,
     277             :                                      eh,
     278           0 :                                      iph->post_ctx.headers,
     279             :                                      &handle_post_instances_finished,
     280             :                                      iph);
     281             :   }
     282           0 :   return iph;
     283             : }
     284             : 
     285             : 
     286             : /**
     287             :  * Cancel /instances request.  Must not be called by clients after
     288             :  * the callback was invoked.
     289             :  *
     290             :  * @param igh request to cancel.
     291             :  */
     292             : void
     293           0 : TALER_MERCHANT_instances_post_cancel (
     294             :   struct TALER_MERCHANT_InstancesPostHandle *iph)
     295             : {
     296           0 :   if (NULL != iph->job)
     297             :   {
     298           0 :     GNUNET_CURL_job_cancel (iph->job);
     299           0 :     iph->job = NULL;
     300             :   }
     301           0 :   TALER_curl_easy_post_finished (&iph->post_ctx);
     302           0 :   GNUNET_free (iph->url);
     303           0 :   GNUNET_free (iph);
     304           0 : }
     305             : 
     306             : 
     307             : /* end of merchant_api_post_instances.c */

Generated by: LCOV version 1.14