pg_lookup_reconciliation_details.c (5770B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 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_reconciliation_details.c 18 * @brief Implementation of the lookup_reconciliation_details function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/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_reconciliation_details.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Context for TMH_PG_lookup_reconciliation_details(). 31 */ 32 struct ReconciliationContext 33 { 34 /** 35 * Function to call with the results. 36 */ 37 TALER_MERCHANTDB_ReconciliationDetailsCallback cb; 38 39 /** 40 * Closure for @e cb. 41 */ 42 void *cb_cls; 43 44 /** 45 * Database context. 46 */ 47 struct PostgresClosure *pg; 48 49 /** 50 * Set to the return value on errors. 51 */ 52 enum GNUNET_DB_QueryStatus qs; 53 54 }; 55 56 57 /** 58 * Function to be called with the results of a SELECT statement 59 * that has returned @a num_results results about reconciliation 60 * details. 61 * 62 * @param cls of type `struct ReconciliationContext *` 63 * @param result the postgres result 64 * @param num_results the number of results in @a result 65 */ 66 static void 67 reconciliation_cb (void *cls, 68 PGresult *result, 69 unsigned int num_results) 70 { 71 struct ReconciliationContext *lic = cls; 72 73 for (unsigned int i = 0; i < num_results; i++) 74 { 75 char *order_id; 76 uint64_t dvs; 77 uint64_t dfs; 78 uint64_t fvs; 79 uint64_t ffs; 80 char *currency; 81 struct TALER_Amount remaining_deposit; 82 struct TALER_Amount deposit_fee; 83 struct GNUNET_PQ_ResultSpec rs[] = { 84 GNUNET_PQ_result_spec_string ("order_id", 85 &order_id), 86 GNUNET_PQ_result_spec_string ("currency", 87 ¤cy), 88 GNUNET_PQ_result_spec_uint64 ("deposit_value_sum", 89 &dvs), 90 GNUNET_PQ_result_spec_uint64 ("deposit_frac_sum", 91 &dfs), 92 GNUNET_PQ_result_spec_uint64 ("fee_value_sum", 93 &fvs), 94 GNUNET_PQ_result_spec_uint64 ("fee_frac_sum", 95 &ffs), 96 GNUNET_PQ_result_spec_end 97 }; 98 99 if (GNUNET_OK != 100 GNUNET_PQ_extract_result (result, 101 rs, 102 i)) 103 { 104 GNUNET_break (0); 105 lic->qs = GNUNET_DB_STATUS_HARD_ERROR; 106 return; 107 } 108 GNUNET_assert (GNUNET_OK == 109 TALER_amount_set_zero (currency, 110 &remaining_deposit)); 111 GNUNET_assert (GNUNET_OK == 112 TALER_amount_set_zero (currency, 113 &deposit_fee)); 114 remaining_deposit.value = dvs + dfs / TALER_AMOUNT_FRAC_BASE; 115 remaining_deposit.fraction = dfs % TALER_AMOUNT_FRAC_BASE; 116 deposit_fee.value = fvs + ffs / TALER_AMOUNT_FRAC_BASE; 117 deposit_fee.fraction = ffs % TALER_AMOUNT_FRAC_BASE; 118 119 lic->cb (lic->cb_cls, 120 order_id, 121 &remaining_deposit, 122 &deposit_fee); 123 GNUNET_PQ_cleanup_result (rs); 124 } 125 } 126 127 128 enum GNUNET_DB_QueryStatus 129 TMH_PG_lookup_reconciliation_details ( 130 void *cls, 131 const char *instance_id, 132 uint64_t expected_incoming_serial, 133 TALER_MERCHANTDB_ReconciliationDetailsCallback cb, 134 void *cb_cls) 135 { 136 struct PostgresClosure *pg = cls; 137 struct ReconciliationContext lic = { 138 .cb = cb, 139 .cb_cls = cb_cls, 140 .pg = pg 141 }; 142 struct GNUNET_PQ_QueryParam params[] = { 143 GNUNET_PQ_query_param_string (instance_id), 144 GNUNET_PQ_query_param_uint64 (&expected_incoming_serial), 145 GNUNET_PQ_query_param_end 146 }; 147 enum GNUNET_DB_QueryStatus qs; 148 149 check_connection (pg); 150 PREPARE (pg, 151 "lookup_reconciliation_details", 152 "SELECT" 153 " mct.order_id" 154 ",SUM((etc.exchange_deposit_value).val)::INT8 AS deposit_value_sum" 155 ",SUM((etc.exchange_deposit_value).frac)::INT8 AS deposit_frac_sum" 156 ",SUM((etc.exchange_deposit_fee).val)::INT8 AS fee_value_sum" 157 ",SUM((etc.exchange_deposit_fee).frac)::INT8 AS fee_frac_sum" 158 ",(etc.exchange_deposit_value).curr AS currency" 159 " FROM merchant_expected_transfer_to_coin etc" 160 " JOIN merchant_deposits md" 161 " USING (deposit_serial)" 162 " JOIN merchant_deposit_confirmations mdc" 163 " USING (deposit_confirmation_serial)" 164 " JOIN merchant_contract_terms mct" 165 " USING (order_serial)" 166 " JOIN merchant_instances mi" 167 " USING (merchant_serial)" 168 " WHERE expected_credit_serial=$2" 169 " AND mi.merchant_id=$1" 170 " GROUP BY mct.order_id, (etc.exchange_deposit_value).curr;"); 171 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 172 "lookup_reconciliation_details", 173 params, 174 &reconciliation_cb, 175 &lic); 176 if (0 > lic.qs) 177 return lic.qs; 178 return qs; 179 }