merchant

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

lookup_deposits_by_order.c (5262B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023, 2024 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_deposits_by_order.c
     18  * @brief Implementation of the lookup_deposits_by_order 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_deposits_by_order.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Closure for lookup_deposits_by_order_cb().
     28  */
     29 struct LookupDepositsByOrderContext
     30 {
     31 
     32   /**
     33    * Plugin context.
     34    */
     35   struct TALER_MERCHANTDB_PostgresContext *pg;
     36 
     37   /**
     38    * Function to call with all results.
     39    */
     40   TALER_MERCHANTDB_DepositedCoinsCallback cb;
     41 
     42   /**
     43    * Closure for @e cb.
     44    */
     45   void *cb_cls;
     46 
     47   /**
     48    * Set to the query result.
     49    */
     50   enum GNUNET_DB_QueryStatus qs;
     51 };
     52 
     53 
     54 /**
     55  * Function to be called with the results of a SELECT statement
     56  * that has returned @a num_results results.
     57  *
     58  * @param cls closure of type `struct LookupDepositsByOrderContext *`
     59  * @param result the postgres result
     60  * @param num_results the number of results in @a result
     61  */
     62 static void
     63 lookup_deposits_by_order_cb (void *cls,
     64                              PGresult *result,
     65                              unsigned int num_results)
     66 {
     67   struct LookupDepositsByOrderContext *ldoc = cls;
     68 
     69   for (unsigned int i = 0; i<num_results; i++)
     70   {
     71     uint64_t deposit_serial;
     72     char *exchange_url;
     73     struct TALER_MerchantWireHashP h_wire;
     74     struct TALER_CoinSpendPublicKeyP coin_pub;
     75     struct GNUNET_TIME_Timestamp deposit_timestamp;
     76     struct TALER_Amount amount_with_fee;
     77     struct TALER_Amount deposit_fee;
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_uint64 ("deposit_serial",
     80                                     &deposit_serial),
     81       GNUNET_PQ_result_spec_string ("exchange_url",
     82                                     &exchange_url),
     83       GNUNET_PQ_result_spec_timestamp ("deposit_timestamp",
     84                                        &deposit_timestamp),
     85       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     86                                             &h_wire),
     87       TALER_PQ_result_spec_amount_with_currency ("amount_with_fee",
     88                                                  &amount_with_fee),
     89       TALER_PQ_result_spec_amount_with_currency ("deposit_fee",
     90                                                  &deposit_fee),
     91       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     92                                             &coin_pub),
     93       GNUNET_PQ_result_spec_end
     94     };
     95 
     96     if (GNUNET_OK !=
     97         GNUNET_PQ_extract_result (result,
     98                                   rs,
     99                                   i))
    100     {
    101       GNUNET_break (0);
    102       ldoc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    103       return;
    104     }
    105     ldoc->cb (ldoc->cb_cls,
    106               deposit_serial,
    107               exchange_url,
    108               &h_wire,
    109               deposit_timestamp,
    110               &amount_with_fee,
    111               &deposit_fee,
    112               &coin_pub);
    113     GNUNET_PQ_cleanup_result (rs); /* technically useless here */
    114   }
    115   ldoc->qs = num_results;
    116 }
    117 
    118 
    119 enum GNUNET_DB_QueryStatus
    120 TALER_MERCHANTDB_lookup_deposits_by_order (
    121   struct TALER_MERCHANTDB_PostgresContext *pg,
    122   uint64_t order_serial,
    123   TALER_MERCHANTDB_DepositedCoinsCallback cb,
    124   void *cb_cls)
    125 {
    126   struct LookupDepositsByOrderContext ldoc = {
    127     .pg = pg,
    128     .cb = cb,
    129     .cb_cls = cb_cls
    130   };
    131   struct GNUNET_PQ_QueryParam params[] = {
    132     GNUNET_PQ_query_param_uint64 (&order_serial),
    133     GNUNET_PQ_query_param_end
    134   };
    135   enum GNUNET_DB_QueryStatus qs;
    136 
    137   GNUNET_assert (NULL != pg->current_merchant_id);
    138   check_connection (pg);
    139   TMH_PQ_prepare_anon (pg,
    140                        "SELECT"
    141                        " dep.deposit_serial"
    142                        ",mcon.exchange_url"
    143                        ",acc.h_wire"
    144                        ",mcon.deposit_timestamp"
    145                        ",dep.amount_with_fee"
    146                        ",dep.deposit_fee"
    147                        ",dep.coin_pub"
    148                        " FROM merchant_deposits dep"
    149                        " JOIN merchant_deposit_confirmations mcon"
    150                        "   USING(deposit_confirmation_serial)"
    151                        " JOIN merchant_accounts acc"
    152                        "   USING (account_serial)"
    153                        " WHERE mcon.order_serial=$1");
    154   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    155                                              "",
    156                                              params,
    157                                              &lookup_deposits_by_order_cb,
    158                                              &ldoc);
    159   if (qs < 0)
    160     return qs;
    161   return ldoc.qs;
    162 }