merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

merchant_api_get_template.c (5686B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022 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_get_template.c
     19  * @brief Implementation of the GET /templates/$ID request of the merchant's HTTP API
     20  * @author Priscilla HUANG
     21  */
     22 #include "platform.h"
     23 #include <curl/curl.h>
     24 #include <jansson.h>
     25 #include <microhttpd.h> /* just for HTTP status codes */
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include "taler_merchant_service.h"
     29 #include "merchant_api_curl_defaults.h"
     30 #include <taler/taler_json_lib.h>
     31 #include <taler/taler_signatures.h>
     32 
     33 
     34 /**
     35  * Handle for a GET /templates/$ID operation.
     36  */
     37 struct TALER_MERCHANT_TemplateGetHandle
     38 {
     39   /**
     40    * The url for this request.
     41    */
     42   char *url;
     43 
     44   /**
     45    * Handle for the request.
     46    */
     47   struct GNUNET_CURL_Job *job;
     48 
     49   /**
     50    * Function to call with the result.
     51    */
     52   TALER_MERCHANT_TemplateGetCallback cb;
     53 
     54   /**
     55    * Closure for @a cb.
     56    */
     57   void *cb_cls;
     58 
     59   /**
     60    * Reference to the execution context.
     61    */
     62   struct GNUNET_CURL_Context *ctx;
     63 
     64 };
     65 
     66 
     67 /**
     68  * Function called when we're done processing the
     69  * HTTP GET /templates/$ID request.
     70  *
     71  * @param cls the `struct TALER_MERCHANT_TemplateGetHandle`
     72  * @param response_code HTTP response code, 0 on error
     73  * @param response response body, NULL if not in JSON
     74  */
     75 static void
     76 handle_get_template_finished (void *cls,
     77                               long response_code,
     78                               const void *response)
     79 {
     80   struct TALER_MERCHANT_TemplateGetHandle *tgh = cls;
     81   const json_t *json = response;
     82   struct TALER_MERCHANT_TemplateGetResponse tgr = {
     83     .hr.http_status = (unsigned int) response_code,
     84     .hr.reply = json
     85   };
     86 
     87   tgh->job = NULL;
     88   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     89               "Got /templates/$ID response with status code %u\n",
     90               (unsigned int) response_code);
     91   switch (response_code)
     92   {
     93   case MHD_HTTP_OK:
     94     {
     95       const json_t *contract;
     96       struct GNUNET_JSON_Specification spec[] = {
     97         GNUNET_JSON_spec_string ("template_description",
     98                                  &tgr.details.ok.template_description),
     99         GNUNET_JSON_spec_mark_optional (
    100           GNUNET_JSON_spec_string ("otp_id",
    101                                    &tgr.details.ok.otp_id),
    102           NULL),
    103         GNUNET_JSON_spec_object_const ("template_contract",
    104                                        &contract),
    105         GNUNET_JSON_spec_end ()
    106       };
    107 
    108       if (GNUNET_OK ==
    109           GNUNET_JSON_parse (json,
    110                              spec,
    111                              NULL, NULL))
    112       {
    113         tgr.details.ok.template_contract = contract;
    114         tgh->cb (tgh->cb_cls,
    115                  &tgr);
    116         TALER_MERCHANT_template_get_cancel (tgh);
    117         return;
    118       }
    119       tgr.hr.http_status = 0;
    120       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    121       break;
    122     }
    123   case MHD_HTTP_UNAUTHORIZED:
    124     tgr.hr.ec = TALER_JSON_get_error_code (json);
    125     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    126     /* Nothing really to verify, merchant says we need to authenticate. */
    127     break;
    128   case MHD_HTTP_NOT_FOUND:
    129     tgr.hr.ec = TALER_JSON_get_error_code (json);
    130     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    131     break;
    132   default:
    133     /* unexpected response code */
    134     tgr.hr.ec = TALER_JSON_get_error_code (json);
    135     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    136     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    137                 "Unexpected response code %u/%d\n",
    138                 (unsigned int) response_code,
    139                 (int) tgr.hr.ec);
    140     break;
    141   }
    142   tgh->cb (tgh->cb_cls,
    143            &tgr);
    144   TALER_MERCHANT_template_get_cancel (tgh);
    145 }
    146 
    147 
    148 struct TALER_MERCHANT_TemplateGetHandle *
    149 TALER_MERCHANT_template_get (
    150   struct GNUNET_CURL_Context *ctx,
    151   const char *backend_url,
    152   const char *template_id,
    153   TALER_MERCHANT_TemplateGetCallback cb,
    154   void *cb_cls)
    155 {
    156   struct TALER_MERCHANT_TemplateGetHandle *tgh;
    157   CURL *eh;
    158 
    159   tgh = GNUNET_new (struct TALER_MERCHANT_TemplateGetHandle);
    160   tgh->ctx = ctx;
    161   tgh->cb = cb;
    162   tgh->cb_cls = cb_cls;
    163   {
    164     char *path;
    165 
    166     GNUNET_asprintf (&path,
    167                      "private/templates/%s",
    168                      template_id);
    169     tgh->url = TALER_url_join (backend_url,
    170                                path,
    171                                NULL);
    172     GNUNET_free (path);
    173   }
    174   if (NULL == tgh->url)
    175   {
    176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    177                 "Could not construct request URL.\n");
    178     GNUNET_free (tgh);
    179     return NULL;
    180   }
    181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    182               "Requesting URL '%s'\n",
    183               tgh->url);
    184   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
    185   tgh->job = GNUNET_CURL_job_add (ctx,
    186                                   eh,
    187                                   &handle_get_template_finished,
    188                                   tgh);
    189   return tgh;
    190 }
    191 
    192 
    193 void
    194 TALER_MERCHANT_template_get_cancel (
    195   struct TALER_MERCHANT_TemplateGetHandle *tgh)
    196 {
    197   if (NULL != tgh->job)
    198     GNUNET_CURL_job_cancel (tgh->job);
    199   GNUNET_free (tgh->url);
    200   GNUNET_free (tgh);
    201 }