merchant

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

select_wirewatch_accounts.c (4057B)


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