aboutsummaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_pay.c
blob: 569b71d4b777eb6c4a58f70f32bf8adcd0e3e38c (plain)
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
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
/*
  This file is part of TALER
  (C) 2014-2017 GNUnet e.V. and INRIA

  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 backend/taler-merchant-httpd_pay.c
 * @brief handling of /pay requests
 * @author Marcello Stanisci
 * @author Christian Grothoff
 * @author Florian Dold
 */
#include "platform.h"
#include <jansson.h>
#include <gnunet/gnunet_util_lib.h>
#include <taler/taler_signatures.h>
#include <taler/taler_json_lib.h>
#include <taler/taler_exchange_service.h>
#include "taler-merchant-httpd.h"
#include "taler-merchant-httpd_parsing.h"
#include "taler-merchant-httpd_responses.h"
#include "taler-merchant-httpd_auditors.h"
#include "taler-merchant-httpd_exchanges.h"
#include "taler-merchant-httpd_refund.h"


/**
 * How long to wait before giving up processing with the exchange?
 */
#define PAY_TIMEOUT (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
                                                    30))

/**
 * How often do we retry the (complex!) database transaction?
 */
#define MAX_RETRIES 5

/**
 * Information we keep for an individual call to the /pay handler.
 */
struct PayContext;

/**
 * Information kept during a /pay request for each coin.
 */
struct DepositConfirmation
{

  /**
   * Reference to the main PayContext
   */
  struct PayContext *pc;

  /**
   * Handle to the deposit operation we are performing for
   * this coin, NULL after the operation is done.
   */
  struct TALER_EXCHANGE_DepositHandle *dh;

  /**
   * URL of the exchange that issued this coin.
   */
  char *exchange_url;

  /**
   * Denomination of this coin.
   */
  struct TALER_DenominationPublicKey denom;

  /**
   * Amount this coin contributes to the total purchase price.
   * This amount includes the deposit fee.
   */
  struct TALER_Amount amount_with_fee;

  /**
   * Fee charged by the exchange for the deposit operation of this coin.
   */
  struct TALER_Amount deposit_fee;

  /**
   * Fee charged by the exchange for the refund operation of this coin.
   */
  struct TALER_Amount refund_fee;

  /**
   * Wire fee charged by the exchange of this coin.
   */
  struct TALER_Amount wire_fee;

  /**
   * Public key of the coin.
   */
  struct TALER_CoinSpendPublicKeyP coin_pub;

  /**
   * Signature using the @e denom key over the @e coin_pub.
   */
  struct TALER_DenominationSignature ub_sig;

  /**
   * Signature of the coin's private key over the contract.
   */
  struct TALER_CoinSpendSignatureP coin_sig;

  /**
   * Offset of this coin into the `dc` array of all coins in the
   * @e pc.
   */
  unsigned int index;

  /**
   * #GNUNET_YES if we found this coin in the database.
   */
  int found_in_db;

  /**
   * #GNUNET_YES if this coin was refunded.
   */
  int refunded;

};


/**
 * Information we keep for an individual call to the /pay handler.
 */
struct PayContext
{

  /**
   * This field MUST be first.
   * FIXME: Explain why!
   */
  struct TM_HandlerContext hc;

  /**
   * Stored in a DLL.
   */
  struct PayContext *next;

  /**
   * Stored in a DLL.
   */
  struct PayContext *prev;

  /**
   * Array with @e coins_cnt coins we are despositing.
   */
  struct DepositConfirmation *dc;

  /**
   * MHD connection to return to
   */
  struct MHD_Connection *connection;

  /**
   * Instance of the payment's instance (in JSON format)
   */
  struct MerchantInstance *mi;

  /**
   * What wire method (of the @e mi) was selected by the wallet?
   * Set in #parse_pay().
   */
  struct WireMethod *wm;

  /**
   * Proposal data for the proposal that is being
   * payed for in this context.
   */
  json_t *contract_terms;

  /**
   * Task called when the (suspended) processing for
   * the /pay request times out.
   * Happens when we don't get a response from the exchange.
   */
  struct GNUNET_SCHEDULER_Task *timeout_task;

  /**
   * Response to return, NULL if we don't have one yet.
   */
  struct MHD_Response *response;

  /**
   * Handle to the exchange that we are doing the payment with.
   * (initially NULL while @e fo is trying to find a exchange).
   */
  struct TALER_EXCHANGE_Handle *mh;

  /**
   * Handle for operation to lookup /keys (and auditors) from
   * the exchange used for this transaction; NULL if no operation is
   * pending.
   */
  struct TMH_EXCHANGES_FindOperation *fo;

  /**
   * URL of the exchange used for the last @e fo.
   */
  const char *current_exchange;

  /**
   * Placeholder for #TMH_PARSE_post_json() to keep its internal state.
   */
  void *json_parse_context;

  /**
   * Hashed proposal.
   */
  struct GNUNET_HashCode h_contract_terms;

  /**
   * "H_wire" from @e contract_terms.  Used to identify
   * the instance's wire transfer method.
   */
  struct GNUNET_HashCode h_wire;

  /**
   * Maximum fee the merchant is willing to pay, from @e root.
   * Note that IF the total fee of the exchange is higher, that is
   * acceptable to the merchant if the customer is willing to
   * pay the difference
   * (i.e. amount - max_fee <= actual-amount - actual-fee).
   */
  struct TALER_Amount max_fee;

  /**
   * Maximum wire fee the merchant is willing to pay, from @e root.
   * Note that IF the total fee of the exchange is higher, that is
   * acceptable to the merchant if the customer is willing to
   * pay the amorized difference.  Wire fees are charged over an
   * aggregate of several translations, hence unlike the deposit
   * fees, they are amortized over several customer's transactions.
   * The contract specifies under @e wire_fee_amortization how many
   * customer's transactions he expects the wire fees to be amortized
   * over on average.  Thus, if the wire fees are larger than
   * @e max_wire_fee, each customer is expected to contribute
   * $\frac{actual-wire-fee - max_wire_fee}{wire_fee_amortization}$.
   * The customer's contribution may be further reduced by the
   * difference between @e max_fee and the sum of the deposit fees.
   *
   * Default is that the merchant is unwilling to pay any wire fees.
   */
  struct TALER_Amount max_wire_fee;

  /**
   * Amount from @e root.  This is the amount the merchant expects
   * to make, minus @e max_fee.
   */
  struct TALER_Amount amount;

  /**
   * Considering all the coins with the "found_in_db" flag
   * set, what is the total amount we were so far paid on
   * this contract?
   */
  struct TALER_Amount total_paid;

  /**
   * Considering all the coins with the "found_in_db" flag
   * set, what is the total amount we had to pay in deposit
   * fees so far on this contract?
   */
  struct TALER_Amount total_fees_paid;

  /**
   * Considering all the coins with the "found_in_db" flag
   * set, what is the total amount we already refunded?
   */
  struct TALER_Amount total_refunded;

  /**
   * Wire transfer deadline. How soon would the merchant like the
   * wire transfer to be executed? (Can be given by the frontend
   * or be determined by our configuration via #wire_transfer_delay.)
   */
  struct GNUNET_TIME_Absolute wire_transfer_deadline;

  /**
   * Timestamp from @e contract_terms.
   */
  struct GNUNET_TIME_Absolute timestamp;

  /**
   * Refund deadline from @e contract_terms.
   */
  struct GNUNET_TIME_Absolute refund_deadline;

  /**
   * Deadline for the customer to pay for this proposal.
   */
  struct GNUNET_TIME_Absolute pay_deadline;

  /**
   * Number of transactions that the wire fees are expected to be
   * amortized over.  Never zero, defaults (conservateively) to 1.
   * May be higher if merchants expect many small transactions to
   * be aggregated and thus wire fees to be reasonably amortized
   * due to aggregation.
   */
  uint32_t wire_fee_amortization;

  /**
   * Number of coins this payment is made of.  Length
   * of the @e dc array.
   */
  unsigned int coins_cnt;

  /**
   * How often have we retried the 'main' transaction?
   */
  unsigned int retry_counter;

  /**
   * Number of transactions still pending.  Initially set to
   * @e coins_cnt, decremented on each transaction that
   * successfully finished.
   */
  unsigned int pending;

  /**
   * Number of transactions still pending for the currently selected
   * exchange.  Initially set to the number of coins started at the
   * exchange, decremented on each transaction that successfully
   * finished.  Once it hits zero, we pick the next exchange.
   */
  unsigned int pending_at_ce;

  /**
   * HTTP status code to use for the reply, i.e 200 for "OK".
   * Special value UINT_MAX is used to indicate hard errors
   * (no reply, return #MHD_NO).
   */
  unsigned int response_code;

  /**
   * #GNUNET_NO if the @e connection was not suspended,
   * #GNUNET_YES if the @e connection was suspended,
   * #GNUNET_SYSERR if @e connection was resumed to as
   * part of #MH_force_pc_resume during shutdown.
   */
  int suspended;

  /**
   * Which operational mode is the /pay request made in?
   */
  enum { PC_MODE_PAY, PC_MODE_ABORT_REFUND } mode;

  /**
   * Optional session id given in @e root.
   * NULL if not given.
   */
  char *session_id;

  /**
   * Transaction ID given in @e root.
   */
  char *order_id;

  /**
   * Fulfillment URL from @e contract_terms.
   */
  char *fulfillment_url;

};


/**
 * Head of active pay context DLL.
 */
static struct PayContext *pc_head;

/**
 * Tail of active pay context DLL.
 */
static struct PayContext *pc_tail;


/**
 * Force all pay contexts to be resumed as we are about
 * to shut down MHD.
 */
void
MH_force_pc_resume ()
{
  for (struct PayContext *pc = pc_head;
       NULL != pc;
       pc = pc->next)
  {
    if (GNUNET_YES == pc->suspended)
    {
      pc->suspended = GNUNET_SYSERR;
      MHD_resume_connection (pc->connection);
    }
  }
}


/**
 * Resume the given pay context and send the given response.
 * Stores the response in the @a pc and signals MHD to resume
 * the connection.  Also ensures MHD runs immediately.
 *
 * @param pc payment context
 * @param response_code response code to use
 * @param response response data to send back
 */
static void
resume_pay_with_response (struct PayContext *pc,
                          unsigned int response_code,
                          struct MHD_Response *response)
{
  pc->response_code = response_code;
  pc->response = response;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Resuming /pay handling as exchange interaction is done (%u)\n",
              response_code);
  if (NULL != pc->timeout_task)
  {
    GNUNET_SCHEDULER_cancel (pc->timeout_task);
    pc->timeout_task = NULL;
  }
  GNUNET_assert (GNUNET_YES == pc->suspended);
  pc->suspended = GNUNET_NO;
  MHD_resume_connection (pc->connection);
  TMH_trigger_daemon (); /* we resumed, kick MHD */
}


/**
 * Abort all pending /deposit operations.
 *
 * @param pc pay context to abort
 */
static void
abort_deposit (struct PayContext *pc)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Aborting pending /deposit operations\n");
  for (unsigned int i = 0; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dci = &pc->dc[i];

    if (NULL != dci->dh)
    {
      TALER_EXCHANGE_deposit_cancel (dci->dh);
      dci->dh = NULL;
    }
  }
}


/**
 * Generate a response that indicates payment success.
 *
 * @param pc payment context
 * @return the mhd response
 */
static struct MHD_Response *
sign_success_response (struct PayContext *pc)
{
  json_t *refunds;
  enum TALER_ErrorCode ec;
  const char *errmsg;
  struct GNUNET_CRYPTO_EddsaSignature sig;
  json_t *resp;
  struct MHD_Response *mret;

  refunds = TM_get_refund_json (pc->mi,
                                &pc->h_contract_terms,
                                &ec,
                                &errmsg);
  if (NULL == refunds)
    return TMH_RESPONSE_make_error (ec,
                                    errmsg);
  {
    struct PaymentResponsePS mr = {
      .purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_PAYMENT_OK),
      .purpose.size = htonl (sizeof (mr)),
      .h_contract_terms = pc->h_contract_terms
    };

    GNUNET_CRYPTO_eddsa_sign (&pc->mi->privkey.eddsa_priv,
                              &mr.purpose,
                              &sig);
  }
  resp = json_pack ("{s:O, s:o, s:o, s:o}",
                    "contract_terms",
                    pc->contract_terms,
                    "sig",
                    GNUNET_JSON_from_data_auto (&sig),
                    "h_contract_terms",
                    GNUNET_JSON_from_data (&pc->h_contract_terms,
                                           sizeof (struct GNUNET_HashCode)),
                    "refund_permissions",
                    refunds);

  mret = TMH_RESPONSE_make_json (resp);
  json_decref (resp);
  return mret;
}


/**
 * Resume payment processing with an error.
 *
 * @param pc operation to resume
 * @param http_status http status code to return
 * @param ec taler error code to return
 * @param msg human readable error message
 */
static void
resume_pay_with_error (struct PayContext *pc,
                       unsigned int http_status,
                       enum TALER_ErrorCode ec,
                       const char *msg)
{
  resume_pay_with_response (pc,
                            http_status,
                            TMH_RESPONSE_make_error (ec,
                                                     msg));
}


/**
 * Custom cleanup routine for a `struct PayContext`.
 *
 * @param hc the `struct PayContext` to clean up.
 */
static void
pay_context_cleanup (struct TM_HandlerContext *hc)
{
  struct PayContext *pc = (struct PayContext *) hc;

  if (NULL != pc->timeout_task)
  {
    GNUNET_SCHEDULER_cancel (pc->timeout_task);
    pc->timeout_task = NULL;
  }
  TMH_PARSE_post_cleanup_callback (pc->json_parse_context);
  for (unsigned int i = 0; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dc = &pc->dc[i];

    if (NULL != dc->dh)
    {
      TALER_EXCHANGE_deposit_cancel (dc->dh);
      dc->dh = NULL;
    }
    if (NULL != dc->denom.rsa_public_key)
    {
      GNUNET_CRYPTO_rsa_public_key_free (dc->denom.rsa_public_key);
      dc->denom.rsa_public_key = NULL;
    }
    if (NULL != dc->ub_sig.rsa_signature)
    {
      GNUNET_CRYPTO_rsa_signature_free (dc->ub_sig.rsa_signature);
      dc->ub_sig.rsa_signature = NULL;
    }
    GNUNET_free_non_null (dc->exchange_url);
  }
  GNUNET_free_non_null (pc->dc);
  if (NULL != pc->fo)
  {
    TMH_EXCHANGES_find_exchange_cancel (pc->fo);
    pc->fo = NULL;
  }
  if (NULL != pc->response)
  {
    MHD_destroy_response (pc->response);
    pc->response = NULL;
  }
  if (NULL != pc->contract_terms)
  {
    json_decref (pc->contract_terms);
    pc->contract_terms = NULL;
  }
  GNUNET_free_non_null (pc->order_id);
  GNUNET_free_non_null (pc->session_id);
  GNUNET_free_non_null (pc->fulfillment_url);
  GNUNET_CONTAINER_DLL_remove (pc_head,
                               pc_tail,
                               pc);
  GNUNET_free (pc);
}


/**
 * Check whether the amount paid is sufficient to cover
 * the contract.
 *
 * @param pc payment context to check
 * @return taler error code, #TALER_EC_NONE if amount is sufficient
 */
static enum TALER_ErrorCode
check_payment_sufficient (struct PayContext *pc)
{
  struct TALER_Amount acc_fee;
  struct TALER_Amount acc_amount;
  struct TALER_Amount wire_fee_delta;
  struct TALER_Amount wire_fee_customer_contribution;
  struct TALER_Amount total_wire_fee;

  if (0 == pc->coins_cnt)
    return TALER_EC_PAY_PAYMENT_INSUFFICIENT;

  acc_fee = pc->dc[0].deposit_fee;
  total_wire_fee = pc->dc[0].wire_fee;
  acc_amount = pc->dc[0].amount_with_fee;

  /**
   * This loops calculates what are the deposit fee / total
   * amount with fee / and wire fee, for all the coins.
   */
  for (unsigned int i = 1; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dc = &pc->dc[i];

    GNUNET_assert (GNUNET_YES == dc->found_in_db);
    if ( (GNUNET_OK !=
          TALER_amount_add (&acc_fee,
                            &dc->deposit_fee,
                            &acc_fee)) ||
         (GNUNET_OK !=
          TALER_amount_add (&acc_amount,
                            &dc->amount_with_fee,
                            &acc_amount)) )
    {
      GNUNET_break_op (0);
      /* Overflow in these amounts? Very strange. */
      return TALER_EC_PAY_AMOUNT_OVERFLOW;
    }
    if (1 ==
        TALER_amount_cmp (&dc->deposit_fee,
                          &dc->amount_with_fee))
    {
      GNUNET_break_op (0);
      /* fee higher than residual coin value, makes no sense. */
      return TALER_EC_PAY_FEES_EXCEED_PAYMENT;
    }

    /* If exchange differs, add wire fee */
    {
      int new_exchange = GNUNET_YES;

      for (unsigned int j = 0; j<i; j++)
        if (0 == strcasecmp (dc->exchange_url,
                             pc->dc[j].exchange_url))
        {
          new_exchange = GNUNET_NO;
          break;
        }
      if (GNUNET_YES == new_exchange)
      {
        if (GNUNET_OK !=
            TALER_amount_add (&total_wire_fee,
                              &total_wire_fee,
                              &dc->wire_fee))
        {
          GNUNET_break_op (0);
          return TALER_EC_PAY_EXCHANGE_REJECTED;
        }
      }
    }
  }

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Amount received from wallet: %s\n",
              TALER_amount_to_string (&acc_amount));

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Deposit fee for all coins: %s\n",
              TALER_amount_to_string (&acc_fee));

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Total wire fee: %s\n",
              TALER_amount_to_string (&total_wire_fee));

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Max wire fee: %s\n",
              TALER_amount_to_string (&pc->max_wire_fee));

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Deposit fee limit for merchant: %s\n",
              TALER_amount_to_string (&pc->max_fee));

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Total refunded amount: %s\n",
              TALER_amount_to_string (&pc->total_refunded));

  /* Now compare exchange wire fee compared to
   * what we are willing to pay */
  if (GNUNET_YES !=
      TALER_amount_cmp_currency (&total_wire_fee,
                                 &pc->max_wire_fee))
  {
    GNUNET_break (0);
    return TALER_EC_PAY_WIRE_FEE_CURRENCY_MISSMATCH;
  }

  if (GNUNET_OK == TALER_amount_subtract (&wire_fee_delta,
                                          &total_wire_fee,
                                          &pc->max_wire_fee))
  {
    /* Actual wire fee is indeed higher than our maximum,
       compute how much the customer is expected to cover!  */
    TALER_amount_divide (&wire_fee_customer_contribution,
                         &wire_fee_delta,
                         pc->wire_fee_amortization);
  }
  else
  {
    /* Wire fee threshold is still above the wire fee amount.
       Customer is not going to contribute on this.  */
    GNUNET_assert
      (GNUNET_OK == TALER_amount_get_zero
        (total_wire_fee.currency,
        &wire_fee_customer_contribution));
  }

  /**
   * Deposit fees of *all* the coins are higher than
   * the fixed limit that the merchant is willing to
   * pay.  The fees difference must be paid by the customer.
   */
  if (-1 == TALER_amount_cmp (&pc->max_fee,
                              &acc_fee))
  {
    struct TALER_Amount excess_fee;
    struct TALER_Amount total_needed;

    /* compute fee amount to be covered by customer */
    GNUNET_assert
      (GNUNET_OK == TALER_amount_subtract (&excess_fee,
                                           &acc_fee,
                                           &pc->max_fee));
    /* add that to the total */
    if (GNUNET_OK != TALER_amount_add (&total_needed,
                                       &excess_fee,
                                       &pc->amount))
    {
      GNUNET_break (0);
      return TALER_EC_PAY_AMOUNT_OVERFLOW;
    }

    /* add wire fee contribution to the total */
    GNUNET_assert
      (GNUNET_OK == TALER_amount_add
        (&total_needed,
        &total_needed,
        &wire_fee_customer_contribution));

    /* check if total payment sufficies */
    if (-1 == TALER_amount_cmp (&acc_amount,
                                &total_needed))
    {
      GNUNET_break_op (0);
      return TALER_EC_PAY_PAYMENT_INSUFFICIENT_DUE_TO_FEES;
    }
  }

  /**
   * The deposit fees of all the coins are below the
   * limit that the merchant is willing to pay, for some X
   * delta.  Since a fraction of the wire fee must be paid
   * by the customer, this block will take from X such a
   * fraction, and check that the remaining paid amount is
   * enough to pay both the remaining wire fee customer's
   * fraction AND the price of the contract itself.
   */
  else
  {
    struct TALER_Amount deposit_fee_savings;

    GNUNET_assert
      (GNUNET_SYSERR != TALER_amount_subtract
        (&deposit_fee_savings,
        &pc->max_fee,
        &acc_fee));

    /* See how much of wire fee
       contribution is covered by
       fee_savings */
    if (-1 == TALER_amount_cmp (&deposit_fee_savings,
                                &wire_fee_customer_contribution))
    {
      /* wire_fee_customer_contribution > deposit_fee_savings */
      GNUNET_assert
        (GNUNET_SYSERR != TALER_amount_subtract
          (&wire_fee_customer_contribution,
          &wire_fee_customer_contribution,
          &deposit_fee_savings));

      /* subtract remaining wire fees from total contribution */
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Subtract remaining wire fees"
                  " from total contribution: %s\n",
                  TALER_amount_to_string
                    (&wire_fee_customer_contribution));

      if (GNUNET_SYSERR == TALER_amount_subtract
            (&acc_amount,
            &acc_amount,
            &wire_fee_customer_contribution))
      {
        GNUNET_break_op (0);
        return TALER_EC_PAY_PAYMENT_INSUFFICIENT_DUE_TO_FEES;
      }
    }

    /* fees are acceptable, merchant covers them all;
       let's check the amount */
    if (-1 == TALER_amount_cmp (&acc_amount,
                                &pc->amount))
    {
      char *str;

      GNUNET_break_op (0);
      str = TALER_amount_to_string (&acc_amount);
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "price vs. sent: %s vs. %s\n",
                  TALER_amount2s (&pc->amount),
                  str);
      GNUNET_free (str);
      return TALER_EC_PAY_PAYMENT_INSUFFICIENT;
    }
  }

  /* Do not count any refunds towards the payment */
  GNUNET_assert
    (GNUNET_SYSERR != TALER_amount_subtract (&acc_amount,
                                             &acc_amount,
                                             &pc->total_refunded));
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Subtracting total refunds from paid amount: %s\n",
              TALER_amount_to_string (&pc->total_refunded));

  return TALER_EC_NONE;
}


/**
 * Generate full error response based on the @a ec
 *
 * @param pc context for which to generate the error
 * @param ec error code identifying the issue
 */
static void
generate_error_response (struct PayContext *pc,
                         enum TALER_ErrorCode ec)
{
  switch (ec)
  {
  case TALER_EC_PAY_AMOUNT_OVERFLOW:
    resume_pay_with_error (pc,
                           MHD_HTTP_BAD_REQUEST,
                           ec,
                           "Overflow adding up amounts");
    break;
  case TALER_EC_PAY_FEES_EXCEED_PAYMENT:
    resume_pay_with_error (pc,
                           MHD_HTTP_BAD_REQUEST,
                           ec,
                           "Deposit fees exceed coin's contribution");
    break;
  case TALER_EC_PAY_PAYMENT_INSUFFICIENT_DUE_TO_FEES:
    resume_pay_with_error (pc,
                           MHD_HTTP_METHOD_NOT_ACCEPTABLE,
                           ec,
                           "insufficient funds (including excessive exchange fees to be covered by customer)");
    break;
  case TALER_EC_PAY_PAYMENT_INSUFFICIENT:
    resume_pay_with_error (pc,
                           MHD_HTTP_METHOD_NOT_ACCEPTABLE,
                           ec,
                           "insufficient funds");
    break;
  case TALER_EC_PAY_WIRE_FEE_CURRENCY_MISSMATCH:
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           ec,
                           "wire_fee currency does not match");
    break;
  case TALER_EC_PAY_EXCHANGE_REJECTED:
    resume_pay_with_error (pc,
                           MHD_HTTP_PRECONDITION_FAILED,
                           ec,
                           "exchange charges incompatible wire fee");
    break;
  default:
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           ec,
                           "unexpected error code");
    GNUNET_break (0);
    break;
  }
}


/**
 * Find the exchange we need to talk to for the next
 * pending deposit permission.
 *
 * @param pc payment context we are processing
 */
static void
find_next_exchange (struct PayContext *pc);


/**
 * Begin of the DB transaction.  If required (from
 * soft/serialization errors), the transaction can be
 * restarted here.
 *
 * @param pc payment context to transact
 */
static void
begin_transaction (struct PayContext *pc);


/**
 * Callback to handle a deposit permission's response.
 *
 * @param cls a `struct DepositConfirmation` (i.e. a pointer
 *   into the global array of confirmations and an index for this call
 *   in that array). That way, the last executed callback can detect
 *   that no other confirmations are on the way, and can pack a response
 *   for the wallet
 * @param http_status HTTP response code, #MHD_HTTP_OK
 *   (200) for successful deposit; 0 if the exchange's reply is bogus (fails
 *   to follow the protocol)
 * @param ec taler-specific error code, #TALER_EC_NONE on success
 * @param exchange_sig signature from the exchange over the deposit confirmation
 * @param sign_key which key did the exchange use to sign the @a proof
 * @param proof the received JSON reply,
 *   should be kept as proof (and, in case of errors, be forwarded to
 *   the customer)
 */
static void
deposit_cb (void *cls,
            unsigned int http_status,
            enum TALER_ErrorCode ec,
            const struct TALER_ExchangeSignatureP *exchange_sig,
            const struct TALER_ExchangePublicKeyP *sign_key,
            const json_t *proof)
{
  struct DepositConfirmation *dc = cls;
  struct PayContext *pc = dc->pc;
  enum GNUNET_DB_QueryStatus qs;

  dc->dh = NULL;
  GNUNET_assert (GNUNET_YES == pc->suspended);
  pc->pending_at_ce--;
  if (MHD_HTTP_OK != http_status)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Deposit operation failed with HTTP code %u\n",
                http_status);
    /* Transaction failed; stop all other ongoing deposits */
    abort_deposit (pc);

    if (NULL == proof)
    {
      /* We can't do anything meaningful here, the exchange did something wrong */
      resume_pay_with_response (pc,
                                MHD_HTTP_SERVICE_UNAVAILABLE,
                                TMH_RESPONSE_make_json_pack (
                                  "{s:s, s:I, s:I, s:I, s:s}",
                                  "error",
                                  "exchange failed",
                                  "code",
                                  (json_int_t) TALER_EC_PAY_EXCHANGE_FAILED,
                                  "exchange-code",
                                  (json_int_t) ec,
                                  "exchange-http-status",
                                  (json_int_t) http_status,
                                  "hint",
                                  "The exchange provided an unexpected response"));
    }
    else
    {
      /* Forward error, adding the "coin_pub" for which the
         error was being generated */
      json_t *eproof;

      eproof = json_copy ((json_t *) proof);
      json_object_set_new (eproof,
                           "coin_pub",
                           GNUNET_JSON_from_data_auto (&dc->coin_pub));
      resume_pay_with_response (pc,
                                http_status,
                                TMH_RESPONSE_make_json (eproof));
      json_decref (eproof);
    }
    return;
  }
  /* store result to DB */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Storing successful payment for h_contract_terms `%s' and merchant `%s'\n",
              GNUNET_h2s (&pc->h_contract_terms),
              TALER_B2S (&pc->mi->pubkey));
  /* NOTE: not run in any transaction block, simply as a
     transaction by itself! */
  db->preflight (db->cls);
  qs = db->store_deposit (db->cls,
                          &pc->h_contract_terms,
                          &pc->mi->pubkey,
                          &dc->coin_pub,
                          dc->exchange_url,
                          &dc->amount_with_fee,
                          &dc->deposit_fee,
                          &dc->refund_fee,
                          &dc->wire_fee,
                          sign_key,
                          proof);
  if (0 > qs)
  {
    /* Special report if retries insufficient */
    abort_deposit (pc);
    if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    {
      begin_transaction (pc);
      return;
    }
    /* Always report on hard error as well to enable diagnostics */
    GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    /* Forward error including 'proof' for the body */
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           TALER_EC_PAY_DB_STORE_PAY_ERROR,
                           "Merchant database error");
    return;
  }
  dc->found_in_db = GNUNET_YES;
  pc->pending--;

  if (0 != pc->pending_at_ce)
    return; /* still more to do with current exchange */
  find_next_exchange (pc);
}


/**
 * Function called with the result of our exchange lookup.
 *
 * @param cls the `struct PayContext`
 * @param mh NULL if exchange was not found to be acceptable
 * @param wire_fee current applicable fee for dealing with @a mh,
 *        NULL if not available
 * @param exchange_trusted #GNUNET_YES if this exchange is
 *        trusted by config
 */
static void
process_pay_with_exchange (void *cls,
                           struct TALER_EXCHANGE_Handle *mh,
                           const struct TALER_Amount *wire_fee,
                           int exchange_trusted)
{
  struct PayContext *pc = cls;
  const struct TALER_EXCHANGE_Keys *keys;

  pc->fo = NULL;
  GNUNET_assert (GNUNET_YES == pc->suspended);
  if (NULL == mh)
  {
    /* The exchange on offer is not in the set of our (trusted)
       exchanges.  Reject the payment. */
    GNUNET_break_op (0);
    resume_pay_with_error (pc,
                           MHD_HTTP_PRECONDITION_FAILED,
                           TALER_EC_PAY_EXCHANGE_REJECTED,
                           "exchange not supported");
    return;
  }
  pc->mh = mh;
  keys = TALER_EXCHANGE_get_keys (mh);
  if (NULL == keys)
  {
    GNUNET_break (0);
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           TALER_EC_PAY_EXCHANGE_KEYS_FAILURE,
                           "no keys");
    return;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Found transaction data for proposal `%s'"
              " of merchant `%s', initiating deposits\n",
              GNUNET_h2s (&pc->h_contract_terms),
              TALER_B2S (&pc->mi->pubkey));

  /* Initiate /deposit operation for all coins of
     the current exchange (!) */
  GNUNET_assert (0 == pc->pending_at_ce);
  for (unsigned int i = 0; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dc = &pc->dc[i];
    const struct TALER_EXCHANGE_DenomPublicKey *denom_details;

    if (GNUNET_YES == dc->found_in_db)
      continue;
    if (0 != strcmp (dc->exchange_url,
                     pc->current_exchange))
      continue;
    denom_details = TALER_EXCHANGE_get_denomination_key
                      (keys,
                      &dc->denom);

    if (NULL == denom_details)
    {
      GNUNET_break_op (0);
      resume_pay_with_response
        (pc,
        MHD_HTTP_BAD_REQUEST,
        TMH_RESPONSE_make_json_pack
          ("{s:s, s:I, s:o, s:o}",
          "error", "denomination not found",
          "code", TALER_EC_PAY_DENOMINATION_KEY_NOT_FOUND,
          "denom_pub", GNUNET_JSON_from_rsa_public_key
            (dc->denom.rsa_public_key),
          "exchange_keys", TALER_EXCHANGE_get_keys_raw (mh)));
      return;
    }
    if (GNUNET_OK !=
        TMH_AUDITORS_check_dk (mh,
                               denom_details,
                               exchange_trusted))
    {
      GNUNET_break_op (0);
      resume_pay_with_response
        (pc,
        MHD_HTTP_BAD_REQUEST,
        TMH_RESPONSE_make_json_pack
          ("{s:s, s:I, s:o}",
          "error", "invalid denomination",
          "code", (json_int_t)
          TALER_EC_PAY_DENOMINATION_KEY_AUDITOR_FAILURE,
          "denom_pub", GNUNET_JSON_from_rsa_public_key
            (dc->denom.rsa_public_key)));
      return;
    }

    dc->deposit_fee = denom_details->fee_deposit;
    dc->refund_fee = denom_details->fee_refund;
    dc->wire_fee = *wire_fee;

    GNUNET_assert (NULL != pc->wm);
    GNUNET_assert (NULL != pc->wm->j_wire);
    GNUNET_log
      (GNUNET_ERROR_TYPE_DEBUG,
      "Timing for this payment, wire_deadline:"
      " %llu, refund_deadline: %llu\n",
      (unsigned long long)
      pc->wire_transfer_deadline.abs_value_us,
      (unsigned long long) pc->refund_deadline.abs_value_us);
    db->preflight (db->cls);

    dc->dh = TALER_EXCHANGE_deposit (mh,
                                     &dc->amount_with_fee,
                                     pc->wire_transfer_deadline,
                                     pc->wm->j_wire,
                                     &pc->h_contract_terms,
                                     &dc->coin_pub,
                                     &dc->ub_sig,
                                     &dc->denom,
                                     pc->timestamp,
                                     &pc->mi->pubkey,
                                     pc->refund_deadline,
                                     &dc->coin_sig,
                                     &deposit_cb,
                                     dc);
    if (NULL == dc->dh)
    {
      /* Signature was invalid.  If the exchange was
         unavailable, we'd get that information in the callback. */
      GNUNET_break_op (0);
      resume_pay_with_response
        (pc,
        MHD_HTTP_UNAUTHORIZED,
        TMH_RESPONSE_make_json_pack
          ("{s:s, s:I, s:i}",
          "hint", "Coin signature invalid.",
          "code", (json_int_t)
          TALER_EC_PAY_COIN_SIGNATURE_INVALID,
          "coin_idx", i));
      return;
    }

    pc->pending_at_ce++;
  }
}


/**
 * Find the exchange we need to talk to for the next
 * pending deposit permission.
 *
 * @param pc payment context we are processing
 */
static void
find_next_exchange (struct PayContext *pc)
{
  for (unsigned int i = 0; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dc = &pc->dc[i];

    if (GNUNET_YES != dc->found_in_db)
    {
      db->preflight (db->cls);
      pc->current_exchange = dc->exchange_url;
      pc->fo = TMH_EXCHANGES_find_exchange (pc->current_exchange,
                                            pc->wm->wire_method,
                                            &process_pay_with_exchange,
                                            pc);
      if (NULL == pc->fo)
      {
        GNUNET_break (0);
        resume_pay_with_error (pc,
                               MHD_HTTP_INTERNAL_SERVER_ERROR,
                               TALER_EC_PAY_EXCHANGE_FAILED,
                               "Failed to lookup exchange by URL");
        return;
      }
      return;
    }
  }
  pc->current_exchange = NULL;
  db->preflight (db->cls);
  /* We are done with all the HTTP requests, go back and try
     the 'big' database transaction! (It should work now!) */
  begin_transaction (pc);
}


/**
 * Handle a timeout for the processing of the pay request.
 *
 * @param cls our `struct PayContext`
 */
static void
handle_pay_timeout (void *cls)
{
  struct PayContext *pc = cls;

  pc->timeout_task = NULL;
  GNUNET_assert (GNUNET_YES == pc->suspended);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Resuming /pay with error after timeout\n");
  if (NULL != pc->fo)
  {
    TMH_EXCHANGES_find_exchange_cancel (pc->fo);
    pc->fo = NULL;
  }
  resume_pay_with_error (pc,
                         MHD_HTTP_SERVICE_UNAVAILABLE,
                         TALER_EC_PAY_EXCHANGE_TIMEOUT,
                         "exchange not reachable");
}


/**
 * Function called with information about a coin that was deposited.
 *
 * @param cls closure
 * @param h_contract_terms hashed proposal data
 * @param coin_pub public key of the coin
 * @param exchange_url URL of the exchange that issued @a coin_pub
 * @param amount_with_fee amount the exchange will deposit for this coin
 * @param deposit_fee fee the exchange will charge for this coin
 * @param refund_fee fee the exchange will charge for refunding this coin
 * @param wire_fee wire fee the exchange of this coin charges
 * @param exchange_proof proof from exchange that coin was accepted
 */
static void
check_coin_paid (void *cls,
                 const struct GNUNET_HashCode *h_contract_terms,
                 const struct TALER_CoinSpendPublicKeyP *coin_pub,
                 const char *exchange_url,
                 const struct TALER_Amount *amount_with_fee,
                 const struct TALER_Amount *deposit_fee,
                 const struct TALER_Amount *refund_fee,
                 const struct TALER_Amount *wire_fee,
                 const json_t *exchange_proof)
{
  struct PayContext *pc = cls;

  if (0 != GNUNET_memcmp (&pc->h_contract_terms,
                          h_contract_terms))
  {
    GNUNET_break (0);
    return;
  }
  for (unsigned int i = 0; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dc = &pc->dc[i];

    if (GNUNET_YES == dc->found_in_db)
      continue; /* processed earlier */

    /* Get matching coin from results*/
    if ( (0 != GNUNET_memcmp (coin_pub,
                              &dc->coin_pub)) ||
         (0 != TALER_amount_cmp (amount_with_fee,
                                 &dc->amount_with_fee)) )
      continue;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Coin (%s) already found in our DB.\n",
                TALER_b2s (coin_pub,
                           sizeof (*coin_pub)));
    if (GNUNET_OK !=
        TALER_amount_add (&pc->total_paid,
                          &pc->total_paid,
                          amount_with_fee))
    {
      /* We accepted this coin for payment on this contract before,
         and now we can't even add the amount!? */
      GNUNET_break (0);
      continue;
    }
    if (GNUNET_OK !=
        TALER_amount_add (&pc->total_fees_paid,
                          &pc->total_fees_paid,
                          deposit_fee))
    {
      /* We accepted this coin for payment on this contract before,
         and now we can't even add the amount!? */
      GNUNET_break (0);
      continue;
    }
    dc->deposit_fee = *deposit_fee;
    dc->refund_fee = *refund_fee;
    dc->wire_fee = *wire_fee;
    dc->amount_with_fee = *amount_with_fee;
    dc->found_in_db = GNUNET_YES;
    pc->pending--;
  }
}


/**
 * Try to parse the pay request into the given pay context.
 * Schedules an error response in the connection on failure.
 *
 * @param connection HTTP connection we are receiving payment on
 * @param root JSON upload with payment data
 * @param pc context we use to handle the payment
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure (response was queued with MHD)
 *         #GNUNET_SYSERR on hard error (MHD connection must be dropped)
 */
static int
parse_pay (struct MHD_Connection *connection,
           const json_t *root,
           struct PayContext *pc)
{
  json_t *coins;
  json_t *coin;
  json_t *merchant;
  unsigned int coins_index;
  const char *order_id;
  const char *mode;
  struct TALER_MerchantPublicKeyP merchant_pub;
  int res;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_string ("mode",
                             &mode),
    GNUNET_JSON_spec_json ("coins",
                           &coins),
    GNUNET_JSON_spec_string ("order_id",
                             &order_id),
    GNUNET_JSON_spec_fixed_auto ("merchant_pub",
                                 &merchant_pub),
    GNUNET_JSON_spec_end ()
  };
  enum GNUNET_DB_QueryStatus qs;
  const char *session_id;
  struct GNUNET_TIME_Relative used_wire_transfer_delay;

  res = TMH_PARSE_json_data (connection,
                             root,
                             spec);
  if (GNUNET_YES != res)
  {
    GNUNET_break (0);
    return res;
  }

  session_id = json_string_value (json_object_get (root,
                                                   "session_id"));

  if (0 != GNUNET_memcmp (&merchant_pub,
                          &pc->mi->pubkey))
  {
    TALER_LOG_INFO ("Unknown merchant public key included in payment (usually wrong instance chosen)\n");
    TMH_RESPONSE_reply_rc (connection,
                           MHD_HTTP_NOT_FOUND,
                           TALER_EC_PAY_WRONG_INSTANCE,
                           "Payment sent to wrong instance (merchant_pub unknown to the merchant)");
    return GNUNET_NO;
  }

  if (NULL != session_id)
    pc->session_id = GNUNET_strdup (session_id);
  pc->order_id = GNUNET_strdup (order_id);
  GNUNET_assert (NULL == pc->contract_terms);
  qs = db->find_contract_terms (db->cls,
                                &pc->contract_terms,
                                order_id,
                                &merchant_pub);
  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 (GNUNET_DB_STATUS_HARD_ERROR == qs);
    return TMH_RESPONSE_reply_internal_error (connection,
                                              TALER_EC_PAY_DB_FETCH_PAY_ERROR,
                                              "db error to previous /pay data");

  }
  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
  {
    GNUNET_JSON_parse_free (spec);
    if (MHD_YES !=
        TMH_RESPONSE_reply_not_found (connection,
                                      TALER_EC_PAY_DB_STORE_PAY_ERROR,
                                      "Proposal not found"))
    {
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    return GNUNET_NO;
  }

  if (GNUNET_OK !=
      TALER_JSON_hash (pc->contract_terms,
                       &pc->h_contract_terms))
  {
    GNUNET_break (0);
    GNUNET_JSON_parse_free (spec);
    if (MHD_YES !=
        TMH_RESPONSE_reply_internal_error (connection,
                                           TALER_EC_PAY_FAILED_COMPUTE_PROPOSAL_HASH,
                                           "Failed to hash proposal"))
    {
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    return GNUNET_NO;
  }

  merchant = json_object_get (pc->contract_terms,
                              "merchant");
  if (NULL == merchant)
  {
    /* invalid contract */
    GNUNET_break (0);
    GNUNET_JSON_parse_free (spec);
    if (MHD_YES !=
        TMH_RESPONSE_reply_internal_error (connection,
                                           TALER_EC_PAY_MERCHANT_FIELD_MISSING,
                                           "No merchant field in proposal"))
    {
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    return GNUNET_NO;
  }
  if (0 != strcasecmp ("abort-refund",
                       mode))
    pc->mode = PC_MODE_PAY;
  else
    pc->mode = PC_MODE_ABORT_REFUND;
  {
    const char *fulfillment_url;
    struct GNUNET_JSON_Specification espec[] = {
      GNUNET_JSON_spec_absolute_time ("refund_deadline",
                                      &pc->refund_deadline),
      GNUNET_JSON_spec_absolute_time ("pay_deadline",
                                      &pc->pay_deadline),
      GNUNET_JSON_spec_absolute_time ("timestamp",
                                      &pc->timestamp),
      TALER_JSON_spec_amount ("max_fee",
                              &pc->max_fee),
      TALER_JSON_spec_amount ("amount",
                              &pc->amount),
      GNUNET_JSON_spec_string ("fulfillment_url",
                               &fulfillment_url),
      GNUNET_JSON_spec_fixed_auto ("H_wire",
                                   &pc->h_wire),
      GNUNET_JSON_spec_end ()
    };

    res = TMH_PARSE_json_data (connection,
                               pc->contract_terms,
                               espec);
    if (GNUNET_YES != res)
    {
      GNUNET_JSON_parse_free (spec);
      GNUNET_break (0);
      return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
    }

    pc->fulfillment_url = GNUNET_strdup (fulfillment_url);

    /* Use the value from config as default.  */
    used_wire_transfer_delay = wire_transfer_delay;

    if (NULL != json_object_get (pc->contract_terms,
                                 "wire_transfer_delay"))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Frontend specified wire transfer delay\n");

      struct GNUNET_JSON_Specification wspec[] = {
        GNUNET_JSON_spec_relative_time ("wire_transfer_delay",
                                        &used_wire_transfer_delay),
        GNUNET_JSON_spec_end ()
      };

      res = TMH_PARSE_json_data (connection,
                                 pc->contract_terms,
                                 wspec);
      if (GNUNET_YES != res)
      {
        GNUNET_JSON_parse_free (spec);
        GNUNET_break (0);
        return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
      }
    }

    pc->wire_transfer_deadline
      = GNUNET_TIME_absolute_add (pc->timestamp,
                                  used_wire_transfer_delay);

    if (pc->wire_transfer_deadline.abs_value_us <
        pc->refund_deadline.abs_value_us)
    {
      GNUNET_break (0);
      GNUNET_JSON_parse_free (spec);
      return TMH_RESPONSE_reply_external_error (connection,
                                                TALER_EC_PAY_REFUND_DEADLINE_PAST_WIRE_TRANSFER_DEADLINE,
                                                "refund deadline after wire transfer deadline");
    }
  }

  /* find wire method */
  {
    struct WireMethod *wm;

    wm = pc->mi->wm_head;
    while (0 != GNUNET_memcmp (&pc->h_wire,
                               &wm->h_wire))
      wm = wm->next;
    if (NULL == wm)
    {
      GNUNET_break (0);
      GNUNET_JSON_parse_free (spec);
      return TMH_RESPONSE_reply_internal_error (connection,
                                                TALER_EC_PAY_WIRE_HASH_UNKNOWN,
                                                "Did not find matching wire details");
    }
    pc->wm = wm;
  }

  /* parse optional details */
  if (NULL != json_object_get (pc->contract_terms,
                               "max_wire_fee"))
  {
    struct GNUNET_JSON_Specification espec[] = {
      TALER_JSON_spec_amount ("max_wire_fee",
                              &pc->max_wire_fee),
      GNUNET_JSON_spec_end ()
    };

    res = TMH_PARSE_json_data (connection,
                               pc->contract_terms,
                               espec);
    if (GNUNET_YES != res)
    {
      GNUNET_break_op (0); /* invalid input, use default */
      /* default is we cover no fee */
      GNUNET_assert (GNUNET_OK ==
                     TALER_amount_get_zero (pc->max_fee.currency,
                                            &pc->max_wire_fee));
    }
  }
  else
  {
    /* default is we cover no fee */
    GNUNET_assert (GNUNET_OK ==
                   TALER_amount_get_zero (pc->max_fee.currency,
                                          &pc->max_wire_fee));
  }
  if (NULL != json_object_get (pc->contract_terms,
                               "wire_fee_amortization"))
  {
    struct GNUNET_JSON_Specification espec[] = {
      GNUNET_JSON_spec_uint32 ("wire_fee_amortization",
                               &pc->wire_fee_amortization),
      GNUNET_JSON_spec_end ()
    };

    res = TMH_PARSE_json_data (connection,
                               pc->contract_terms,
                               espec);
    if ( (GNUNET_YES != res) ||
         (0 == pc->wire_fee_amortization) )
    {
      GNUNET_break_op (0); /* invalid input, use default */
      /* default is no amortization */
      pc->wire_fee_amortization = 1;
    }
  }
  else
  {
    pc->wire_fee_amortization = 1;
  }

  pc->coins_cnt = json_array_size (coins);
  if (0 == pc->coins_cnt)
  {
    GNUNET_JSON_parse_free (spec);
    return TMH_RESPONSE_reply_arg_invalid (connection,
                                           TALER_EC_PAY_COINS_ARRAY_EMPTY,
                                           "coins");
  }
  /* note: 1 coin = 1 deposit confirmation expected */
  pc->dc = GNUNET_new_array (pc->coins_cnt,
                             struct DepositConfirmation);

  /* This loop populates the array 'dc' in 'pc' */
  json_array_foreach (coins, coins_index, coin)
  {
    struct DepositConfirmation *dc = &pc->dc[coins_index];
    const char *exchange_url;
    struct GNUNET_JSON_Specification ispec[] = {
      TALER_JSON_spec_denomination_public_key ("denom_pub",
                                               &dc->denom),
      TALER_JSON_spec_amount ("contribution",
                              &dc->amount_with_fee),
      GNUNET_JSON_spec_string ("exchange_url",
                               &exchange_url),
      GNUNET_JSON_spec_fixed_auto ("coin_pub",
                                   &dc->coin_pub),
      TALER_JSON_spec_denomination_signature ("ub_sig",
                                              &dc->ub_sig),
      GNUNET_JSON_spec_fixed_auto ("coin_sig",
                                   &dc->coin_sig),
      GNUNET_JSON_spec_end ()
    };

    res = TMH_PARSE_json_data (connection,
                               coin,
                               ispec);
    if (GNUNET_YES != res)
    {
      GNUNET_JSON_parse_free (spec);
      GNUNET_break_op (0);
      return res;
    }
    dc->exchange_url = GNUNET_strdup (exchange_url);
    dc->index = coins_index;
    dc->pc = pc;
  }
  pc->pending = pc->coins_cnt;
  GNUNET_JSON_parse_free (spec);
  return GNUNET_OK;
}


/**
 * Function called with information about a refund.
 * Check if this coin was claimed by the wallet for the
 * transaction, and if so add the refunded amount to the
 * pc's "total_refunded" amount.
 *
 * @param cls closure with a `struct PayContext`
 * @param coin_pub public coin from which the refund comes from
 * @param rtransaction_id identificator of the refund
 * @param reason human-readable explaination of the refund
 * @param refund_amount refund amount which is being taken from coin_pub
 * @param refund_fee cost of this refund operation
 */
static void
check_coin_refunded (void *cls,
                     const struct TALER_CoinSpendPublicKeyP *coin_pub,
                     uint64_t rtransaction_id,
                     const char *reason,
                     const struct TALER_Amount *refund_amount,
                     const struct TALER_Amount *refund_fee)
{
  struct PayContext *pc = cls;

  for (unsigned int i = 0; i<pc->coins_cnt; i++)
  {
    struct DepositConfirmation *dc = &pc->dc[i];

    /* Get matching coin from results*/
    if (0 == GNUNET_memcmp (coin_pub,
                            &dc->coin_pub))
    {
      dc->refunded = GNUNET_YES;
      GNUNET_break (GNUNET_OK ==
                    TALER_amount_add (&pc->total_refunded,
                                      &pc->total_refunded,
                                      refund_amount));
    }
  }
}


/**
 * Begin of the DB transaction.  If required (from
 * soft/serialization errors), the transaction can be
 * restarted here.
 *
 * @param pc payment context to transact
 */
static void
begin_transaction (struct PayContext *pc)
{
  enum GNUNET_DB_QueryStatus qs;

  /* Avoid re-trying transactions on soft errors forever! */
  if (pc->retry_counter++ > MAX_RETRIES)
  {
    GNUNET_break (0);
    resume_pay_with_response (pc,
                              MHD_HTTP_INTERNAL_SERVER_ERROR,
                              TMH_RESPONSE_make_json_pack ("{s:I, s:s}",
                                                           "code",
                                                           (json_int_t)
                                                           TALER_EC_PAY_DB_STORE_TRANSACTION_ERROR,
                                                           "hint",
                                                           "Soft merchant database error: retry counter exceeded"));
    return;
  }

  GNUNET_assert (GNUNET_YES == pc->suspended);

  /* Init. some price accumulators.  */
  GNUNET_break (GNUNET_OK ==
                TALER_amount_get_zero (pc->amount.currency,
                                       &pc->total_paid));
  GNUNET_break (GNUNET_OK ==
                TALER_amount_get_zero (pc->amount.currency,
                                       &pc->total_fees_paid));
  GNUNET_break (GNUNET_OK ==
                TALER_amount_get_zero (pc->amount.currency,
                                       &pc->total_refunded));

  /* First, try to see if we have all we need already done */
  db->preflight (db->cls);
  if (GNUNET_OK !=
      db->start (db->cls,
                 "run pay"))
  {
    GNUNET_break (0);
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR,
                           "Merchant database error (could not start transaction)");
    return;
  }

  /* Check if some of these coins already succeeded for _this_ contract.  */
  qs = db->find_payments (db->cls,
                          &pc->h_contract_terms,
                          &pc->mi->pubkey,
                          &check_coin_paid,
                          pc);
  if (0 > qs)
  {
    db->rollback (db->cls);
    if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    {
      begin_transaction (pc);
      return;
    }
    /* Always report on hard error as well to enable diagnostics */
    GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR,
                           "Merchant database error");
    return;
  }

  /* Check if we refunded some of the coins */
  qs = db->get_refunds_from_contract_terms_hash (db->cls,
                                                 &pc->mi->pubkey,
                                                 &pc->h_contract_terms,
                                                 &check_coin_refunded,
                                                 pc);
  if (0 > qs)
  {
    db->rollback (db->cls);
    if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
    {
      begin_transaction (pc);
      return;
    }
    /* Always report on hard error as well to enable diagnostics */
    GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    resume_pay_with_error (pc,
                           MHD_HTTP_INTERNAL_SERVER_ERROR,
                           TALER_EC_PAY_DB_FETCH_TRANSACTION_ERROR,
                           "Merchant database error");
    return;
  }

  /* All the coins known to the database have
   * been processed, now delve into specific case
   * (pay vs. abort) */

  if (PC_MODE_ABORT_REFUND == pc->mode)
  {
    json_t *terms;

    /* The wallet is going for a refund,
       (on aborted operation)! */

    /* check payment was indeed incomplete */
    qs = db->find_paid_contract_terms_from_hash (db->cls,
                                                 &terms,
                                                 &pc->h_contract_terms,
                                                 &pc->mi->pubkey);
    if (0 > qs)
    {
      db->rollback (db->cls);
      if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      {
        begin_transaction (pc);
        return;
      }
      /* Always report on hard error as well to enable diagnostics */
      GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
      resume_pay_with_error (pc,
                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                             TALER_EC_PAY_DB_STORE_PAY_ERROR,
                             "Merchant database error");
      return;
    }
    if (0 < qs)
    {
      /* Payment had been complete! */
      json_decref (terms);
      db->rollback (db->cls);
      resume_pay_with_error (pc,
                             MHD_HTTP_FORBIDDEN,
                             TALER_EC_PAY_ABORT_REFUND_REFUSED_PAYMENT_COMPLETE,
                             "Payment complete, refusing to abort");
      return;
    }

    /* Store refund in DB */
    qs = db->increase_refund_for_contract_NT (db->cls,
                                              &pc->h_contract_terms,
                                              &pc->mi->pubkey,
                                              &pc->total_paid,
                                              /* justification */
                                              "incomplete payment aborted");
    if (0 > qs)
    {
      db->rollback (db->cls);
      if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      {
        begin_transaction (pc);
        return;
      }
      /* Always report on hard error as well to enable diagnostics */
      GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
      resume_pay_with_error (pc,
                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                             TALER_EC_PAY_DB_STORE_PAY_ERROR,
                             "Merchant database error");
      return;
    }
    qs = db->commit (db->cls);
    if (0 > qs)
    {
      db->rollback (db->cls);
      if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      {
        begin_transaction (pc);
        return;
      }
      resume_pay_with_error (pc,
                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                             TALER_EC_PAY_DB_STORE_PAY_ERROR,
                             "Merchant database error: could not commit");
      return;
    }
    /* At this point, the refund got correctly committed
     * into the database.  */
    {
      json_t *refunds;

      refunds = json_array ();
      if (NULL == refunds)
      {
        GNUNET_break (0);
        resume_pay_with_error (pc,
                               MHD_HTTP_INTERNAL_SERVER_ERROR,
                               TALER_EC_JSON_ALLOCATION_FAILURE,
                               "could not create JSON array");
        return;
      }
      for (unsigned int i = 0; i<pc->coins_cnt; i++)
      {
        struct TALER_RefundRequestPS rr;
        struct TALER_MerchantSignatureP msig;
        uint64_t rtransactionid;

        /* Will only work with coins found in DB.  */
        if (GNUNET_YES != pc->dc[i].found_in_db)
          continue;

        rtransactionid = 0;
        rr.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_REFUND);
        rr.purpose.size = htonl (sizeof (struct TALER_RefundRequestPS));
        rr.h_contract_terms = pc->h_contract_terms;
        rr.coin_pub = pc->dc[i].coin_pub;
        rr.merchant = pc->mi->pubkey;
        rr.rtransaction_id = GNUNET_htonll (rtransactionid);
        TALER_amount_hton (&rr.refund_amount,
                           &pc->dc[i].amount_with_fee);
        TALER_amount_hton (&rr.refund_fee,
                           &pc->dc[i].refund_fee);

        if (GNUNET_OK !=
            GNUNET_CRYPTO_eddsa_sign (&pc->mi->privkey.eddsa_priv,
                                      &rr.purpose,
                                      &msig.eddsa_sig))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                      "Failed to sign successful refund confirmation\n");
          json_decref (refunds);
          resume_pay_with_error (pc,
                                 MHD_HTTP_INTERNAL_SERVER_ERROR,
                                 TALER_EC_PAY_REFUND_SIGNATURE_FAILED,
                                 "Refund approved, but failed to sign confirmation");
          return;
        }

        /* Pack refund for i-th coin.  */
        if (0 !=
            json_array_append_new (refunds,
                                   json_pack ("{s:I, s:o, s:o s:o s:o}",
                                              "rtransaction_id",
                                              (json_int_t) rtransactionid,
                                              "coin_pub",
                                              GNUNET_JSON_from_data_auto (
                                                &rr.coin_pub),
                                              "merchant_sig",
                                              GNUNET_JSON_from_data_auto (
                                                &msig),
                                              "refund_amount",
                                              TALER_JSON_from_amount_nbo (
                                                &rr.refund_amount),
                                              "refund_fee",
                                              TALER_JSON_from_amount_nbo (
                                                &rr.refund_fee))))
        {
          json_decref (refunds);
          GNUNET_break (0);
          resume_pay_with_error (pc,
                                 MHD_HTTP_INTERNAL_SERVER_ERROR,
                                 TALER_EC_JSON_ALLOCATION_FAILURE,
                                 "could not create JSON array");
          return;
        }
      }

      /* Resume and send back the response.  */
      resume_pay_with_response
        (pc,
        MHD_HTTP_OK,
        TMH_RESPONSE_make_json_pack
          ("{s:o, s:o, s:o}",
          /* Refunds pack.  */
          "refund_permissions", refunds,
          "merchant_pub",
          GNUNET_JSON_from_data_auto (&pc->mi->pubkey),
          "h_contract_terms",
          GNUNET_JSON_from_data_auto (&pc->h_contract_terms)));
    }
    return;
  }
  /* Default PC_MODE_PAY mode */

  /* Final termination case: all coins already known, just
     generate ultimate outcome. */
  if (0 == pc->pending)
  {
    enum TALER_ErrorCode ec;

    ec = check_payment_sufficient (pc);
    if (TALER_EC_NONE != ec)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "Payment is not sufficient (ec=%u).\n",
                  (unsigned int) ec);
      db->rollback (db->cls);
      generate_error_response (pc,
                               ec);
      return;
    }
    /* Payment succeeded, save in database */
    qs = db->mark_proposal_paid (db->cls,
                                 &pc->h_contract_terms,
                                 &pc->mi->pubkey);

    if (qs < 0)
    {
      db->rollback (db->cls);
      if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      {
        begin_transaction (pc);
        return;
      }
      resume_pay_with_error
        (pc,
        MHD_HTTP_INTERNAL_SERVER_ERROR,
        TALER_EC_PAY_DB_STORE_PAYMENTS_ERROR,
        "Merchant database error: could not "
        "mark proposal as 'paid'");
      return;
    }

    if ( (NULL != pc->session_id) && (NULL != pc->fulfillment_url) )
    {
      qs = db->insert_session_info (db->cls,
                                    pc->session_id,
                                    pc->fulfillment_url,
                                    pc->order_id,
                                    &pc->mi->pubkey);
    }

    /* Now commit! */

    if (0 <= qs)
      qs = db->commit (db->cls);
    else
      db->rollback (db->cls);
    if (0 > qs)
    {
      if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      {
        begin_transaction (pc);
        return;
      }
      resume_pay_with_error
        (pc,
        MHD_HTTP_INTERNAL_SERVER_ERROR,
        TALER_EC_PAY_DB_STORE_PAYMENTS_ERROR,
        "Merchant database error: could not "
        "mark proposal as 'paid'");
      return;
    }


    resume_pay_with_response (pc,
                              MHD_HTTP_OK,
                              sign_success_response (pc));
    return;
  }


  /* we made no DB changes,
     so we can just rollback */
  db->rollback (db->cls);

  /* Ok, we need to first go to the network.
     Do that interaction in *tiny* transactions. */
  find_next_exchange (pc);
}


/**
 * Process a payment for a proposal.
 *
 * @param connection HTTP connection we are receiving payment on
 * @param root JSON upload with payment data
 * @param pc context we use to handle the payment
 * @return value to return to MHD (#MHD_NO to drop connection,
 *         #MHD_YES to keep handling it)
 */
static int
handler_pay_json (struct MHD_Connection *connection,
                  const json_t *root,
                  struct PayContext *pc)
{
  int ret;

  ret = parse_pay (connection,
                   root,
                   pc);

  if (GNUNET_OK != ret)
    return (GNUNET_NO == ret) ? MHD_YES : MHD_NO;

  MHD_suspend_connection (connection);
  pc->suspended = GNUNET_YES;
  GNUNET_log
    (GNUNET_ERROR_TYPE_DEBUG,
    "Suspending /pay handling while working with the exchange\n");

  pc->timeout_task = GNUNET_SCHEDULER_add_delayed
                       (PAY_TIMEOUT,
                       &handle_pay_timeout,
                       pc);

  begin_transaction (pc);
  return MHD_YES;
}


/**
 * Process a payment for a proposal.  Takes data from the given MHD
 * connection.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] connection_cls the connection's closure
 *       (can be updated)
 * @param upload_data upload data
 * @param[in,out] upload_data_size number of bytes (left) in @a
 *       upload_data
 * @param mi merchant backend instance, never NULL
 * @return MHD result code
 */
int
MH_handler_pay (struct TMH_RequestHandler *rh,
                struct MHD_Connection *connection,
                void **connection_cls,
                const char *upload_data,
                size_t *upload_data_size,
                struct MerchantInstance *mi)
{
  struct PayContext *pc;
  int res;
  json_t *root;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "In handler for /pay.\n");
  if (NULL == *connection_cls)
  {
    pc = GNUNET_new (struct PayContext);
    GNUNET_CONTAINER_DLL_insert (pc_head,
                                 pc_tail,
                                 pc);
    pc->hc.cc = &pay_context_cleanup;
    pc->connection = connection;
    *connection_cls = pc;
    pc->mi = mi;
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "/pay: picked instance %s\n",
                mi->id);

  }
  else
  {
    /* not the first call, recover state */
    pc = *connection_cls;
  }
  if (0 != pc->response_code)
  {
    /* We are *done* processing the request,
       just queue the response (!) */
    if (UINT_MAX == pc->response_code)
    {
      GNUNET_break (0);
      return MHD_NO; /* hard error */
    }
    res = MHD_queue_response (connection,
                              pc->response_code,
                              pc->response);
    MHD_destroy_response (pc->response);
    pc->response = NULL;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Queueing response (%u) for /pay (%s).\n",
                (unsigned int) pc->response_code,
                res ? "OK" : "FAILED");
    return res;
  }

  res = TMH_PARSE_post_json (connection,
                             &pc->json_parse_context,
                             upload_data,
                             upload_data_size,
                             &root);
  if (GNUNET_SYSERR == res)
  {
    GNUNET_break (0);
    return TMH_RESPONSE_reply_invalid_json (connection);
  }
  if ( (GNUNET_NO == res) ||
       (NULL == root) )
    return MHD_YES; /* the POST's body has to be further fetched */

  res = handler_pay_json (connection,
                          root,
                          pc);
  json_decref (root);
  if (GNUNET_SYSERR == res)
    return MHD_NO;
  return MHD_YES;
}

/* end of taler-merchant-httpd_pay.c */