Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2018, 2020, 2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Affero 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 taler-merchant-httpd_private-get-reserves-ID.c
18 : * @brief implement GET /reserves/$RESERVE_PUB endpoint
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <jansson.h>
23 : #include <taler/taler_util.h>
24 : #include <taler/taler_json_lib.h>
25 : #include "taler-merchant-httpd.h"
26 : #include "taler-merchant-httpd_mhd.h"
27 : #include "taler-merchant-httpd_exchanges.h"
28 : #include "taler-merchant-httpd_private-get-reserves-ID.h"
29 :
30 :
31 : /**
32 : * Closure for handle_reserve_details().
33 : */
34 : struct GetReserveContext
35 : {
36 : /**
37 : * Connection we are handling.
38 : */
39 : struct MHD_Connection *connection;
40 :
41 : /**
42 : * Value to return from the callback.
43 : */
44 : MHD_RESULT res;
45 :
46 : /**
47 : * Should we return details about tips?
48 : */
49 : bool tips;
50 : };
51 :
52 :
53 : /**
54 : * Callback with reserve details.
55 : *
56 : * @param cls closure with a `struct GetReserveContext`
57 : * @param creation_time time when the reserve was setup
58 : * @param expiration_time time when the reserve will be closed by the exchange
59 : * @param merchant_initial_amount initial amount that the merchant claims to have filled the
60 : * reserve with
61 : * @param exchange_initial_amount initial amount that the exchange claims to have received
62 : * @param picked_up_amount total of tips that were picked up from this reserve
63 : * @param committed_amount total of tips that the merchant committed to, but that were not
64 : * picked up yet
65 : * @param active true if the reserve is still active (we have the private key)
66 : * @param exchange_url URL of the exchange, NULL if not active
67 : * @param payto_uri payto:// URI to fill the reserve, NULL if not active or already paid
68 : * @param tips_length length of the @a tips array
69 : * @param tips information about the tips created by this reserve
70 : */
71 : static void
72 0 : handle_reserve_details (void *cls,
73 : struct GNUNET_TIME_Timestamp creation_time,
74 : struct GNUNET_TIME_Timestamp expiration_time,
75 : const struct TALER_Amount *merchant_initial_amount,
76 : const struct TALER_Amount *exchange_initial_amount,
77 : const struct TALER_Amount *picked_up_amount,
78 : const struct TALER_Amount *committed_amount,
79 : bool active,
80 : const char *exchange_url,
81 : const char *payto_uri,
82 : unsigned int tips_length,
83 : const struct TALER_MERCHANTDB_TipDetails *tips)
84 : {
85 0 : struct GetReserveContext *ctx = cls;
86 : json_t *tips_json;
87 :
88 0 : if (NULL != tips)
89 : {
90 0 : tips_json = json_array ();
91 0 : GNUNET_assert (NULL != tips_json);
92 0 : for (unsigned int i = 0; i<tips_length; i++)
93 : {
94 0 : GNUNET_assert (0 ==
95 : json_array_append_new (
96 : tips_json,
97 : GNUNET_JSON_PACK (
98 : GNUNET_JSON_pack_data_auto ("tip_id",
99 : &tips[i].tip_id),
100 : TALER_JSON_pack_amount ("total_amount",
101 : &tips[i].total_amount),
102 : GNUNET_JSON_pack_string ("reason",
103 : tips[i].reason))));
104 : }
105 : }
106 : else
107 : {
108 0 : tips_json = NULL;
109 : }
110 0 : ctx->res = TALER_MHD_REPLY_JSON_PACK (
111 : ctx->connection,
112 : MHD_HTTP_OK,
113 : GNUNET_JSON_pack_timestamp ("creation_time",
114 : creation_time),
115 : GNUNET_JSON_pack_timestamp ("expiration_time",
116 : expiration_time),
117 : TALER_JSON_pack_amount ("merchant_initial_amount",
118 : merchant_initial_amount),
119 : TALER_JSON_pack_amount ("exchange_initial_amount",
120 : exchange_initial_amount),
121 : TALER_JSON_pack_amount ("pickup_amount",
122 : picked_up_amount),
123 : TALER_JSON_pack_amount ("committed_amount",
124 : committed_amount),
125 : GNUNET_JSON_pack_allow_null (
126 : GNUNET_JSON_pack_array_steal ("tips",
127 : tips_json)),
128 : GNUNET_JSON_pack_bool ("active",
129 : active),
130 : GNUNET_JSON_pack_allow_null (
131 : GNUNET_JSON_pack_string ("exchange_url",
132 : exchange_url)),
133 : GNUNET_JSON_pack_allow_null (
134 : GNUNET_JSON_pack_string ("payto_uri",
135 : payto_uri)));
136 0 : }
137 :
138 :
139 : /**
140 : * Manages a GET /reserves/$RESERVE_PUB call.
141 : *
142 : * @param rh context of the handler
143 : * @param connection the MHD connection to handle
144 : * @param[in,out] hc context with further information about the request
145 : * @return MHD result code
146 : */
147 : MHD_RESULT
148 0 : TMH_private_get_reserves_ID (const struct TMH_RequestHandler *rh,
149 : struct MHD_Connection *connection,
150 : struct TMH_HandlerContext *hc)
151 : {
152 : struct TALER_ReservePublicKeyP reserve_pub;
153 : bool tips;
154 :
155 : (void) rh;
156 0 : if (GNUNET_OK !=
157 0 : GNUNET_STRINGS_string_to_data (hc->infix,
158 0 : strlen (hc->infix),
159 : &reserve_pub,
160 : sizeof (reserve_pub)))
161 : {
162 : /* tip_id has wrong encoding */
163 0 : GNUNET_break_op (0);
164 0 : return TALER_MHD_reply_with_error (connection,
165 : MHD_HTTP_BAD_REQUEST,
166 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
167 0 : hc->infix);
168 : }
169 : {
170 : const char *tstr;
171 :
172 0 : tstr = MHD_lookup_connection_value (connection,
173 : MHD_GET_ARGUMENT_KIND,
174 : "tips");
175 0 : tips = (NULL != tstr)
176 0 : ? 0 == strcasecmp (tstr, "yes")
177 0 : : false;
178 : }
179 : {
180 0 : struct GetReserveContext ctx = {
181 : .connection = connection,
182 : .tips = tips
183 : };
184 : enum GNUNET_DB_QueryStatus qs;
185 :
186 0 : TMH_db->preflight (TMH_db->cls);
187 0 : qs = TMH_db->lookup_reserve (TMH_db->cls,
188 0 : hc->instance->settings.id,
189 : &reserve_pub,
190 : tips,
191 : &handle_reserve_details,
192 : &ctx);
193 0 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
194 : {
195 : unsigned int response_code;
196 : enum TALER_ErrorCode ec;
197 :
198 0 : switch (qs)
199 : {
200 0 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
201 0 : ec = TALER_EC_MERCHANT_GENERIC_TIP_ID_UNKNOWN;
202 0 : response_code = MHD_HTTP_NOT_FOUND;
203 0 : break;
204 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
205 0 : ec = TALER_EC_GENERIC_DB_SOFT_FAILURE;
206 0 : response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
207 0 : break;
208 0 : case GNUNET_DB_STATUS_HARD_ERROR:
209 0 : ec = TALER_EC_GENERIC_DB_FETCH_FAILED;
210 0 : response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
211 0 : break;
212 0 : default:
213 0 : GNUNET_break (0);
214 0 : ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
215 0 : response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
216 0 : break;
217 : }
218 0 : return TALER_MHD_reply_with_error (connection,
219 : response_code,
220 : ec,
221 0 : hc->infix);
222 : }
223 0 : return ctx.res;
224 : }
225 : }
226 :
227 :
228 : /* end of taler-merchant-httpd_private-get-reserves-ID.c */
|