LCOV - code coverage report
Current view: top level - lib - merchant_api_wallet_get_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 98 0.0 %
Date: 2021-04-12 06:04:58 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018, 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 merchant_api_wallet_get_order.c
      19             :  * @brief Implementation of the GET /orders/$ID request
      20             :  * @author Christian Grothoff
      21             :  * @author Marcello Stanisci
      22             :  * @author Florian Dold
      23             :  */
      24             : #include "platform.h"
      25             : #include <curl/curl.h>
      26             : #include <jansson.h>
      27             : #include <microhttpd.h> /* just for HTTP status codes */
      28             : #include <gnunet/gnunet_util_lib.h>
      29             : #include <gnunet/gnunet_curl_lib.h>
      30             : #include "taler_merchant_service.h"
      31             : #include <taler/taler_json_lib.h>
      32             : #include <taler/taler_signatures.h>
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A GET /orders/$ID handle
      37             :  */
      38             : struct TALER_MERCHANT_OrderWalletGetHandle
      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_OrderWalletGetCallback 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             : 
      68             : /**
      69             :  * Convenience function to call the callback in @a owgh with an error code of
      70             :  * @a ec and the exchange body being set to @a reply.
      71             :  *
      72             :  * @param owgh handle providing callback
      73             :  * @param ec error code to return to application
      74             :  * @param reply JSON reply we got from the exchange, can be NULL
      75             :  */
      76             : static void
      77           0 : cb_failure (struct TALER_MERCHANT_OrderWalletGetHandle *owgh,
      78             :             enum TALER_ErrorCode ec,
      79             :             const json_t *reply)
      80             : {
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82             :     .ec = ec,
      83             :     .reply = reply
      84             :   };
      85             : 
      86           0 :   owgh->cb (owgh->cb_cls,
      87             :             &hr,
      88             :             GNUNET_SYSERR,
      89             :             GNUNET_SYSERR,
      90             :             GNUNET_SYSERR,
      91             :             NULL,
      92             :             NULL,
      93             :             NULL);
      94           0 : }
      95             : 
      96             : 
      97             : /**
      98             :  * Function called when we're done processing the GET /check-payment request.
      99             :  *
     100             :  * @param cls the `struct TALER_MERCHANT_OrderWalletGetHandle`
     101             :  * @param response_code HTTP response code, 0 on error
     102             :  * @param response response body, should be NULL
     103             :  */
     104             : static void
     105           0 : handle_wallet_get_order_finished (void *cls,
     106             :                                   long response_code,
     107             :                                   const void *response)
     108             : {
     109           0 :   struct TALER_MERCHANT_OrderWalletGetHandle *owgh = cls;
     110           0 :   const json_t *json = response;
     111             : 
     112           0 :   owgh->job = NULL;
     113           0 :   switch (response_code)
     114             :   {
     115           0 :   case MHD_HTTP_OK:
     116             :     {
     117             :       struct TALER_Amount refund_amount;
     118             :       bool refunded;
     119             :       bool refund_pending;
     120             :       struct GNUNET_JSON_Specification spec[] = {
     121           0 :         GNUNET_JSON_spec_bool ("refunded",
     122             :                                &refunded),
     123           0 :         GNUNET_JSON_spec_bool ("refund_pending",
     124             :                                &refund_pending),
     125           0 :         TALER_JSON_spec_amount ("refund_amount",
     126             :                                 &refund_amount),
     127           0 :         GNUNET_JSON_spec_end ()
     128             :       };
     129           0 :       struct TALER_MERCHANT_HttpResponse hr = {
     130             :         .reply = json,
     131             :         .http_status = MHD_HTTP_OK
     132             :       };
     133             : 
     134           0 :       if (GNUNET_OK !=
     135           0 :           GNUNET_JSON_parse (json,
     136             :                              spec,
     137             :                              NULL, NULL))
     138             :       {
     139           0 :         GNUNET_break_op (0);
     140           0 :         cb_failure (owgh,
     141             :                     TALER_EC_GENERIC_REPLY_MALFORMED,
     142             :                     json);
     143           0 :         TALER_MERCHANT_wallet_order_get_cancel (owgh);
     144           0 :         return;
     145             :       }
     146             : 
     147           0 :       owgh->cb (owgh->cb_cls,
     148             :                 &hr,
     149             :                 GNUNET_YES,
     150             :                 refunded ? GNUNET_YES : GNUNET_NO,
     151             :                 refund_pending ? GNUNET_YES : GNUNET_NO,
     152           0 :                 refunded ? &refund_amount : NULL,
     153             :                 NULL, /* paid! */
     154             :                 NULL);/* paid! */
     155           0 :       GNUNET_JSON_parse_free (spec);
     156           0 :       break;
     157             :     }
     158           0 :   case MHD_HTTP_PAYMENT_REQUIRED:
     159             :     {
     160             :       /* Status is: unpaid */
     161           0 :       const char *taler_pay_uri = json_string_value (json_object_get (json,
     162             :                                                                       "taler_pay_uri"));
     163           0 :       const char *already_paid = json_string_value (json_object_get (json,
     164             :                                                                      "already_paid_order_id"));
     165           0 :       if (NULL == taler_pay_uri)
     166             :       {
     167           0 :         GNUNET_break_op (0);
     168           0 :         cb_failure (owgh,
     169             :                     TALER_EC_GENERIC_REPLY_MALFORMED,
     170             :                     json);
     171             :       }
     172             :       else
     173             :       {
     174           0 :         struct TALER_MERCHANT_HttpResponse hr = {
     175             :           .reply = json,
     176             :           .http_status = MHD_HTTP_OK
     177             :         };
     178             : 
     179           0 :         owgh->cb (owgh->cb_cls,
     180             :                   &hr,
     181             :                   GNUNET_NO,
     182             :                   GNUNET_NO,
     183             :                   GNUNET_NO,
     184             :                   NULL,
     185             :                   taler_pay_uri,
     186             :                   already_paid);
     187             :       }
     188           0 :       break;
     189             :     }
     190           0 :   default:
     191             :     {
     192             :       struct TALER_MERCHANT_HttpResponse hr;
     193             : 
     194           0 :       TALER_MERCHANT_parse_error_details_ (response,
     195             :                                            response_code,
     196             :                                            &hr);
     197           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     198             :                   "Checking order status failed with HTTP status code %u/%d\n",
     199             :                   (unsigned int) response_code,
     200             :                   (int) hr.ec);
     201           0 :       GNUNET_break_op (0);
     202           0 :       owgh->cb (owgh->cb_cls,
     203             :                 &hr,
     204             :                 GNUNET_SYSERR,
     205             :                 GNUNET_SYSERR,
     206             :                 GNUNET_SYSERR,
     207             :                 NULL,
     208             :                 NULL,
     209             :                 NULL);
     210           0 :       break;
     211             :     }
     212             :   }
     213           0 :   TALER_MERCHANT_wallet_order_get_cancel (owgh);
     214             : }
     215             : 
     216             : 
     217             : struct TALER_MERCHANT_OrderWalletGetHandle *
     218           0 : TALER_MERCHANT_wallet_order_get (struct GNUNET_CURL_Context *ctx,
     219             :                                  const char *backend_url,
     220             :                                  const char *order_id,
     221             :                                  const struct GNUNET_HashCode *h_contract,
     222             :                                  struct GNUNET_TIME_Relative timeout,
     223             :                                  const char *session_id,
     224             :                                  const struct TALER_Amount *min_refund,
     225             :                                  bool await_refund_obtained,
     226             :                                  TALER_MERCHANT_OrderWalletGetCallback cb,
     227             :                                  void *cb_cls)
     228             : {
     229             :   struct TALER_MERCHANT_OrderWalletGetHandle *owgh;
     230             :   unsigned long long tms;
     231             :   long tlong;
     232             : 
     233           0 :   GNUNET_assert (NULL != backend_url);
     234           0 :   GNUNET_assert (NULL != order_id);
     235           0 :   owgh = GNUNET_new (struct TALER_MERCHANT_OrderWalletGetHandle);
     236           0 :   owgh->ctx = ctx;
     237           0 :   owgh->cb = cb;
     238           0 :   owgh->cb_cls = cb_cls;
     239           0 :   tms = (unsigned long long) (timeout.rel_value_us
     240           0 :                               / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
     241             :   /* set curl timeout to *our* long poll timeout plus one minute
     242             :      (for network latency and processing delays) */
     243           0 :   tlong = (long) (GNUNET_TIME_relative_add (timeout,
     244           0 :                                             GNUNET_TIME_UNIT_MINUTES).
     245             :                   rel_value_us
     246           0 :                   / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
     247             :   {
     248             :     char timeout_ms[32];
     249             :     struct GNUNET_CRYPTO_HashAsciiEncoded h_contract_s;
     250             :     char *path;
     251             : 
     252           0 :     GNUNET_CRYPTO_hash_to_enc (h_contract,
     253             :                                &h_contract_s);
     254           0 :     GNUNET_snprintf (timeout_ms,
     255             :                      sizeof (timeout_ms),
     256             :                      "%llu",
     257             :                      tms);
     258           0 :     GNUNET_asprintf (&path,
     259             :                      "orders/%s",
     260             :                      order_id);
     261           0 :     owgh->url = TALER_url_join (backend_url,
     262             :                                 path,
     263             :                                 "h_contract",
     264             :                                 h_contract_s.encoding,
     265             :                                 "session_id",
     266             :                                 session_id,
     267             :                                 (0 != tms)
     268             :                                 ? "timeout_ms"
     269             :                                 : NULL,
     270             :                                 timeout_ms,
     271             :                                 (NULL != min_refund)
     272             :                                 ? "refund"
     273             :                                 : NULL,
     274             :                                 (NULL != min_refund)
     275           0 :                                 ? TALER_amount2s (min_refund)
     276             :                                 : NULL,
     277             :                                 await_refund_obtained
     278             :                                 ? "await_refund_obtained"
     279             :                                 : NULL,
     280             :                                 await_refund_obtained
     281             :                                 ? "yes"
     282             :                                 : NULL,
     283             :                                 NULL);
     284           0 :     GNUNET_free (path);
     285             :   }
     286           0 :   if (NULL == owgh->url)
     287             :   {
     288           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     289             :                 "Could not construct request URL.\n");
     290           0 :     GNUNET_free (owgh);
     291           0 :     return NULL;
     292             :   }
     293             : 
     294             :   {
     295             :     CURL *eh;
     296             : 
     297           0 :     eh = curl_easy_init ();
     298           0 :     if (NULL == eh)
     299             :     {
     300           0 :       GNUNET_break (0);
     301           0 :       GNUNET_free (owgh->url);
     302           0 :       GNUNET_free (owgh);
     303           0 :       return NULL;
     304             :     }
     305           0 :     if (CURLE_OK !=
     306           0 :         curl_easy_setopt (eh,
     307             :                           CURLOPT_URL,
     308             :                           owgh->url))
     309             :     {
     310           0 :       GNUNET_break (0);
     311           0 :       curl_easy_cleanup (eh);
     312           0 :       GNUNET_free (owgh->url);
     313           0 :       GNUNET_free (owgh);
     314           0 :       return NULL;
     315             :     }
     316           0 :     if (CURLE_OK !=
     317           0 :         curl_easy_setopt (eh,
     318             :                           CURLOPT_TIMEOUT_MS,
     319             :                           tlong))
     320             :     {
     321           0 :       GNUNET_break (0);
     322           0 :       curl_easy_cleanup (eh);
     323           0 :       GNUNET_free (owgh->url);
     324           0 :       GNUNET_free (owgh);
     325           0 :       return NULL;
     326             :     }
     327             : 
     328           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     329             :                 "Checking order status at %s\n",
     330             :                 owgh->url);
     331           0 :     if (NULL == (owgh->job =
     332           0 :                    GNUNET_CURL_job_add (ctx,
     333             :                                         eh,
     334             :                                         &handle_wallet_get_order_finished,
     335             :                                         owgh)))
     336             :     {
     337           0 :       GNUNET_break (0);
     338           0 :       GNUNET_free (owgh->url);
     339           0 :       GNUNET_free (owgh);
     340           0 :       return NULL;
     341             :     }
     342             :   }
     343           0 :   return owgh;
     344             : }
     345             : 
     346             : 
     347             : void
     348           0 : TALER_MERCHANT_wallet_order_get_cancel (
     349             :   struct TALER_MERCHANT_OrderWalletGetHandle *owgh)
     350             : {
     351           0 :   if (NULL != owgh->job)
     352             :   {
     353           0 :     GNUNET_CURL_job_cancel (owgh->job);
     354           0 :     owgh->job = NULL;
     355             :   }
     356           0 :   GNUNET_free (owgh->url);
     357           0 :   GNUNET_free (owgh);
     358           0 : }
     359             : 
     360             : 
     361             : /* end of merchant_api_wallet_get_order.c */

Generated by: LCOV version 1.14