get_external_refunds_total.c (4126B)
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 src/backenddb/get_external_refunds_total.c 18 * @brief Implementation of the get_external_refunds_total function for Postgres 19 * @author Bohdan Potuzhnyi 20 * @author Volodymyr Potuzhnyi 21 */ 22 #include "platform.h" 23 #include <taler/taler_pq_lib.h> 24 #include "merchant-database/get_external_refunds_total.h" 25 #include "helper.h" 26 27 28 enum GNUNET_DB_QueryStatus 29 TALER_MERCHANTDB_get_external_refunds_total ( 30 struct TALER_MERCHANTDB_PostgresContext *pg, 31 const char *instance_id, 32 const char *order_id, 33 struct TALER_Amount *total, 34 bool *currency_mismatch) 35 { 36 struct GNUNET_PQ_QueryParam params[] = { 37 GNUNET_PQ_query_param_string (order_id), 38 GNUNET_PQ_query_param_end 39 }; 40 uint64_t value_sum; 41 uint64_t frac_sum; 42 uint64_t currencies; 43 char *currency; 44 struct GNUNET_PQ_ResultSpec rs[] = { 45 GNUNET_PQ_result_spec_uint64 ("refund_value_sum", 46 &value_sum), 47 GNUNET_PQ_result_spec_uint64 ("refund_frac_sum", 48 &frac_sum), 49 GNUNET_PQ_result_spec_uint64 ("currencies", 50 ¤cies), 51 GNUNET_PQ_result_spec_string ("currency", 52 ¤cy), 53 GNUNET_PQ_result_spec_end 54 }; 55 enum GNUNET_DB_QueryStatus qs; 56 57 GNUNET_assert (NULL != pg->current_merchant_id); 58 GNUNET_assert (0 == strcmp (instance_id, 59 pg->current_merchant_id)); 60 *currency_mismatch = false; 61 /* no preflight check here, run in transaction by caller! */ 62 TMH_PQ_prepare_anon (pg, 63 "SELECT" 64 " SUM((refund_amount).val)::INT8" 65 " AS refund_value_sum" 66 ",SUM((refund_amount).frac)::INT8" 67 " AS refund_frac_sum" 68 ",COUNT(DISTINCT (refund_amount).curr)::INT8" 69 " AS currencies" 70 ",MIN((refund_amount).curr) AS currency" 71 " FROM merchant_refunds_external" 72 " WHERE order_serial=" 73 " (SELECT order_serial" 74 " FROM merchant_contract_terms" 75 " WHERE order_id=$1)" 76 " HAVING COUNT(*) > 0"); 77 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 78 "", 79 params, 80 rs); 81 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) 82 return qs; 83 if (1 != currencies) 84 { 85 /* Refunds in different currencies were recorded; their sum is 86 meaningless, so we only report the mismatch. */ 87 *currency_mismatch = true; 88 GNUNET_PQ_cleanup_result (rs); 89 return qs; 90 } 91 if (GNUNET_OK != 92 TALER_amount_set_zero (currency, 93 total)) 94 { 95 GNUNET_break (0); 96 GNUNET_PQ_cleanup_result (rs); 97 return GNUNET_DB_STATUS_HARD_ERROR; 98 } 99 GNUNET_PQ_cleanup_result (rs); 100 total->value = value_sum + frac_sum / TALER_AMOUNT_FRAC_BASE; 101 total->fraction = (uint32_t) (frac_sum % TALER_AMOUNT_FRAC_BASE); 102 if (GNUNET_SYSERR == 103 TALER_amount_normalize (total)) 104 { 105 /* Only reachable if the recorded refunds sum beyond the maximum 106 representable amount, which the per-order cap prevents. */ 107 GNUNET_break (0); 108 return GNUNET_DB_STATUS_HARD_ERROR; 109 } 110 return qs; 111 }