merchant

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

select_accounts.c (5279B)


      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.c
     18  * @brief Implementation of the select_accounts 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.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    * Set to the return value on errors.
     49    */
     50   enum GNUNET_DB_QueryStatus qs;
     51 
     52 };
     53 
     54 
     55 /**
     56  * Function to be called with the results of a SELECT statement
     57  * that has returned @a num_results results about accounts.
     58  *
     59  * @param cls of type `struct SelectAccountsContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 select_account_cb (void *cls,
     65                    PGresult *result,
     66                    unsigned int num_results)
     67 {
     68   struct SelectAccountsContext *lic = cls;
     69 
     70   for (unsigned int i = 0; i < num_results; i++)
     71   {
     72     struct TALER_FullPayto payto;
     73     char *instance_id;
     74     char *facade_url = NULL;
     75     char *extra_wire_subject_metadata = NULL;
     76     json_t *credential = NULL;
     77     struct TALER_MERCHANTDB_AccountDetails acc;
     78     struct TALER_MerchantPrivateKeyP merchant_priv;
     79     bool no_priv;
     80     struct GNUNET_PQ_ResultSpec rs[] = {
     81       GNUNET_PQ_result_spec_auto_from_type ("out_h_wire",
     82                                             &acc.h_wire),
     83       GNUNET_PQ_result_spec_auto_from_type ("out_salt",
     84                                             &acc.salt),
     85       GNUNET_PQ_result_spec_string ("out_payto_uri",
     86                                     &payto.full_payto),
     87       GNUNET_PQ_result_spec_string ("out_merchant_id",
     88                                     &instance_id),
     89       GNUNET_PQ_result_spec_allow_null (
     90         GNUNET_PQ_result_spec_string ("out_credit_facade_url",
     91                                       &facade_url),
     92         NULL),
     93       GNUNET_PQ_result_spec_allow_null (
     94         GNUNET_PQ_result_spec_string ("out_extra_wire_subject_metadata",
     95                                       &extra_wire_subject_metadata),
     96         NULL),
     97       GNUNET_PQ_result_spec_allow_null (
     98         TALER_PQ_result_spec_json ("out_credit_facade_credentials",
     99                                    &credential),
    100         NULL),
    101       GNUNET_PQ_result_spec_bool ("out_active",
    102                                   &acc.active),
    103       GNUNET_PQ_result_spec_allow_null (
    104         GNUNET_PQ_result_spec_auto_from_type ("out_merchant_priv",
    105                                               &merchant_priv),
    106         &no_priv),
    107       GNUNET_PQ_result_spec_end
    108     };
    109 
    110     if (GNUNET_OK !=
    111         GNUNET_PQ_extract_result (result,
    112                                   rs,
    113                                   i))
    114     {
    115       GNUNET_break (0);
    116       lic->qs = GNUNET_DB_STATUS_HARD_ERROR;
    117       return;
    118     }
    119     acc.instance_id = instance_id;
    120     acc.payto_uri = payto;
    121     acc.credit_facade_url = facade_url;
    122     acc.credit_facade_credentials = credential;
    123     acc.extra_wire_subject_metadata = extra_wire_subject_metadata;
    124     if (NULL != lic->cb)
    125       lic->cb (lic->cb_cls,
    126                no_priv ? NULL : &merchant_priv,
    127                &acc);
    128     GNUNET_PQ_cleanup_result (rs);
    129   }
    130 }
    131 
    132 
    133 enum GNUNET_DB_QueryStatus
    134 TALER_MERCHANTDB_select_accounts (
    135   struct TALER_MERCHANTDB_PostgresContext *pg,
    136   TALER_MERCHANTDB_AccountCallback cb,
    137   void *cb_cls)
    138 {
    139   struct SelectAccountsContext lic = {
    140     .cb = cb,
    141     .cb_cls = cb_cls,
    142     .pg = pg
    143   };
    144   struct GNUNET_PQ_QueryParam params[] = {
    145     GNUNET_PQ_query_param_end
    146   };
    147   enum GNUNET_DB_QueryStatus qs;
    148 
    149   check_connection (pg);
    150   PREPARE (pg,
    151            "select_accounts",
    152            "SELECT"
    153            "  out_merchant_id"
    154            " ,out_merchant_priv"
    155            " ,out_h_wire"
    156            " ,out_salt"
    157            " ,out_payto_uri"
    158            " ,out_credit_facade_url"
    159            " ,out_credit_facade_credentials::TEXT"
    160            " ,out_extra_wire_subject_metadata"
    161            " ,out_active"
    162            " FROM merchant.select_accounts()");
    163   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    164                                              "select_accounts",
    165                                              params,
    166                                              &select_account_cb,
    167                                              &lic);
    168   if (0 > lic.qs)
    169     return lic.qs;
    170   return qs;
    171 }