merchant

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

select_accounts_by_exchange.c (4193B)


      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 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_exchange.c
     18  * @brief Implementation of the select_accounts_by_exchange function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/select_accounts_by_exchange.h"
     26 #include "helper.h"
     27 
     28 
     29 /**
     30  * Closure for #parse_accounts.
     31  */
     32 struct SelectAccountContext
     33 {
     34   /**
     35    * Function to call on each result.
     36    */
     37   TALER_MERCHANTDB_ExchangeAccountCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Set to true on failure.
     46    */
     47   bool failed;
     48 };
     49 
     50 /**
     51  * Function to be called with the results of a SELECT statement
     52  * that has returned @a num_results results about accounts.
     53  *
     54  * @param cls of type `struct SelectAccountContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 parse_accounts (void *cls,
     60                 PGresult *result,
     61                 unsigned int num_results)
     62 {
     63   struct SelectAccountContext *ctx = cls;
     64 
     65   for (unsigned int i = 0; i < num_results; i++)
     66   {
     67     struct TALER_FullPayto payto_uri;
     68     char *conversion_url = NULL;
     69     json_t *debit_restrictions;
     70     json_t *credit_restrictions;
     71     struct TALER_MasterSignatureP master_sig;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     74                                             &master_sig),
     75       GNUNET_PQ_result_spec_string ("payto_uri",
     76                                     &payto_uri.full_payto),
     77       GNUNET_PQ_result_spec_allow_null (
     78         GNUNET_PQ_result_spec_string ("conversion_url",
     79                                       &conversion_url),
     80         NULL),
     81       TALER_PQ_result_spec_json ("debit_restrictions",
     82                                  &debit_restrictions),
     83       TALER_PQ_result_spec_json ("credit_restrictions",
     84                                  &credit_restrictions),
     85       GNUNET_PQ_result_spec_end
     86     };
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       ctx->failed = true;
     95       return;
     96     }
     97     ctx->cb (ctx->cb_cls,
     98              payto_uri,
     99              conversion_url,
    100              debit_restrictions,
    101              credit_restrictions,
    102              &master_sig);
    103     GNUNET_PQ_cleanup_result (rs);
    104   }
    105 }
    106 
    107 
    108 enum GNUNET_DB_QueryStatus
    109 TALER_MERCHANTDB_select_accounts_by_exchange (
    110   struct TALER_MERCHANTDB_PostgresContext *pg,
    111   const struct TALER_MasterPublicKeyP *master_pub,
    112   TALER_MERCHANTDB_ExchangeAccountCallback cb,
    113   void *cb_cls)
    114 {
    115   struct SelectAccountContext ctx = {
    116     .cb = cb,
    117     .cb_cls = cb_cls
    118   };
    119   struct GNUNET_PQ_QueryParam params[] = {
    120     GNUNET_PQ_query_param_auto_from_type (master_pub),
    121     GNUNET_PQ_query_param_end
    122   };
    123   enum GNUNET_DB_QueryStatus qs;
    124 
    125   check_connection (pg);
    126   PREPARE (pg,
    127            "select_exchange_accounts",
    128            "SELECT"
    129            " payto_uri"
    130            ",conversion_url"
    131            ",debit_restrictions::TEXT"
    132            ",credit_restrictions::TEXT"
    133            ",master_sig"
    134            " FROM merchant_exchange_accounts"
    135            " WHERE master_pub=$1;");
    136   qs = GNUNET_PQ_eval_prepared_multi_select (
    137     pg->conn,
    138     "select_exchange_accounts",
    139     params,
    140     &parse_accounts,
    141     &ctx);
    142   if (ctx.failed)
    143     return GNUNET_DB_STATUS_HARD_ERROR;
    144   return qs;
    145 }