merchant

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

account_kyc_get_outdated.c (4226B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2026 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/account_kyc_get_outdated.c
     18  * @brief Implementation of the account_kyc_get_outdated 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/account_kyc_get_outdated.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Closure for kyc_status_cb().
     30  */
     31 struct KycStatusContext
     32 {
     33   /**
     34    * Function to call with results.
     35    */
     36   TALER_MERCHANTDB_KycOutdatedCallback kyc_cb;
     37 
     38   /**
     39    * Closure for @e kyc_cb.
     40    */
     41   void *kyc_cb_cls;
     42 
     43   /**
     44    * Number of results found.
     45    */
     46   unsigned int count;
     47 
     48   /**
     49    * Set to true on failure(s).
     50    */
     51   bool failure;
     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 accounts.
     58  *
     59  * @param[in,out] cls of type `struct KycStatusContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 kyc_status_cb (void *cls,
     65                PGresult *result,
     66                unsigned int num_results)
     67 {
     68   struct KycStatusContext *ksc = cls;
     69 
     70   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     71               "Got %u outdated KYC records\n",
     72               num_results);
     73   for (unsigned int i = 0; i < num_results; i++)
     74   {
     75     struct TALER_MerchantWireHashP h_wire;
     76     char *exchange_url;
     77     char *instance_id;
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     80                                             &h_wire),
     81       GNUNET_PQ_result_spec_string ("exchange_url",
     82                                     &exchange_url),
     83       GNUNET_PQ_result_spec_string ("instance_id",
     84                                     &instance_id),
     85       GNUNET_PQ_result_spec_end
     86     };
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       ksc->failure = true;
     95       return;
     96     }
     97     ksc->kyc_cb (ksc->kyc_cb_cls,
     98                  instance_id,
     99                  exchange_url,
    100                  &h_wire);
    101     GNUNET_PQ_cleanup_result (rs);
    102   }
    103 }
    104 
    105 
    106 enum GNUNET_DB_QueryStatus
    107 TALER_MERCHANTDB_account_kyc_get_outdated (struct TALER_MERCHANTDB_PostgresContext *pg,
    108                                            TALER_MERCHANTDB_KycOutdatedCallback kyc_cb,
    109                                            void *kyc_cb_cls)
    110 {
    111   struct KycStatusContext ksc = {
    112     .kyc_cb = kyc_cb,
    113     .kyc_cb_cls = kyc_cb_cls
    114   };
    115   struct GNUNET_TIME_Absolute now
    116     = GNUNET_TIME_absolute_get ();
    117   struct GNUNET_PQ_QueryParam params[] = {
    118     GNUNET_PQ_query_param_absolute_time (&now),
    119     GNUNET_PQ_query_param_end
    120   };
    121   enum GNUNET_DB_QueryStatus qs;
    122 
    123   check_connection (pg);
    124   PREPARE (pg,
    125            "account_kyc_get_outdated",
    126            "SELECT "
    127            "  mi.merchant_id"
    128            " ,ma.h_wire"
    129            " ,kyc.exchange_url"
    130            " FROM merchant_kyc kyc"
    131            " JOIN merchant_accounts ma"
    132            "   USING (account_serial)"
    133            " JOIN merchant_instances mi"
    134            "   USING (merchant_serial)"
    135            " WHERE kyc.next_kyc_poll < $1"
    136            " ORDER BY kyc.next_kyc_poll ASC;");
    137   qs = GNUNET_PQ_eval_prepared_multi_select (
    138     pg->conn,
    139     "account_kyc_get_outdated",
    140     params,
    141     &kyc_status_cb,
    142     &ksc);
    143   if (ksc.failure)
    144   {
    145     GNUNET_break (0);
    146     return GNUNET_DB_STATUS_HARD_ERROR;
    147   }
    148   if (0 > qs)
    149     return qs;
    150   return ksc.count;
    151 }