merchant

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

select_accounts_by_instance.c (5437B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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 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/backenddb/select_accounts_by_instance.c
     18  * @brief Implementation of the select_accounts_by_instance function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/select_accounts_by_instance.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context for select_accounts().
     29  */
     30 struct SelectAccountsContext
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_MERCHANTDB_AccountCallback cb;
     36 
     37   /**
     38    * Closure for @e cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Database context.
     44    */
     45   struct TALER_MERCHANTDB_PostgresContext *pg;
     46 
     47   /**
     48    * Merchant instance ID.
     49    */
     50   const char *id;
     51 
     52   /**
     53    * Set to the return value on errors.
     54    */
     55   enum GNUNET_DB_QueryStatus qs;
     56 
     57 };
     58 
     59 
     60 /**
     61  * Function to be called with the results of a SELECT statement
     62  * that has returned @a num_results results about accounts.
     63  *
     64  * @param cls of type `struct SelectAccountsContext *`
     65  * @param result the postgres result
     66  * @param num_results the number of results in @a result
     67  */
     68 static void
     69 select_account_cb (void *cls,
     70                    PGresult *result,
     71                    unsigned int num_results)
     72 {
     73   struct SelectAccountsContext *lic = cls;
     74 
     75   for (unsigned int i = 0; i < num_results; i++)
     76   {
     77     struct TALER_FullPayto payto;
     78     char *facade_url = NULL;
     79     char *extra_wire_subject_metadata = NULL;
     80     json_t *credential = NULL;
     81     struct TALER_MERCHANTDB_AccountDetails acc;
     82     struct TALER_MerchantPrivateKeyP merchant_priv;
     83     bool no_priv;
     84     struct GNUNET_PQ_ResultSpec rs[] = {
     85       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     86                                             &acc.h_wire),
     87       GNUNET_PQ_result_spec_auto_from_type ("salt",
     88                                             &acc.salt),
     89       GNUNET_PQ_result_spec_string ("payto_uri",
     90                                     &payto.full_payto),
     91       GNUNET_PQ_result_spec_allow_null (
     92         GNUNET_PQ_result_spec_string ("credit_facade_url",
     93                                       &facade_url),
     94         NULL),
     95       GNUNET_PQ_result_spec_allow_null (
     96         GNUNET_PQ_result_spec_string ("extra_wire_subject_metadata",
     97                                       &extra_wire_subject_metadata),
     98         NULL),
     99       GNUNET_PQ_result_spec_allow_null (
    100         TALER_PQ_result_spec_json ("credit_facade_credentials",
    101                                    &credential),
    102         NULL),
    103       GNUNET_PQ_result_spec_bool ("active",
    104                                   &acc.active),
    105       GNUNET_PQ_result_spec_allow_null (
    106         GNUNET_PQ_result_spec_auto_from_type ("merchant_priv",
    107                                               &merchant_priv),
    108         &no_priv),
    109       GNUNET_PQ_result_spec_end
    110     };
    111 
    112     if (GNUNET_OK !=
    113         GNUNET_PQ_extract_result (result,
    114                                   rs,
    115                                   i))
    116     {
    117       GNUNET_break (0);
    118       lic->qs = GNUNET_DB_STATUS_HARD_ERROR;
    119       return;
    120     }
    121     acc.instance_id = lic->id;
    122     acc.payto_uri = payto;
    123     acc.credit_facade_url = facade_url;
    124     acc.credit_facade_credentials = credential;
    125     acc.extra_wire_subject_metadata = extra_wire_subject_metadata;
    126     if (NULL != lic->cb)
    127       lic->cb (lic->cb_cls,
    128                no_priv ? NULL : &merchant_priv,
    129                &acc);
    130     GNUNET_PQ_cleanup_result (rs);
    131   }
    132 }
    133 
    134 
    135 enum GNUNET_DB_QueryStatus
    136 TALER_MERCHANTDB_select_accounts_by_instance (
    137   struct TALER_MERCHANTDB_PostgresContext *pg,
    138   const char *id,
    139   TALER_MERCHANTDB_AccountCallback cb,
    140   void *cb_cls)
    141 {
    142   struct SelectAccountsContext lic = {
    143     .cb = cb,
    144     .cb_cls = cb_cls,
    145     .pg = pg,
    146     .id = id
    147   };
    148   struct GNUNET_PQ_QueryParam params[] = {
    149     GNUNET_PQ_query_param_string (id),
    150     GNUNET_PQ_query_param_end
    151   };
    152   enum GNUNET_DB_QueryStatus qs;
    153 
    154   check_connection (pg);
    155   TMH_PQ_prepare_anon (pg,
    156                        "SELECT"
    157                        " ma.h_wire"
    158                        ",ma.salt"
    159                        ",ma.payto_uri"
    160                        ",ma.credit_facade_url"
    161                        ",ma.credit_facade_credentials::TEXT"
    162                        ",ma.extra_wire_subject_metadata"
    163                        ",ma.active"
    164                        ",mi.merchant_priv"
    165                        " FROM merchant_accounts ma"
    166                        " JOIN merchant.merchant_instances mi"
    167                        "  ON (mi.merchant_id=$1)");
    168   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    169                                              "",
    170                                              params,
    171                                              &select_account_cb,
    172                                              &lic);
    173   if (0 > lic.qs)
    174     return lic.qs;
    175   return qs;
    176 }