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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
|
/*
This file is part of TALER
(C) 2014-2022 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file taler-merchant-httpd_get-orders-ID.c
* @brief implementation of GET /orders/$ID
* @author Marcello Stanisci
* @author Christian Grothoff
*/
#include "platform.h"
#include <jansson.h>
#include <gnunet/gnunet_uri_lib.h>
#include <taler/taler_signatures.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_json_lib.h>
#include <taler/taler_templating_lib.h>
#include <taler/taler_exchange_service.h>
#include "taler-merchant-httpd_exchanges.h"
#include "taler-merchant-httpd_helper.h"
#include "taler-merchant-httpd_get-orders-ID.h"
#include "taler-merchant-httpd_mhd.h"
#include "taler-merchant-httpd_qr.h"
/**
* How often do we retry DB transactions on serialization failures?
*/
#define MAX_RETRIES 5
/**
* Context for the operation.
*/
struct GetOrderData
{
/**
* Hashed version of contract terms. All zeros if not provided.
*/
struct TALER_PrivateContractHashP h_contract_terms;
/**
* Claim token used for access control. All zeros if not provided.
*/
struct TALER_ClaimTokenP claim_token;
/**
* DLL of (suspended) requests.
*/
struct GetOrderData *next;
/**
* DLL of (suspended) requests.
*/
struct GetOrderData *prev;
/**
* Context of the request.
*/
struct TMH_HandlerContext *hc;
/**
* Entry in the #resume_timeout_heap for this check payment, if we are
* suspended.
*/
struct TMH_SuspendedConnection sc;
/**
* Database event we are waiting on to be resuming.
*/
struct GNUNET_DB_EventHandler *pay_eh;
/**
* Database event we are waiting on to be resuming.
*/
struct GNUNET_DB_EventHandler *refund_eh;
/**
* Which merchant instance is this for?
*/
struct MerchantInstance *mi;
/**
* order ID for the payment
*/
const char *order_id;
/**
* Where to get the contract
*/
const char *contract_url;
/**
* fulfillment URL of the contract (valid as long as @e contract_terms is
* valid; but can also be NULL if the contract_terms does not come with
* a fulfillment URL).
*/
const char *fulfillment_url;
/**
* session of the client
*/
const char *session_id;
/**
* Contract terms of the payment we are checking. NULL when they
* are not (yet) known.
*/
json_t *contract_terms;
/**
* Merchant base URL from @e contract_terms.
*/
const char *merchant_base_url;
/**
* Public reorder URL from @e contract_terms.
* Could be NULL if contract does not have one.
*/
const char *public_reorder_url;
/**
* Total amount in contract.
*/
struct TALER_Amount contract_total;
/**
* Total refunds granted for this payment. Only initialized
* if @e refunded is set to true.
*/
struct TALER_Amount refund_amount;
/**
* Total refunds already collected.
* if @e refunded is set to true.
*/
struct TALER_Amount refund_taken;
/**
* Return code: #TALER_EC_NONE if successful.
*/
enum TALER_ErrorCode ec;
/**
* Did we suspend @a connection and are thus in
* the #god_head DLL (#GNUNET_YES). Set to
* #GNUNET_NO if we are not suspended, and to
* #GNUNET_SYSERR if we should close the connection
* without a response due to shutdown.
*/
enum GNUNET_GenericReturnValue suspended;
/**
* Set to YES if refunded orders should be included when
* doing repurchase detection.
*/
enum TALER_EXCHANGE_YesNoAll allow_refunded_for_repurchase;
/**
* Set to true if the client passed 'h_contract'.
*/
bool h_contract_provided;
/**
* Set to true if the client passed a 'claim' token.
*/
bool claim_token_provided;
/**
* Set to true if we are dealing with a claimed order
* (and thus @e h_contract_terms is set, otherwise certain
* DB queries will not work).
*/
bool claimed;
/**
* Set to true if this payment has been refunded and
* @e refund_amount is initialized.
*/
bool refunded;
/**
* Set to true if a refund is still available for the
* wallet for this payment.
* @deprecated: true if refund_taken < refund_amount
*/
bool refund_pending;
/**
* Set to true if the client requested HTML, otherwise we generate JSON.
*/
bool generate_html;
/**
* Did we parse the contract terms?
*/
bool contract_parsed;
/**
* Set to true if the refunds found in the DB have
* a different currency then the main contract.
*/
bool bad_refund_currency_in_db;
};
/**
* Head of DLL of (suspended) requests.
*/
static struct GetOrderData *god_head;
/**
* Tail of DLL of (suspended) requests.
*/
static struct GetOrderData *god_tail;
void
TMH_force_wallet_get_order_resume (void)
{
struct GetOrderData *god;
while (NULL != (god = god_head))
{
GNUNET_CONTAINER_DLL_remove (god_head,
god_tail,
god);
GNUNET_assert (god->suspended);
god->suspended = GNUNET_SYSERR;
MHD_resume_connection (god->sc.con);
TALER_MHD_daemon_trigger (); /* we resumed, kick MHD */
}
}
/**
* We have received a trigger from the database
* that we should (possibly) resume the request.
*
* @param cls a `struct GetOrderData` to resume
* @param extra string encoding refund amount (or NULL)
* @param extra_size number of bytes in @a extra
*/
static void
resume_by_event (void *cls,
const void *extra,
size_t extra_size)
{
struct GetOrderData *god = cls;
struct GNUNET_AsyncScopeSave old;
GNUNET_async_scope_enter (&god->hc->async_scope_id,
&old);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Received event for %s with argument `%.*s`\n",
god->order_id,
(int) extra_size,
(const char *) extra);
if (! god->suspended)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Not suspended, ignoring event\n");
GNUNET_async_scope_restore (&old);
return; /* duplicate event is possible */
}
if (GNUNET_TIME_absolute_is_future (god->sc.long_poll_timeout) &&
god->sc.awaiting_refund)
{
char *as;
struct TALER_Amount a;
if (0 == extra_size)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"No amount given, but need refund above threshold\n");
GNUNET_async_scope_restore (&old);
return; /* not relevant */
}
as = GNUNET_strndup (extra,
extra_size);
if (GNUNET_OK !=
TALER_string_to_amount (as,
&a))
{
GNUNET_break (0);
GNUNET_async_scope_restore (&old);
GNUNET_free (as);
return;
}
GNUNET_free (as);
if (GNUNET_OK !=
TALER_amount_cmp_currency (&god->sc.refund_expected,
&a))
{
GNUNET_break (0);
GNUNET_async_scope_restore (&old);
return; /* bad currency!? */
}
if (1 == TALER_amount_cmp (&god->sc.refund_expected,
&a))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Amount too small to trigger resuming\n");
GNUNET_async_scope_restore (&old);
return; /* refund too small */
}
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Resuming (%d/%d) by event with argument `%.*s`\n",
(int) GNUNET_TIME_absolute_is_future (god->sc.long_poll_timeout),
god->sc.awaiting_refund,
(int) extra_size,
(const char *) extra);
god->suspended = GNUNET_NO;
GNUNET_CONTAINER_DLL_remove (god_head,
god_tail,
god);
MHD_resume_connection (god->sc.con);
TALER_MHD_daemon_trigger (); /* we resumed, kick MHD */
GNUNET_async_scope_restore (&old);
}
/**
* Suspend this @a god until the trigger is satisfied.
*
* @param god request to suspend
*/
static void
suspend_god (struct GetOrderData *god)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Suspending GET /orders/%s\n",
god->order_id);
if (NULL != god->contract_terms)
{
json_decref (god->contract_terms);
god->fulfillment_url = NULL;
god->contract_terms = NULL;
god->contract_parsed = false;
god->merchant_base_url = NULL;
god->public_reorder_url = NULL;
}
GNUNET_assert (! god->suspended);
god->suspended = GNUNET_YES;
GNUNET_CONTAINER_DLL_insert (god_head,
god_tail,
god);
MHD_suspend_connection (god->sc.con);
}
/**
* Create a taler://refund/ URI for the given @a con and @a order_id
* and @a instance_id.
*
* @param merchant_base_url URL to take host and path from;
* we cannot take it from the MHD connection as a browser
* may have changed 'http' to 'https' and we MUST be consistent
* with what the merchant's frontend used initially
* @param order_id the order id
* @return corresponding taler://refund/ URI, or NULL on missing "host"
*/
static char *
make_taler_refund_uri (const char *merchant_base_url,
const char *order_id)
{
struct GNUNET_Buffer buf = { 0 };
char *url;
struct GNUNET_Uri uri;
url = GNUNET_strdup (merchant_base_url);
if (-1 == GNUNET_uri_parse (&uri,
url))
{
GNUNET_break (0);
GNUNET_free (url);
return NULL;
}
GNUNET_assert (NULL != order_id);
GNUNET_buffer_write_str (&buf,
"taler");
if (0 == strcasecmp ("http",
uri.scheme))
GNUNET_buffer_write_str (&buf,
"+http");
GNUNET_buffer_write_str (&buf,
"://refund/");
GNUNET_buffer_write_str (&buf,
uri.host);
if (0 != uri.port)
GNUNET_buffer_write_fstr (&buf,
":%u",
(unsigned int) uri.port);
if (NULL != uri.path)
GNUNET_buffer_write_path (&buf,
uri.path);
GNUNET_buffer_write_path (&buf,
order_id);
GNUNET_buffer_write_path (&buf,
""); // Trailing slash
GNUNET_free (url);
return GNUNET_buffer_reap_str (&buf);
}
char *
TMH_make_order_status_url (struct MHD_Connection *con,
const char *order_id,
const char *session_id,
const char *instance_id,
struct TALER_ClaimTokenP *claim_token,
struct TALER_PrivateContractHashP *h_contract)
{
struct GNUNET_Buffer buf;
/* Number of query parameters written so far */
unsigned int num_qp = 0;
GNUNET_assert (NULL != instance_id);
GNUNET_assert (NULL != order_id);
if (GNUNET_OK !=
TMH_base_url_by_connection (con,
instance_id,
&buf))
{
GNUNET_break (0);
return NULL;
}
GNUNET_buffer_write_path (&buf,
"/orders");
GNUNET_buffer_write_path (&buf,
order_id);
if ( (NULL != claim_token) &&
(! GNUNET_is_zero (claim_token)) )
{
/* 'token=' for human readability */
GNUNET_buffer_write_str (&buf,
"?token=");
GNUNET_buffer_write_data_encoded (&buf,
(char *) claim_token,
sizeof (*claim_token));
num_qp++;
}
if (NULL != session_id)
{
if (num_qp > 0)
GNUNET_buffer_write_str (&buf,
"&session_id=");
else
GNUNET_buffer_write_str (&buf,
"?session_id=");
GNUNET_buffer_write_str (&buf,
session_id);
num_qp++;
}
if (NULL != h_contract)
{
if (num_qp > 0)
GNUNET_buffer_write_str (&buf,
"&h_contract=");
else
GNUNET_buffer_write_str (&buf,
"?h_contract=");
GNUNET_buffer_write_data_encoded (&buf,
(char *) h_contract,
sizeof (*h_contract));
}
return GNUNET_buffer_reap_str (&buf);
}
char *
TMH_make_taler_pay_uri (struct MHD_Connection *con,
const char *order_id,
const char *session_id,
const char *instance_id,
struct TALER_ClaimTokenP *claim_token)
{
struct GNUNET_Buffer buf;
GNUNET_assert (NULL != instance_id);
GNUNET_assert (NULL != order_id);
if (GNUNET_OK !=
TMH_taler_uri_by_connection (con,
"pay",
instance_id,
&buf))
{
GNUNET_break (0);
return NULL;
}
GNUNET_buffer_write_path (&buf,
order_id);
GNUNET_buffer_write_path (&buf,
(NULL == session_id)
? ""
: session_id);
if ( (NULL != claim_token) &&
(! GNUNET_is_zero (claim_token)))
{
/* Just 'c=' because this goes into QR
codes, so this is more compact. */
GNUNET_buffer_write_str (&buf,
"?c=");
GNUNET_buffer_write_data_encoded (&buf,
(char *) claim_token,
sizeof (struct TALER_ClaimTokenP));
}
return GNUNET_buffer_reap_str (&buf);
}
/**
* Return the order summary of the contract of @a god in the
* preferred language of the HTTP client.
*
* @param god order to extract summary from
* @return dummy error message summary if no summary was provided in the contract
*/
static const char *
get_order_summary (const struct GetOrderData *god)
{
const char *language_pattern;
const char *ret;
language_pattern = MHD_lookup_connection_value (god->sc.con,
MHD_HEADER_KIND,
MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
if (NULL == language_pattern)
language_pattern = "en";
ret = json_string_value (TALER_JSON_extract_i18n (god->contract_terms,
language_pattern,
"summary"));
if (NULL == ret)
{
/* Upon order creation (and insertion into the database), the presence
of a summary should have been checked. So if we get here, someone
did something fishy to our database... */
GNUNET_break (0);
ret = "<bug: no summary>";
}
return ret;
}
/**
* The client did not yet pay, send it the payment request.
*
* @param god check pay request context
* @param already_paid_order_id if for the fulfillment URI there is
* already a paid order, this is the order ID to redirect
* the wallet to; NULL if not applicable
* @return #MHD_YES on success
*/
static MHD_RESULT
send_pay_request (struct GetOrderData *god,
const char *already_paid_order_id)
{
MHD_RESULT ret;
char *taler_pay_uri;
char *order_status_url;
struct GNUNET_TIME_Relative remaining;
remaining = GNUNET_TIME_absolute_get_remaining (god->sc.long_poll_timeout);
if ( (! GNUNET_TIME_relative_is_zero (remaining)) &&
(NULL == already_paid_order_id) )
{
/* long polling: do not queue a response, suspend connection instead */
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Suspending request: long polling for payment\n");
suspend_god (god);
return MHD_YES;
}
/* Check if resource_id has been paid for in the same session
* with another order_id.
*/
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Sending payment request\n");
taler_pay_uri = TMH_make_taler_pay_uri (god->sc.con,
god->order_id,
god->session_id,
god->hc->instance->settings.id,
&god->claim_token);
order_status_url = TMH_make_order_status_url (god->sc.con,
god->order_id,
god->session_id,
god->hc->instance->settings.id,
&god->claim_token,
NULL);
if ( (NULL == taler_pay_uri) ||
(NULL == order_status_url) )
{
GNUNET_break_op (0);
GNUNET_free (taler_pay_uri);
GNUNET_free (order_status_url);
return TALER_MHD_reply_with_error (god->sc.con,
MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_HTTP_HEADERS_MALFORMED,
"host");
}
if (god->generate_html)
{
if (NULL != already_paid_order_id)
{
struct MHD_Response *reply;
GNUNET_assert (NULL != god->fulfillment_url);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Redirecting to already paid order %s via fulfillment URL %s\n",
already_paid_order_id,
god->fulfillment_url);
reply = MHD_create_response_from_buffer (0,
NULL,
MHD_RESPMEM_PERSISTENT);
if (NULL == reply)
{
GNUNET_break (0);
return MHD_NO;
}
GNUNET_break (MHD_YES ==
MHD_add_response_header (reply,
MHD_HTTP_HEADER_LOCATION,
god->fulfillment_url));
{
MHD_RESULT ret;
ret = MHD_queue_response (god->sc.con,
MHD_HTTP_FOUND,
reply);
MHD_destroy_response (reply);
return ret;
}
}
{
char *qr;
qr = TMH_create_qrcode (taler_pay_uri);
if (NULL == qr)
{
GNUNET_break (0);
return MHD_NO;
}
{
enum GNUNET_GenericReturnValue res;
json_t *context;
context = GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("taler_pay_uri",
taler_pay_uri),
GNUNET_JSON_pack_string ("order_status_url",
order_status_url),
GNUNET_JSON_pack_string ("taler_pay_qrcode_svg",
qr),
GNUNET_JSON_pack_string ("order_summary",
get_order_summary (god)));
res = TALER_TEMPLATING_reply (god->sc.con,
MHD_HTTP_PAYMENT_REQUIRED,
"request_payment",
god->hc->instance->settings.id,
taler_pay_uri,
context);
if (GNUNET_SYSERR == res)
{
GNUNET_break (0);
ret = MHD_NO;
}
else
{
ret = MHD_YES;
}
json_decref (context);
}
GNUNET_free (qr);
}
}
else /* end of 'generate HTML' */
{
ret = TALER_MHD_REPLY_JSON_PACK (
god->sc.con,
MHD_HTTP_PAYMENT_REQUIRED,
GNUNET_JSON_pack_string ("taler_pay_uri",
taler_pay_uri),
GNUNET_JSON_pack_allow_null (
GNUNET_JSON_pack_string ("fulfillment_url",
god->fulfillment_url)),
GNUNET_JSON_pack_allow_null (
GNUNET_JSON_pack_string ("already_paid_order_id",
already_paid_order_id)));
}
GNUNET_free (taler_pay_uri);
GNUNET_free (order_status_url);
return ret;
}
/**
* Function called with detailed information about a refund.
* It is responsible for packing up the data to return.
*
* @param cls closure
* @param refund_serial unique serial number of the refund
* @param timestamp time of the refund (for grouping of refunds in the wallet UI)
* @param coin_pub public coin from which the refund comes from
* @param exchange_url URL of the exchange that issued @a coin_pub
* @param rtransaction_id identificator of the refund
* @param reason human-readable explanation of the refund
* @param refund_amount refund amount which is being taken from @a coin_pub
* @param pending true if the this refund was not yet processed by the wallet/exchange
*/
static void
process_refunds_cb (void *cls,
uint64_t refund_serial,
struct GNUNET_TIME_Timestamp timestamp,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const char *exchange_url,
uint64_t rtransaction_id,
const char *reason,
const struct TALER_Amount *refund_amount,
bool pending)
{
struct GetOrderData *god = cls;
(void) refund_serial;
(void) timestamp;
(void) exchange_url;
(void) rtransaction_id;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Found refund of %s for coin %s with reason `%s' in database\n",
TALER_amount2s (refund_amount),
TALER_B2S (coin_pub),
reason);
god->refund_pending |= pending;
if ( (GNUNET_OK !=
TALER_amount_cmp_currency (&god->refund_taken,
refund_amount)) ||
(GNUNET_OK !=
TALER_amount_cmp_currency (&god->refund_amount,
refund_amount)) )
{
god->bad_refund_currency_in_db = true;
return;
}
if (! pending)
{
GNUNET_assert (0 <=
TALER_amount_add (&god->refund_taken,
&god->refund_taken,
refund_amount));
}
GNUNET_assert (0 <=
TALER_amount_add (&god->refund_amount,
&god->refund_amount,
refund_amount));
god->refunded = true;
}
/**
* Clean up the session state for a GET /orders/$ID request.
*
* @param cls must be a `struct GetOrderData *`
*/
static void
god_cleanup (void *cls)
{
struct GetOrderData *god = cls;
if (NULL != god->contract_terms)
{
json_decref (god->contract_terms);
god->contract_terms = NULL;
}
if (NULL != god->refund_eh)
{
TMH_db->event_listen_cancel (god->refund_eh);
god->refund_eh = NULL;
}
if (NULL != god->pay_eh)
{
TMH_db->event_listen_cancel (god->pay_eh);
god->pay_eh = NULL;
}
GNUNET_free (god);
}
// FIXME: this function should probably be refactored...
MHD_RESULT
TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
struct MHD_Connection *connection,
struct TMH_HandlerContext *hc)
{
struct GetOrderData *god = hc->ctx;
const char *order_id = hc->infix;
enum GNUNET_DB_QueryStatus qs;
bool contract_match = false;
bool token_match = false;
bool contract_available = false;
(void) rh;
if (NULL == god)
{
god = GNUNET_new (struct GetOrderData);
hc->ctx = god;
hc->cc = &god_cleanup;
god->sc.con = connection;
god->hc = hc;
god->order_id = order_id;
god->generate_html = TMH_MHD_test_html_desired (connection);
/* first-time initialization / sanity checks */
TALER_MHD_parse_request_arg_auto (connection,
"h_contract",
&god->h_contract_terms,
god->h_contract_provided);
TALER_MHD_parse_request_arg_auto (connection,
"token",
&god->claim_token,
god->claim_token_provided);
if (! (TALER_arg_to_yna (connection,
"allow_refunded_for_repurchase",
TALER_EXCHANGE_YNA_NO,
&god->allow_refunded_for_repurchase)) )
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_PARAMETER_MALFORMED,
"allow_refunded_for_repurchase");
god->session_id = MHD_lookup_connection_value (connection,
MHD_GET_ARGUMENT_KIND,
"session_id");
/* process await_refund_obtained argument */
{
const char *await_refund_obtained_s;
await_refund_obtained_s =
MHD_lookup_connection_value (connection,
MHD_GET_ARGUMENT_KIND,
"await_refund_obtained");
god->sc.awaiting_refund_obtained =
(NULL != await_refund_obtained_s)
? 0 == strcasecmp (await_refund_obtained_s,
"yes")
: false;
if (god->sc.awaiting_refund_obtained)
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Awaiting refund obtained\n");
}
{
const char *min_refund;
min_refund = MHD_lookup_connection_value (connection,
MHD_GET_ARGUMENT_KIND,
"refund");
if (NULL != min_refund)
{
if ( (GNUNET_OK !=
TALER_string_to_amount (min_refund,
&god->sc.refund_expected)) ||
(0 != strcasecmp (god->sc.refund_expected.currency,
TMH_currency) ) )
{
GNUNET_break_op (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_PARAMETER_MALFORMED,
"refund");
}
god->sc.awaiting_refund = true;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Awaiting minimum refund of %s\n",
min_refund);
}
}
TALER_MHD_parse_request_timeout (connection,
&god->sc.long_poll_timeout);
/* If HTML is requested, we never actually long poll. Makes no sense */
if ( (! god->generate_html) &&
(GNUNET_TIME_absolute_is_future (god->sc.long_poll_timeout)) )
{
if (god->sc.awaiting_refund ||
god->sc.awaiting_refund_obtained)
{
struct TMH_OrderPayEventP refund_eh = {
.header.size = htons (sizeof (refund_eh)),
.header.type = htons (god->sc.awaiting_refund_obtained
? TALER_DBEVENT_MERCHANT_REFUND_OBTAINED
: TALER_DBEVENT_MERCHANT_ORDER_REFUND),
.merchant_pub = hc->instance->merchant_pub
};
GNUNET_CRYPTO_hash (god->order_id,
strlen (god->order_id),
&refund_eh.h_order_id);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Subscribing %p to refunds on %s\n",
god,
god->order_id);
god->refund_eh
= TMH_db->event_listen (
TMH_db->cls,
&refund_eh.header,
GNUNET_TIME_absolute_get_remaining (
god->sc.long_poll_timeout),
&resume_by_event,
god);
}
{
struct TMH_OrderPayEventP pay_eh = {
.header.size = htons (sizeof (pay_eh)),
.header.type = htons (TALER_DBEVENT_MERCHANT_ORDER_PAID),
.merchant_pub = hc->instance->merchant_pub
};
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Subscribing to payments on %s\n",
god->order_id);
GNUNET_CRYPTO_hash (god->order_id,
strlen (god->order_id),
&pay_eh.h_order_id);
god->pay_eh
= TMH_db->event_listen (
TMH_db->cls,
&pay_eh.header,
GNUNET_TIME_absolute_get_remaining (
god->sc.long_poll_timeout),
&resume_by_event,
god);
}
} /* end of do long-polling / no HTML */
} /* end of first-time initialization / sanity checks */
if (GNUNET_SYSERR == god->suspended)
return MHD_NO; /* we are in shutdown */
if (GNUNET_YES == god->suspended)
{
god->suspended = GNUNET_NO;
GNUNET_CONTAINER_DLL_remove (god_head,
god_tail,
god);
}
/* Convert order_id to h_contract_terms */
TMH_db->preflight (TMH_db->cls);
if (NULL == god->contract_terms)
{
uint64_t order_serial;
struct TALER_ClaimTokenP db_claim_token;
qs = TMH_db->lookup_contract_terms (TMH_db->cls,
hc->instance->settings.id,
order_id,
&god->contract_terms,
&order_serial,
&db_claim_token);
if (0 > qs)
{
/* single, read-only SQL statements should never cause
serialization problems */
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
/* Always report on hard error as well to enable diagnostics */
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"lookup_contract_terms");
}
/* Note: when "!ord.requireClaimToken" and the client does not provide
a claim token (all zeros!), then token_match==TRUE below: */
token_match = (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
&& (0 == GNUNET_memcmp (&db_claim_token,
&god->claim_token));
}
/* Check if client provided the right hash code of the contract terms */
if (NULL != god->contract_terms)
{
contract_available = true;
if (GNUNET_YES ==
GNUNET_is_zero (&god->h_contract_terms))
{
if (GNUNET_OK !=
TALER_JSON_contract_hash (god->contract_terms,
&god->h_contract_terms))
{
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_FAILED_COMPUTE_JSON_HASH,
"contract terms");
}
}
else
{
struct TALER_PrivateContractHashP h;
if (GNUNET_OK !=
TALER_JSON_contract_hash (god->contract_terms,
&h))
{
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_FAILED_COMPUTE_JSON_HASH,
"contract terms");
}
contract_match = (0 ==
GNUNET_memcmp (&h,
&god->h_contract_terms));
if (! contract_match)
{
GNUNET_break_op (0);
return TALER_MHD_reply_with_error (
connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_MERCHANT_GENERIC_CONTRACT_HASH_DOES_NOT_MATCH_ORDER,
NULL);
}
}
}
if (contract_available)
{
god->claimed = true;
}
else
{
struct TALER_ClaimTokenP db_claim_token;
struct TALER_MerchantPostDataHashP unused;
qs = TMH_db->lookup_order (TMH_db->cls,
hc->instance->settings.id,
order_id,
&db_claim_token,
&unused,
(NULL == god->contract_terms)
? &god->contract_terms
: NULL);
if (0 > qs)
{
/* single, read-only SQL statements should never cause
serialization problems */
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
/* Always report on hard error as well to enable diagnostics */
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"lookup_order");
}
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Unknown order id given: `%s'\n",
order_id);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_NOT_FOUND,
TALER_EC_MERCHANT_GENERIC_ORDER_UNKNOWN,
order_id);
}
/* Note: when "!ord.requireClaimToken" and the client does not provide
a claim token (all zeros!), then token_match==TRUE below: */
token_match = (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) &&
(0 == GNUNET_memcmp (&db_claim_token,
&god->claim_token));
} /* end unclaimed order logic */
GNUNET_assert (NULL != god->contract_terms);
if (! god->contract_parsed)
{
struct GNUNET_JSON_Specification espec[] = {
TALER_JSON_spec_amount_any ("amount",
&god->contract_total),
TALER_JSON_spec_web_url ("merchant_base_url",
&god->merchant_base_url),
GNUNET_JSON_spec_mark_optional (
/* this one does NOT have to be a Web URL! */
GNUNET_JSON_spec_string ("fulfillment_url",
&god->fulfillment_url),
NULL),
GNUNET_JSON_spec_mark_optional (
TALER_JSON_spec_web_url ("public_reorder_url",
&god->public_reorder_url),
NULL),
GNUNET_JSON_spec_end ()
};
enum GNUNET_GenericReturnValue res;
const char *ename;
unsigned int eline;
res = GNUNET_JSON_parse (god->contract_terms,
espec,
&ename,
&eline);
if (GNUNET_OK != res)
{
GNUNET_break (0);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to parse contract %s in DB at field %s\n",
order_id,
ename);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_MERCHANT_GENERIC_DB_CONTRACT_CONTENT_INVALID,
order_id);
}
god->contract_parsed = true;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Token match: %d, contract_available: %d, contract match: %d, claimed: %d\n",
token_match,
contract_available,
contract_match,
god->claimed);
if (god->claim_token_provided && ! token_match)
{
/* Authentication provided but wrong. */
GNUNET_break_op (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_MERCHANT_GET_ORDERS_ID_INVALID_TOKEN,
"authentication with claim token provided but wrong");
}
if (god->h_contract_provided && ! contract_match)
{
/* Authentication provided but wrong. */
GNUNET_break_op (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_MERCHANT_GET_ORDERS_ID_INVALID_CONTRACT_HASH,
NULL);
}
if (! (token_match ||
contract_match) )
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Neither claim token nor contract matched\n");
/* Client has no rights to this order */
if (NULL == god->public_reorder_url)
{
/* We cannot give the client a new order, just fail */
if (! GNUNET_is_zero (&god->h_contract_terms))
{
GNUNET_break_op (0);
return TALER_MHD_reply_with_error (
connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_MERCHANT_GENERIC_CONTRACT_HASH_DOES_NOT_MATCH_ORDER,
NULL);
}
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_MERCHANT_GET_ORDERS_ID_INVALID_TOKEN,
"no 'public_reorder_url'");
}
/* We have a fulfillment URL, redirect the client there, maybe
the frontend can generate a fresh order for this new customer */
if (god->generate_html)
{
/* Contract was claimed (maybe by another device), so this client
cannot get the status information. Redirect to fulfillment page,
where the client may be able to pickup a fresh order -- or might
be able authenticate via session ID */
struct MHD_Response *reply;
MHD_RESULT ret;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Contract claimed, redirecting to fulfillment page for order %s\n",
order_id);
reply = MHD_create_response_from_buffer (0,
NULL,
MHD_RESPMEM_PERSISTENT);
if (NULL == reply)
{
GNUNET_break (0);
return MHD_NO;
}
GNUNET_break (MHD_YES ==
MHD_add_response_header (reply,
MHD_HTTP_HEADER_LOCATION,
god->public_reorder_url));
ret = MHD_queue_response (connection,
MHD_HTTP_FOUND,
reply);
MHD_destroy_response (reply);
return ret;
}
/* Need to generate JSON reply */
return TALER_MHD_REPLY_JSON_PACK (
connection,
MHD_HTTP_ACCEPTED,
GNUNET_JSON_pack_string ("public_reorder_url",
god->public_reorder_url));
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Claim token or contract matched\n");
if ( (NULL != god->session_id) &&
(NULL != god->fulfillment_url) )
{
/* Check if client paid for this fulfillment article
already within this session, but using a different
order ID. If so, redirect the client to the order
it already paid. Allows, for example, the case
where a mobile phone pays for a browser's session,
where the mobile phone has a different order
ID (because it purchased the article earlier)
than the one that the browser is waiting for. */
char *already_paid_order_id = NULL;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Running re-purchase detection for %s/%s\n",
god->session_id,
god->fulfillment_url);
qs = TMH_db->lookup_order_by_fulfillment (TMH_db->cls,
hc->instance->settings.id,
god->fulfillment_url,
god->session_id,
TALER_EXCHANGE_YNA_NO !=
god->allow_refunded_for_repurchase,
&already_paid_order_id);
if (qs < 0)
{
/* single, read-only SQL statements should never cause
serialization problems */
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
/* Always report on hard error as well to enable diagnostics */
GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"order by fulfillment");
}
if ( (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) ||
(0 != strcmp (order_id,
already_paid_order_id)) )
{
MHD_RESULT ret;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Sending pay request for order %s (already paid: %s)\n",
order_id,
already_paid_order_id);
ret = send_pay_request (god,
already_paid_order_id);
GNUNET_free (already_paid_order_id);
return ret;
}
GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs);
GNUNET_free (already_paid_order_id);
}
if (! god->claimed)
{
/* Order is unclaimed, no need to check for payments or even
refunds, simply always generate payment request */
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Order unclaimed, sending pay request for order %s\n",
order_id);
return send_pay_request (god,
NULL);
}
{
/* Check if paid. */
struct TALER_PrivateContractHashP h_contract;
bool paid;
qs = TMH_db->lookup_order_status (TMH_db->cls,
hc->instance->settings.id,
order_id,
&h_contract,
&paid);
if (0 >= qs)
{
/* Always report on hard error as well to enable diagnostics */
GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"lookup_order_status");
}
GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs);
if (! paid)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Order claimed but unpaid, sending pay request for order %s\n",
order_id);
return send_pay_request (god,
NULL);
}
}
if ( (god->sc.awaiting_refund) &&
(GNUNET_OK !=
TALER_amount_cmp_currency (&god->contract_total,
&god->sc.refund_expected)) )
{
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_CONFLICT,
TALER_EC_MERCHANT_GENERIC_CURRENCY_MISMATCH,
god->contract_total.currency);
}
/* At this point, we know the contract was paid. Let's check for
refunds. First, clear away refunds found from previous invocations. */
GNUNET_assert (GNUNET_OK ==
TALER_amount_set_zero (god->contract_total.currency,
&god->refund_amount));
GNUNET_assert (GNUNET_OK ==
TALER_amount_set_zero (god->contract_total.currency,
&god->refund_taken));
qs = TMH_db->lookup_refunds_detailed (TMH_db->cls,
hc->instance->settings.id,
&god->h_contract_terms,
&process_refunds_cb,
god);
if (0 > qs)
{
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"lookup_refunds_detailed");
}
if (god->bad_refund_currency_in_db)
{
GNUNET_break (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"currency mix-up between contract price and refunds in database");
}
if ( ((god->sc.awaiting_refund) &&
( (! god->refunded) ||
(1 != TALER_amount_cmp (&god->refund_amount,
&god->sc.refund_expected)) )) ||
( (god->sc.awaiting_refund_obtained) &&
(god->refund_pending) ) )
{
/* Client is waiting for a refund larger than what we have, suspend
until timeout */
struct GNUNET_TIME_Relative remaining;
remaining = GNUNET_TIME_absolute_get_remaining (god->sc.long_poll_timeout);
if ( (! GNUNET_TIME_relative_is_zero (remaining)) &&
(! god->generate_html) )
{
/* yes, indeed suspend */
if (god->sc.awaiting_refund)
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Awaiting refund exceeding %s\n",
TALER_amount2s (&god->sc.refund_expected));
if (god->sc.awaiting_refund_obtained)
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Awaiting pending refunds\n");
suspend_god (god);
return MHD_YES;
}
}
/* All operations done, build final response */
if (god->generate_html)
{
enum GNUNET_GenericReturnValue res;
if (god->refund_pending)
{
char *qr;
char *uri;
GNUNET_assert (NULL != god->contract_terms);
uri = make_taler_refund_uri (god->merchant_base_url,
order_id);
if (NULL == uri)
{
GNUNET_break (0);
return TALER_MHD_reply_with_error (god->sc.con,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_ALLOCATION_FAILURE,
"refund URI");
}
qr = TMH_create_qrcode (uri);
if (NULL == qr)
{
GNUNET_break (0);
GNUNET_free (uri);
return TALER_MHD_reply_with_error (god->sc.con,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_ALLOCATION_FAILURE,
"qr code");
}
{
json_t *context;
context = GNUNET_JSON_PACK (
GNUNET_JSON_pack_string ("order_summary",
get_order_summary (god)),
TALER_JSON_pack_amount ("refund_amount",
&god->refund_amount),
TALER_JSON_pack_amount ("refund_taken",
&god->refund_taken),
GNUNET_JSON_pack_string ("taler_refund_uri",
uri),
GNUNET_JSON_pack_string ("taler_refund_qrcode_svg",
qr));
res = TALER_TEMPLATING_reply (god->sc.con,
MHD_HTTP_OK,
"offer_refund",
hc->instance->settings.id,
uri,
context);
json_decref (context);
}
GNUNET_free (uri);
GNUNET_free (qr);
}
else
{
json_t *context;
context = GNUNET_JSON_PACK (
GNUNET_JSON_pack_object_incref ("contract_terms",
god->contract_terms),
GNUNET_JSON_pack_string ("order_summary",
get_order_summary (god)),
TALER_JSON_pack_amount ("refund_amount",
&god->refund_amount),
TALER_JSON_pack_amount ("refund_taken",
&god->refund_taken));
res = TALER_TEMPLATING_reply (god->sc.con,
MHD_HTTP_OK,
"show_order_details",
hc->instance->settings.id,
NULL,
context);
json_decref (context);
}
if (GNUNET_SYSERR == res)
{
GNUNET_break (0);
return MHD_NO;
}
return MHD_YES;
}
return TALER_MHD_REPLY_JSON_PACK (
connection,
MHD_HTTP_OK,
GNUNET_JSON_pack_allow_null (
GNUNET_JSON_pack_string ("fulfillment_url",
god->fulfillment_url)),
GNUNET_JSON_pack_bool ("refunded",
god->refunded),
GNUNET_JSON_pack_bool ("refund_pending",
god->refund_pending),
TALER_JSON_pack_amount ("refund_taken",
&god->refund_taken),
TALER_JSON_pack_amount ("refund_amount",
&god->refund_amount));
}
/* end of taler-merchant-httpd_get-orders-ID.c */
|