merchant

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

pg_select_wirewatch_accounts.c (4198B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 2023 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_wirewatch_accounts.c
     18  * @brief Implementation of the select_wirewatch_accounts 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 "pg_select_wirewatch_accounts.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #handle_results().
     31  */
     32 struct Context
     33 {
     34   /**
     35    * Function to call with results.
     36    */
     37   TALER_MERCHANTDB_WirewatchWorkCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Set to true if the parsing failed.
     46    */
     47   bool failure;
     48 };
     49 
     50 
     51 /**
     52  * Function to be called with the results of a SELECT statement
     53  * that has returned @a num_results results about accounts.
     54  *
     55  * @param cls of type `struct Context *`
     56  * @param result the postgres result
     57  * @param num_results the number of results in @a result
     58  */
     59 static void
     60 handle_results (void *cls,
     61                 PGresult *result,
     62                 unsigned int num_results)
     63 {
     64   struct Context *ctx = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     char *instance;
     69     struct TALER_FullPayto payto_uri;
     70     char *facade_url;
     71     json_t *credential;
     72     uint64_t last_serial;
     73     struct GNUNET_PQ_ResultSpec rs[] = {
     74       GNUNET_PQ_result_spec_string ("merchant_id",
     75                                     &instance),
     76       GNUNET_PQ_result_spec_string ("payto_uri",
     77                                     &payto_uri.full_payto),
     78       GNUNET_PQ_result_spec_string ("credit_facade_url",
     79                                     &facade_url),
     80       GNUNET_PQ_result_spec_allow_null (
     81         TALER_PQ_result_spec_json ("credit_facade_credentials",
     82                                    &credential),
     83         NULL),
     84       GNUNET_PQ_result_spec_uint64 ("last_bank_serial",
     85                                     &last_serial),
     86       GNUNET_PQ_result_spec_end
     87     };
     88 
     89     if (GNUNET_OK !=
     90         GNUNET_PQ_extract_result (result,
     91                                   rs,
     92                                   i))
     93     {
     94       GNUNET_break (0);
     95       ctx->failure = true;
     96       return;
     97     }
     98     ctx->cb (ctx->cb_cls,
     99              instance,
    100              payto_uri,
    101              facade_url,
    102              credential,
    103              last_serial);
    104     GNUNET_PQ_cleanup_result (rs);
    105   }
    106 }
    107 
    108 
    109 enum GNUNET_DB_QueryStatus
    110 TMH_PG_select_wirewatch_accounts (
    111   void *cls,
    112   TALER_MERCHANTDB_WirewatchWorkCallback cb,
    113   void *cb_cls)
    114 {
    115   struct PostgresClosure *pg = cls;
    116   struct Context ctx = {
    117     .cb = cb,
    118     .cb_cls = cb_cls
    119   };
    120   struct GNUNET_PQ_QueryParam params[] = {
    121     GNUNET_PQ_query_param_end
    122   };
    123   enum GNUNET_DB_QueryStatus qs;
    124 
    125   PREPARE (pg,
    126            "select_wirewatch_progress",
    127            "SELECT"
    128            " last_bank_serial"
    129            ",merchant_id"
    130            ",payto_uri"
    131            ",credit_facade_url"
    132            ",credit_facade_credentials::TEXT"
    133            " FROM merchant_accounts"
    134            " JOIN merchant_instances"
    135            "   USING (merchant_serial)"
    136            " WHERE active"
    137            "   AND credit_facade_url IS NOT NULL");
    138   check_connection (pg);
    139   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    140                                              "select_wirewatch_progress",
    141                                              params,
    142                                              &handle_results,
    143                                              &ctx);
    144   if (ctx.failure)
    145     return GNUNET_DB_STATUS_HARD_ERROR;
    146   return qs;
    147 }