merchant

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

select_open_transfers.c (4250B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 2025 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_open_transfers.c
     18  * @brief Implementation of the select_open_transfers function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/select_open_transfers.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context used for open_transfers_cb().
     29  */
     30 struct SelectOpenTransfersContext
     31 {
     32   /**
     33    * Postgres context.
     34    */
     35   struct TALER_MERCHANTDB_PostgresContext *pg;
     36 
     37   /**
     38    * Function to call with the results.
     39    */
     40   TALER_MERCHANTDB_OpenTransferCallback cb;
     41 
     42   /**
     43    * Closure for @a cb.
     44    */
     45   void *cb_cls;
     46 
     47   /**
     48    * Internal result.
     49    */
     50   enum GNUNET_DB_QueryStatus qs;
     51 };
     52 
     53 
     54 /**
     55  * Function to be called with the results of a SELECT statement
     56  * that has returned @a num_results results about rewards.
     57  *
     58  * @param[in,out] cls of type `struct SelectOpenTransfersContext *`
     59  * @param result the postgres result
     60  * @param num_results the number of results in @a result
     61  */
     62 static void
     63 open_transfers_cb (void *cls,
     64                    PGresult *result,
     65                    unsigned int num_results)
     66 {
     67   struct SelectOpenTransfersContext *plc = cls;
     68 
     69   for (unsigned int i = 0; i < num_results; i++)
     70   {
     71     uint64_t rowid;
     72     char *instance_id;
     73     char *exchange_url;
     74     struct TALER_FullPayto payto_uri;
     75     struct TALER_WireTransferIdentifierRawP wtid;
     76     struct GNUNET_TIME_Absolute retry_time;
     77     struct GNUNET_PQ_ResultSpec rs[] = {
     78       GNUNET_PQ_result_spec_uint64 ("out_expected_credit_serial",
     79                                     &rowid),
     80       GNUNET_PQ_result_spec_string ("out_instance_id",
     81                                     &instance_id),
     82       GNUNET_PQ_result_spec_string ("out_exchange_url",
     83                                     &exchange_url),
     84       GNUNET_PQ_result_spec_string ("out_payto_uri",
     85                                     &payto_uri.full_payto),
     86       GNUNET_PQ_result_spec_auto_from_type ("out_wtid",
     87                                             &wtid),
     88       GNUNET_PQ_result_spec_absolute_time ("out_retry_time",
     89                                            &retry_time),
     90       GNUNET_PQ_result_spec_end
     91     };
     92 
     93     if (GNUNET_OK !=
     94         GNUNET_PQ_extract_result (result,
     95                                   rs,
     96                                   i))
     97     {
     98       GNUNET_break (0);
     99       plc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    100       return;
    101     }
    102     plc->cb (plc->cb_cls,
    103              rowid,
    104              instance_id,
    105              exchange_url,
    106              payto_uri,
    107              &wtid,
    108              retry_time);
    109     GNUNET_PQ_cleanup_result (rs);
    110   }
    111 }
    112 
    113 
    114 enum GNUNET_DB_QueryStatus
    115 TALER_MERCHANTDB_select_open_transfers (
    116   struct TALER_MERCHANTDB_PostgresContext *pg,
    117   uint64_t limit,
    118   TALER_MERCHANTDB_OpenTransferCallback cb,
    119   void *cb_cls)
    120 {
    121   struct SelectOpenTransfersContext plc = {
    122     .pg = pg,
    123     .cb = cb,
    124     .cb_cls = cb_cls
    125   };
    126   struct GNUNET_PQ_QueryParam params[] = {
    127     GNUNET_PQ_query_param_uint64 (&limit),
    128     GNUNET_PQ_query_param_end
    129   };
    130   enum GNUNET_DB_QueryStatus qs;
    131 
    132   PREPARE (pg,
    133            "select_open_transfers",
    134            "SELECT"
    135            "  out_expected_credit_serial"
    136            " ,out_instance_id"
    137            " ,out_exchange_url"
    138            " ,out_payto_uri"
    139            " ,out_wtid"
    140            " ,out_retry_time"
    141            " FROM merchant.select_open_transfers($1)");
    142   qs = GNUNET_PQ_eval_prepared_multi_select (
    143     pg->conn,
    144     "select_open_transfers",
    145     params,
    146     &open_transfers_cb,
    147     &plc);
    148   if (0 != plc.qs)
    149     return plc.qs;
    150   return qs;
    151 }