merchant

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

lookup_transfer_summary.c (4472B)


      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_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_transfer_summary.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Closure for #lookup_transfer_summary_cb().
     30  */
     31 struct LookupTransferSummaryContext
     32 {
     33   /**
     34    * Function to call for each order that was aggregated.
     35    */
     36   TALER_MERCHANTDB_TransferSummaryCallback cb;
     37 
     38   /**
     39    * Closure for @e cb.
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Plugin context.
     45    */
     46   struct TALER_MERCHANTDB_PostgresContext *pg;
     47 
     48   /**
     49    * Transaction result.
     50    */
     51   enum GNUNET_DB_QueryStatus qs;
     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 LookupTransferSummaryContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 lookup_transfer_summary_cb (void *cls,
     65                             PGresult *result,
     66                             unsigned int num_results)
     67 {
     68   struct LookupTransferSummaryContext *ltdc = cls;
     69 
     70   for (unsigned int i = 0; i<num_results; i++)
     71   {
     72     char *order_id;
     73     struct TALER_Amount deposit_value;
     74     struct TALER_Amount deposit_fee;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_string ("order_id",
     77                                     &order_id),
     78       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
     79                                                  &deposit_value),
     80       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
     81                                                  &deposit_fee),
     82       GNUNET_PQ_result_spec_end
     83     };
     84 
     85     if (GNUNET_OK !=
     86         GNUNET_PQ_extract_result (result,
     87                                   rs,
     88                                   i))
     89     {
     90       GNUNET_break (0);
     91       ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     92       return;
     93     }
     94     ltdc->cb (ltdc->cb_cls,
     95               order_id,
     96               &deposit_value,
     97               &deposit_fee);
     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_summary (
    106   struct TALER_MERCHANTDB_PostgresContext *pg,
    107   const char *exchange_url,
    108   const struct TALER_WireTransferIdentifierRawP *wtid,
    109   TALER_MERCHANTDB_TransferSummaryCallback 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 LookupTransferSummaryContext ltdc = {
    118     .cb  = cb,
    119     .cb_cls = cb_cls,
    120     .pg = pg
    121   };
    122   enum GNUNET_DB_QueryStatus qs;
    123 
    124   check_connection (pg);
    125   PREPARE (pg,
    126            "lookup_transfer_summary",
    127            "SELECT"
    128            " mct.order_id"
    129            ",mtc.exchange_deposit_value"
    130            ",mtc.exchange_deposit_fee"
    131            " FROM merchant_expected_transfers met"
    132            "  JOIN merchant_expected_transfer_to_coin mtc"
    133            "    USING (expected_credit_serial)"
    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 mct"
    139            "    USING (order_serial)"
    140            " WHERE met.wtid=$2"
    141            "   AND met.exchange_url=$1");
    142 
    143   qs = GNUNET_PQ_eval_prepared_multi_select (
    144     pg->conn,
    145     "lookup_transfer_summary",
    146     params,
    147     &lookup_transfer_summary_cb,
    148     &ltdc);
    149   if (0 >= qs)
    150     return qs;
    151   return ltdc.qs;
    152 }