Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 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_lookup_transfer.c
18 : * @brief Implementation of the lookup_transfer function for Postgres
19 : * @author Iván Ávalos
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_lookup_transfer.h"
26 : #include "pg_helper.h"
27 :
28 : enum GNUNET_DB_QueryStatus
29 2 : TMH_PG_lookup_transfer (void *cls,
30 : const char *instance_id,
31 : const char *exchange_url,
32 : const struct TALER_WireTransferIdentifierRawP *wtid,
33 : struct TALER_Amount *total_amount,
34 : struct TALER_Amount *wire_fee,
35 : struct TALER_Amount *exchange_amount,
36 : struct GNUNET_TIME_Timestamp *execution_time,
37 : bool *have_exchange_sig,
38 : bool *verified)
39 : {
40 2 : struct PostgresClosure *pg = cls;
41 2 : struct GNUNET_PQ_QueryParam params[] = {
42 2 : GNUNET_PQ_query_param_string (exchange_url),
43 2 : GNUNET_PQ_query_param_auto_from_type (wtid),
44 2 : GNUNET_PQ_query_param_string (instance_id),
45 : GNUNET_PQ_query_param_end
46 : };
47 : uint8_t verified8;
48 : /** Amount we got actually credited, _excludes_ the wire fee */
49 : bool no_sig;
50 : struct TALER_Amount credit_amount;
51 2 : struct GNUNET_PQ_ResultSpec rs[] = {
52 2 : TALER_PQ_result_spec_amount_with_currency ("credit_amount",
53 : &credit_amount),
54 2 : GNUNET_PQ_result_spec_allow_null (
55 : TALER_PQ_result_spec_amount_with_currency ("wire_fee",
56 : wire_fee),
57 : &no_sig),
58 2 : GNUNET_PQ_result_spec_allow_null (
59 : TALER_PQ_result_spec_amount_with_currency ("exchange_amount",
60 : exchange_amount),
61 : NULL),
62 2 : GNUNET_PQ_result_spec_allow_null (
63 : GNUNET_PQ_result_spec_timestamp ("execution_time",
64 : execution_time),
65 : NULL),
66 2 : GNUNET_PQ_result_spec_auto_from_type ("verified",
67 : &verified8),
68 : GNUNET_PQ_result_spec_end
69 : };
70 : enum GNUNET_DB_QueryStatus qs;
71 :
72 2 : check_connection (pg);
73 2 : *execution_time = GNUNET_TIME_UNIT_ZERO_TS;
74 :
75 2 : PREPARE (pg,
76 : "lookup_transfer",
77 : "SELECT"
78 : " mt.credit_amount AS credit_amount"
79 : ",mts.credit_amount AS exchange_amount"
80 : ",wire_fee"
81 : ",execution_time"
82 : ",verified"
83 : " FROM merchant_transfers mt"
84 : " JOIN merchant_accounts USING (account_serial)"
85 : " JOIN merchant_instances USING (merchant_serial)"
86 : " LEFT JOIN merchant_transfer_signatures mts USING (credit_serial)"
87 : " WHERE wtid=$2"
88 : " AND exchange_url=$1"
89 : " AND merchant_id=$3;");
90 :
91 2 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
92 : "lookup_transfer",
93 : params,
94 : rs);
95 2 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
96 : "Lookup transfer returned %d\n",
97 : qs);
98 2 : if (qs > 0)
99 : {
100 2 : *have_exchange_sig = ! no_sig;
101 2 : *verified = (0 != verified8);
102 2 : if (GNUNET_OK !=
103 2 : TALER_amount_cmp_currency (&credit_amount,
104 : wire_fee))
105 : {
106 0 : GNUNET_break (0);
107 0 : return GNUNET_DB_STATUS_HARD_ERROR;
108 : }
109 4 : if ( (! no_sig) &&
110 : (0 >
111 2 : TALER_amount_add (total_amount,
112 : &credit_amount,
113 : wire_fee)) )
114 : {
115 0 : GNUNET_break (0);
116 0 : return GNUNET_DB_STATUS_HARD_ERROR;
117 : }
118 : }
119 : else
120 : {
121 0 : *verified = false;
122 0 : *have_exchange_sig = false;
123 : }
124 2 : return qs;
125 : }
|