iterate_external_refunds.c (4953B)
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/iterate_external_refunds.c 18 * @brief Implementation of the iterate_external_refunds 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/iterate_external_refunds.h" 25 #include "helper.h" 26 27 /** 28 * Closure for #iterate_external_refunds_cb(). 29 */ 30 struct IterateExternalRefundsContext 31 { 32 /** 33 * Function to call for each external refund. 34 */ 35 TALER_MERCHANTDB_ExternalRefundCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Transaction result. 44 */ 45 enum GNUNET_DB_QueryStatus qs; 46 }; 47 48 49 /** 50 * Function to be called with the results of a SELECT statement 51 * that has returned @a num_results results. 52 * 53 * @param cls of type `struct IterateExternalRefundsContext *` 54 * @param result the postgres result 55 * @param num_results the number of results in @a result 56 */ 57 static void 58 iterate_external_refunds_cb (void *cls, 59 PGresult *result, 60 unsigned int num_results) 61 { 62 struct IterateExternalRefundsContext *ierc = cls; 63 64 for (unsigned int i = 0; i<num_results; i++) 65 { 66 char *refund_id; 67 struct GNUNET_TIME_Timestamp timestamp; 68 char *method; 69 char *payment_id = NULL; 70 struct TALER_Amount refund_amount; 71 char *reason; 72 struct GNUNET_PQ_ResultSpec rs[] = { 73 GNUNET_PQ_result_spec_string ("refund_id", 74 &refund_id), 75 GNUNET_PQ_result_spec_timestamp ("refund_timestamp", 76 ×tamp), 77 GNUNET_PQ_result_spec_string ("refund_method", 78 &method), 79 GNUNET_PQ_result_spec_allow_null ( 80 GNUNET_PQ_result_spec_string ("payment_id", 81 &payment_id), 82 NULL), 83 TALER_PQ_result_spec_amount_with_currency ("refund_amount", 84 &refund_amount), 85 GNUNET_PQ_result_spec_string ("reason", 86 &reason), 87 GNUNET_PQ_result_spec_end 88 }; 89 90 if (GNUNET_OK != 91 GNUNET_PQ_extract_result (result, 92 rs, 93 i)) 94 { 95 GNUNET_break (0); 96 ierc->qs = GNUNET_DB_STATUS_HARD_ERROR; 97 return; 98 } 99 ierc->cb (ierc->cb_cls, 100 refund_id, 101 timestamp, 102 method, 103 payment_id, 104 &refund_amount, 105 reason); 106 GNUNET_PQ_cleanup_result (rs); 107 } 108 ierc->qs = num_results; 109 } 110 111 112 enum GNUNET_DB_QueryStatus 113 TALER_MERCHANTDB_iterate_external_refunds ( 114 struct TALER_MERCHANTDB_PostgresContext *pg, 115 const char *instance_id, 116 const char *order_id, 117 TALER_MERCHANTDB_ExternalRefundCallback cb, 118 void *cb_cls) 119 { 120 struct GNUNET_PQ_QueryParam params[] = { 121 GNUNET_PQ_query_param_string (order_id), 122 GNUNET_PQ_query_param_end 123 }; 124 struct IterateExternalRefundsContext ierc = { 125 .cb = cb, 126 .cb_cls = cb_cls 127 }; 128 enum GNUNET_DB_QueryStatus qs; 129 130 GNUNET_assert (NULL != pg->current_merchant_id); 131 GNUNET_assert (0 == strcmp (instance_id, 132 pg->current_merchant_id)); 133 /* no preflight check here, run in transaction by caller! */ 134 TMH_PQ_prepare_anon (pg, 135 "SELECT" 136 " refund_id" 137 ",refund_timestamp" 138 ",refund_method" 139 ",payment_id" 140 ",refund_amount" 141 ",reason" 142 " FROM merchant_refunds_external" 143 " WHERE order_serial=" 144 " (SELECT order_serial" 145 " FROM merchant_contract_terms" 146 " WHERE order_id=$1)" 147 " ORDER BY refund_external_serial ASC"); 148 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 149 "", 150 params, 151 &iterate_external_refunds_cb, 152 &ierc); 153 if (0 >= qs) 154 return qs; 155 return ierc.qs; 156 }