Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022 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_account_kyc_get_status.c
18 : * @brief Implementation of the account_kyc_get_status 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_account_kyc_get_status.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Closure for kyc_status_cb().
30 : */
31 : struct KycStatusContext
32 : {
33 : /**
34 : * Function to call with results.
35 : */
36 : TALER_MERCHANTDB_KycCallback kyc_cb;
37 :
38 : /**
39 : * Closure for @e kyc_cb.
40 : */
41 : void *kyc_cb_cls;
42 :
43 : /**
44 : * Number of results found.
45 : */
46 : unsigned int count;
47 :
48 : /**
49 : * Set to true on failure(s).
50 : */
51 : bool failure;
52 : };
53 :
54 :
55 : /**
56 : * Function to be called with the results of a SELECT statement
57 : * that has returned @a num_results results about accounts.
58 : *
59 : * @param[in,out] cls of type `struct KycStatusContext *`
60 : * @param result the postgres result
61 : * @param num_results the number of results in @a result
62 : */
63 : static void
64 10 : kyc_status_cb (void *cls,
65 : PGresult *result,
66 : unsigned int num_results)
67 : {
68 10 : struct KycStatusContext *ksc = cls;
69 :
70 10 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
71 : "Got %u KYC records\n",
72 : num_results);
73 19 : for (unsigned int i = 0; i < num_results; i++)
74 : {
75 : struct TALER_MerchantWireHashP h_wire;
76 : char *exchange_url;
77 : struct TALER_FullPayto payto_uri;
78 : struct GNUNET_TIME_Timestamp last_check;
79 : bool kyc_ok;
80 : struct TALER_AccountAccessTokenP access_token;
81 : bool no_auth;
82 : uint32_t h32;
83 : uint32_t e32;
84 : bool in_aml_review;
85 9 : json_t *jlimits = NULL;
86 9 : struct GNUNET_PQ_ResultSpec rs[] = {
87 9 : GNUNET_PQ_result_spec_auto_from_type ("h_wire",
88 : &h_wire),
89 9 : GNUNET_PQ_result_spec_string ("payto_uri",
90 : &payto_uri.full_payto),
91 9 : GNUNET_PQ_result_spec_string ("exchange_url",
92 : &exchange_url),
93 9 : GNUNET_PQ_result_spec_timestamp ("kyc_timestamp",
94 : &last_check),
95 9 : GNUNET_PQ_result_spec_bool ("kyc_ok",
96 : &kyc_ok),
97 9 : GNUNET_PQ_result_spec_allow_null (
98 : GNUNET_PQ_result_spec_auto_from_type ("access_token",
99 : &access_token),
100 : &no_auth),
101 9 : GNUNET_PQ_result_spec_uint32 ("exchange_http_status",
102 : &h32),
103 9 : GNUNET_PQ_result_spec_uint32 ("exchange_ec_code",
104 : &e32),
105 9 : GNUNET_PQ_result_spec_bool ("aml_review",
106 : &in_aml_review),
107 9 : GNUNET_PQ_result_spec_allow_null (
108 : TALER_PQ_result_spec_json ("jaccount_limits",
109 : &jlimits),
110 : NULL),
111 : GNUNET_PQ_result_spec_end
112 : };
113 : unsigned int last_http_status;
114 : enum TALER_ErrorCode last_ec;
115 :
116 9 : if (GNUNET_OK !=
117 9 : GNUNET_PQ_extract_result (result,
118 : rs,
119 : i))
120 : {
121 0 : GNUNET_break (0);
122 0 : ksc->failure = true;
123 0 : return;
124 : }
125 9 : last_http_status = (unsigned int) h32;
126 9 : last_ec = (enum TALER_ErrorCode) (int) e32;
127 9 : ksc->count++;
128 9 : ksc->kyc_cb (ksc->kyc_cb_cls,
129 : &h_wire,
130 : payto_uri,
131 : exchange_url,
132 : last_check,
133 : kyc_ok,
134 : (no_auth)
135 : ? NULL
136 : : &access_token,
137 : last_http_status,
138 : last_ec,
139 : in_aml_review,
140 : jlimits);
141 9 : GNUNET_PQ_cleanup_result (rs);
142 : }
143 : }
144 :
145 :
146 : enum GNUNET_DB_QueryStatus
147 10 : TMH_PG_account_kyc_get_status (
148 : void *cls,
149 : const char *merchant_id,
150 : const struct TALER_MerchantWireHashP *h_wire,
151 : const char *exchange_url,
152 : TALER_MERCHANTDB_KycCallback kyc_cb,
153 : void *kyc_cb_cls)
154 : {
155 10 : struct PostgresClosure *pg = cls;
156 10 : struct KycStatusContext ksc = {
157 : .kyc_cb = kyc_cb,
158 : .kyc_cb_cls = kyc_cb_cls
159 : };
160 10 : struct GNUNET_PQ_QueryParam params[] = {
161 10 : GNUNET_PQ_query_param_string (merchant_id),
162 : NULL == exchange_url
163 2 : ? GNUNET_PQ_query_param_null ()
164 10 : : GNUNET_PQ_query_param_string (exchange_url),
165 : NULL == h_wire
166 7 : ? GNUNET_PQ_query_param_null ()
167 10 : : GNUNET_PQ_query_param_auto_from_type (h_wire),
168 : GNUNET_PQ_query_param_end
169 : };
170 : enum GNUNET_DB_QueryStatus qs;
171 :
172 10 : check_connection (pg);
173 10 : PREPARE (pg,
174 : "lookup_kyc_status",
175 : "SELECT"
176 : " ma.h_wire"
177 : ",ma.payto_uri"
178 : ",mk.exchange_url"
179 : ",mk.kyc_timestamp"
180 : ",mk.kyc_ok"
181 : ",mk.access_token"
182 : ",mk.exchange_http_status"
183 : ",mk.exchange_ec_code"
184 : ",mk.aml_review"
185 : ",mk.jaccount_limits"
186 : " FROM merchant_instances mi"
187 : " JOIN merchant_accounts ma"
188 : " USING (merchant_serial)"
189 : " JOIN merchant_kyc mk"
190 : " USING (account_serial)"
191 : " WHERE (mi.merchant_id=$1)"
192 : " AND ma.active"
193 : " AND ( ($2::TEXT IS NULL)"
194 : " OR (mk.exchange_url=$2) )"
195 : " AND ( ($3::BYTEA IS NULL)"
196 : " OR (ma.h_wire=$3) );");
197 10 : qs = GNUNET_PQ_eval_prepared_multi_select (
198 : pg->conn,
199 : "lookup_kyc_status",
200 : params,
201 : &kyc_status_cb,
202 : &ksc);
203 10 : if (ksc.failure)
204 : {
205 0 : GNUNET_break (0);
206 0 : return GNUNET_DB_STATUS_HARD_ERROR;
207 : }
208 10 : if (0 > qs)
209 0 : return qs;
210 10 : return ksc.count;
211 : }
|