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
|
/*
This file is part of TALER
(C) 2014-2023 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_post-orders-ID-paid.c
* @brief handling of POST /orders/$ID/paid requests
* @author Christian Grothoff
*/
#include "platform.h"
#include <taler/taler_dbevents.h>
#include <taler/taler_signatures.h>
#include <taler/taler_json_lib.h>
#include <taler/taler_exchange_service.h>
#include "taler-merchant-httpd_helper.h"
#include "taler-merchant-httpd_post-orders-ID-paid.h"
/**
* Function called with information about a refund.
* Sets the boolean in @a cls to true when called.
*
* @param cls closure, a `bool *`
* @param coin_pub public coin from which the refund comes from
* @param refund_amount refund amount which is being taken from @a coin_pub
*/
static void
refund_cb (
void *cls,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
const struct TALER_Amount *refund_amount)
{
bool *refunded = cls;
*refunded = true;
}
/**
* Use database to notify other clients about the
* session being captured.
*
* @param hc http context
* @param session_id the captured session
* @param fulfillment_url the URL that is now paid for by @a session_id
*/
static void
trigger_session_notification (struct TMH_HandlerContext *hc,
const char *session_id,
const char *fulfillment_url)
{
struct TMH_SessionEventP session_eh = {
.header.size = htons (sizeof (session_eh)),
.header.type = htons (TALER_DBEVENT_MERCHANT_SESSION_CAPTURED),
.merchant_pub = hc->instance->merchant_pub
};
GNUNET_CRYPTO_hash (session_id,
strlen (session_id),
&session_eh.h_session_id);
GNUNET_CRYPTO_hash (fulfillment_url,
strlen (fulfillment_url),
&session_eh.h_fulfillment_url);
TMH_db->event_notify (TMH_db->cls,
&session_eh.header,
NULL,
0);
}
MHD_RESULT
TMH_post_orders_ID_paid (const struct TMH_RequestHandler *rh,
struct MHD_Connection *connection,
struct TMH_HandlerContext *hc)
{
const char *order_id = hc->infix;
struct TALER_MerchantSignatureP merchant_sig;
const char *session_id;
struct TALER_PrivateContractHashP hct;
json_t *contract_terms;
const char *fulfillment_url;
enum GNUNET_DB_QueryStatus qs;
{
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_fixed_auto ("sig",
&merchant_sig),
GNUNET_JSON_spec_fixed_auto ("h_contract",
&hct),
GNUNET_JSON_spec_string ("session_id",
&session_id),
GNUNET_JSON_spec_end ()
};
enum GNUNET_GenericReturnValue res;
res = TALER_MHD_parse_json_data (connection,
hc->request_body,
spec);
if (GNUNET_YES != res)
{
GNUNET_break_op (0);
return (GNUNET_NO == res)
? MHD_YES
: MHD_NO;
}
}
if (GNUNET_OK !=
TALER_merchant_pay_verify (&hct,
&hc->instance->merchant_pub,
&merchant_sig))
{
GNUNET_break_op (0);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_FORBIDDEN,
TALER_EC_MERCHANT_POST_ORDERS_ID_PAID_COIN_SIGNATURE_INVALID,
NULL);
}
TMH_db->preflight (TMH_db->cls);
{
uint64_t order_serial;
qs = TMH_db->lookup_contract_terms (TMH_db->cls,
hc->instance->settings.id,
order_id,
&contract_terms,
&order_serial,
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 (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_contract_terms");
}
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);
}
{
struct TALER_PrivateContractHashP h_contract_terms;
if (GNUNET_OK !=
TALER_JSON_contract_hash (contract_terms,
&h_contract_terms))
{
GNUNET_break (0);
json_decref (contract_terms);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_FAILED_COMPUTE_JSON_HASH,
NULL);
}
if (0 != GNUNET_memcmp (&hct,
&h_contract_terms))
{
json_decref (contract_terms);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_MERCHANT_POST_ORDERS_ID_PAID_CONTRACT_HASH_MISMATCH,
NULL);
}
}
fulfillment_url
= json_string_value (json_object_get (contract_terms,
"fulfillment_url"));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Marking contract %s with %s/%s as paid\n",
order_id,
session_id,
fulfillment_url);
qs = TMH_db->mark_contract_paid (TMH_db->cls,
hc->instance->settings.id,
&hct,
session_id);
/* If the order was paid already, we get qs == 0. */
if (0 > qs)
{
GNUNET_break (0);
json_decref (contract_terms);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_STORE_FAILED,
"mark_contract_paid");
}
/* Wake everybody up who waits for this fulfillment_url and session_id */
if ( (NULL != fulfillment_url) &&
(NULL != session_id) )
trigger_session_notification (hc,
session_id,
fulfillment_url);
/*Trigger webhook */
/*Commented out until its purpose is defined
{
enum GNUNET_DB_QueryStatus qs;
json_t *jhook;
jhook = GNUNET_JSON_PACK (
GNUNET_JSON_pack_object_incref ("contract_terms",
contract_terms),
GNUNET_JSON_pack_string ("order_id",
order_id)
);
GNUNET_assert (NULL != jhook);
qs = TMH_trigger_webhook (hc->instance->settings.id,
"paid",
jhook);
json_decref (jhook);
if (qs < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to init the webhook for contract %s with %s/%s as paid\n",
order_id,
session_id,
fulfillment_url);
}
}*/
/* fulfillment_url is part of the contract_terms */
{
bool refunded = false;
qs = TMH_db->lookup_refunds (TMH_db->cls,
hc->instance->settings.id,
&hct,
&refund_cb,
&refunded);
json_decref (contract_terms);
return TALER_MHD_REPLY_JSON_PACK (
connection,
MHD_HTTP_OK,
GNUNET_JSON_pack_bool ("refunded",
refunded));
}
}
/* end of taler-merchant-httpd_post-orders-ID-paid.c */
|