insert_deposit_confirmation.c (5471B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022, 2023, 2024, 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/insert_deposit_confirmation.c 18 * @brief Implementation of the insert_deposit_confirmation function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/insert_deposit_confirmation.h" 24 #include "helper.h" 25 26 27 enum GNUNET_DB_QueryStatus 28 TALER_MERCHANTDB_insert_deposit_confirmation ( 29 struct TALER_MERCHANTDB_PostgresContext *pg, 30 const char *instance_id, 31 struct GNUNET_TIME_Timestamp deposit_timestamp, 32 const struct TALER_PrivateContractHashP *h_contract_terms, 33 const char *exchange_url, 34 struct GNUNET_TIME_Timestamp wire_transfer_deadline, 35 const struct TALER_Amount *total_without_fees, 36 const struct TALER_Amount *wire_fee, 37 const struct TALER_MerchantWireHashP *h_wire, 38 const struct TALER_ExchangeSignatureP *exchange_sig, 39 const struct TALER_ExchangePublicKeyP *exchange_pub, 40 uint64_t *deposit_confirmation_serial_id) 41 { 42 struct GNUNET_TIME_AbsoluteNBO nbo 43 = GNUNET_TIME_absolute_hton (wire_transfer_deadline.abs_time); 44 char *nbo_str 45 = GNUNET_STRINGS_data_to_string_alloc (&nbo, 46 sizeof (nbo)); 47 struct GNUNET_PQ_QueryParam params[] = { 48 GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 49 GNUNET_PQ_query_param_timestamp (&deposit_timestamp), 50 GNUNET_PQ_query_param_string (exchange_url), 51 TALER_PQ_query_param_amount_with_currency (pg->conn, 52 total_without_fees), 53 TALER_PQ_query_param_amount_with_currency (pg->conn, 54 wire_fee), 55 GNUNET_PQ_query_param_auto_from_type (h_wire), /* 6 */ 56 GNUNET_PQ_query_param_auto_from_type (exchange_sig), 57 GNUNET_PQ_query_param_auto_from_type (exchange_pub), 58 GNUNET_PQ_query_param_timestamp (&wire_transfer_deadline), 59 GNUNET_PQ_query_param_string (nbo_str), 60 GNUNET_PQ_query_param_end 61 }; 62 bool no_order; 63 bool no_account; 64 bool no_signkey; 65 bool conflict; 66 struct GNUNET_PQ_ResultSpec rs[] = { 67 GNUNET_PQ_result_spec_bool ("no_order", 68 &no_order), 69 GNUNET_PQ_result_spec_bool ("no_account", 70 &no_account), 71 GNUNET_PQ_result_spec_bool ("no_signkey", 72 &no_signkey), 73 GNUNET_PQ_result_spec_bool ("conflict", 74 &conflict), 75 GNUNET_PQ_result_spec_uint64 ("deposit_confirmation_serial", 76 deposit_confirmation_serial_id), 77 GNUNET_PQ_result_spec_end 78 }; 79 enum GNUNET_DB_QueryStatus qs; 80 81 GNUNET_assert (NULL != pg->current_merchant_id); 82 GNUNET_assert (0 == strcmp (instance_id, 83 pg->current_merchant_id)); 84 /* no preflight check here, run in transaction by caller! */ 85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 86 "Storing deposit confirmation for instance `%s' h_contract_terms `%s', total_without_fees: %s and wire transfer deadline in %s\n", 87 instance_id, 88 GNUNET_h2s (&h_contract_terms->hash), 89 TALER_amount2s (total_without_fees), 90 GNUNET_TIME_relative2s ( 91 GNUNET_TIME_absolute_get_remaining ( 92 wire_transfer_deadline.abs_time), 93 true)); 94 check_connection (pg); 95 TMH_PQ_prepare_anon (pg, 96 "SELECT " 97 " out_no_account AS no_account" 98 " ,out_no_order AS no_order" 99 " ,out_no_signkey AS no_signkey" 100 " ,out_conflict AS conflict" 101 " ,out_deposit_confirmation_serial AS deposit_confirmation_serial" 102 " FROM merchant_do_insert_deposit_confirmation" 103 " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);"); 104 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 105 "", 106 params, 107 rs); 108 GNUNET_free (nbo_str); 109 GNUNET_break (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs); 110 if (qs < 0) 111 return qs; 112 // FIXME: in the future, return these codes to the client and 113 // return more specific error codes to the client from the API! 114 if (no_order) 115 { 116 GNUNET_break (0); 117 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 118 } 119 if (no_account) 120 { 121 GNUNET_break (0); 122 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 123 } 124 if (no_signkey) 125 { 126 GNUNET_break (0); 127 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 128 } 129 if (conflict) 130 { 131 GNUNET_break (0); 132 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 133 } 134 GNUNET_assert (0 != *deposit_confirmation_serial_id); 135 return qs; 136 }