1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/*
This file is part of TALER
Copyright (C) 2018, 2019, 2020 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2.1, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
TALER; see the file COPYING.LGPL. If not, see
<http://www.gnu.org/licenses/>
*/
/**
* @file merchant_api_merchant_get_order.c
* @brief Implementation of the GET /private/orders/$ORDER request
* @author Christian Grothoff
* @author Marcello Stanisci
* @author Florian Dold
*/
#include "platform.h"
#include <curl/curl.h>
#include <jansson.h>
#include <microhttpd.h> /* just for HTTP status codes */
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_curl_lib.h>
#include "taler_merchant_service.h"
#include "merchant_api_curl_defaults.h"
#include <taler/taler_json_lib.h>
#include <taler/taler_signatures.h>
/**
* @brief A GET /private/orders/$ORDER handle
*/
struct TALER_MERCHANT_OrderMerchantGetHandle
{
/**
* The url for this request.
*/
char *url;
/**
* Handle for the request.
*/
struct GNUNET_CURL_Job *job;
/**
* Function to call with the result.
*/
TALER_MERCHANT_OrderMerchantGetCallback cb;
/**
* Closure for @a cb.
*/
void *cb_cls;
/**
* Reference to the execution context.
*/
struct GNUNET_CURL_Context *ctx;
};
/**
* Function called when we're done processing the GET /private/orders/$ORDER
* request and we got an HTTP status of OK and the order was unpaid. Parse
* the response and call the callback.
*
* @param omgh handle for the request
* @param[in,out] osr HTTP response we got
*/
static void
handle_unpaid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
struct TALER_MERCHANT_OrderStatusResponse *osr)
{
struct GNUNET_JSON_Specification spec[] = {
TALER_JSON_spec_amount_any (
"total_amount",
&osr->details.ok.details.unpaid.contract_amount),
GNUNET_JSON_spec_mark_optional (
GNUNET_JSON_spec_string (
"already_paid_order_id",
&osr->details.ok.details.unpaid.already_paid_order_id),
NULL),
GNUNET_JSON_spec_string (
"taler_pay_uri",
&osr->details.ok.details.unpaid.taler_pay_uri),
GNUNET_JSON_spec_string (
"summary",
&osr->details.ok.details.unpaid.summary),
GNUNET_JSON_spec_timestamp (
"creation_time",
&osr->details.ok.details.unpaid.creation_time),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
GNUNET_JSON_parse (osr->hr.reply,
spec,
NULL, NULL))
{
GNUNET_break_op (0);
osr->hr.http_status = 0;
osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
osr);
return;
}
osr->details.ok.status = TALER_MERCHANT_OSC_UNPAID;
omgh->cb (omgh->cb_cls,
osr);
}
/**
* Function called when we're done processing the GET /private/orders/$ORDER
* request and we got an HTTP status of OK and the order was claimed but not
* paid. Parse the response and call the callback.
*
* @param omgh handle for the request
* @param[in,out] osr HTTP response we got
*/
static void
handle_claimed (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
struct TALER_MERCHANT_OrderStatusResponse *osr)
{
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_object_const (
"contract_terms",
&osr->details.ok.details.claimed.contract_terms),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
GNUNET_JSON_parse (osr->hr.reply,
spec,
NULL, NULL))
{
GNUNET_break_op (0);
osr->hr.http_status = 0;
osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
osr);
return;
}
osr->details.ok.status = TALER_MERCHANT_OSC_CLAIMED;
omgh->cb (omgh->cb_cls,
osr);
}
/**
* Function called when we're done processing the GET /private/orders/$ORDER
* request and we got an HTTP status of OK and the order was paid. Parse
* the response and call the callback.
*
* @param omgh handle for the request
* @param[in,out] osr HTTP response we got
*/
static void
handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
struct TALER_MERCHANT_OrderStatusResponse *osr)
{
uint32_t ec32;
uint32_t hc32;
const json_t *wire_details;
const json_t *wire_reports;
const json_t *refund_details;
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_bool ("refunded",
&osr->details.ok.details.paid.refunded),
GNUNET_JSON_spec_bool ("refund_pending",
&osr->details.ok.details.paid.refund_pending),
GNUNET_JSON_spec_bool ("wired",
&osr->details.ok.details.paid.wired),
TALER_JSON_spec_amount_any ("deposit_total",
&osr->details.ok.details.paid.deposit_total),
GNUNET_JSON_spec_uint32 ("exchange_code",
&ec32),
GNUNET_JSON_spec_uint32 ("exchange_http_status",
&hc32),
TALER_JSON_spec_amount_any ("refund_amount",
&osr->details.ok.details.paid.refund_amount),
GNUNET_JSON_spec_object_const (
"contract_terms",
&osr->details.ok.details.paid.contract_terms),
GNUNET_JSON_spec_array_const ("wire_details",
&wire_details),
GNUNET_JSON_spec_array_const ("wire_reports",
&wire_reports),
GNUNET_JSON_spec_array_const ("refund_details",
&refund_details),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
GNUNET_JSON_parse (osr->hr.reply,
spec,
NULL, NULL))
{
GNUNET_break_op (0);
osr->hr.http_status = 0;
osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
osr);
return;
}
osr->details.ok.status = TALER_MERCHANT_OSC_PAID;
osr->details.ok.details.paid.exchange_ec = (enum TALER_ErrorCode) ec32;
osr->details.ok.details.paid.exchange_hc = (unsigned int) hc32;
{
unsigned int wts_len = json_array_size (wire_details);
unsigned int wrs_len = json_array_size (wire_reports);
unsigned int ref_len = json_array_size (refund_details);
struct TALER_MERCHANT_WireTransfer wts[GNUNET_NZL (wts_len)];
struct TALER_MERCHANT_WireReport wrs[GNUNET_NZL (wrs_len)];
struct TALER_MERCHANT_RefundOrderDetail ref[GNUNET_NZL (ref_len)];
for (unsigned int i = 0; i<wts_len; i++)
{
struct TALER_MERCHANT_WireTransfer *wt = &wts[i];
const json_t *w = json_array_get (wire_details,
i);
struct GNUNET_JSON_Specification ispec[] = {
GNUNET_JSON_spec_string ("exchange_url",
&wt->exchange_url),
GNUNET_JSON_spec_fixed_auto ("wtid",
&wt->wtid),
GNUNET_JSON_spec_timestamp ("execution_time",
&wt->execution_time),
TALER_JSON_spec_amount_any ("amount",
&wt->total_amount),
GNUNET_JSON_spec_bool ("confirmed",
&wt->confirmed),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
GNUNET_JSON_parse (w,
ispec,
NULL, NULL))
{
GNUNET_break_op (0);
osr->hr.http_status = 0;
osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
osr);
return;
}
}
for (unsigned int i = 0; i<wrs_len; i++)
{
struct TALER_MERCHANT_WireReport *wr = &wrs[i];
const json_t *w = json_array_get (wire_reports, i);
uint32_t c32;
uint32_t eec32;
uint32_t ehs32;
struct GNUNET_JSON_Specification ispec[] = {
GNUNET_JSON_spec_uint32 ("code",
&c32),
GNUNET_JSON_spec_string ("hint",
&wr->hint),
GNUNET_JSON_spec_uint32 ("exchange_code",
&eec32),
GNUNET_JSON_spec_uint32 ("exchange_http_status",
&ehs32),
GNUNET_JSON_spec_fixed_auto ("coin_pub",
&wr->coin_pub),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
GNUNET_JSON_parse (w,
ispec,
NULL, NULL))
{
GNUNET_break_op (0);
osr->hr.http_status = 0;
osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
osr);
return;
}
wr->code = (enum TALER_ErrorCode) c32;
wr->hr.ec = (enum TALER_ErrorCode) eec32;
wr->hr.http_status = (unsigned int) ehs32;
}
for (unsigned int i = 0; i<ref_len; i++)
{
struct TALER_MERCHANT_RefundOrderDetail *ro = &ref[i];
const json_t *w = json_array_get (refund_details,
i);
struct GNUNET_JSON_Specification ispec[] = {
TALER_JSON_spec_amount_any ("amount",
&ro->refund_amount),
GNUNET_JSON_spec_string ("reason",
&ro->reason),
GNUNET_JSON_spec_timestamp ("timestamp",
&ro->refund_time),
GNUNET_JSON_spec_end ()
};
if (GNUNET_OK !=
GNUNET_JSON_parse (w,
ispec,
NULL, NULL))
{
GNUNET_break_op (0);
osr->hr.http_status = 0;
osr->hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
osr);
return;
}
}
osr->details.ok.details.paid.wts = wts;
osr->details.ok.details.paid.wts_len = wts_len;
osr->details.ok.details.paid.wrs = wrs;
osr->details.ok.details.paid.wrs_len = wrs_len;
osr->details.ok.details.paid.refunds = ref;
osr->details.ok.details.paid.refunds_len = ref_len;
omgh->cb (omgh->cb_cls,
osr);
}
}
/**
* Function called when we're done processing the GET /private/orders/$ORDER
* request.
*
* @param cls the `struct TALER_MERCHANT_OrderMerchantGetHandle`
* @param response_code HTTP response code, 0 on error
* @param response response body, NULL if not in JSON
*/
static void
handle_merchant_order_get_finished (void *cls,
long response_code,
const void *response)
{
struct TALER_MERCHANT_OrderMerchantGetHandle *omgh = cls;
const json_t *json = response;
const char *order_status;
struct TALER_MERCHANT_OrderStatusResponse osr = {
.hr.http_status = (unsigned int) response_code,
.hr.reply = json
};
omgh->job = NULL;
switch (response_code)
{
case MHD_HTTP_OK:
/* see below */
break;
case MHD_HTTP_ACCEPTED:
/* see below */
omgh->cb (omgh->cb_cls,
&osr);
TALER_MERCHANT_merchant_order_get_cancel (omgh);
return;
case MHD_HTTP_UNAUTHORIZED:
osr.hr.ec = TALER_JSON_get_error_code (json);
osr.hr.hint = TALER_JSON_get_error_hint (json);
omgh->cb (omgh->cb_cls,
&osr);
TALER_MERCHANT_merchant_order_get_cancel (omgh);
return;
case MHD_HTTP_NOT_FOUND:
osr.hr.ec = TALER_JSON_get_error_code (json);
osr.hr.hint = TALER_JSON_get_error_hint (json);
omgh->cb (omgh->cb_cls,
&osr);
TALER_MERCHANT_merchant_order_get_cancel (omgh);
return;
case MHD_HTTP_GATEWAY_TIMEOUT:
osr.hr.ec = TALER_JSON_get_error_code (json);
osr.hr.hint = TALER_JSON_get_error_hint (json);
omgh->cb (omgh->cb_cls,
&osr);
TALER_MERCHANT_merchant_order_get_cancel (omgh);
return;
default:
osr.hr.ec = TALER_JSON_get_error_code (json);
osr.hr.hint = TALER_JSON_get_error_hint (json);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Polling payment failed with HTTP status code %u/%d\n",
(unsigned int) response_code,
(int) osr.hr.ec);
GNUNET_break_op (0);
omgh->cb (omgh->cb_cls,
&osr);
TALER_MERCHANT_merchant_order_get_cancel (omgh);
return;
}
order_status = json_string_value (json_object_get (json,
"order_status"));
if (NULL == order_status)
{
GNUNET_break_op (0);
osr.hr.http_status = 0;
osr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
&osr);
TALER_MERCHANT_merchant_order_get_cancel (omgh);
return;
}
if (0 == strcmp ("paid",
order_status))
{
handle_paid (omgh,
&osr);
}
else if (0 == strcmp ("claimed",
order_status))
{
handle_claimed (omgh,
&osr);
}
else if (0 == strcmp ("unpaid",
order_status))
{
handle_unpaid (omgh,
&osr);
}
else
{
GNUNET_break_op (0);
osr.hr.http_status = 0;
osr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
omgh->cb (omgh->cb_cls,
&osr);
}
TALER_MERCHANT_merchant_order_get_cancel (omgh);
}
struct TALER_MERCHANT_OrderMerchantGetHandle *
TALER_MERCHANT_merchant_order_get (struct GNUNET_CURL_Context *ctx,
const char *backend_url,
const char *order_id,
const char *session_id,
bool transfer,
struct GNUNET_TIME_Relative timeout,
TALER_MERCHANT_OrderMerchantGetCallback cb,
void *cb_cls)
{
struct TALER_MERCHANT_OrderMerchantGetHandle *omgh;
unsigned long long tms;
long tlong;
tms = (unsigned long long) (timeout.rel_value_us
/ GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
/* set curl timeout to *our* long poll timeout plus one minute
(for network latency and processing delays) */
tlong = (long) (GNUNET_TIME_relative_add (timeout,
GNUNET_TIME_UNIT_MINUTES).
rel_value_us
/ GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
omgh = GNUNET_new (struct TALER_MERCHANT_OrderMerchantGetHandle);
omgh->ctx = ctx;
omgh->cb = cb;
omgh->cb_cls = cb_cls;
{
char *path;
char timeout_ms[32];
GNUNET_snprintf (timeout_ms,
sizeof (timeout_ms),
"%llu",
tms);
GNUNET_asprintf (&path,
"private/orders/%s",
order_id);
omgh->url = TALER_url_join (backend_url,
path,
"session_id", session_id,
"transfer", transfer ? "YES" : "NO",
"timeout_ms", (0 != tms) ? timeout_ms : NULL,
NULL);
GNUNET_free (path);
}
if (NULL == omgh->url)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Could not construct request URL.\n");
GNUNET_free (omgh);
return NULL;
}
{
CURL *eh;
eh = TALER_MERCHANT_curl_easy_get_ (omgh->url);
if (NULL == eh)
{
GNUNET_break (0);
GNUNET_free (omgh->url);
GNUNET_free (omgh);
return NULL;
}
if (CURLE_OK !=
curl_easy_setopt (eh,
CURLOPT_TIMEOUT_MS,
tlong))
{
GNUNET_break (0);
curl_easy_cleanup (eh);
GNUNET_free (omgh->url);
GNUNET_free (omgh);
return NULL;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Getting order status from %s\n",
omgh->url);
if (NULL == (omgh->job =
GNUNET_CURL_job_add (ctx,
eh,
&handle_merchant_order_get_finished,
omgh)))
{
GNUNET_break (0);
GNUNET_free (omgh->url);
GNUNET_free (omgh);
return NULL;
}
}
return omgh;
}
void
TALER_MERCHANT_merchant_order_get_cancel (
struct TALER_MERCHANT_OrderMerchantGetHandle *omgh)
{
if (NULL != omgh->job)
{
GNUNET_CURL_job_cancel (omgh->job);
omgh->job = NULL;
}
GNUNET_free (omgh->url);
GNUNET_free (omgh);
}
/* end of merchant_api_merchant_get_order.c */
|