Line data Source code
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 TALER_MERCHANTDB_DepositConfirmationStatus
28 74 : 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 74 : = GNUNET_TIME_absolute_hton (wire_transfer_deadline.abs_time);
44 : char *nbo_str
45 74 : = GNUNET_STRINGS_data_to_string_alloc (&nbo,
46 : sizeof (nbo));
47 74 : struct GNUNET_PQ_QueryParam params[] = {
48 74 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
49 74 : GNUNET_PQ_query_param_timestamp (&deposit_timestamp),
50 74 : GNUNET_PQ_query_param_string (exchange_url),
51 74 : TALER_PQ_query_param_amount_with_currency (pg->conn,
52 : total_without_fees),
53 74 : TALER_PQ_query_param_amount_with_currency (pg->conn,
54 : wire_fee),
55 74 : GNUNET_PQ_query_param_auto_from_type (h_wire), /* 6 */
56 74 : GNUNET_PQ_query_param_auto_from_type (exchange_sig),
57 74 : GNUNET_PQ_query_param_auto_from_type (exchange_pub),
58 74 : GNUNET_PQ_query_param_timestamp (&wire_transfer_deadline),
59 74 : 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 74 : struct GNUNET_PQ_ResultSpec rs[] = {
67 74 : GNUNET_PQ_result_spec_bool ("no_order",
68 : &no_order),
69 74 : GNUNET_PQ_result_spec_bool ("no_account",
70 : &no_account),
71 74 : GNUNET_PQ_result_spec_bool ("no_signkey",
72 : &no_signkey),
73 74 : GNUNET_PQ_result_spec_bool ("conflict",
74 : &conflict),
75 74 : 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 74 : GNUNET_assert (NULL != pg->current_merchant_id);
82 74 : GNUNET_assert (0 == strcmp (instance_id,
83 : pg->current_merchant_id));
84 : /* no preflight check here, run in transaction by caller! */
85 74 : 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 74 : TMH_PQ_prepare_anon (pg,
95 : "SELECT "
96 : " out_no_account AS no_account"
97 : " ,out_no_order AS no_order"
98 : " ,out_no_signkey AS no_signkey"
99 : " ,out_conflict AS conflict"
100 : " ,out_deposit_confirmation_serial AS deposit_confirmation_serial"
101 : " FROM merchant_do_insert_deposit_confirmation"
102 : " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);");
103 74 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
104 : "",
105 : params,
106 : rs);
107 74 : GNUNET_free (nbo_str);
108 74 : switch (qs)
109 : {
110 0 : case GNUNET_DB_STATUS_HARD_ERROR:
111 0 : return TALER_MERCHANTDB_DCS_HARD_ERROR;
112 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
113 0 : return TALER_MERCHANTDB_DCS_SOFT_ERROR;
114 0 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
115 : /* stored procedure always returns exactly one row */
116 0 : GNUNET_break (0);
117 0 : return TALER_MERCHANTDB_DCS_NO_RESULTS;
118 74 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
119 74 : break;
120 : }
121 : /* Checked in the same order as the stored procedure evaluates them. */
122 74 : if (no_account)
123 : {
124 1 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125 : "Merchant account `%s' unknown, cannot store deposit confirmation for contract `%s'\n",
126 : TALER_B2S (h_wire),
127 : GNUNET_h2s (&h_contract_terms->hash));
128 1 : return TALER_MERCHANTDB_DCS_NO_ACCOUNT;
129 : }
130 73 : if (no_signkey)
131 : {
132 1 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 : "Exchange signing key `%s' of `%s' unknown, cannot store deposit confirmation for contract `%s'\n",
134 : TALER_B2S (exchange_pub),
135 : exchange_url,
136 : GNUNET_h2s (&h_contract_terms->hash));
137 1 : return TALER_MERCHANTDB_DCS_NO_SIGNKEY;
138 : }
139 72 : if (no_order)
140 : {
141 1 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
142 : "Order for contract `%s' unknown, cannot store deposit confirmation\n",
143 : GNUNET_h2s (&h_contract_terms->hash));
144 1 : return TALER_MERCHANTDB_DCS_NO_ORDER;
145 : }
146 71 : if (conflict)
147 : {
148 1 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 : "Conflicting deposit confirmation already stored for contract `%s' at `%s'\n",
150 : GNUNET_h2s (&h_contract_terms->hash),
151 : exchange_url);
152 1 : return TALER_MERCHANTDB_DCS_CONFLICT;
153 : }
154 70 : GNUNET_assert (0 != *deposit_confirmation_serial_id);
155 70 : return TALER_MERCHANTDB_DCS_SUCCESS;
156 : }
|