Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2014-2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Affero General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file taler-merchant-httpd_private-post-transfers.c
18 : * @brief implement API for registering wire transfers
19 : * @author Marcello Stanisci
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <jansson.h>
24 : #include <taler/taler_signatures.h>
25 : #include <taler/taler_json_lib.h>
26 : #include <taler/taler_dbevents.h>
27 : #include "taler-merchant-httpd_exchanges.h"
28 : #include "taler-merchant-httpd_helper.h"
29 : #include "taler-merchant-httpd_private-post-transfers.h"
30 :
31 :
32 : /**
33 : * How often do we retry the simple INSERT database transaction?
34 : */
35 : #define MAX_RETRIES 3
36 :
37 :
38 : MHD_RESULT
39 13 : TMH_private_post_transfers (const struct TMH_RequestHandler *rh,
40 : struct MHD_Connection *connection,
41 : struct TMH_HandlerContext *hc)
42 : {
43 : struct TALER_FullPayto payto_uri;
44 : const char *exchange_url;
45 : struct TALER_WireTransferIdentifierRawP wtid;
46 : struct TALER_Amount amount;
47 : struct GNUNET_JSON_Specification spec[] = {
48 13 : TALER_JSON_spec_amount_any ("credit_amount",
49 : &amount),
50 13 : GNUNET_JSON_spec_fixed_auto ("wtid",
51 : &wtid),
52 13 : TALER_JSON_spec_full_payto_uri ("payto_uri",
53 : &payto_uri),
54 13 : TALER_JSON_spec_web_url ("exchange_url",
55 : &exchange_url),
56 13 : GNUNET_JSON_spec_end ()
57 : };
58 : enum GNUNET_GenericReturnValue res;
59 : enum GNUNET_DB_QueryStatus qs;
60 :
61 13 : res = TALER_MHD_parse_json_data (connection,
62 13 : hc->request_body,
63 : spec);
64 13 : if (GNUNET_OK != res)
65 : return (GNUNET_NO == res)
66 : ? MHD_YES
67 0 : : MHD_NO;
68 13 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 : "New inbound wire transfer over %s to %s from %s\n",
70 : TALER_amount2s (&amount),
71 : payto_uri.full_payto,
72 : exchange_url);
73 :
74 : /* Check if transfer data is in database, if not, add it. */
75 49 : for (unsigned int retry = 0; retry<MAX_RETRIES; retry++)
76 : {
77 37 : TMH_db->preflight (TMH_db->cls);
78 37 : if (GNUNET_OK !=
79 37 : TMH_db->start (TMH_db->cls,
80 : "post-transfers"))
81 : {
82 0 : GNUNET_break (0);
83 0 : return TALER_MHD_reply_with_error (connection,
84 : MHD_HTTP_INTERNAL_SERVER_ERROR,
85 : TALER_EC_GENERIC_DB_START_FAILED,
86 : "transfer");
87 : }
88 37 : qs = TMH_db->insert_transfer (TMH_db->cls,
89 37 : hc->instance->settings.id,
90 : exchange_url,
91 : &wtid,
92 : &amount,
93 : payto_uri,
94 : true /* confirmed! */);
95 37 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
96 26 : qs = TMH_db->set_transfer_status_to_confirmed (TMH_db->cls,
97 26 : hc->instance->settings.id,
98 : exchange_url,
99 : &wtid,
100 : &amount);
101 37 : switch (qs)
102 : {
103 0 : case GNUNET_DB_STATUS_HARD_ERROR:
104 0 : GNUNET_break (0);
105 0 : TMH_db->rollback (TMH_db->cls);
106 0 : return TALER_MHD_reply_with_error (connection,
107 : MHD_HTTP_INTERNAL_SERVER_ERROR,
108 : TALER_EC_GENERIC_DB_STORE_FAILED,
109 : "insert_transfer");
110 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
111 0 : TMH_db->rollback (TMH_db->cls);
112 0 : continue;
113 1 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
114 : /* Could not set to confirmed, must differ by amount! */
115 1 : TMH_db->rollback (TMH_db->cls);
116 1 : return TALER_MHD_reply_with_error (
117 : connection,
118 : MHD_HTTP_CONFLICT,
119 : TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_SUBMISSION,
120 : NULL);
121 36 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
122 36 : break;
123 : }
124 : {
125 36 : struct GNUNET_DB_EventHeaderP es = {
126 36 : .size = htons (sizeof (es)),
127 36 : .type = htons (TALER_DBEVENT_MERCHANT_WIRE_TRANSFER_CONFIRMED)
128 : };
129 :
130 36 : TMH_db->event_notify (TMH_db->cls,
131 : &es,
132 : NULL,
133 : 0);
134 : }
135 36 : qs = TMH_db->commit (TMH_db->cls);
136 36 : switch (qs)
137 : {
138 0 : case GNUNET_DB_STATUS_HARD_ERROR:
139 0 : GNUNET_break (0);
140 0 : TMH_db->rollback (TMH_db->cls);
141 0 : return TALER_MHD_reply_with_error (connection,
142 : MHD_HTTP_INTERNAL_SERVER_ERROR,
143 : TALER_EC_GENERIC_DB_COMMIT_FAILED,
144 : NULL);
145 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
146 0 : TMH_db->rollback (TMH_db->cls);
147 0 : continue;
148 36 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
149 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
150 36 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
151 : "post-transfer committed successfully\n");
152 36 : break;
153 : }
154 : }
155 12 : return TALER_MHD_reply_static (connection,
156 : MHD_HTTP_NO_CONTENT,
157 : NULL,
158 : NULL,
159 : 0);
160 : }
161 :
162 :
163 : /* end of taler-merchant-httpd_private-post-transfers.c */
|