merchant

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

select_donau_instances.c (5943B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 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_donau_instances.c
     18  * @brief Implementation of the select_donau_instance function for Postgres
     19  * @author Bohdan Potuzhnyi
     20  * @author Vlada Svirsh
     21  */
     22 #include "platform.h"
     23 #include <taler/taler_pq_lib.h>
     24 #include <donau/donau_util.h>
     25 #include "merchant-database/select_donau_instances.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Context for select_donau_instances().
     30  */
     31 struct SelectDonauInstanceContext
     32 {
     33   /**
     34    * Database connection.
     35    */
     36   struct TALER_MERCHANTDB_PostgresContext *pg;
     37 
     38   /**
     39    * Function to call with the results.
     40    */
     41   TALER_MERCHANTDB_DonauInstanceCallback cb;
     42 
     43   /**
     44    * Closure for @e cb.
     45    */
     46   void *cb_cls;
     47 
     48   /**
     49    * Did database result extraction fail?
     50    */
     51   bool extract_failed;
     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 donau instances.
     58  *
     59  * @param[in, out] cls of type `struct SelectDonauInstanceContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 select_donau_instance_cb (void *cls,
     65                           PGresult *result,
     66                           unsigned int num_results)
     67 {
     68   struct SelectDonauInstanceContext *sdc = cls;
     69   struct TALER_MERCHANTDB_PostgresContext *pg = sdc->pg;
     70   struct DONAU_CharityPublicKeyP charity_pub_key;
     71 
     72   GNUNET_static_assert (sizeof (charity_pub_key) ==
     73                         sizeof (pg->current_merchant_pub));
     74   memcpy (&charity_pub_key,
     75           &pg->current_merchant_pub,
     76           sizeof (charity_pub_key));
     77   for (unsigned int i = 0; i < num_results; i++)
     78   {
     79     uint64_t donau_instance_serial;
     80     char *donau_url;
     81     char *charity_name;
     82     uint64_t charity_id;
     83     struct TALER_Amount charity_max_per_year;
     84     struct TALER_Amount charity_receipts_to_date;
     85     int64_t current_year;
     86     json_t *donau_keys_json = NULL;
     87     struct GNUNET_PQ_ResultSpec rs[] = {
     88       GNUNET_PQ_result_spec_uint64 ("donau_instances_serial",
     89                                     &donau_instance_serial),
     90       GNUNET_PQ_result_spec_string ("donau_url",
     91                                     &donau_url),
     92       GNUNET_PQ_result_spec_string ("charity_name",
     93                                     &charity_name),
     94       GNUNET_PQ_result_spec_uint64 ("charity_id",
     95                                     &charity_id),
     96       TALER_PQ_result_spec_amount_with_currency ("charity_max_per_year",
     97                                                  &charity_max_per_year),
     98       TALER_PQ_result_spec_amount_with_currency ("charity_receipts_to_date",
     99                                                  &charity_receipts_to_date),
    100       GNUNET_PQ_result_spec_int64 ("current_year",
    101                                    &current_year),
    102       GNUNET_PQ_result_spec_allow_null (
    103         TALER_PQ_result_spec_json ("keys_json",
    104                                    &donau_keys_json),
    105         NULL),
    106       GNUNET_PQ_result_spec_end
    107     };
    108 
    109     if (GNUNET_OK !=
    110         GNUNET_PQ_extract_result (result,
    111                                   rs,
    112                                   i))
    113     {
    114       GNUNET_break (0);
    115       sdc->extract_failed = true;
    116       return;
    117     }
    118     sdc->cb (sdc->cb_cls,
    119              donau_instance_serial,
    120              donau_url,
    121              charity_name,
    122              &charity_pub_key,
    123              charity_id,
    124              &charity_max_per_year,
    125              &charity_receipts_to_date,
    126              current_year,
    127              donau_keys_json);
    128     GNUNET_PQ_cleanup_result (rs);
    129   }
    130 }
    131 
    132 
    133 enum GNUNET_DB_QueryStatus
    134 TALER_MERCHANTDB_select_donau_instances (
    135   struct TALER_MERCHANTDB_PostgresContext *pg,
    136   const char *id,
    137   TALER_MERCHANTDB_DonauInstanceCallback cb,
    138   void *cb_cls)
    139 {
    140   struct SelectDonauInstanceContext sdc = {
    141     .pg = pg,
    142     .cb = cb,
    143     .cb_cls = cb_cls,
    144     /* Can be overwritten by the select_donau_instance_cb */
    145     .extract_failed = false,
    146   };
    147   struct GNUNET_PQ_QueryParam params[] = {
    148     GNUNET_PQ_query_param_end
    149   };
    150   enum GNUNET_DB_QueryStatus qs;
    151 
    152   GNUNET_assert (NULL != pg->current_merchant_id);
    153   GNUNET_assert (0 == strcmp (id,
    154                               pg->current_merchant_id));
    155   check_connection (pg);
    156   TMH_PQ_prepare_anon (pg,
    157                        "SELECT"
    158                        " di.donau_instances_serial"
    159                        ",di.donau_url"
    160                        ",di.charity_name"
    161                        ",di.charity_id"
    162                        ",di.charity_max_per_year"
    163                        ",di.charity_receipts_to_date"
    164                        ",di.current_year"
    165                        ",dk.keys_json::TEXT"
    166                        " FROM merchant_donau_instances di"
    167                        " LEFT JOIN merchant.merchant_donau_keys dk"
    168                        "   ON di.donau_url = dk.donau_url");
    169   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    170                                              "",
    171                                              params,
    172                                              &select_donau_instance_cb,
    173                                              &sdc);
    174 
    175   /* If there was an error inside select_donau_instance_cb, return a hard error. */
    176   if (sdc.extract_failed)
    177     return GNUNET_DB_STATUS_HARD_ERROR;
    178 
    179   return qs;
    180 }