LCOV - code coverage report
Current view: top level - lib - merchant_api_post_orders.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 117 0.0 %
Date: 2020-08-15 06:12:35 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) 2014-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, but
      11             :   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 Public
      16             :   License along with TALER; see the file COPYING.LGPL.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file lib/merchant_api_post_orders.c
      21             :  * @brief Implementation of the POST /orders
      22             :  * @author Christian Grothoff
      23             :  * @author Marcello Stanisci
      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_curl_lib.h>
      35             : 
      36             : 
      37             : /**
      38             :  * @brief A POST /orders Handle
      39             :  */
      40             : struct TALER_MERCHANT_PostOrdersOperation
      41             : {
      42             : 
      43             :   /**
      44             :    * The url for this request.
      45             :    */
      46             :   char *url;
      47             : 
      48             :   /**
      49             :    * Handle for the request.
      50             :    */
      51             :   struct GNUNET_CURL_Job *job;
      52             : 
      53             :   /**
      54             :    * Function to call with the result.
      55             :    */
      56             :   TALER_MERCHANT_PostOrdersCallback cb;
      57             : 
      58             :   /**
      59             :    * Closure for @a cb.
      60             :    */
      61             :   void *cb_cls;
      62             : 
      63             :   /**
      64             :    * Reference to the execution context.
      65             :    */
      66             :   struct GNUNET_CURL_Context *ctx;
      67             : 
      68             :   /**
      69             :    * Minor context that holds body and headers.
      70             :    */
      71             :   struct TALER_CURL_PostContext post_ctx;
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * Function called when we're done processing the
      77             :  * HTTP POST /orders request.
      78             :  *
      79             :  * @param cls the `struct TALER_MERCHANT_PostOrdersOperation`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param json response body, NULL if not JSON
      82             :  */
      83             : static void
      84           0 : handle_post_order_finished (void *cls,
      85             :                             long response_code,
      86             :                             const void *response)
      87             : {
      88           0 :   struct TALER_MERCHANT_PostOrdersOperation *po = cls;
      89           0 :   const char *order_id = NULL;
      90           0 :   struct TALER_ClaimTokenP token = {0};
      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           0 :   bool has_token = ((NULL != json_object_get (json,
      97           0 :                                               "token")) &&
      98           0 :                     (false == json_is_null (json_object_get (json,
      99             :                                                              "token"))));
     100           0 :   struct GNUNET_JSON_Specification spec[] = {
     101           0 :     GNUNET_JSON_spec_string ("order_id",
     102             :                              &order_id),
     103           0 :     (!has_token) ?
     104           0 :       GNUNET_JSON_spec_end () :
     105           0 :       GNUNET_JSON_spec_fixed_auto ("token",
     106             :                                    &token),
     107           0 :     GNUNET_JSON_spec_end ()
     108             :   };
     109             : 
     110           0 :   po->job = NULL;
     111           0 :   switch (response_code)
     112             :   {
     113           0 :   case 0:
     114           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     115           0 :     break;
     116           0 :   case MHD_HTTP_OK:
     117           0 :     if (GNUNET_OK !=
     118           0 :         GNUNET_JSON_parse (json,
     119             :                            spec,
     120             :                            NULL, NULL))
     121             :     {
     122           0 :       GNUNET_break_op (0);
     123           0 :       hr.http_status = 0;
     124           0 :       hr.ec = TALER_EC_PROPOSAL_REPLY_MALFORMED;
     125             :     }
     126           0 :     break;
     127           0 :   case MHD_HTTP_BAD_REQUEST:
     128           0 :     hr.ec = TALER_JSON_get_error_code (json);
     129           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     130             :     /* This should never happen, either us or
     131             :        the merchant is buggy (or API version conflict);
     132             :        just pass JSON reply to the application */
     133           0 :     break;
     134           0 :   case MHD_HTTP_CONFLICT:
     135           0 :     hr.ec = TALER_JSON_get_error_code (json);
     136           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     137           0 :     break;
     138           0 :   case MHD_HTTP_FORBIDDEN:
     139             :     /* Nothing really to verify, merchant says one
     140             :        of the signatures is invalid; as we checked them,
     141             :        this should never happen, we should pass the JSON
     142             :        reply to the application */
     143           0 :     hr.ec = TALER_JSON_get_error_code (json);
     144           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     145           0 :     break;
     146           0 :   case MHD_HTTP_NOT_FOUND:
     147             :     /* Nothing really to verify, this should never
     148             :        happen, we should pass the JSON reply to the application */
     149           0 :     hr.ec = TALER_JSON_get_error_code (json);
     150           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     151           0 :     break;
     152           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     153             :     /* Server had an internal issue; we should retry,
     154             :        but this API leaves this to the application */
     155           0 :     hr.ec = TALER_JSON_get_error_code (json);
     156           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     157           0 :     break;
     158           0 :   case MHD_HTTP_GONE:
     159             :     /* The quantity of some product requested was not available. */
     160             :     // FIXME: parse the OutOfStockResponse.
     161           0 :     break;
     162           0 :   default:
     163             :     /* unexpected response code */
     164           0 :     hr.ec = TALER_JSON_get_error_code (json);
     165           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     166           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167             :                 "Unexpected response code %u/%d\n",
     168             :                 (unsigned int) response_code,
     169             :                 (int) hr.ec);
     170           0 :     GNUNET_break_op (0);
     171           0 :     break;
     172             :   }
     173           0 :   po->cb (po->cb_cls,
     174             :           &hr,
     175             :           order_id,
     176             :           has_token ? &token : NULL);
     177           0 :   if (MHD_HTTP_OK == response_code)
     178           0 :     GNUNET_JSON_parse_free (spec);
     179           0 :   TALER_MERCHANT_orders_post_cancel (po);
     180           0 : }
     181             : 
     182             : 
     183             : /**
     184             :  * POST an order to the backend and receives the related proposal.
     185             :  *
     186             :  * @param ctx execution context
     187             :  * @param backend_url URL of the backend
     188             :  * @param order basic information about this purchase,
     189             :  *        to be extended by the backend
     190             :  * @param refund_delay how long can refunds happen for this order; 0 to use
     191             :  *             absolute value from contract (or not allow refunds).
     192             :  * @param cb the callback to call when a reply
     193             :  *        for this request is available
     194             :  * @param cb_cls closure for @a proposal_cb
     195             :  * @return a handle for this request, NULL on error
     196             :  */
     197             : struct TALER_MERCHANT_PostOrdersOperation *
     198           0 : TALER_MERCHANT_orders_post (struct GNUNET_CURL_Context *ctx,
     199             :                             const char *backend_url,
     200             :                             const json_t *order,
     201             :                             struct GNUNET_TIME_Relative refund_delay,
     202             :                             TALER_MERCHANT_PostOrdersCallback cb,
     203             :                             void *cb_cls)
     204             : {
     205           0 :   return TALER_MERCHANT_orders_post2 (ctx,
     206             :                                       backend_url,
     207             :                                       order,
     208             :                                       refund_delay,
     209             :                                       NULL,
     210             :                                       0,
     211             :                                       NULL,
     212             :                                       0,
     213             :                                       NULL,
     214             :                                       true,
     215             :                                       cb,
     216             :                                       cb_cls);
     217             : }
     218             : 
     219             : 
     220             : /**
     221             :  * POST to /orders at the backend to setup an order and obtain
     222             :  * the order ID (which may have been set by the front-end).
     223             :  *
     224             :  * @param ctx execution context
     225             :  * @param backend_url URL of the backend
     226             :  * @param order basic information about this purchase, to be extended by the backend
     227             :  * @param refund_delay how long can refunds happen for this order; 0 to use
     228             :  *             absolute value from contract (or not allow refunds).
     229             :  * @param payment_target desired payment target identifier (to select merchant bank details)
     230             :  * @param inventory_products_length length of the @a inventory_products array
     231             :  * @param inventory_products products to add to the order from the inventory
     232             :  * @param lock_uuids_length length of the @a uuids array
     233             :  * @param uuids array of UUIDs with locks on @a inventory_products
     234             :  * @param create_token whether to create a claim token
     235             :  * @param cb the callback to call when a reply for this request is available
     236             :  * @param cb_cls closure for @a cb
     237             :  * @return a handle for this request, NULL on error
     238             :  */
     239             : struct TALER_MERCHANT_PostOrdersOperation *
     240           0 : TALER_MERCHANT_orders_post2 (
     241             :   struct GNUNET_CURL_Context *ctx,
     242             :   const char *backend_url,
     243             :   const json_t *order,
     244             :   struct GNUNET_TIME_Relative refund_delay,
     245             :   const char *payment_target,
     246             :   unsigned int inventory_products_length,
     247             :   const struct TALER_MERCHANT_InventoryProduct inventory_products[],
     248             :   unsigned int uuids_length,
     249             :   const struct GNUNET_Uuid uuids[],
     250             :   bool create_token,
     251             :   TALER_MERCHANT_PostOrdersCallback cb,
     252             :   void *cb_cls)
     253             : {
     254             :   struct TALER_MERCHANT_PostOrdersOperation *po;
     255             :   json_t *req;
     256             :   CURL *eh;
     257             :   const char *delay_s;
     258             : 
     259           0 :   delay_s = GNUNET_STRINGS_relative_time_to_string (refund_delay,
     260             :                                                     GNUNET_NO);
     261           0 :   po = GNUNET_new (struct TALER_MERCHANT_PostOrdersOperation);
     262           0 :   po->ctx = ctx;
     263           0 :   po->cb = cb;
     264           0 :   po->cb_cls = cb_cls;
     265           0 :   po->url = TALER_url_join (backend_url,
     266             :                             "private/orders",
     267             :                             NULL);
     268           0 :   req = json_pack ("{s:O}",
     269             :                    "order", (json_t *) order);
     270           0 :   GNUNET_assert (NULL != req);
     271           0 :   if (0 != refund_delay.rel_value_us)
     272             :   {
     273           0 :     GNUNET_assert (0 ==
     274             :                    json_object_set_new (req,
     275             :                                         "refund_delay",
     276             :                                         GNUNET_JSON_from_time_rel (
     277             :                                           refund_delay)));
     278             :   }
     279           0 :   if (NULL != payment_target)
     280             :   {
     281           0 :     GNUNET_assert (0 ==
     282             :                    json_object_set_new (req,
     283             :                                         "payment_target",
     284             :                                         json_string (payment_target)));
     285             :   }
     286           0 :   if (0 != inventory_products_length)
     287             :   {
     288           0 :     json_t *ipa = json_array ();
     289             : 
     290           0 :     GNUNET_assert (NULL != ipa);
     291           0 :     for (unsigned int i = 0; i<inventory_products_length; i++)
     292             :     {
     293             :       json_t *ip;
     294             : 
     295           0 :       ip = json_pack ("{s:s, s:I}",
     296             :                       "product_id",
     297           0 :                       inventory_products[i].product_id,
     298             :                       "quantity",
     299           0 :                       (json_int_t) inventory_products[i].quantity);
     300           0 :       GNUNET_assert (NULL != ip);
     301           0 :       GNUNET_assert (0 ==
     302             :                      json_array_append_new (ipa,
     303             :                                             ip));
     304             :     }
     305           0 :     GNUNET_assert (0 ==
     306             :                    json_object_set_new (req,
     307             :                                         "inventory_products",
     308             :                                         ipa));
     309             :   }
     310           0 :   if (0 != uuids_length)
     311             :   {
     312           0 :     json_t *ua = json_array ();
     313             : 
     314           0 :     GNUNET_assert (NULL != ua);
     315           0 :     for (unsigned int i = 0; i<uuids_length; i++)
     316             :     {
     317             :       json_t *u;
     318             : 
     319           0 :       u = json_pack ("{s:o}",
     320             :                      "uuid",
     321           0 :                      GNUNET_JSON_from_data_auto (&uuids[i]));
     322           0 :       GNUNET_assert (NULL != u);
     323           0 :       GNUNET_assert (0 ==
     324             :                      json_array_append_new (ua,
     325             :                                             u));
     326             :     }
     327           0 :     GNUNET_assert (0 ==
     328             :                    json_object_set_new (req,
     329             :                                         "lock_uuids",
     330             :                                         ua));
     331             :   }
     332           0 :   if (! create_token)
     333             :   {
     334           0 :     GNUNET_assert (0 ==
     335             :                    json_object_set_new (req,
     336             :                                         "create_token",
     337             :                                         json_boolean (create_token)));
     338             :   }
     339           0 :   eh = curl_easy_init ();
     340           0 :   GNUNET_assert (NULL != eh);
     341           0 :   if (GNUNET_OK != TALER_curl_easy_post (&po->post_ctx,
     342             :                                          eh,
     343             :                                          req))
     344             :   {
     345           0 :     GNUNET_break (0);
     346           0 :     json_decref (req);
     347           0 :     GNUNET_free (po);
     348           0 :     return NULL;
     349             :   }
     350           0 :   json_decref (req);
     351           0 :   GNUNET_assert (CURLE_OK ==
     352             :                  curl_easy_setopt (eh,
     353             :                                    CURLOPT_URL,
     354             :                                    po->url));
     355           0 :   po->job = GNUNET_CURL_job_add2 (ctx,
     356             :                                   eh,
     357           0 :                                   po->post_ctx.headers,
     358             :                                   &handle_post_order_finished,
     359             :                                   po);
     360           0 :   return po;
     361             : }
     362             : 
     363             : 
     364             : /**
     365             :  * Cancel a POST /proposal request.  This function cannot be used
     366             :  * on a request handle if a response is already served for it.
     367             :  *
     368             :  * @param po the proposal operation request handle
     369             :  */
     370             : void
     371           0 : TALER_MERCHANT_orders_post_cancel (
     372             :   struct TALER_MERCHANT_PostOrdersOperation *po)
     373             : {
     374           0 :   if (NULL != po->job)
     375             :   {
     376           0 :     GNUNET_CURL_job_cancel (po->job);
     377           0 :     po->job = NULL;
     378             :   }
     379           0 :   GNUNET_free (po->url);
     380           0 :   TALER_curl_easy_post_finished (&po->post_ctx);
     381           0 :   GNUNET_free (po);
     382           0 : }
     383             : 
     384             : 
     385             : /* end of merchant_api_post_orders.c */

Generated by: LCOV version 1.14