merchant

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

lookup_transfer_summary.c (4583B)


      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_transfer_summary.c
     18  * @brief Implementation of the lookup_transfer_summary 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_summary.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Closure for #lookup_transfer_summary_cb().
     28  */
     29 struct LookupTransferSummaryContext
     30 {
     31   /**
     32    * Function to call for each order that was aggregated.
     33    */
     34   TALER_MERCHANTDB_TransferSummaryCallback 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 LookupTransferSummaryContext *`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 lookup_transfer_summary_cb (void *cls,
     63                             PGresult *result,
     64                             unsigned int num_results)
     65 {
     66   struct LookupTransferSummaryContext *ltdc = cls;
     67 
     68   for (unsigned int i = 0; i<num_results; i++)
     69   {
     70     char *order_id;
     71     struct TALER_Amount deposit_value;
     72     struct TALER_Amount deposit_fee;
     73     struct GNUNET_PQ_ResultSpec rs[] = {
     74       GNUNET_PQ_result_spec_string ("order_id",
     75                                     &order_id),
     76       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
     77                                                  &deposit_value),
     78       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
     79                                                  &deposit_fee),
     80       GNUNET_PQ_result_spec_end
     81     };
     82 
     83     if (GNUNET_OK !=
     84         GNUNET_PQ_extract_result (result,
     85                                   rs,
     86                                   i))
     87     {
     88       GNUNET_break (0);
     89       ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     90       return;
     91     }
     92     ltdc->cb (ltdc->cb_cls,
     93               order_id,
     94               &deposit_value,
     95               &deposit_fee);
     96     GNUNET_PQ_cleanup_result (rs);
     97   }
     98   ltdc->qs = num_results;
     99 }
    100 
    101 
    102 enum GNUNET_DB_QueryStatus
    103 TALER_MERCHANTDB_lookup_transfer_summary (
    104   struct TALER_MERCHANTDB_PostgresContext *pg,
    105   const char *exchange_url,
    106   const struct TALER_WireTransferIdentifierRawP *wtid,
    107   TALER_MERCHANTDB_TransferSummaryCallback cb,
    108   void *cb_cls)
    109 {
    110   struct GNUNET_PQ_QueryParam params[] = {
    111     GNUNET_PQ_query_param_string (exchange_url),
    112     GNUNET_PQ_query_param_auto_from_type (wtid),
    113     GNUNET_PQ_query_param_end
    114   };
    115   struct LookupTransferSummaryContext ltdc = {
    116     .cb  = cb,
    117     .cb_cls = cb_cls,
    118     .pg = pg
    119   };
    120   enum GNUNET_DB_QueryStatus qs;
    121 
    122   GNUNET_assert (NULL != pg->current_merchant_id);
    123   check_connection (pg);
    124   TMH_PQ_prepare_anon (pg,
    125                        "SELECT"
    126                        " mct.order_id"
    127                        ",mtc.exchange_deposit_value"
    128                        ",mtc.exchange_deposit_fee"
    129                        " FROM merchant_expected_transfers met"
    130                        "  JOIN merchant_expected_transfer_to_coin mtc"
    131                        "    USING (expected_credit_serial)"
    132                        "  JOIN merchant_deposits dep"
    133                        "    USING (deposit_serial)"
    134                        "  JOIN merchant_deposit_confirmations mcon"
    135                        "    USING (deposit_confirmation_serial)"
    136                        "  JOIN merchant_contract_terms mct"
    137                        "    USING (order_serial)"
    138                        " WHERE met.wtid=$2"
    139                        "   AND met.exchange_url=$1");
    140 
    141   qs = GNUNET_PQ_eval_prepared_multi_select (
    142     pg->conn,
    143     "",
    144     params,
    145     &lookup_transfer_summary_cb,
    146     &ltdc);
    147   if (0 >= qs)
    148     return qs;
    149   return ltdc.qs;
    150 }