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

Generated by: LCOV version 1.14