merchant

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

lookup_pending_deposits.c (5777B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023, 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/lookup_pending_deposits.c
     18  * @brief Implementation of the lookup_pending_deposits function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_pending_deposits.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context for lookup_pending_deposits().
     29  */
     30 struct LookupDepositsContext
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_MERCHANTDB_PendingDepositsCallback cb;
     36 
     37   /**
     38    * Closure for @e cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Database context.
     44    */
     45   struct TALER_MERCHANTDB_PostgresContext *pg;
     46 
     47   /**
     48    * Set to the return value on errors.
     49    */
     50   enum GNUNET_DB_QueryStatus qs;
     51 
     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 instances.
     58  *
     59  * @param cls of type `struct LookupDepositsContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 lookup_deposits_cb (void *cls,
     65                     PGresult *result,
     66                     unsigned int num_results)
     67 {
     68   struct LookupDepositsContext *ldc = cls;
     69 
     70   for (unsigned int i = 0; i < num_results; i++)
     71   {
     72     uint64_t deposit_serial;
     73     struct GNUNET_TIME_Absolute wire_deadline;
     74     struct GNUNET_TIME_Absolute retry_time;
     75     struct TALER_PrivateContractHashP h_contract_terms;
     76     struct TALER_MerchantPrivateKeyP merchant_priv;
     77     char *instance_id;
     78     struct TALER_MerchantWireHashP h_wire;
     79     struct TALER_Amount amount_with_fee;
     80     struct TALER_Amount deposit_fee;
     81     struct TALER_CoinSpendPublicKeyP coin_pub;
     82     struct GNUNET_PQ_ResultSpec rs[] = {
     83       GNUNET_PQ_result_spec_uint64 ("out_deposit_serial",
     84                                     &deposit_serial),
     85       GNUNET_PQ_result_spec_auto_from_type ("out_h_contract_terms",
     86                                             &h_contract_terms),
     87       GNUNET_PQ_result_spec_auto_from_type ("out_merchant_priv",
     88                                             &merchant_priv),
     89       GNUNET_PQ_result_spec_string ("out_merchant_id",
     90                                     &instance_id),
     91       GNUNET_PQ_result_spec_absolute_time ("out_wire_transfer_deadline",
     92                                            &wire_deadline),
     93       GNUNET_PQ_result_spec_absolute_time ("out_retry_time",
     94                                            &retry_time),
     95       GNUNET_PQ_result_spec_auto_from_type ("out_h_wire",
     96                                             &h_wire),
     97       TALER_PQ_result_spec_amount_with_currency ("out_amount_with_fee",
     98                                                  &amount_with_fee),
     99       TALER_PQ_result_spec_amount_with_currency ("out_deposit_fee",
    100                                                  &deposit_fee),
    101       GNUNET_PQ_result_spec_auto_from_type ("out_coin_pub",
    102                                             &coin_pub),
    103       GNUNET_PQ_result_spec_end
    104     };
    105 
    106     if (GNUNET_OK !=
    107         GNUNET_PQ_extract_result (result,
    108                                   rs,
    109                                   i))
    110     {
    111       GNUNET_break (0);
    112       ldc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    113       return;
    114     }
    115     ldc->cb (ldc->cb_cls,
    116              deposit_serial,
    117              wire_deadline,
    118              retry_time,
    119              &h_contract_terms,
    120              &merchant_priv,
    121              instance_id,
    122              &h_wire,
    123              &amount_with_fee,
    124              &deposit_fee,
    125              &coin_pub);
    126     GNUNET_PQ_cleanup_result (rs);
    127   }
    128 }
    129 
    130 
    131 enum GNUNET_DB_QueryStatus
    132 TALER_MERCHANTDB_lookup_pending_deposits (
    133   struct TALER_MERCHANTDB_PostgresContext *pg,
    134   const char *exchange_url,
    135   uint64_t limit,
    136   bool allow_future,
    137   TALER_MERCHANTDB_PendingDepositsCallback cb,
    138   void *cb_cls)
    139 {
    140   struct LookupDepositsContext ldc = {
    141     .cb = cb,
    142     .cb_cls = cb_cls,
    143     .pg = pg
    144   };
    145   struct GNUNET_TIME_Absolute now
    146     = GNUNET_TIME_absolute_get ();
    147   struct GNUNET_PQ_QueryParam params[] = {
    148     GNUNET_PQ_query_param_string (exchange_url),
    149     GNUNET_PQ_query_param_absolute_time (&now),
    150     GNUNET_PQ_query_param_uint64 (&limit),
    151     GNUNET_PQ_query_param_bool (allow_future),
    152     GNUNET_PQ_query_param_end
    153   };
    154   enum GNUNET_DB_QueryStatus qs;
    155 
    156   check_connection (pg);
    157   PREPARE (pg,
    158            "lookup_pending_deposits",
    159            "SELECT"
    160            "  out_deposit_serial"
    161            " ,out_h_contract_terms"
    162            " ,out_merchant_priv"
    163            " ,out_merchant_id"
    164            " ,out_wire_transfer_deadline"
    165            " ,out_retry_time"
    166            " ,out_h_wire"
    167            " ,out_amount_with_fee"
    168            " ,out_deposit_fee"
    169            " ,out_coin_pub"
    170            " FROM merchant.lookup_pending_deposits($1, $2, $3, $4)");
    171   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    172                                              "lookup_pending_deposits",
    173                                              params,
    174                                              &lookup_deposits_cb,
    175                                              &ldc);
    176   if (0 > ldc.qs)
    177     return ldc.qs;
    178   return qs;
    179 }