pg_insert_deposit_confirmation.c (5366B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022, 2023, 2024 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_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_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "pg_insert_deposit_confirmation.h" 26 #include "pg_helper.h" 27 28 29 enum GNUNET_DB_QueryStatus 30 TMH_PG_insert_deposit_confirmation ( 31 void *cls, 32 const char *instance_id, 33 struct GNUNET_TIME_Timestamp deposit_timestamp, 34 const struct TALER_PrivateContractHashP *h_contract_terms, 35 const char *exchange_url, 36 struct GNUNET_TIME_Timestamp wire_transfer_deadline, 37 const struct TALER_Amount *total_without_fees, 38 const struct TALER_Amount *wire_fee, 39 const struct TALER_MerchantWireHashP *h_wire, 40 const struct TALER_ExchangeSignatureP *exchange_sig, 41 const struct TALER_ExchangePublicKeyP *exchange_pub, 42 uint64_t *deposit_confirmation_serial_id) 43 { 44 struct GNUNET_DB_EventHeaderP es = { 45 .size = htons (sizeof (es)), 46 .type = htons (TALER_DBEVENT_MERCHANT_NEW_WIRE_DEADLINE) 47 }; 48 struct PostgresClosure *pg = cls; 49 struct GNUNET_PQ_QueryParam params[] = { 50 GNUNET_PQ_query_param_string (instance_id), 51 GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 52 GNUNET_PQ_query_param_timestamp (&deposit_timestamp), 53 GNUNET_PQ_query_param_string (exchange_url), 54 TALER_PQ_query_param_amount_with_currency (pg->conn, 55 total_without_fees), 56 TALER_PQ_query_param_amount_with_currency (pg->conn, 57 wire_fee), 58 GNUNET_PQ_query_param_auto_from_type (h_wire), /* 7 */ 59 GNUNET_PQ_query_param_auto_from_type (exchange_sig), 60 GNUNET_PQ_query_param_auto_from_type (exchange_pub), 61 GNUNET_PQ_query_param_timestamp (&wire_transfer_deadline), 62 GNUNET_PQ_query_param_end 63 }; 64 struct GNUNET_PQ_ResultSpec rs[] = { 65 GNUNET_PQ_result_spec_uint64 ("deposit_confirmation_serial", 66 deposit_confirmation_serial_id), 67 GNUNET_PQ_result_spec_end 68 }; 69 enum GNUNET_DB_QueryStatus qs; 70 71 /* no preflight check here, run in transaction by caller! */ 72 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 73 "Storing deposit confirmation for instance `%s' h_contract_terms `%s', total_without_fees: %s and wire transfer deadline in %s\n", 74 instance_id, 75 GNUNET_h2s (&h_contract_terms->hash), 76 TALER_amount2s (total_without_fees), 77 GNUNET_TIME_relative2s ( 78 GNUNET_TIME_absolute_get_remaining ( 79 wire_transfer_deadline.abs_time), 80 true)); 81 check_connection (pg); 82 PREPARE (pg, 83 "insert_deposit_confirmation", 84 "WITH md AS" 85 " (SELECT account_serial, merchant_serial" 86 " FROM merchant_accounts" 87 " WHERE h_wire=$7" 88 " AND merchant_serial=" 89 " (SELECT merchant_serial" 90 " FROM merchant_instances" 91 " WHERE merchant_id=$1))" 92 ", ed AS" 93 " (SELECT signkey_serial" 94 " FROM merchant_exchange_signing_keys" 95 " WHERE exchange_pub=$9" 96 " ORDER BY start_date DESC" 97 " LIMIT 1)" 98 "INSERT INTO merchant_deposit_confirmations" 99 "(order_serial" 100 ",deposit_timestamp" 101 ",exchange_url" 102 ",total_without_fee" 103 ",wire_fee" 104 ",exchange_sig" 105 ",wire_transfer_deadline" 106 ",signkey_serial" 107 ",account_serial)" 108 " SELECT " 109 " order_serial" 110 " ,$3, $4, $5, $6, $8, $10" 111 " ,ed.signkey_serial" 112 " ,md.account_serial" 113 " FROM merchant_contract_terms" 114 " JOIN md USING (merchant_serial)" 115 " FULL OUTER JOIN ed ON TRUE" 116 " WHERE h_contract_terms=$2" 117 " RETURNING deposit_confirmation_serial"); 118 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 119 "insert_deposit_confirmation", 120 params, 121 rs); 122 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) 123 { 124 /* inform taler-merchant-depositcheck about new deadline */ 125 struct GNUNET_TIME_AbsoluteNBO nbo; 126 127 nbo = GNUNET_TIME_absolute_hton (wire_transfer_deadline.abs_time); 128 GNUNET_PQ_event_notify (pg->conn, 129 &es, 130 &nbo, 131 sizeof (nbo)); 132 } 133 return qs; 134 }