merchant

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

lookup_transfer_details.c (4916B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023 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_transfer_details.c
     18  * @brief Implementation of the lookup_transfer_details function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_transfer_details.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Closure for #lookup_transfer_details_cb().
     28  */
     29 struct LookupTransferDetailsContext
     30 {
     31   /**
     32    * Function to call for each order that was aggregated.
     33    */
     34   TALER_MERCHANTDB_TransferDetailsCallback cb;
     35 
     36   /**
     37    * Closure for @e cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Plugin context.
     43    */
     44   struct TALER_MERCHANTDB_PostgresContext *pg;
     45 
     46   /**
     47    * Transaction result.
     48    */
     49   enum GNUNET_DB_QueryStatus qs;
     50 };
     51 
     52 
     53 /**
     54  * Function to be called with the results of a SELECT statement
     55  * that has returned @a num_results results.
     56  *
     57  * @param cls of type `struct LookupTransferDetailsContext *`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 lookup_transfer_details_cb (void *cls,
     63                             PGresult *result,
     64                             unsigned int num_results)
     65 {
     66   struct LookupTransferDetailsContext *ltdc = cls;
     67 
     68   for (unsigned int i = 0; i<num_results; i++)
     69   {
     70     uint64_t current_offset;
     71     struct TALER_TrackTransferDetails ttd;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_uint64 ("offset_in_exchange_list",
     74                                     &current_offset),
     75       GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
     76                                             &ttd.h_contract_terms),
     77       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     78                                             &ttd.coin_pub),
     79       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
     80                                                  &ttd.coin_value),
     81       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
     82                                                  &ttd.coin_fee),
     83       GNUNET_PQ_result_spec_end
     84     };
     85 
     86     if (GNUNET_OK !=
     87         GNUNET_PQ_extract_result (result,
     88                                   rs,
     89                                   i))
     90     {
     91       GNUNET_break (0);
     92       ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     93       return;
     94     }
     95     ltdc->cb (ltdc->cb_cls,
     96               (unsigned int) current_offset,
     97               &ttd);
     98     GNUNET_PQ_cleanup_result (rs);
     99   }
    100   ltdc->qs = num_results;
    101 }
    102 
    103 
    104 enum GNUNET_DB_QueryStatus
    105 TALER_MERCHANTDB_lookup_transfer_details (
    106   struct TALER_MERCHANTDB_PostgresContext *pg,
    107   const char *exchange_url,
    108   const struct TALER_WireTransferIdentifierRawP *wtid,
    109   TALER_MERCHANTDB_TransferDetailsCallback cb,
    110   void *cb_cls)
    111 {
    112   struct GNUNET_PQ_QueryParam params[] = {
    113     GNUNET_PQ_query_param_string (exchange_url),
    114     GNUNET_PQ_query_param_auto_from_type (wtid),
    115     GNUNET_PQ_query_param_end
    116   };
    117   struct LookupTransferDetailsContext ltdc = {
    118     .cb  = cb,
    119     .cb_cls = cb_cls,
    120     .pg = pg
    121   };
    122   enum GNUNET_DB_QueryStatus qs;
    123 
    124   GNUNET_assert (NULL != pg->current_merchant_id);
    125   check_connection (pg);
    126   TMH_PQ_prepare_anon (pg,
    127                        "SELECT"
    128                        " mterm.h_contract_terms"
    129                        ",mtcoin.offset_in_exchange_list"
    130                        ",dep.coin_pub"
    131                        ",mtcoin.exchange_deposit_value"
    132                        ",mtcoin.exchange_deposit_fee"
    133                        " FROM merchant_expected_transfer_to_coin mtcoin"
    134                        " JOIN merchant_deposits dep"
    135                        "   USING (deposit_serial)"
    136                        " JOIN merchant_deposit_confirmations mcon"
    137                        "   USING (deposit_confirmation_serial)"
    138                        " JOIN merchant_contract_terms mterm"
    139                        "   USING (order_serial)"
    140                        " JOIN merchant_expected_transfers met"
    141                        "   USING (expected_credit_serial)"
    142                        " WHERE met.wtid=$2"
    143                        "   AND met.exchange_url=$1");
    144 
    145   qs = GNUNET_PQ_eval_prepared_multi_select (
    146     pg->conn,
    147     "",
    148     params,
    149     &lookup_transfer_details_cb,
    150     &ltdc);
    151   if (0 >= qs)
    152     return qs;
    153   return ltdc.qs;
    154 }