lookup_deposits.c (5114B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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.c 18 * @brief Implementation of the lookup_deposits 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.h" 24 #include "helper.h" 25 26 /** 27 * Closure for #lookup_deposits_cb(). 28 */ 29 struct LookupDepositsContext 30 { 31 /** 32 * Function to call with results. 33 */ 34 TALER_MERCHANTDB_DepositsCallback cb; 35 36 /** 37 * Closure for @e cls. 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_MERCHANTDB_PostgresContext *pg; 45 46 /** 47 * Transaction status (set). 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[in,out] cls of type `struct LookupDepositsContext *` 58 * @param result the postgres result 59 * @param num_results the number of results in @a result 60 */ 61 static void 62 lookup_deposits_cb (void *cls, 63 PGresult *result, 64 unsigned int num_results) 65 { 66 struct LookupDepositsContext *ldc = cls; 67 68 for (unsigned int i = 0; i<num_results; i++) 69 { 70 struct TALER_CoinSpendPublicKeyP coin_pub; 71 struct TALER_Amount amount_with_fee; 72 struct TALER_Amount deposit_fee; 73 struct TALER_Amount refund_fee; 74 char *exchange_url; 75 struct GNUNET_PQ_ResultSpec rs[] = { 76 GNUNET_PQ_result_spec_string ("exchange_url", 77 &exchange_url), 78 GNUNET_PQ_result_spec_auto_from_type ("coin_pub", 79 &coin_pub), 80 TALER_PQ_result_spec_amount_with_currency ("amount_with_fee", 81 &amount_with_fee), 82 TALER_PQ_result_spec_amount_with_currency ("deposit_fee", 83 &deposit_fee), 84 TALER_PQ_result_spec_amount_with_currency ("refund_fee", 85 &refund_fee), 86 GNUNET_PQ_result_spec_end 87 }; 88 89 if (GNUNET_OK != 90 GNUNET_PQ_extract_result (result, 91 rs, 92 i)) 93 { 94 GNUNET_break (0); 95 ldc->qs = GNUNET_DB_STATUS_HARD_ERROR; 96 return; 97 } 98 ldc->cb (ldc->cb_cls, 99 exchange_url, 100 &coin_pub, 101 &amount_with_fee, 102 &deposit_fee, 103 &refund_fee); 104 GNUNET_PQ_cleanup_result (rs); 105 } 106 ldc->qs = num_results; 107 } 108 109 110 enum GNUNET_DB_QueryStatus 111 TALER_MERCHANTDB_lookup_deposits ( 112 struct TALER_MERCHANTDB_PostgresContext *pg, 113 const char *instance_id, 114 const struct TALER_PrivateContractHashP *h_contract_terms, 115 TALER_MERCHANTDB_DepositsCallback cb, 116 void *cb_cls) 117 { 118 struct GNUNET_PQ_QueryParam params[] = { 119 GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 120 GNUNET_PQ_query_param_end 121 }; 122 struct LookupDepositsContext ldc = { 123 .cb = cb, 124 .cb_cls = cb_cls, 125 .pg = pg 126 }; 127 enum GNUNET_DB_QueryStatus qs; 128 129 GNUNET_assert (NULL != pg->current_merchant_id); 130 GNUNET_assert (0 == strcmp (instance_id, 131 pg->current_merchant_id)); 132 /* no preflight check here, run in its own transaction by the caller! */ 133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 134 "Finding deposits for h_contract_terms '%s'\n", 135 GNUNET_h2s (&h_contract_terms->hash)); 136 check_connection (pg); 137 TMH_PQ_prepare_anon (pg, 138 "SELECT" 139 " dcom.exchange_url" 140 ",dep.coin_pub" 141 ",dep.amount_with_fee" 142 ",dep.deposit_fee" 143 ",dep.refund_fee" 144 " FROM merchant_deposits dep" 145 " JOIN merchant_deposit_confirmations dcom" 146 " USING (deposit_confirmation_serial)" 147 " WHERE dcom.order_serial=" 148 " (SELECT order_serial" 149 " FROM merchant_contract_terms" 150 " WHERE h_contract_terms=$1)"); 151 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 152 "", 153 params, 154 &lookup_deposits_cb, 155 &ldc); 156 if (qs <= 0) 157 return qs; 158 return ldc.qs; 159 }