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 119 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) 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 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_PostOrdersHandle
      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_PostOrdersHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param response 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_PostOrdersHandle *po = cls;
      89           0 :   const json_t *json = response;
      90           0 :   struct TALER_MERCHANT_PostOrdersReply por = {
      91           0 :     .hr.http_status = (unsigned int) response_code,
      92             :     .hr.reply = json
      93             :   };
      94           0 :   struct TALER_ClaimTokenP token = {0};
      95             : 
      96           0 :   po->job = NULL;
      97           0 :   switch (response_code)
      98             :   {
      99           0 :   case 0:
     100           0 :     por.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     101           0 :     break;
     102           0 :   case MHD_HTTP_OK:
     103             :     {
     104             :       struct GNUNET_JSON_Specification spec[] = {
     105           0 :         GNUNET_JSON_spec_string ("order_id",
     106             :                                  &por.details.ok.order_id),
     107           0 :         GNUNET_JSON_spec_mark_optional (
     108             :           GNUNET_JSON_spec_fixed_auto ("token",
     109             :                                        &token)),
     110           0 :         GNUNET_JSON_spec_end ()
     111             :       };
     112             : 
     113           0 :       if (GNUNET_OK !=
     114           0 :           GNUNET_JSON_parse (json,
     115             :                              spec,
     116             :                              NULL, NULL))
     117             :       {
     118           0 :         GNUNET_break_op (0);
     119           0 :         por.hr.http_status = 0;
     120           0 :         por.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     121             :       }
     122             :       else
     123             :       {
     124           0 :         if (! GNUNET_is_zero (&token))
     125           0 :           por.details.ok.token = &token;
     126             :       }
     127             :     }
     128           0 :     break;
     129           0 :   case MHD_HTTP_BAD_REQUEST:
     130           0 :     por.hr.ec = TALER_JSON_get_error_code (json);
     131           0 :     por.hr.hint = TALER_JSON_get_error_hint (json);
     132             :     /* This should never happen, either us or
     133             :        the merchant is buggy (or API version conflict);
     134             :        just pass JSON reply to the application */
     135           0 :     break;
     136           0 :   case MHD_HTTP_FORBIDDEN:
     137             :     /* Nothing really to verify, merchant says one
     138             :        of the signatures is invalid; as we checked them,
     139             :        this should never happen, we should pass the JSON
     140             :        reply to the application */
     141           0 :     por.hr.ec = TALER_JSON_get_error_code (json);
     142           0 :     por.hr.hint = TALER_JSON_get_error_hint (json);
     143           0 :     break;
     144           0 :   case MHD_HTTP_NOT_FOUND:
     145             :     /* Nothing really to verify, this should never
     146             :        happen, we should pass the JSON reply to the application */
     147           0 :     por.hr.ec = TALER_JSON_get_error_code (json);
     148           0 :     por.hr.hint = TALER_JSON_get_error_hint (json);
     149           0 :     break;
     150           0 :   case MHD_HTTP_CONFLICT:
     151           0 :     por.hr.ec = TALER_JSON_get_error_code (json);
     152           0 :     por.hr.hint = TALER_JSON_get_error_hint (json);
     153           0 :     break;
     154           0 :   case MHD_HTTP_GONE:
     155             :     /* The quantity of some product requested was not available. */
     156             :     {
     157             : 
     158             :       struct GNUNET_JSON_Specification spec[] = {
     159           0 :         GNUNET_JSON_spec_string (
     160             :           "product_id",
     161             :           &por.details.gone.product_id),
     162           0 :         GNUNET_JSON_spec_uint64 (
     163             :           "requested_quantity",
     164             :           &por.details.gone.requested_quantity),
     165           0 :         GNUNET_JSON_spec_uint64 (
     166             :           "available_quantity",
     167             :           &por.details.gone.available_quantity),
     168           0 :         GNUNET_JSON_spec_mark_optional (
     169             :           GNUNET_JSON_spec_absolute_time (
     170             :             "restock_expected",
     171             :             &por.details.gone.restock_expected)),
     172           0 :         GNUNET_JSON_spec_end ()
     173             :       };
     174             : 
     175           0 :       if (GNUNET_OK !=
     176           0 :           GNUNET_JSON_parse (json,
     177             :                              spec,
     178             :                              NULL, NULL))
     179             :       {
     180           0 :         GNUNET_break_op (0);
     181           0 :         por.hr.http_status = 0;
     182           0 :         por.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     183             :       }
     184           0 :       break;
     185             :     }
     186           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     187             :     /* Server had an internal issue; we should retry,
     188             :        but this API leaves this to the application */
     189           0 :     por.hr.ec = TALER_JSON_get_error_code (json);
     190           0 :     por.hr.hint = TALER_JSON_get_error_hint (json);
     191           0 :     break;
     192           0 :   default:
     193             :     /* unexpected response code */
     194           0 :     por.hr.ec = TALER_JSON_get_error_code (json);
     195           0 :     por.hr.hint = TALER_JSON_get_error_hint (json);
     196           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     197             :                 "Unexpected response code %u/%d\n",
     198             :                 (unsigned int) response_code,
     199             :                 (int) por.hr.ec);
     200           0 :     GNUNET_break_op (0);
     201           0 :     break;
     202             :   }
     203           0 :   po->cb (po->cb_cls,
     204             :           &por);
     205           0 :   TALER_MERCHANT_orders_post_cancel (po);
     206           0 : }
     207             : 
     208             : 
     209             : struct TALER_MERCHANT_PostOrdersHandle *
     210           0 : TALER_MERCHANT_orders_post (struct GNUNET_CURL_Context *ctx,
     211             :                             const char *backend_url,
     212             :                             const json_t *order,
     213             :                             struct GNUNET_TIME_Relative refund_delay,
     214             :                             TALER_MERCHANT_PostOrdersCallback cb,
     215             :                             void *cb_cls)
     216             : {
     217           0 :   return TALER_MERCHANT_orders_post2 (ctx,
     218             :                                       backend_url,
     219             :                                       order,
     220             :                                       refund_delay,
     221             :                                       NULL,
     222             :                                       0,
     223             :                                       NULL,
     224             :                                       0,
     225             :                                       NULL,
     226             :                                       true,
     227             :                                       cb,
     228             :                                       cb_cls);
     229             : }
     230             : 
     231             : 
     232             : struct TALER_MERCHANT_PostOrdersHandle *
     233           0 : TALER_MERCHANT_orders_post2 (
     234             :   struct GNUNET_CURL_Context *ctx,
     235             :   const char *backend_url,
     236             :   const json_t *order,
     237             :   struct GNUNET_TIME_Relative refund_delay,
     238             :   const char *payment_target,
     239             :   unsigned int inventory_products_length,
     240             :   const struct TALER_MERCHANT_InventoryProduct inventory_products[],
     241             :   unsigned int uuids_length,
     242             :   const struct GNUNET_Uuid uuids[],
     243             :   bool create_token,
     244             :   TALER_MERCHANT_PostOrdersCallback cb,
     245             :   void *cb_cls)
     246             : {
     247             :   struct TALER_MERCHANT_PostOrdersHandle *po;
     248             :   json_t *req;
     249             :   CURL *eh;
     250             : 
     251           0 :   po = GNUNET_new (struct TALER_MERCHANT_PostOrdersHandle);
     252           0 :   po->ctx = ctx;
     253           0 :   po->cb = cb;
     254           0 :   po->cb_cls = cb_cls;
     255           0 :   po->url = TALER_url_join (backend_url,
     256             :                             "private/orders",
     257             :                             NULL);
     258           0 :   req = json_pack ("{s:O}",
     259             :                    "order", (json_t *) order);
     260           0 :   GNUNET_assert (NULL != req);
     261           0 :   if (0 != refund_delay.rel_value_us)
     262             :   {
     263           0 :     GNUNET_assert (0 ==
     264             :                    json_object_set_new (req,
     265             :                                         "refund_delay",
     266             :                                         GNUNET_JSON_from_time_rel (
     267             :                                           refund_delay)));
     268             :   }
     269           0 :   if (NULL != payment_target)
     270             :   {
     271           0 :     GNUNET_assert (0 ==
     272             :                    json_object_set_new (req,
     273             :                                         "payment_target",
     274             :                                         json_string (payment_target)));
     275             :   }
     276           0 :   if (0 != inventory_products_length)
     277             :   {
     278           0 :     json_t *ipa = json_array ();
     279             : 
     280           0 :     GNUNET_assert (NULL != ipa);
     281           0 :     for (unsigned int i = 0; i<inventory_products_length; i++)
     282             :     {
     283             :       json_t *ip;
     284             : 
     285           0 :       ip = json_pack ("{s:s, s:I}",
     286             :                       "product_id",
     287           0 :                       inventory_products[i].product_id,
     288             :                       "quantity",
     289           0 :                       (json_int_t) inventory_products[i].quantity);
     290           0 :       GNUNET_assert (NULL != ip);
     291           0 :       GNUNET_assert (0 ==
     292             :                      json_array_append_new (ipa,
     293             :                                             ip));
     294             :     }
     295           0 :     GNUNET_assert (0 ==
     296             :                    json_object_set_new (req,
     297             :                                         "inventory_products",
     298             :                                         ipa));
     299             :   }
     300           0 :   if (0 != uuids_length)
     301             :   {
     302           0 :     json_t *ua = json_array ();
     303             : 
     304           0 :     GNUNET_assert (NULL != ua);
     305           0 :     for (unsigned int i = 0; i<uuids_length; i++)
     306             :     {
     307             :       json_t *u;
     308             : 
     309           0 :       u = json_pack ("{s:o}",
     310             :                      "uuid",
     311           0 :                      GNUNET_JSON_from_data_auto (&uuids[i]));
     312           0 :       GNUNET_assert (NULL != u);
     313           0 :       GNUNET_assert (0 ==
     314             :                      json_array_append_new (ua,
     315             :                                             u));
     316             :     }
     317           0 :     GNUNET_assert (0 ==
     318             :                    json_object_set_new (req,
     319             :                                         "lock_uuids",
     320             :                                         ua));
     321             :   }
     322           0 :   if (! create_token)
     323             :   {
     324           0 :     GNUNET_assert (0 ==
     325             :                    json_object_set_new (req,
     326             :                                         "create_token",
     327             :                                         json_boolean (create_token)));
     328             :   }
     329           0 :   eh = curl_easy_init ();
     330           0 :   GNUNET_assert (NULL != eh);
     331           0 :   if (GNUNET_OK != TALER_curl_easy_post (&po->post_ctx,
     332             :                                          eh,
     333             :                                          req))
     334             :   {
     335           0 :     GNUNET_break (0);
     336           0 :     json_decref (req);
     337           0 :     GNUNET_free (po);
     338           0 :     return NULL;
     339             :   }
     340           0 :   json_decref (req);
     341           0 :   GNUNET_assert (CURLE_OK ==
     342             :                  curl_easy_setopt (eh,
     343             :                                    CURLOPT_URL,
     344             :                                    po->url));
     345           0 :   po->job = GNUNET_CURL_job_add2 (ctx,
     346             :                                   eh,
     347           0 :                                   po->post_ctx.headers,
     348             :                                   &handle_post_order_finished,
     349             :                                   po);
     350           0 :   return po;
     351             : }
     352             : 
     353             : 
     354             : void
     355           0 : TALER_MERCHANT_orders_post_cancel (
     356             :   struct TALER_MERCHANT_PostOrdersHandle *po)
     357             : {
     358           0 :   if (NULL != po->job)
     359             :   {
     360           0 :     GNUNET_CURL_job_cancel (po->job);
     361           0 :     po->job = NULL;
     362             :   }
     363           0 :   GNUNET_free (po->url);
     364           0 :   TALER_curl_easy_post_finished (&po->post_ctx);
     365           0 :   GNUNET_free (po);
     366           0 : }
     367             : 
     368             : 
     369             : /* end of merchant_api_post_orders.c */

Generated by: LCOV version 1.14