anastasis-db_test_auth_iban_payment.c (4472B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020-2022 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file stasis/anastasis-db_test_auth_iban_payment.c 18 * @brief Anastasis database: test auth iban payment 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "anastasis-db_pg.h" 23 #include "anastasis/anastasis-database/test_auth_iban_payment.h" 24 #include "anastasis/anastasis-database/transaction.h" 25 #include "anastasis/anastasis-database/preflight.h" 26 #include <taler/taler_pq_lib.h> 27 28 29 struct TestIbanContext 30 { 31 32 /** 33 * Function to call on each wire transfer found. 34 */ 35 ANASTASIS_DB_AuthIbanTransfercheck cb; 36 37 /** 38 * Closure for @a cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Value to return. 44 */ 45 enum GNUNET_DB_QueryStatus qs; 46 }; 47 48 49 /** 50 * Helper function for #postgres_test_auth_iban_payment(). 51 * To be called with the results of a SELECT statement 52 * that has returned @a num_results results. 53 * 54 * @param cls closure of type `struct TestIbanContext *` 55 * @param result the postgres result 56 * @param num_results the number of results in @a result 57 */ 58 static void 59 test_auth_cb (void *cls, 60 PGresult *result, 61 unsigned int num_results) 62 { 63 struct TestIbanContext *tic = cls; 64 65 for (unsigned int i = 0; i<num_results; i++) 66 { 67 struct TALER_Amount credit; 68 char *wire_subject; 69 struct GNUNET_PQ_ResultSpec rs[] = { 70 TALER_PQ_result_spec_amount ("credit", 71 pg->currency, 72 &credit), 73 GNUNET_PQ_result_spec_string ("wire_subject", 74 &wire_subject), 75 GNUNET_PQ_result_spec_end 76 }; 77 78 if (GNUNET_OK != 79 GNUNET_PQ_extract_result (result, 80 rs, 81 i)) 82 { 83 GNUNET_break (0); 84 tic->qs = GNUNET_DB_STATUS_HARD_ERROR; 85 return; 86 } 87 if (tic->cb (tic->cb_cls, 88 &credit, 89 wire_subject)) 90 { 91 GNUNET_free (wire_subject); 92 tic->qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 93 return; 94 } 95 GNUNET_free (wire_subject); 96 } 97 } 98 99 100 /** 101 * Function to check if we are aware of a wire transfer 102 * that satisfies the IBAN plugin's authentication check. 103 * 104 * @param cls closure 105 * @param debit_account which debit account to check 106 * @param earliest_date earliest date to check 107 * @param cb function to call on all entries found 108 * @param cb_cls closure for @a cb 109 * @return transaction status, 110 * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb 111 * returned 'true' once 112 * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no 113 * wire transfers existed for which @a cb returned true 114 */ 115 116 117 /** 118 * Closure for #test_auth_cb. 119 */ 120 enum GNUNET_DB_QueryStatus 121 ANASTASIS_DB_test_auth_iban_payment ( 122 const char *debit_account, 123 struct GNUNET_TIME_Timestamp earliest_date, 124 ANASTASIS_DB_AuthIbanTransfercheck cb, 125 void *cb_cls) 126 { 127 struct TestIbanContext tic = { 128 .cb = cb, 129 .cb_cls = cb_cls 130 }; 131 struct GNUNET_PQ_QueryParam params[] = { 132 GNUNET_PQ_query_param_string (debit_account), 133 GNUNET_PQ_query_param_timestamp (&earliest_date), 134 GNUNET_PQ_query_param_end 135 }; 136 enum GNUNET_DB_QueryStatus qs; 137 138 PREPARE ("test_auth_iban_payment", 139 "SELECT" 140 " credit" 141 ",wire_subject" 142 " FROM anastasis_auth_iban_in" 143 " WHERE debit_account_details=$1" 144 " AND execution_date>=$2;"); 145 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 146 "test_auth_iban_payment", 147 params, 148 &test_auth_cb, 149 &tic); 150 if (qs < 0) 151 return qs; 152 return tic.qs; 153 } 154 155 156 /* end of anastasis-db_test_auth_iban_payment.c */