merchant

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

merchant_api_get_account.c (6134B)


      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_account.c
     19  * @brief Implementation of the GET /accounts/$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 /accounts/$ID operation.
     36  */
     37 struct TALER_MERCHANT_AccountGetHandle
     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_AccountGetCallback 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 /accounts/$ID request.
     70  *
     71  * @param cls the `struct TALER_MERCHANT_AccountGetHandle`
     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_account_finished (void *cls,
     77                              long response_code,
     78                              const void *response)
     79 {
     80   struct TALER_MERCHANT_AccountGetHandle *tgh = cls;
     81   const json_t *json = response;
     82   struct TALER_MERCHANT_AccountGetResponse 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 /accounts/$ID response with status code %u\n",
     90               (unsigned int) response_code);
     91   switch (response_code)
     92   {
     93   case MHD_HTTP_OK:
     94     {
     95       struct GNUNET_JSON_Specification spec[] = {
     96         GNUNET_JSON_spec_fixed_auto ("salt",
     97                                      &tgr.details.ok.ad.salt),
     98         GNUNET_JSON_spec_mark_optional (
     99           TALER_JSON_spec_web_url ("credit_facade_url",
    100                                    &tgr.details.ok.ad.credit_facade_url),
    101           NULL),
    102         TALER_JSON_spec_full_payto_uri ("payto_uri",
    103                                         &tgr.details.ok.ad.payto_uri),
    104         GNUNET_JSON_spec_fixed_auto ("h_wire",
    105                                      &tgr.details.ok.ad.h_wire),
    106         GNUNET_JSON_spec_bool ("active",
    107                                &tgr.details.ok.ad.active),
    108         GNUNET_JSON_spec_end ()
    109       };
    110 
    111       if (GNUNET_OK ==
    112           GNUNET_JSON_parse (json,
    113                              spec,
    114                              NULL, NULL))
    115       {
    116         tgh->cb (tgh->cb_cls,
    117                  &tgr);
    118         TALER_MERCHANT_account_get_cancel (tgh);
    119         return;
    120       }
    121       tgr.hr.http_status = 0;
    122       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    123       break;
    124     }
    125   case MHD_HTTP_UNAUTHORIZED:
    126     tgr.hr.ec = TALER_JSON_get_error_code (json);
    127     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    128     /* Nothing really to verify, merchant says we need to authenticate. */
    129     break;
    130   case MHD_HTTP_NOT_FOUND:
    131     tgr.hr.ec = TALER_JSON_get_error_code (json);
    132     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    133     break;
    134   default:
    135     /* unexpected response code */
    136     tgr.hr.ec = TALER_JSON_get_error_code (json);
    137     tgr.hr.hint = TALER_JSON_get_error_hint (json);
    138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    139                 "Unexpected response code %u/%d\n",
    140                 (unsigned int) response_code,
    141                 (int) tgr.hr.ec);
    142     break;
    143   }
    144   tgh->cb (tgh->cb_cls,
    145            &tgr);
    146   TALER_MERCHANT_account_get_cancel (tgh);
    147 }
    148 
    149 
    150 struct TALER_MERCHANT_AccountGetHandle *
    151 TALER_MERCHANT_account_get (
    152   struct GNUNET_CURL_Context *ctx,
    153   const char *backend_url,
    154   const char *instance_id,
    155   const struct TALER_MerchantWireHashP *h_wire,
    156   TALER_MERCHANT_AccountGetCallback cb,
    157   void *cb_cls)
    158 {
    159   struct TALER_MERCHANT_AccountGetHandle *tgh;
    160   CURL *eh;
    161 
    162   tgh = GNUNET_new (struct TALER_MERCHANT_AccountGetHandle);
    163   tgh->ctx = ctx;
    164   tgh->cb = cb;
    165   tgh->cb_cls = cb_cls;
    166   {
    167     char w_str[sizeof (*h_wire) * 2];
    168     char *path;
    169     char *end;
    170 
    171     end = GNUNET_STRINGS_data_to_string (h_wire,
    172                                          sizeof (*h_wire),
    173                                          w_str,
    174                                          sizeof (w_str));
    175     *end = '\0';
    176     GNUNET_asprintf (&path,
    177                      "private/accounts/%s",
    178                      w_str);
    179     tgh->url = TALER_url_join (backend_url,
    180                                path,
    181                                NULL);
    182     GNUNET_free (path);
    183   }
    184   if (NULL == tgh->url)
    185   {
    186     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    187                 "Could not construct request URL.\n");
    188     GNUNET_free (tgh);
    189     return NULL;
    190   }
    191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    192               "Requesting URL '%s'\n",
    193               tgh->url);
    194   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
    195   tgh->job = GNUNET_CURL_job_add (ctx,
    196                                   eh,
    197                                   &handle_get_account_finished,
    198                                   tgh);
    199   return tgh;
    200 }
    201 
    202 
    203 void
    204 TALER_MERCHANT_account_get_cancel (
    205   struct TALER_MERCHANT_AccountGetHandle *tgh)
    206 {
    207   if (NULL != tgh->job)
    208     GNUNET_CURL_job_cancel (tgh->job);
    209   GNUNET_free (tgh->url);
    210   GNUNET_free (tgh);
    211 }