merchant

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

pg_lookup_transfer_details.c (4893B)


      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 backenddb/pg_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_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "pg_lookup_transfer_details.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Closure for #lookup_transfer_details_cb().
     30  */
     31 struct LookupTransferDetailsContext
     32 {
     33   /**
     34    * Function to call for each order that was aggregated.
     35    */
     36   TALER_MERCHANTDB_TransferDetailsCallback cb;
     37 
     38   /**
     39    * Closure for @e cb.
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Plugin context.
     45    */
     46   struct PostgresClosure *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 LookupTransferDetailsContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 lookup_transfer_details_cb (void *cls,
     65                             PGresult *result,
     66                             unsigned int num_results)
     67 {
     68   struct LookupTransferDetailsContext *ltdc = cls;
     69 
     70   for (unsigned int i = 0; i<num_results; i++)
     71   {
     72     uint64_t current_offset;
     73     struct TALER_TrackTransferDetails ttd;
     74     struct GNUNET_PQ_ResultSpec rs[] = {
     75       GNUNET_PQ_result_spec_uint64 ("offset_in_exchange_list",
     76                                     &current_offset),
     77       GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
     78                                             &ttd.h_contract_terms),
     79       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     80                                             &ttd.coin_pub),
     81       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
     82                                                  &ttd.coin_value),
     83       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
     84                                                  &ttd.coin_fee),
     85       GNUNET_PQ_result_spec_end
     86     };
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     95       return;
     96     }
     97     ltdc->cb (ltdc->cb_cls,
     98               (unsigned int) current_offset,
     99               &ttd);
    100     GNUNET_PQ_cleanup_result (rs);
    101   }
    102   ltdc->qs = num_results;
    103 }
    104 
    105 
    106 enum GNUNET_DB_QueryStatus
    107 TMH_PG_lookup_transfer_details (void *cls,
    108                                 const char *exchange_url,
    109                                 const struct TALER_WireTransferIdentifierRawP *
    110                                 wtid,
    111                                 TALER_MERCHANTDB_TransferDetailsCallback cb,
    112                                 void *cb_cls)
    113 {
    114   struct PostgresClosure *pg = cls;
    115   struct GNUNET_PQ_QueryParam params[] = {
    116     GNUNET_PQ_query_param_string (exchange_url),
    117     GNUNET_PQ_query_param_auto_from_type (wtid),
    118     GNUNET_PQ_query_param_end
    119   };
    120   struct LookupTransferDetailsContext ltdc = {
    121     .cb  = cb,
    122     .cb_cls = cb_cls,
    123     .pg = pg
    124   };
    125   enum GNUNET_DB_QueryStatus qs;
    126 
    127   check_connection (pg);
    128   PREPARE (pg,
    129            "lookup_transfer_details",
    130            "SELECT"
    131            " mterm.h_contract_terms"
    132            ",mtcoin.offset_in_exchange_list"
    133            ",dep.coin_pub"
    134            ",mtcoin.exchange_deposit_value"
    135            ",mtcoin.exchange_deposit_fee"
    136            " FROM merchant_expected_transfer_to_coin mtcoin"
    137            " JOIN merchant_deposits dep"
    138            "   USING (deposit_serial)"
    139            " JOIN merchant_deposit_confirmations mcon"
    140            "   USING (deposit_confirmation_serial)"
    141            " JOIN merchant_contract_terms mterm"
    142            "   USING (order_serial)"
    143            " JOIN merchant_expected_transfers met"
    144            "   USING (expected_credit_serial)"
    145            " WHERE met.wtid=$2"
    146            "   AND met.exchange_url=$1");
    147 
    148   qs = GNUNET_PQ_eval_prepared_multi_select (
    149     pg->conn,
    150     "lookup_transfer_details",
    151     params,
    152     &lookup_transfer_details_cb,
    153     &ltdc);
    154   if (0 >= qs)
    155     return qs;
    156   return ltdc.qs;
    157 }