merchant

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

taler-merchant-httpd_get-private-accounts-H_WIRE.c (6564B)


      1 /*
      2   This file is part of TALER
      3   (C) 2023, 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, 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 General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file src/backend/taler-merchant-httpd_get-private-accounts-H_WIRE.c
     18  * @brief implement GET /accounts/$ID
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "taler-merchant-httpd_get-private-accounts-H_WIRE.h"
     23 #include <taler/taler_json_lib.h>
     24 #include <taler/taler_templating_lib.h>
     25 #include "merchant-database/select_account.h"
     26 
     27 
     28 /**
     29  * Handle a GET "/accounts/$ID" request.
     30  *
     31  * @param rh context of the handler
     32  * @param connection the MHD connection to handle
     33  * @param[in,out] hc context with further information about the request
     34  * @return MHD result code
     35  */
     36 enum MHD_Result
     37 TMH_private_get_accounts_ID (const struct TMH_RequestHandler *rh,
     38                              struct MHD_Connection *connection,
     39                              struct TMH_HandlerContext *hc)
     40 {
     41   struct TMH_MerchantInstance *mi = hc->instance;
     42   const char *h_wire_s = hc->infix;
     43   struct TALER_MerchantWireHashP h_wire;
     44   struct TALER_MERCHANTDB_AccountDetails tp = { 0 };
     45   enum GNUNET_DB_QueryStatus qs;
     46   enum
     47   {
     48     POF_JSON,
     49     POF_TEXT
     50   } format;
     51   enum MHD_Result ret;
     52 
     53   GNUNET_assert (NULL != mi);
     54   GNUNET_assert (NULL != h_wire_s);
     55   if (GNUNET_OK !=
     56       GNUNET_STRINGS_string_to_data (h_wire_s,
     57                                      strlen (h_wire_s),
     58                                      &h_wire,
     59                                      sizeof (h_wire)))
     60   {
     61     GNUNET_break_op (0);
     62     return TALER_MHD_reply_with_error (connection,
     63                                        MHD_HTTP_BAD_REQUEST,
     64                                        TALER_EC_MERCHANT_GENERIC_H_WIRE_MALFORMED,
     65                                        h_wire_s);
     66   }
     67   qs = TALER_MERCHANTDB_select_account (TMH_db,
     68                                         mi->settings.id,
     69                                         &h_wire,
     70                                         &tp);
     71   if (0 > qs)
     72   {
     73     GNUNET_break (0);
     74     return TALER_MHD_reply_with_error (connection,
     75                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     76                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     77                                        "lookup_account");
     78   }
     79   if (0 == qs)
     80   {
     81     return TALER_MHD_reply_with_error (connection,
     82                                        MHD_HTTP_NOT_FOUND,
     83                                        TALER_EC_MERCHANT_GENERIC_ACCOUNT_UNKNOWN,
     84                                        hc->infix);
     85   }
     86 
     87   /* Determine desired output format from Accept header */
     88   {
     89     const char *mime;
     90 
     91     mime = MHD_lookup_connection_value (connection,
     92                                         MHD_HEADER_KIND,
     93                                         MHD_HTTP_HEADER_ACCEPT);
     94     if (NULL == mime)
     95       mime = "application/json";
     96     if (0 == strcmp (mime,
     97                      "*/*"))
     98       mime = "application/json";
     99     if (0 == strcmp (mime,
    100                      "application/json"))
    101     {
    102       format = POF_JSON;
    103     }
    104     else if (0 == strcmp (mime,
    105                           "text/plain"))
    106     {
    107       format = POF_TEXT;
    108     }
    109     else
    110     {
    111       GNUNET_break_op (0);
    112       return TALER_MHD_REPLY_JSON_PACK (
    113         connection,
    114         MHD_HTTP_NOT_ACCEPTABLE,
    115         GNUNET_JSON_pack_string ("hint",
    116                                  mime));
    117     }
    118   }
    119 
    120   switch (format)
    121   {
    122   case POF_JSON:
    123     ret = TALER_MHD_REPLY_JSON_PACK (
    124       connection,
    125       MHD_HTTP_OK,
    126       GNUNET_JSON_pack_bool ("active",
    127                              tp.active),
    128       TALER_JSON_pack_full_payto ("payto_uri",
    129                                   tp.payto_uri),
    130       GNUNET_JSON_pack_data_auto ("h_wire",
    131                                   &tp.h_wire),
    132       GNUNET_JSON_pack_data_auto ("salt",
    133                                   &tp.salt),
    134       GNUNET_JSON_pack_allow_null (
    135         GNUNET_JSON_pack_string ("extra_wire_subject_metadata",
    136                                  tp.extra_wire_subject_metadata)),
    137       GNUNET_JSON_pack_allow_null (
    138         GNUNET_JSON_pack_string ("credit_facade_url",
    139                                  tp.credit_facade_url)));
    140     /* We do not return the credentials, as they may
    141        be sensitive */
    142     break;
    143   case POF_TEXT:
    144     {
    145       struct MHD_Response *resp;
    146       json_t *obj;
    147       unsigned int response_code = MHD_HTTP_OK;
    148       enum GNUNET_GenericReturnValue res;
    149 
    150       obj = GNUNET_JSON_PACK (
    151         GNUNET_JSON_pack_bool ("active",
    152                                tp.active),
    153         TALER_JSON_pack_full_payto ("payto_uri",
    154                                     tp.payto_uri));
    155       res = TALER_TEMPLATING_build (connection,
    156                                     &response_code,
    157                                     "account_status",
    158                                     mi->settings.id,
    159                                     NULL,
    160                                     obj,
    161                                     &resp);
    162       json_decref (obj);
    163       if (GNUNET_OK != res)
    164       {
    165         ret = TALER_MHD_REPLY_JSON_PACK (
    166           connection,
    167           MHD_HTTP_NOT_ACCEPTABLE,
    168           GNUNET_JSON_pack_string ("hint",
    169                                    "text/plain"));
    170         break;
    171       }
    172       TALER_MHD_add_global_headers (resp,
    173                                     false);
    174       GNUNET_break (MHD_YES ==
    175                     MHD_add_response_header (resp,
    176                                              MHD_HTTP_HEADER_CONTENT_TYPE,
    177                                              "text/plain"));
    178       ret = MHD_queue_response (connection,
    179                                 response_code,
    180                                 resp);
    181       MHD_destroy_response (resp);
    182     }
    183     break;
    184   }
    185   json_decref (tp.credit_facade_credentials);
    186   GNUNET_free (tp.extra_wire_subject_metadata);
    187   GNUNET_free (tp.payto_uri.full_payto);
    188   GNUNET_free (tp.credit_facade_url);
    189   return ret;
    190 }
    191 
    192 
    193 /* end of taler-merchant-httpd_get-private-accounts-H_WIRE.c */