merchant

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

pg_select_accounts.c (5853B)


      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 backenddb/pg_select_accounts.c
     18  * @brief Implementation of the select_accounts function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "pg_select_accounts.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Context for select_accounts().
     31  */
     32 struct SelectAccountsContext
     33 {
     34   /**
     35    * Function to call with the results.
     36    */
     37   TALER_MERCHANTDB_AccountCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Database context.
     46    */
     47   struct PostgresClosure *pg;
     48 
     49   /**
     50    * Set to the return value on errors.
     51    */
     52   enum GNUNET_DB_QueryStatus qs;
     53 
     54 };
     55 
     56 
     57 /**
     58  * Function to be called with the results of a SELECT statement
     59  * that has returned @a num_results results about accounts.
     60  *
     61  * @param cls of type `struct SelectAccountsContext *`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 select_account_cb (void *cls,
     67                    PGresult *result,
     68                    unsigned int num_results)
     69 {
     70   struct SelectAccountsContext *lic = cls;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     struct TALER_FullPayto payto;
     75     char *instance_id;
     76     char *facade_url = NULL;
     77     char *extra_wire_subject_metadata = NULL;
     78     json_t *credential = NULL;
     79     struct TALER_MERCHANTDB_AccountDetails acc;
     80     struct TALER_MerchantPrivateKeyP merchant_priv;
     81     bool no_priv;
     82     struct GNUNET_PQ_ResultSpec rs[] = {
     83       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     84                                             &acc.h_wire),
     85       GNUNET_PQ_result_spec_auto_from_type ("salt",
     86                                             &acc.salt),
     87       GNUNET_PQ_result_spec_string ("payto_uri",
     88                                     &payto.full_payto),
     89       GNUNET_PQ_result_spec_string ("merchant_id",
     90                                     &instance_id),
     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 = instance_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 TMH_PG_select_accounts (void *cls,
    137                         const char *id,
    138                         TALER_MERCHANTDB_AccountCallback cb,
    139                         void *cb_cls)
    140 {
    141   struct PostgresClosure *pg = cls;
    142   struct SelectAccountsContext lic = {
    143     .cb = cb,
    144     .cb_cls = cb_cls,
    145     .pg = pg
    146   };
    147   struct GNUNET_PQ_QueryParam params[] = {
    148     NULL == id
    149     ? GNUNET_PQ_query_param_null ()
    150     : GNUNET_PQ_query_param_string (id),
    151     GNUNET_PQ_query_param_end
    152   };
    153   enum GNUNET_DB_QueryStatus qs;
    154 
    155   check_connection (pg);
    156   PREPARE (pg,
    157            "select_accounts",
    158            "SELECT"
    159            " ma.h_wire"
    160            ",ma.salt"
    161            ",ma.payto_uri"
    162            ",ma.credit_facade_url"
    163            ",ma.credit_facade_credentials::TEXT"
    164            ",ma.extra_wire_subject_metadata"
    165            ",ma.active"
    166            ",mk.merchant_priv"
    167            ",mi.merchant_id"
    168            " FROM merchant_accounts ma"
    169            " JOIN merchant_instances mi"
    170            "   ON (mi.merchant_serial=ma.merchant_serial)"
    171            " LEFT JOIN merchant_keys mk"
    172            "   ON (mk.merchant_serial=ma.merchant_serial)"
    173            " WHERE"
    174            "  ($1::TEXT IS NULL) OR"
    175            "  (ma.merchant_serial="
    176            "    (SELECT merchant_serial "
    177            "       FROM merchant_instances"
    178            "      WHERE merchant_id=$1));");
    179   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    180                                              "select_accounts",
    181                                              params,
    182                                              &select_account_cb,
    183                                              &lic);
    184   if (0 > lic.qs)
    185     return lic.qs;
    186   return qs;
    187 }