merchant

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

lookup_pending_deposits.c (6662B)


      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_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_pending_deposits.h"
     26 #include "helper.h"
     27 
     28 
     29 /**
     30  * Context for lookup_pending_deposits().
     31  */
     32 struct LookupDepositsContext
     33 {
     34   /**
     35    * Function to call with the results.
     36    */
     37   TALER_MERCHANTDB_PendingDepositsCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Database context.
     46    */
     47   struct TALER_MERCHANTDB_PostgresContext *pg;
     48 
     49   /**
     50    * Set to the return value on errors.
     51    */
     52   enum GNUNET_DB_QueryStatus qs;
     53 
     54 };
     55 
     56 
     57 /**
     58  * Function to be called with the results of a SELECT statement
     59  * that has returned @a num_results results about instances.
     60  *
     61  * @param cls of type `struct LookupDepositsContext *`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 lookup_deposits_cb (void *cls,
     67                     PGresult *result,
     68                     unsigned int num_results)
     69 {
     70   struct LookupDepositsContext *ldc = cls;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     uint64_t deposit_serial;
     75     struct GNUNET_TIME_Absolute wire_deadline;
     76     struct GNUNET_TIME_Absolute retry_time;
     77     struct TALER_PrivateContractHashP h_contract_terms;
     78     struct TALER_MerchantPrivateKeyP merchant_priv;
     79     char *instance_id;
     80     struct TALER_MerchantWireHashP h_wire;
     81     struct TALER_Amount amount_with_fee;
     82     struct TALER_Amount deposit_fee;
     83     struct TALER_CoinSpendPublicKeyP coin_pub;
     84     struct GNUNET_PQ_ResultSpec rs[] = {
     85       GNUNET_PQ_result_spec_uint64 ("deposit_serial",
     86                                     &deposit_serial),
     87       GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
     88                                             &h_contract_terms),
     89       GNUNET_PQ_result_spec_auto_from_type ("merchant_priv",
     90                                             &merchant_priv),
     91       GNUNET_PQ_result_spec_string ("merchant_id",
     92                                     &instance_id),
     93       GNUNET_PQ_result_spec_absolute_time ("wire_transfer_deadline",
     94                                            &wire_deadline),
     95       GNUNET_PQ_result_spec_absolute_time ("retry_time",
     96                                            &retry_time),
     97       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     98                                             &h_wire),
     99       TALER_PQ_result_spec_amount_with_currency ("amount_with_fee",
    100                                                  &amount_with_fee),
    101       TALER_PQ_result_spec_amount_with_currency ("deposit_fee",
    102                                                  &deposit_fee),
    103       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
    104                                             &coin_pub),
    105       GNUNET_PQ_result_spec_end
    106     };
    107 
    108     if (GNUNET_OK !=
    109         GNUNET_PQ_extract_result (result,
    110                                   rs,
    111                                   i))
    112     {
    113       GNUNET_break (0);
    114       ldc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    115       return;
    116     }
    117     ldc->cb (ldc->cb_cls,
    118              deposit_serial,
    119              wire_deadline,
    120              retry_time,
    121              &h_contract_terms,
    122              &merchant_priv,
    123              instance_id,
    124              &h_wire,
    125              &amount_with_fee,
    126              &deposit_fee,
    127              &coin_pub);
    128     GNUNET_PQ_cleanup_result (rs);
    129   }
    130 }
    131 
    132 
    133 enum GNUNET_DB_QueryStatus
    134 TALER_MERCHANTDB_lookup_pending_deposits (
    135   struct TALER_MERCHANTDB_PostgresContext *pg,
    136   const char *exchange_url,
    137   uint64_t limit,
    138   bool allow_future,
    139   TALER_MERCHANTDB_PendingDepositsCallback cb,
    140   void *cb_cls)
    141 {
    142   struct LookupDepositsContext ldc = {
    143     .cb = cb,
    144     .cb_cls = cb_cls,
    145     .pg = pg
    146   };
    147   struct GNUNET_TIME_Absolute now
    148     = GNUNET_TIME_absolute_get ();
    149   struct GNUNET_PQ_QueryParam params[] = {
    150     GNUNET_PQ_query_param_string (exchange_url),
    151     GNUNET_PQ_query_param_absolute_time (&now),
    152     GNUNET_PQ_query_param_uint64 (&limit),
    153     GNUNET_PQ_query_param_bool (allow_future),
    154     GNUNET_PQ_query_param_end
    155   };
    156   enum GNUNET_DB_QueryStatus qs;
    157 
    158   check_connection (pg);
    159   PREPARE (pg,
    160            "lookup_pending_deposits",
    161            "SELECT"
    162            " md.deposit_serial"
    163            ",mct.h_contract_terms"
    164            ",mk.merchant_priv"
    165            ",mi.merchant_id"
    166            ",mdc.wire_transfer_deadline"
    167            ",md.settlement_retry_time AS retry_time"
    168            ",ma.h_wire"
    169            ",md.amount_with_fee"
    170            ",md.deposit_fee"
    171            ",md.coin_pub"
    172            " FROM merchant_deposits md"
    173            " JOIN merchant_deposit_confirmations mdc"
    174            "  USING (deposit_confirmation_serial)"
    175            " JOIN merchant_contract_terms mct"
    176            "  ON (mct.order_serial=mdc.order_serial)"
    177            " JOIN merchant_accounts ma"
    178            "  ON (mdc.account_serial=ma.account_serial)"
    179            " LEFT JOIN merchant_kyc kyc"
    180            "  ON (mdc.account_serial=kyc.account_serial)"
    181            " JOIN merchant_instances mi"
    182            "  ON (mct.merchant_serial=mi.merchant_serial)"
    183            " JOIN merchant_keys mk"
    184            "  ON (mi.merchant_serial=mk.merchant_serial)"
    185            " WHERE (mdc.exchange_url=$1)"
    186            "  AND md.settlement_retry_needed"
    187            "  AND ($4 OR (md.settlement_retry_time < $2))"
    188            "  AND ( (kyc.kyc_ok IS NULL) OR kyc.kyc_ok)"
    189            " ORDER BY md.settlement_retry_time ASC"
    190            " LIMIT $3");
    191   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    192                                              "lookup_pending_deposits",
    193                                              params,
    194                                              &lookup_deposits_cb,
    195                                              &ldc);
    196   if (0 > ldc.qs)
    197     return ldc.qs;
    198   return qs;
    199 }