LCOV - code coverage report
Current view: top level - lib - merchant_api_merchant_get_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 202 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018, 2019, 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 lib/merchant_api_merchant_get_order.c
      19             :  * @brief Implementation of the GET /private/orders/$ORDER 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 /private/orders/$ORDER handle
      37             :  */
      38             : struct TALER_MERCHANT_OrderMerchantGetHandle
      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_OrderMerchantGetCallback 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             :  * Function called when we're done processing the GET /private/orders/$ORDER
      70             :  * request and we got an HTTP status of OK and the order was unpaid. Parse
      71             :  * the response and call the callback.
      72             :  *
      73             :  * @param omgh handle for the request
      74             :  * @param[in,out] hr HTTP response we got
      75             :  */
      76             : static void
      77           0 : handle_unpaid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
      78             :                struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80             :   const char *taler_pay_uri
      81           0 :     = json_string_value (json_object_get (hr->reply,
      82             :                                           "taler_pay_uri"));
      83             :   const char *already_paid_order_id
      84           0 :     = json_string_value (json_object_get (hr->reply,
      85             :                                           "already_paid_order_id"));
      86           0 :   if (NULL == taler_pay_uri)
      87             :   {
      88           0 :     GNUNET_break_op (0);
      89           0 :     hr->http_status = 0;
      90           0 :     hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
      91           0 :     omgh->cb (omgh->cb_cls,
      92             :               hr,
      93             :               NULL);
      94           0 :     return;
      95             :   }
      96             :   {
      97           0 :     struct TALER_MERCHANT_OrderStatusResponse osr = {
      98             :       .paid = false,
      99             :       .details.unpaid.taler_pay_uri = taler_pay_uri,
     100             :       .details.unpaid.already_paid_order_id = already_paid_order_id
     101             :     };
     102             : 
     103           0 :     omgh->cb (omgh->cb_cls,
     104             :               hr,
     105             :               &osr);
     106             :   }
     107             : }
     108             : 
     109             : 
     110             : /**
     111             :  * Function called when we're done processing the GET /private/orders/$ORDER
     112             :  * request and we got an HTTP status of OK and the order was paid. Parse
     113             :  * the response and call the callback.
     114             :  *
     115             :  * @param omgh handle for the request
     116             :  * @param[in,out] hr HTTP response we got
     117             :  */
     118             : static void
     119           0 : handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
     120             :              struct TALER_MERCHANT_HttpResponse *hr)
     121             : {
     122             :   uint32_t ec32;
     123             :   uint32_t hc32;
     124             :   json_t *wire_details;
     125             :   json_t *wire_reports;
     126             :   json_t *refund_details;
     127           0 :   struct TALER_MERCHANT_OrderStatusResponse osr = {
     128             :     .paid = true
     129             :   };
     130             :   struct GNUNET_JSON_Specification spec[] = {
     131           0 :     GNUNET_JSON_spec_bool ("refunded",
     132             :                            &osr.details.paid.refunded),
     133           0 :     GNUNET_JSON_spec_bool ("wired",
     134             :                            &osr.details.paid.wired),
     135           0 :     TALER_JSON_spec_amount ("deposit_total",
     136             :                             &osr.details.paid.deposit_total),
     137           0 :     GNUNET_JSON_spec_uint32 ("exchange_ec",
     138             :                              &ec32),
     139           0 :     GNUNET_JSON_spec_uint32 ("exchange_hc",
     140             :                              &hc32),
     141           0 :     TALER_JSON_spec_amount ("refund_amount",
     142             :                             &osr.details.paid.refund_amount),
     143           0 :     GNUNET_JSON_spec_json ("contract_terms",
     144             :                            (json_t **) &osr.details.paid.contract_terms),
     145           0 :     GNUNET_JSON_spec_json ("wire_details",
     146             :                            &wire_details),
     147           0 :     GNUNET_JSON_spec_json ("wire_reports",
     148             :                            &wire_reports),
     149           0 :     GNUNET_JSON_spec_json ("refund_details",
     150             :                            &refund_details),
     151           0 :     GNUNET_JSON_spec_end ()
     152             :   };
     153             : 
     154           0 :   if (GNUNET_OK !=
     155           0 :       GNUNET_JSON_parse (hr->reply,
     156             :                          spec,
     157             :                          NULL, NULL))
     158             :   {
     159           0 :     GNUNET_break_op (0);
     160           0 :     hr->http_status = 0;
     161           0 :     hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     162           0 :     omgh->cb (omgh->cb_cls,
     163             :               hr,
     164             :               NULL);
     165           0 :     return;
     166             :   }
     167           0 :   if (! (json_is_array (wire_details) &&
     168           0 :          json_is_array (wire_reports) &&
     169           0 :          json_is_array (refund_details) &&
     170           0 :          json_is_object (osr.details.paid.contract_terms)) )
     171             :   {
     172           0 :     GNUNET_break_op (0);
     173           0 :     hr->http_status = 0;
     174           0 :     hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     175           0 :     omgh->cb (omgh->cb_cls,
     176             :               hr,
     177             :               NULL);
     178           0 :     GNUNET_JSON_parse_free (spec);
     179           0 :     return;
     180             :   }
     181           0 :   osr.details.paid.exchange_ec = (enum TALER_ErrorCode) ec32;
     182           0 :   osr.details.paid.exchange_hc = (unsigned int) hc32;
     183           0 :   {
     184           0 :     unsigned int wts_len = json_array_size (wire_details);
     185           0 :     unsigned int wrs_len = json_array_size (wire_reports);
     186           0 :     unsigned int ref_len = json_array_size (refund_details);
     187           0 :     struct TALER_MERCHANT_WireTransfer wts[wts_len];
     188           0 :     struct TALER_MERCHANT_WireReport wrs[wrs_len];
     189           0 :     struct TALER_MERCHANT_RefundOrderDetail ref[ref_len];
     190             : 
     191           0 :     for (unsigned int i = 0; i<wts_len; i++)
     192             :     {
     193           0 :       struct TALER_MERCHANT_WireTransfer *wt = &wts[i];
     194           0 :       const json_t *w = json_array_get (wire_details,
     195             :                                         i);
     196             :       struct GNUNET_JSON_Specification ispec[] = {
     197           0 :         GNUNET_JSON_spec_string ("exchange_url",
     198             :                                  &wt->exchange_url),
     199           0 :         GNUNET_JSON_spec_fixed_auto ("wtid",
     200             :                                      &wt->wtid),
     201           0 :         TALER_JSON_spec_absolute_time ("execution_time",
     202             :                                        &wt->execution_time),
     203           0 :         TALER_JSON_spec_amount ("amount",
     204             :                                 &wt->total_amount),
     205           0 :         GNUNET_JSON_spec_bool ("confirmed",
     206             :                                &wt->confirmed),
     207           0 :         GNUNET_JSON_spec_end ()
     208             :       };
     209             : 
     210           0 :       if (GNUNET_OK !=
     211           0 :           GNUNET_JSON_parse (w,
     212             :                              ispec,
     213             :                              NULL, NULL))
     214             :       {
     215           0 :         GNUNET_break_op (0);
     216           0 :         hr->http_status = 0;
     217           0 :         hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     218           0 :         omgh->cb (omgh->cb_cls,
     219             :                   hr,
     220             :                   NULL);
     221           0 :         GNUNET_JSON_parse_free (spec);
     222           0 :         return;
     223             :       }
     224             :     }
     225             : 
     226           0 :     for (unsigned int i = 0; i<wrs_len; i++)
     227             :     {
     228           0 :       struct TALER_MERCHANT_WireReport *wr = &wrs[i];
     229           0 :       const json_t *w = json_array_get (wire_reports, i);
     230             :       uint32_t c32;
     231             :       uint32_t eec32;
     232             :       uint32_t ehs32;
     233             :       struct GNUNET_JSON_Specification ispec[] = {
     234           0 :         GNUNET_JSON_spec_uint32 ("code",
     235             :                                  &c32),
     236           0 :         GNUNET_JSON_spec_string ("hint",
     237             :                                  &wr->hint),
     238           0 :         GNUNET_JSON_spec_uint32 ("exchange_ec",
     239             :                                  &eec32),
     240           0 :         GNUNET_JSON_spec_uint32 ("exchange_hc",
     241             :                                  &ehs32),
     242           0 :         GNUNET_JSON_spec_fixed_auto ("coin_pub",
     243             :                                      &wr->coin_pub),
     244           0 :         GNUNET_JSON_spec_end ()
     245             :       };
     246             : 
     247           0 :       if (GNUNET_OK !=
     248           0 :           GNUNET_JSON_parse (w,
     249             :                              ispec,
     250             :                              NULL, NULL))
     251             :       {
     252           0 :         GNUNET_break_op (0);
     253           0 :         hr->http_status = 0;
     254           0 :         hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     255           0 :         omgh->cb (omgh->cb_cls,
     256             :                   hr,
     257             :                   NULL);
     258           0 :         GNUNET_JSON_parse_free (spec);
     259           0 :         return;
     260             :       }
     261           0 :       wr->code = (enum TALER_ErrorCode) c32;
     262           0 :       wr->hr.ec = (enum TALER_ErrorCode) eec32;
     263           0 :       wr->hr.http_status = (unsigned int) ehs32;
     264             :     }
     265             : 
     266           0 :     for (unsigned int i = 0; i<ref_len; i++)
     267             :     {
     268           0 :       struct TALER_MERCHANT_RefundOrderDetail *ro = &ref[i];
     269           0 :       const json_t *w = json_array_get (refund_details,
     270             :                                         i);
     271             :       struct GNUNET_JSON_Specification ispec[] = {
     272           0 :         TALER_JSON_spec_amount ("amount",
     273             :                                 &ro->refund_amount),
     274           0 :         GNUNET_JSON_spec_string ("reason",
     275             :                                  &ro->reason),
     276           0 :         TALER_JSON_spec_absolute_time ("timestamp",
     277             :                                        &ro->refund_time),
     278           0 :         GNUNET_JSON_spec_end ()
     279             :       };
     280             : 
     281           0 :       if (GNUNET_OK !=
     282           0 :           GNUNET_JSON_parse (w,
     283             :                              ispec,
     284             :                              NULL, NULL))
     285             :       {
     286           0 :         GNUNET_break_op (0);
     287           0 :         hr->http_status = 0;
     288           0 :         hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     289           0 :         omgh->cb (omgh->cb_cls,
     290             :                   hr,
     291             :                   NULL);
     292           0 :         GNUNET_JSON_parse_free (spec);
     293           0 :         return;
     294             :       }
     295             :     }
     296             : 
     297           0 :     osr.details.paid.wts = wts;
     298           0 :     osr.details.paid.wts_len = wts_len;
     299           0 :     osr.details.paid.wrs = wrs;
     300           0 :     osr.details.paid.wrs_len = wrs_len;
     301           0 :     osr.details.paid.refunds = ref;
     302           0 :     osr.details.paid.refunds_len = ref_len;
     303           0 :     omgh->cb (omgh->cb_cls,
     304             :               hr,
     305             :               &osr);
     306             :   }
     307           0 :   GNUNET_JSON_parse_free (spec);
     308             : }
     309             : 
     310             : 
     311             : /**
     312             :  * Function called when we're done processing the GET /private/orders/$ORDER
     313             :  * request.
     314             :  *
     315             :  * @param cls the `struct TALER_MERCHANT_OrderMerchantGetHandle`
     316             :  * @param response_code HTTP response code, 0 on error
     317             :  * @param json response body, should be NULL
     318             :  */
     319             : static void
     320           0 : handle_merchant_order_get_finished (void *cls,
     321             :                                     long response_code,
     322             :                                     const void *response)
     323             : {
     324           0 :   struct TALER_MERCHANT_OrderMerchantGetHandle *omgh = cls;
     325           0 :   const json_t *json = response;
     326             :   const char *order_status;
     327           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     328           0 :     .http_status = (unsigned int) response_code,
     329             :     .reply = json
     330             :   };
     331             : 
     332           0 :   omgh->job = NULL;
     333           0 :   switch (response_code)
     334             :   {
     335           0 :   case MHD_HTTP_NOT_FOUND:
     336           0 :     hr.ec = TALER_JSON_get_error_code (json);
     337           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     338           0 :     omgh->cb (omgh->cb_cls,
     339             :               &hr,
     340             :               NULL);
     341           0 :     TALER_MERCHANT_merchant_order_get_cancel (omgh);
     342           0 :     return;
     343           0 :   case MHD_HTTP_OK:
     344             :     /* see below */
     345           0 :     break;
     346           0 :   default:
     347           0 :     hr.ec = TALER_JSON_get_error_code (json);
     348           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     349           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     350             :                 "Polling payment failed with HTTP status code %u/%d\n",
     351             :                 (unsigned int) response_code,
     352             :                 (int) hr.ec);
     353           0 :     GNUNET_break_op (0);
     354           0 :     omgh->cb (omgh->cb_cls,
     355             :               &hr,
     356             :               NULL);
     357           0 :     TALER_MERCHANT_merchant_order_get_cancel (omgh);
     358           0 :     return;
     359             :   }
     360             : 
     361           0 :   order_status = json_string_value (json_object_get (json, "order_status"));
     362             : 
     363           0 :   if (NULL == order_status)
     364             :   {
     365           0 :     GNUNET_break_op (0);
     366           0 :     hr.http_status = 0;
     367           0 :     hr.ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     368           0 :     omgh->cb (omgh->cb_cls,
     369             :               &hr,
     370             :               NULL);
     371           0 :     TALER_MERCHANT_merchant_order_get_cancel (omgh);
     372           0 :     return;
     373             :   }
     374             : 
     375           0 :   if (0 == strcmp ("paid", order_status))
     376             :   {
     377           0 :     handle_paid (omgh,
     378             :                  &hr);
     379             :   }
     380           0 :   else if (0 == strcmp ("unpaid", order_status))
     381             :   {
     382           0 :     handle_unpaid (omgh,
     383             :                    &hr);
     384             :   }
     385             :   else
     386             :   {
     387           0 :     GNUNET_break_op (0);
     388           0 :     hr.http_status = 0;
     389           0 :     hr.ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
     390           0 :     omgh->cb (omgh->cb_cls,
     391             :               &hr,
     392             :               NULL);
     393           0 :     TALER_MERCHANT_merchant_order_get_cancel (omgh);
     394             :   }
     395             : }
     396             : 
     397             : 
     398             : /**
     399             :  * Checks the status of a payment.  Issue a GET /private/orders/$ID request to
     400             :  * the backend.
     401             :  *
     402             :  * @param ctx execution context
     403             :  * @param backend_url base URL of the merchant backend
     404             :  * @param order_id order id to identify the payment
     405             :  * @param session_id sesion id for the payment (or NULL if the check is not
     406             :  *                   bound to a session)
     407             :  * @param transfer if true, obtain the wire transfer status from the exhcange.
     408             :  *               Otherwise, the wire transfer status MAY be returned if it is available.
     409             :  * @param timeout timeout to use in long polling (how long may the server wait to reply
     410             :  *        before generating an unpaid response). Note that this is just provided to
     411             :  *        the server, we as client will block until the response comes back or until
     412             :  *        #TALER_MERCHANT_order_get_cancel() is called.
     413             :  * @param cb callback which will work the response gotten from the backend
     414             :  * @param cb_cls closure to pass to @a cb
     415             :  * @return handle for this operation, NULL upon errors
     416             :  */
     417             : struct TALER_MERCHANT_OrderMerchantGetHandle *
     418           0 : TALER_MERCHANT_merchant_order_get (struct GNUNET_CURL_Context *ctx,
     419             :                                    const char *backend_url,
     420             :                                    const char *order_id,
     421             :                                    const char *session_id,
     422             :                                    bool transfer,
     423             :                                    struct GNUNET_TIME_Relative timeout,
     424             :                                    TALER_MERCHANT_OrderMerchantGetCallback cb,
     425             :                                    void *cb_cls)
     426             : {
     427             :   struct TALER_MERCHANT_OrderMerchantGetHandle *omgh;
     428             :   unsigned long long tms;
     429             :   long tlong;
     430             : 
     431           0 :   GNUNET_assert (NULL != backend_url);
     432           0 :   GNUNET_assert (NULL != order_id);
     433           0 :   tms = (unsigned long long) (timeout.rel_value_us
     434           0 :                               / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
     435             :   /* set curl timeout to *our* long poll timeout plus one minute
     436             :      (for network latency and processing delays) */
     437           0 :   tlong = (long) (GNUNET_TIME_relative_add (timeout,
     438           0 :                                             GNUNET_TIME_UNIT_MINUTES).
     439             :                   rel_value_us
     440           0 :                   / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
     441           0 :   omgh = GNUNET_new (struct TALER_MERCHANT_OrderMerchantGetHandle);
     442           0 :   omgh->ctx = ctx;
     443           0 :   omgh->cb = cb;
     444           0 :   omgh->cb_cls = cb_cls;
     445             :   {
     446             :     char *path;
     447             :     char timeout_ms[32];
     448             : 
     449           0 :     GNUNET_snprintf (timeout_ms,
     450             :                      sizeof (timeout_ms),
     451             :                      "%llu",
     452             :                      tms);
     453           0 :     GNUNET_asprintf (&path,
     454             :                      "private/orders/%s",
     455             :                      order_id);
     456           0 :     omgh->url = TALER_url_join (backend_url,
     457             :                                 path,
     458             :                                 "session_id", session_id,
     459             :                                 "transfer", transfer ? "YES" : "NO",
     460             :                                 (0 != tms) ? "timeout_ms" : NULL,
     461             :                                 timeout_ms,
     462             :                                 NULL);
     463             :   }
     464           0 :   if (NULL == omgh->url)
     465             :   {
     466           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     467             :                 "Could not construct request URL.\n");
     468           0 :     GNUNET_free (omgh);
     469           0 :     return NULL;
     470             :   }
     471             : 
     472             :   {
     473             :     CURL *eh;
     474             : 
     475           0 :     eh = curl_easy_init ();
     476           0 :     if (NULL == eh)
     477             :     {
     478           0 :       GNUNET_break (0);
     479           0 :       GNUNET_free (omgh->url);
     480           0 :       GNUNET_free (omgh);
     481           0 :       return NULL;
     482             :     }
     483           0 :     if (CURLE_OK != curl_easy_setopt (eh,
     484             :                                       CURLOPT_URL,
     485             :                                       omgh->url))
     486             :     {
     487           0 :       GNUNET_break (0);
     488           0 :       curl_easy_cleanup (eh);
     489           0 :       GNUNET_free (omgh->url);
     490           0 :       GNUNET_free (omgh);
     491           0 :       return NULL;
     492             :     }
     493           0 :     if (CURLE_OK != curl_easy_setopt (eh,
     494             :                                       CURLOPT_TIMEOUT_MS,
     495             :                                       tlong))
     496             :     {
     497           0 :       GNUNET_break (0);
     498           0 :       curl_easy_cleanup (eh);
     499           0 :       GNUNET_free (omgh->url);
     500           0 :       GNUNET_free (omgh);
     501           0 :       return NULL;
     502             :     }
     503             : 
     504           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     505             :                 "Getting order status from %s\n",
     506             :                 omgh->url);
     507           0 :     if (NULL == (omgh->job =
     508           0 :                    GNUNET_CURL_job_add (ctx,
     509             :                                         eh,
     510             :                                         &handle_merchant_order_get_finished,
     511             :                                         omgh)))
     512             :     {
     513           0 :       GNUNET_break (0);
     514           0 :       GNUNET_free (omgh->url);
     515           0 :       GNUNET_free (omgh);
     516           0 :       return NULL;
     517             :     }
     518             :   }
     519           0 :   return omgh;
     520             : }
     521             : 
     522             : 
     523             : /**
     524             :  * Cancel a GET /private/orders/$ORDER request.
     525             :  *
     526             :  * @param omgh handle to the request to be canceled
     527             :  */
     528             : void
     529           0 : TALER_MERCHANT_merchant_order_get_cancel (
     530             :   struct TALER_MERCHANT_OrderMerchantGetHandle *omgh)
     531             : {
     532           0 :   if (NULL != omgh->job)
     533             :   {
     534           0 :     GNUNET_CURL_job_cancel (omgh->job);
     535           0 :     omgh->job = NULL;
     536             :   }
     537           0 :   GNUNET_free (omgh->url);
     538           0 :   GNUNET_free (omgh);
     539           0 : }
     540             : 
     541             : 
     542             : /* end of merchant_api_merchant_get_order.c */

Generated by: LCOV version 1.14