merchant

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

account_kyc_get_status.c (6784B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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_status.c
     18  * @brief Implementation of the account_kyc_get_status function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/account_kyc_get_status.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Closure for kyc_status_cb().
     28  */
     29 struct KycStatusContext
     30 {
     31   /**
     32    * Function to call with results.
     33    */
     34   TALER_MERCHANTDB_KycCallback kyc_cb;
     35 
     36   /**
     37    * Closure for @e kyc_cb.
     38    */
     39   void *kyc_cb_cls;
     40 
     41   /**
     42    * Number of results found.
     43    */
     44   unsigned int count;
     45 
     46   /**
     47    * Set to true on failure(s).
     48    */
     49   bool failure;
     50 };
     51 
     52 
     53 /**
     54  * Function to be called with the results of a SELECT statement
     55  * that has returned @a num_results results about accounts.
     56  *
     57  * @param[in,out] cls of type `struct KycStatusContext *`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 kyc_status_cb (void *cls,
     63                PGresult *result,
     64                unsigned int num_results)
     65 {
     66   struct KycStatusContext *ksc = cls;
     67 
     68   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     69               "Got %u KYC records\n",
     70               num_results);
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     struct TALER_MerchantWireHashP h_wire;
     74     char *exchange_url = NULL;
     75     struct TALER_FullPayto payto_uri;
     76     struct GNUNET_TIME_Timestamp last_check;
     77     bool kyc_ok = false;
     78     struct TALER_AccountAccessTokenP access_token;
     79     bool no_auth = false;
     80     uint32_t h32 = 0;
     81     uint32_t e32 = 0;
     82     bool in_aml_review = false;
     83     bool no_mk; /* left join on merchant_kyc table yielded nothing */
     84     json_t *jlimits = NULL;
     85     struct GNUNET_PQ_ResultSpec rs[] = {
     86       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     87                                             &h_wire),
     88       GNUNET_PQ_result_spec_string ("payto_uri",
     89                                     &payto_uri.full_payto),
     90       GNUNET_PQ_result_spec_allow_null (
     91         GNUNET_PQ_result_spec_string ("exchange_url",
     92                                       &exchange_url),
     93         &no_mk),
     94       GNUNET_PQ_result_spec_allow_null (
     95         GNUNET_PQ_result_spec_timestamp ("kyc_timestamp",
     96                                          &last_check),
     97         &no_mk),
     98       GNUNET_PQ_result_spec_allow_null (
     99         GNUNET_PQ_result_spec_bool ("kyc_ok",
    100                                     &kyc_ok),
    101         &no_mk),
    102       GNUNET_PQ_result_spec_allow_null (
    103         GNUNET_PQ_result_spec_auto_from_type ("access_token",
    104                                               &access_token),
    105         &no_auth),
    106       GNUNET_PQ_result_spec_allow_null (
    107         GNUNET_PQ_result_spec_uint32 ("exchange_http_status",
    108                                       &h32),
    109         &no_mk),
    110       GNUNET_PQ_result_spec_allow_null (
    111         GNUNET_PQ_result_spec_uint32 ("exchange_ec_code",
    112                                       &e32),
    113         &no_mk),
    114       GNUNET_PQ_result_spec_allow_null (
    115         GNUNET_PQ_result_spec_bool ("aml_review",
    116                                     &in_aml_review),
    117         &no_mk),
    118       GNUNET_PQ_result_spec_allow_null (
    119         TALER_PQ_result_spec_json ("jaccount_limits",
    120                                    &jlimits),
    121         NULL),
    122       GNUNET_PQ_result_spec_end
    123     };
    124     unsigned int last_http_status;
    125     enum TALER_ErrorCode last_ec;
    126 
    127     if (GNUNET_OK !=
    128         GNUNET_PQ_extract_result (result,
    129                                   rs,
    130                                   i))
    131     {
    132       GNUNET_break (0);
    133       ksc->failure = true;
    134       return;
    135     }
    136     last_http_status = (unsigned int) h32;
    137     last_ec = (enum TALER_ErrorCode) (int) e32;
    138     ksc->count++;
    139     ksc->kyc_cb (ksc->kyc_cb_cls,
    140                  &h_wire,
    141                  payto_uri,
    142                  exchange_url,
    143                  last_check,
    144                  kyc_ok,
    145                  (no_auth)
    146                  ? NULL
    147                  : &access_token,
    148                  last_http_status,
    149                  last_ec,
    150                  in_aml_review,
    151                  jlimits);
    152     GNUNET_PQ_cleanup_result (rs);
    153   }
    154 }
    155 
    156 
    157 enum GNUNET_DB_QueryStatus
    158 TALER_MERCHANTDB_account_kyc_get_status (
    159   struct TALER_MERCHANTDB_PostgresContext *pg,
    160   const char *merchant_id,
    161   const struct TALER_MerchantWireHashP *h_wire,
    162   const char *exchange_url,
    163   TALER_MERCHANTDB_KycCallback kyc_cb,
    164   void *kyc_cb_cls)
    165 {
    166   struct KycStatusContext ksc = {
    167     .kyc_cb = kyc_cb,
    168     .kyc_cb_cls = kyc_cb_cls
    169   };
    170   struct GNUNET_TIME_Absolute now
    171     = GNUNET_TIME_absolute_get ();
    172   struct GNUNET_PQ_QueryParam params[] = {
    173     GNUNET_PQ_query_param_absolute_time (&now),
    174     NULL == exchange_url
    175     ? GNUNET_PQ_query_param_null ()
    176     : GNUNET_PQ_query_param_string (exchange_url),
    177     NULL == h_wire
    178     ? GNUNET_PQ_query_param_null ()
    179     : GNUNET_PQ_query_param_auto_from_type (h_wire),
    180     GNUNET_PQ_query_param_end
    181   };
    182   enum GNUNET_DB_QueryStatus qs;
    183 
    184   GNUNET_assert (NULL != pg->current_merchant_id);
    185   GNUNET_assert (0 == strcmp (merchant_id,
    186                               pg->current_merchant_id));
    187   check_connection (pg);
    188   TMH_PQ_prepare_anon (pg,
    189                        "SELECT "
    190                        "  out_h_wire AS h_wire"
    191                        " ,out_payto_uri AS payto_uri"
    192                        " ,out_exchange_url AS exchange_url"
    193                        " ,out_kyc_timestamp AS kyc_timestamp"
    194                        " ,out_kyc_ok AS kyc_ok"
    195                        " ,out_access_token AS access_token"
    196                        " ,out_exchange_http_status AS exchange_http_status"
    197                        " ,out_exchange_ec_code AS exchange_ec_code"
    198                        " ,out_aml_review AS aml_review"
    199                        " ,out_jaccount_limits::TEXT AS jaccount_limits"
    200                        " FROM merchant_do_account_kyc_get_status($1, $2, $3);");
    201   qs = GNUNET_PQ_eval_prepared_multi_select (
    202     pg->conn,
    203     "",
    204     params,
    205     &kyc_status_cb,
    206     &ksc);
    207   if (ksc.failure)
    208   {
    209     GNUNET_break (0);
    210     return GNUNET_DB_STATUS_HARD_ERROR;
    211   }
    212   if (0 > qs)
    213     return qs;
    214   return ksc.count;
    215 }