Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-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 Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, 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 Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file merchant_api_get_orders.c
19 : * @brief Implementation of the GET /orders request of the merchant's HTTP API
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_merchant_service.h"
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 :
33 :
34 : /**
35 : * Handle for a GET /orders operation.
36 : */
37 : struct TALER_MERCHANT_OrdersGetHandle
38 : {
39 : /**
40 : * The url for this request.
41 : */
42 : char *url;
43 :
44 : /**
45 : * Handle for the request.
46 : */
47 : struct GNUNET_CURL_Job *job;
48 :
49 : /**
50 : * Function to call with the result.
51 : */
52 : TALER_MERCHANT_OrdersGetCallback cb;
53 :
54 : /**
55 : * Closure for @a cb.
56 : */
57 : void *cb_cls;
58 :
59 : /**
60 : * Reference to the execution context.
61 : */
62 : struct GNUNET_CURL_Context *ctx;
63 :
64 : };
65 :
66 :
67 : /**
68 : * Parse order information from @a ia.
69 : *
70 : * @param ia JSON array (or NULL!) with order data
71 : * @param ogh operation handle
72 : * @return #GNUNET_OK on success
73 : */
74 : static enum GNUNET_GenericReturnValue
75 0 : parse_orders (const json_t *ia,
76 : struct TALER_MERCHANT_OrdersGetHandle *ogh)
77 0 : {
78 0 : unsigned int oes_len = json_array_size (ia);
79 0 : struct TALER_MERCHANT_OrderEntry oes[oes_len];
80 : size_t index;
81 : json_t *value;
82 : int ret;
83 :
84 0 : ret = GNUNET_OK;
85 0 : json_array_foreach (ia, index, value) {
86 0 : struct TALER_MERCHANT_OrderEntry *ie = &oes[index];
87 : struct GNUNET_JSON_Specification spec[] = {
88 0 : GNUNET_JSON_spec_string ("order_id",
89 : &ie->order_id),
90 0 : GNUNET_JSON_spec_timestamp ("timestamp",
91 : &ie->timestamp),
92 0 : GNUNET_JSON_spec_uint64 ("row_id",
93 : &ie->order_serial),
94 0 : TALER_JSON_spec_amount_any ("amount",
95 : &ie->amount),
96 0 : GNUNET_JSON_spec_string ("summary",
97 : &ie->summary),
98 0 : GNUNET_JSON_spec_bool ("refundable",
99 : &ie->refundable),
100 0 : GNUNET_JSON_spec_bool ("paid",
101 : &ie->paid),
102 0 : GNUNET_JSON_spec_end ()
103 : };
104 :
105 0 : if (GNUNET_OK !=
106 0 : GNUNET_JSON_parse (value,
107 : spec,
108 : NULL, NULL))
109 : {
110 0 : GNUNET_break_op (0);
111 0 : ret = GNUNET_SYSERR;
112 0 : continue;
113 : }
114 0 : if (GNUNET_SYSERR == ret)
115 0 : break;
116 : }
117 0 : if (GNUNET_OK == ret)
118 : {
119 0 : struct TALER_MERCHANT_HttpResponse hr = {
120 : .http_status = MHD_HTTP_OK
121 : };
122 :
123 0 : ogh->cb (ogh->cb_cls,
124 : &hr,
125 : oes_len,
126 : oes);
127 0 : ogh->cb = NULL; /* just to be sure */
128 : }
129 0 : return ret;
130 : }
131 :
132 :
133 : /**
134 : * Function called when we're done processing the
135 : * HTTP /orders request.
136 : *
137 : * @param cls the `struct TALER_MERCHANT_OrdersGetHandle`
138 : * @param response_code HTTP response code, 0 on error
139 : * @param response response body, NULL if not in JSON
140 : */
141 : static void
142 0 : handle_get_orders_finished (void *cls,
143 : long response_code,
144 : const void *response)
145 : {
146 0 : struct TALER_MERCHANT_OrdersGetHandle *ogh = cls;
147 0 : const json_t *json = response;
148 0 : struct TALER_MERCHANT_HttpResponse hr = {
149 0 : .http_status = (unsigned int) response_code,
150 : .reply = json
151 : };
152 :
153 0 : ogh->job = NULL;
154 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 : "Got /orders response with status code %u\n",
156 : (unsigned int) response_code);
157 0 : switch (response_code)
158 : {
159 0 : case MHD_HTTP_OK:
160 : {
161 : json_t *orders;
162 : struct GNUNET_JSON_Specification spec[] = {
163 0 : GNUNET_JSON_spec_json ("orders",
164 : &orders),
165 0 : GNUNET_JSON_spec_end ()
166 : };
167 :
168 0 : if (GNUNET_OK !=
169 0 : GNUNET_JSON_parse (json,
170 : spec,
171 : NULL, NULL))
172 : {
173 0 : hr.http_status = 0;
174 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
175 : }
176 : else
177 : {
178 0 : if ( (! json_is_array (orders)) ||
179 : (GNUNET_OK ==
180 0 : parse_orders (orders,
181 : ogh)) )
182 : {
183 0 : GNUNET_JSON_parse_free (spec);
184 0 : TALER_MERCHANT_orders_get_cancel (ogh);
185 0 : return;
186 : }
187 : else
188 : {
189 0 : hr.http_status = 0;
190 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
191 : }
192 : }
193 0 : GNUNET_JSON_parse_free (spec);
194 0 : break;
195 : }
196 0 : case MHD_HTTP_UNAUTHORIZED:
197 0 : hr.ec = TALER_JSON_get_error_code (json);
198 0 : hr.hint = TALER_JSON_get_error_hint (json);
199 : /* Nothing really to verify, merchant says we need to authenticate. */
200 0 : break;
201 0 : case MHD_HTTP_NOT_FOUND:
202 0 : hr.ec = TALER_JSON_get_error_code (json);
203 0 : hr.hint = TALER_JSON_get_error_hint (json);
204 0 : break;
205 0 : default:
206 : /* unexpected response code */
207 0 : hr.ec = TALER_JSON_get_error_code (json);
208 0 : hr.hint = TALER_JSON_get_error_hint (json);
209 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 : "Unexpected response code %u/%d\n",
211 : (unsigned int) response_code,
212 : (int) hr.ec);
213 0 : break;
214 : }
215 0 : ogh->cb (ogh->cb_cls,
216 : &hr,
217 : 0,
218 : NULL);
219 0 : TALER_MERCHANT_orders_get_cancel (ogh);
220 : }
221 :
222 :
223 : struct TALER_MERCHANT_OrdersGetHandle *
224 0 : TALER_MERCHANT_orders_get (
225 : struct GNUNET_CURL_Context *ctx,
226 : const char *backend_url,
227 : TALER_MERCHANT_OrdersGetCallback cb,
228 : void *cb_cls)
229 : {
230 0 : return TALER_MERCHANT_orders_get2 (ctx,
231 : backend_url,
232 : TALER_EXCHANGE_YNA_ALL,
233 : TALER_EXCHANGE_YNA_ALL,
234 : TALER_EXCHANGE_YNA_ALL,
235 0 : GNUNET_TIME_UNIT_FOREVER_TS,
236 : UINT64_MAX,
237 : -20, /* default is most recent 20 entries */
238 0 : GNUNET_TIME_UNIT_ZERO,
239 : cb,
240 : cb_cls);
241 : }
242 :
243 :
244 : struct TALER_MERCHANT_OrdersGetHandle *
245 0 : TALER_MERCHANT_orders_get2 (
246 : struct GNUNET_CURL_Context *ctx,
247 : const char *backend_url,
248 : enum TALER_EXCHANGE_YesNoAll paid,
249 : enum TALER_EXCHANGE_YesNoAll refunded,
250 : enum TALER_EXCHANGE_YesNoAll wired,
251 : struct GNUNET_TIME_Timestamp date,
252 : uint64_t start_row,
253 : int64_t delta,
254 : struct GNUNET_TIME_Relative timeout,
255 : TALER_MERCHANT_OrdersGetCallback cb,
256 : void *cb_cls)
257 : {
258 : struct TALER_MERCHANT_OrdersGetHandle *ogh;
259 : CURL *eh;
260 0 : unsigned int timeout_ms = timeout.rel_value_us
261 0 : / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
262 :
263 0 : GNUNET_assert (NULL != backend_url);
264 0 : if (0 == delta)
265 : {
266 0 : GNUNET_break (0);
267 0 : return NULL;
268 : }
269 0 : ogh = GNUNET_new (struct TALER_MERCHANT_OrdersGetHandle);
270 0 : ogh->ctx = ctx;
271 0 : ogh->cb = cb;
272 0 : ogh->cb_cls = cb_cls;
273 :
274 : /* build ogh->url with the various optional arguments */
275 : {
276 : char *dstr;
277 : bool have_date;
278 : bool have_srow;
279 : char cbuf[30];
280 : char dbuf[30];
281 : char tbuf[30];
282 :
283 0 : GNUNET_snprintf (tbuf,
284 : sizeof (tbuf),
285 : "%llu",
286 : (unsigned long long) timeout_ms);
287 0 : GNUNET_snprintf (dbuf,
288 : sizeof (dbuf),
289 : "%lld",
290 : (long long) delta);
291 0 : GNUNET_snprintf (cbuf,
292 : sizeof (cbuf),
293 : "%llu",
294 : (unsigned long long) start_row);
295 0 : dstr = GNUNET_strdup (GNUNET_TIME_timestamp2s (date));
296 0 : if (delta > 0)
297 : {
298 0 : have_date = ! GNUNET_TIME_absolute_is_zero (date.abs_time);
299 0 : have_srow = (0 != start_row);
300 : }
301 : else
302 : {
303 0 : have_date = ! GNUNET_TIME_absolute_is_never (date.abs_time);
304 0 : have_srow = (UINT64_MAX != start_row);
305 : }
306 0 : ogh->url = TALER_url_join (backend_url,
307 : "private/orders",
308 : "paid",
309 : (TALER_EXCHANGE_YNA_ALL != paid)
310 0 : ? TALER_yna_to_string (paid)
311 : : NULL,
312 : "refunded",
313 : (TALER_EXCHANGE_YNA_ALL != refunded)
314 0 : ? TALER_yna_to_string (refunded)
315 : : NULL,
316 : "wired",
317 : (TALER_EXCHANGE_YNA_ALL != wired)
318 0 : ? TALER_yna_to_string (wired)
319 : : NULL,
320 : "date_s",
321 : (have_date)
322 : ? dstr
323 : : NULL,
324 : "start",
325 : (have_srow)
326 : ? cbuf
327 : : NULL,
328 : "delta",
329 : (-20 != delta)
330 : ? dbuf
331 : : NULL,
332 : "timeout_ms",
333 : (0 != timeout_ms)
334 : ? tbuf
335 : : NULL,
336 : NULL);
337 0 : GNUNET_free (dstr);
338 : }
339 0 : if (NULL == ogh->url)
340 : {
341 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
342 : "Could not construct request URL.\n");
343 0 : GNUNET_free (ogh);
344 0 : return NULL;
345 : }
346 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 : "Requesting URL '%s'\n",
348 : ogh->url);
349 0 : eh = TALER_MERCHANT_curl_easy_get_ (ogh->url);
350 0 : ogh->job = GNUNET_CURL_job_add (ctx,
351 : eh,
352 : &handle_get_orders_finished,
353 : ogh);
354 0 : return ogh;
355 : }
356 :
357 :
358 : void
359 0 : TALER_MERCHANT_orders_get_cancel (
360 : struct TALER_MERCHANT_OrdersGetHandle *ogh)
361 : {
362 0 : if (NULL != ogh->job)
363 0 : GNUNET_CURL_job_cancel (ogh->job);
364 0 : GNUNET_free (ogh->url);
365 0 : GNUNET_free (ogh);
366 0 : }
|