merchant

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

lookup_expected_transfers.c (10218B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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_expected_transfers.c
     18  * @brief Implementation of the lookup_expected_transfers function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_expected_transfers.h"
     24 #include "helper.h"
     25 #include <microhttpd.h> /* for HTTP status codes */
     26 
     27 /**
     28  * Closure for #lookup_expected_transfers_cb().
     29  */
     30 struct LookupExpectedTransfersContext
     31 {
     32   /**
     33    * Function to call on results.
     34    */
     35   TALER_MERCHANTDB_IncomingCallback cb;
     36 
     37   /**
     38    * Closure for @e cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Postgres context.
     44    */
     45   struct TALER_MERCHANTDB_PostgresContext *pg;
     46 
     47   /**
     48    * Transaction status (set).
     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.
     58  *
     59  * @param cls of type `struct LookupExpectedTransfersContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 lookup_expected_transfers_cb (void *cls,
     65                               PGresult *result,
     66                               unsigned int num_results)
     67 {
     68   struct LookupExpectedTransfersContext *ltc = cls;
     69 
     70   for (unsigned int i = 0; i<num_results; i++)
     71   {
     72     struct TALER_Amount expected_credit_amount;
     73     struct TALER_WireTransferIdentifierRawP wtid;
     74     struct TALER_FullPayto payto_uri;
     75     char *exchange_url;
     76     uint64_t expected_transfer_serial_id;
     77     struct GNUNET_TIME_Timestamp execution_time
     78       = GNUNET_TIME_UNIT_ZERO_TS;
     79     bool confirmed;
     80     bool validated;
     81     bool no_amount;
     82     char *last_detail = NULL;
     83     uint32_t last_http_status = 0;
     84     uint32_t last_ec = TALER_EC_NONE;
     85     struct GNUNET_PQ_ResultSpec rs[] = {
     86       GNUNET_PQ_result_spec_allow_null (
     87         TALER_PQ_result_spec_amount_with_currency ("expected_credit_amount",
     88                                                    &expected_credit_amount),
     89         &no_amount),
     90       GNUNET_PQ_result_spec_auto_from_type ("wtid",
     91                                             &wtid),
     92       GNUNET_PQ_result_spec_string ("payto_uri",
     93                                     &payto_uri.full_payto),
     94       GNUNET_PQ_result_spec_string ("exchange_url",
     95                                     &exchange_url),
     96       GNUNET_PQ_result_spec_uint64 ("expected_credit_serial",
     97                                     &expected_transfer_serial_id),
     98       GNUNET_PQ_result_spec_allow_null (
     99         GNUNET_PQ_result_spec_timestamp ("execution_time",
    100                                          &execution_time),
    101         NULL),
    102       GNUNET_PQ_result_spec_bool ("confirmed",
    103                                   &confirmed),
    104       GNUNET_PQ_result_spec_allow_null (
    105         GNUNET_PQ_result_spec_uint32 ("last_http_status",
    106                                       &last_http_status),
    107         NULL),
    108       GNUNET_PQ_result_spec_allow_null (
    109         GNUNET_PQ_result_spec_uint32 ("last_ec",
    110                                       &last_ec),
    111         NULL),
    112       GNUNET_PQ_result_spec_allow_null (
    113         GNUNET_PQ_result_spec_string ("last_detail",
    114                                       &last_detail),
    115         NULL),
    116       GNUNET_PQ_result_spec_end
    117     };
    118 
    119     if (GNUNET_OK !=
    120         GNUNET_PQ_extract_result (result,
    121                                   rs,
    122                                   i))
    123     {
    124       GNUNET_break (0);
    125       ltc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    126       return;
    127     }
    128     validated = ( (MHD_HTTP_OK == last_http_status) &&
    129                   (TALER_EC_NONE == last_ec) );
    130     ltc->cb (ltc->cb_cls,
    131              no_amount
    132              ? NULL
    133              : &expected_credit_amount,
    134              &wtid,
    135              payto_uri,
    136              exchange_url,
    137              expected_transfer_serial_id,
    138              execution_time,
    139              confirmed,
    140              validated,
    141              last_http_status,
    142              last_ec,
    143              last_detail);
    144     GNUNET_PQ_cleanup_result (rs);
    145   }
    146   ltc->qs = num_results;
    147 }
    148 
    149 
    150 enum GNUNET_DB_QueryStatus
    151 TALER_MERCHANTDB_lookup_expected_transfers (
    152   struct TALER_MERCHANTDB_PostgresContext *pg,
    153   const char *instance_id,
    154   struct TALER_FullPayto payto_uri,
    155   struct GNUNET_TIME_Timestamp before,
    156   struct GNUNET_TIME_Timestamp after,
    157   int64_t limit,
    158   uint64_t offset,
    159   enum TALER_EXCHANGE_YesNoAll confirmed,
    160   enum TALER_EXCHANGE_YesNoAll verified,
    161   TALER_MERCHANTDB_IncomingCallback cb,
    162   void *cb_cls)
    163 {
    164   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    165   bool by_time = ( (! GNUNET_TIME_absolute_is_never (before.abs_time)) ||
    166                    (! GNUNET_TIME_absolute_is_zero (after.abs_time)) );
    167   struct LookupExpectedTransfersContext ltc = {
    168     .cb = cb,
    169     .cb_cls = cb_cls,
    170     .pg = pg
    171   };
    172   struct GNUNET_PQ_QueryParam params[] = {
    173     GNUNET_PQ_query_param_timestamp (&before),
    174     GNUNET_PQ_query_param_timestamp (&after),
    175     GNUNET_PQ_query_param_uint64 (&offset),
    176     GNUNET_PQ_query_param_uint64 (&plimit),
    177     NULL == payto_uri.full_payto
    178     ? GNUNET_PQ_query_param_null () /* NULL: do not filter by payto URI */
    179     : GNUNET_PQ_query_param_string (payto_uri.full_payto),
    180     GNUNET_PQ_query_param_bool (! by_time),     /* $6: filter by time? */
    181     GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_ALL == confirmed), /* filter by confirmed? */
    182     GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_YES == confirmed),
    183     GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_ALL == verified), /* filter by verified? */
    184     GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_YES == verified),
    185 
    186     GNUNET_PQ_query_param_end
    187   };
    188   enum GNUNET_DB_QueryStatus qs;
    189 
    190   GNUNET_assert (NULL != pg->current_merchant_id);
    191   GNUNET_assert (0 == strcmp (instance_id,
    192                               pg->current_merchant_id));
    193   check_connection (pg);
    194   if (limit > 0)
    195   {
    196     TMH_PQ_prepare_anon (pg,
    197                          "SELECT"
    198                          " met.expected_credit_amount"
    199                          ",met.wtid"
    200                          ",mac.payto_uri"
    201                          ",met.exchange_url"
    202                          ",met.expected_credit_serial"
    203                          ",mts.execution_time"
    204                          ",met.confirmed"
    205                          ",met.last_http_status"
    206                          ",met.last_ec"
    207                          ",met.last_detail"
    208                          " FROM merchant_expected_transfers met"
    209                          "  JOIN merchant_accounts mac"
    210                          "    USING (account_serial)"
    211                          "  LEFT JOIN merchant_transfer_signatures mts"
    212                          "    USING (expected_credit_serial)"
    213                          " WHERE ( $6 OR "
    214                          "         (mts.execution_time IS NOT NULL AND"
    215                          "          mts.execution_time < $1 AND"
    216                          "          mts.execution_time >= $2) )"
    217                          "   AND ( (CAST($5 AS TEXT) IS NULL) OR "
    218                          "         (REGEXP_REPLACE(mac.payto_uri,'\\?.*','')"
    219                          "         =REGEXP_REPLACE($5,'\\?.*','')) )"
    220                          "   AND ( $7 OR "
    221                          "         (met.confirmed = $8) )"
    222                          "   AND ( $9 OR "
    223                          "         ($10 = (200=met.last_http_status) AND"
    224                          "                (0=met.last_ec) ) )"
    225                          "   AND (met.expected_credit_serial > $3)"
    226                          " ORDER BY met.expected_credit_serial ASC"
    227                          " LIMIT $4");
    228   }
    229   else
    230   {
    231     TMH_PQ_prepare_anon (pg,
    232                          "SELECT"
    233                          " met.expected_credit_amount"
    234                          ",met.wtid"
    235                          ",mac.payto_uri"
    236                          ",met.exchange_url"
    237                          ",met.expected_credit_serial"
    238                          ",mts.execution_time"
    239                          ",met.confirmed"
    240                          ",met.last_http_status"
    241                          ",met.last_ec"
    242                          ",met.last_detail"
    243                          " FROM merchant_expected_transfers met"
    244                          "  JOIN merchant_accounts mac"
    245                          "    USING (account_serial)"
    246                          "  LEFT JOIN merchant_transfer_signatures mts"
    247                          "    USING (expected_credit_serial)"
    248                          " WHERE ( $6 OR "
    249                          "         (mts.execution_time IS NOT NULL AND"
    250                          "          mts.execution_time < $1 AND"
    251                          "          mts.execution_time >= $2) )"
    252                          "   AND ( (CAST($5 AS TEXT) IS NULL) OR "
    253                          "         (REGEXP_REPLACE(mac.payto_uri,'\\?.*','')"
    254                          "         =REGEXP_REPLACE($5,'\\?.*','')) )"
    255                          "   AND ( $7 OR "
    256                          "         (met.confirmed = $8) )"
    257                          "   AND ( $9 OR "
    258                          "         ($10 = (200=met.last_http_status) AND"
    259                          "                (0=met.last_ec) ) )"
    260                          "   AND (met.expected_credit_serial < $3)"
    261                          " ORDER BY met.expected_credit_serial DESC"
    262                          " LIMIT $4");
    263   }
    264   qs = GNUNET_PQ_eval_prepared_multi_select (
    265     pg->conn,
    266     "",
    267     params,
    268     &lookup_expected_transfers_cb,
    269     &ltc);
    270   if (0 >= qs)
    271     return qs;
    272   return ltc.qs;
    273 }