LCOV - code coverage report
Current view: top level - lib - merchant_api_post_order_paid.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 78 0.0 %
Date: 2022-06-30 06:15:34 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-2021 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 merchant_api_post_order_paid.c
      21             :  * @brief Implementation of the POST /order/$ID/paid request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Jonathan Buchanan
      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 "merchant_api_curl_defaults.h"
      33             : #include <taler/taler_json_lib.h>
      34             : #include <taler/taler_signatures.h>
      35             : #include <taler/taler_exchange_service.h>
      36             : #include <taler/taler_curl_lib.h>
      37             : 
      38             : 
      39             : /**
      40             :  * @brief Handle to a POST /orders/$ID/paid operation at a merchant.
      41             :  */
      42             : struct TALER_MERCHANT_OrderPaidHandle
      43             : {
      44             : 
      45             :   /**
      46             :    * The url for this request.
      47             :    */
      48             :   char *url;
      49             : 
      50             :   /**
      51             :    * Handle for the request.
      52             :    */
      53             :   struct GNUNET_CURL_Job *job;
      54             : 
      55             :   /**
      56             :    * Function to call with the result.
      57             :    */
      58             :   TALER_MERCHANT_OrderPaidCallback paid_cb;
      59             : 
      60             :   /**
      61             :    * Closure for @a paid_cb.
      62             :    */
      63             :   void *paid_cb_cls;
      64             : 
      65             :   /**
      66             :    * Reference to the execution context.
      67             :    */
      68             :   struct GNUNET_CURL_Context *ctx;
      69             : 
      70             :   /**
      71             :    * Minor context that holds body and headers.
      72             :    */
      73             :   struct TALER_CURL_PostContext post_ctx;
      74             : };
      75             : 
      76             : 
      77             : /**
      78             :  * Function called when we're done processing the
      79             :  * HTTP /paid request.
      80             :  *
      81             :  * @param cls the `struct TALER_MERCHANT_OrderPaidHandle`
      82             :  * @param response_code HTTP response code, 0 on error
      83             :  * @param response response body, NULL if not in JSON
      84             :  */
      85             : static void
      86           0 : handle_paid_finished (void *cls,
      87             :                       long response_code,
      88             :                       const void *response)
      89             : {
      90           0 :   struct TALER_MERCHANT_OrderPaidHandle *oph = cls;
      91           0 :   const json_t *json = response;
      92           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      93           0 :     .http_status = (unsigned int) response_code,
      94             :     .reply = json
      95             :   };
      96             : 
      97           0 :   oph->job = NULL;
      98           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      99             :               "/paid completed with response code %u\n",
     100             :               (unsigned int) response_code);
     101           0 :   switch (response_code)
     102             :   {
     103           0 :   case 0:
     104           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     105           0 :     break;
     106           0 :   case MHD_HTTP_NO_CONTENT:
     107           0 :     break;
     108           0 :   case MHD_HTTP_BAD_REQUEST:
     109           0 :     hr.ec = TALER_JSON_get_error_code (json);
     110           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     111             :     /* This should never happen, either us
     112             :      * or the merchant is buggy (or API version conflict);
     113             :      * just pass JSON reply to the application */
     114           0 :     break;
     115           0 :   case MHD_HTTP_FORBIDDEN:
     116           0 :     hr.ec = TALER_JSON_get_error_code (json);
     117           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     118             :     /* The signature provided was invalid */
     119           0 :     break;
     120           0 :   case MHD_HTTP_NOT_FOUND:
     121           0 :     hr.ec = TALER_JSON_get_error_code (json);
     122           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     123             :     /* Nothing really to verify, this should never
     124             :        happen, we should pass the JSON reply to the
     125             :        application */
     126           0 :     break;
     127           0 :   case MHD_HTTP_CONFLICT:
     128           0 :     hr.ec = TALER_JSON_get_error_code (json);
     129           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     130             :     /* The hashed contract terms don't match with the order_id. */
     131           0 :     break;
     132           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     133           0 :     hr.ec = TALER_JSON_get_error_code (json);
     134           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     135             :     /* Server had an internal issue; we should retry,
     136             :        but this API leaves this to the application */
     137           0 :     break;
     138           0 :   case MHD_HTTP_SERVICE_UNAVAILABLE:
     139           0 :     TALER_MERCHANT_parse_error_details_ (json,
     140             :                                          response_code,
     141             :                                          &hr);
     142             :     /* Exchange couldn't respond properly; the retry is
     143             :        left to the application */
     144           0 :     break;
     145           0 :   default:
     146           0 :     TALER_MERCHANT_parse_error_details_ (json,
     147             :                                          response_code,
     148             :                                          &hr);
     149             :     /* unexpected response code */
     150           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     151             :                 "Unexpected response code %u/%d\n",
     152             :                 (unsigned int) response_code,
     153             :                 (int) hr.ec);
     154           0 :     GNUNET_break_op (0);
     155           0 :     break;
     156             :   }
     157           0 :   oph->paid_cb (oph->paid_cb_cls,
     158             :                 &hr);
     159           0 :   TALER_MERCHANT_order_paid_cancel (oph);
     160           0 : }
     161             : 
     162             : 
     163             : struct TALER_MERCHANT_OrderPaidHandle *
     164           0 : TALER_MERCHANT_order_paid (
     165             :   struct GNUNET_CURL_Context *ctx,
     166             :   const char *merchant_url,
     167             :   const char *order_id,
     168             :   const char *session_id,
     169             :   const struct TALER_PrivateContractHashP *h_contract_terms,
     170             :   const struct TALER_MerchantSignatureP *merchant_sig,
     171             :   TALER_MERCHANT_OrderPaidCallback paid_cb,
     172             :   void *paid_cb_cls)
     173             : {
     174             :   struct TALER_MERCHANT_OrderPaidHandle *oph;
     175             :   json_t *req_obj;
     176             : 
     177           0 :   req_obj = GNUNET_JSON_PACK (
     178             :     GNUNET_JSON_pack_data_auto ("sig",
     179             :                                 merchant_sig),
     180             :     GNUNET_JSON_pack_data_auto ("h_contract",
     181             :                                 h_contract_terms),
     182             :     GNUNET_JSON_pack_string ("session_id",
     183             :                              session_id));
     184           0 :   oph = GNUNET_new (struct TALER_MERCHANT_OrderPaidHandle);
     185           0 :   oph->ctx = ctx;
     186           0 :   oph->paid_cb = paid_cb;
     187           0 :   oph->paid_cb_cls = paid_cb_cls;
     188             :   {
     189             :     char *path;
     190             : 
     191           0 :     GNUNET_asprintf (&path,
     192             :                      "orders/%s/paid",
     193             :                      order_id);
     194           0 :     oph->url = TALER_url_join (merchant_url,
     195             :                                path,
     196             :                                NULL);
     197           0 :     GNUNET_free (path);
     198             :   }
     199           0 :   if (NULL == oph->url)
     200             :   {
     201           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     202             :                 "Could not construct request URL.\n");
     203           0 :     json_decref (req_obj);
     204           0 :     GNUNET_free (oph);
     205           0 :     return NULL;
     206             :   }
     207             :   {
     208             :     CURL *eh;
     209             : 
     210           0 :     eh = TALER_MERCHANT_curl_easy_get_ (oph->url);
     211           0 :     if (GNUNET_OK !=
     212           0 :         TALER_curl_easy_post (&oph->post_ctx,
     213             :                               eh,
     214             :                               req_obj))
     215             :     {
     216           0 :       GNUNET_break (0);
     217           0 :       curl_easy_cleanup (eh);
     218           0 :       json_decref (req_obj);
     219           0 :       GNUNET_free (oph);
     220           0 :       return NULL;
     221             :     }
     222           0 :     json_decref (req_obj);
     223           0 :     oph->job = GNUNET_CURL_job_add2 (ctx,
     224             :                                      eh,
     225           0 :                                      oph->post_ctx.headers,
     226             :                                      &handle_paid_finished,
     227             :                                      oph);
     228             :   }
     229           0 :   return oph;
     230             : }
     231             : 
     232             : 
     233             : void
     234           0 : TALER_MERCHANT_order_paid_cancel (
     235             :   struct TALER_MERCHANT_OrderPaidHandle *oph)
     236             : {
     237           0 :   if (NULL != oph->job)
     238             :   {
     239           0 :     GNUNET_CURL_job_cancel (oph->job);
     240           0 :     oph->job = NULL;
     241             :   }
     242           0 :   TALER_curl_easy_post_finished (&oph->post_ctx);
     243           0 :   GNUNET_free (oph->url);
     244           0 :   GNUNET_free (oph);
     245           0 : }
     246             : 
     247             : 
     248             : /* end of merchant_api_post_order_paid.c */

Generated by: LCOV version 1.14