Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, 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 Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file merchant_api_wallet_post_order_refund.c
19 : * @brief Implementation of the (public) POST /orders/ID/refund request
20 : * @author Jonathan Buchanan
21 : */
22 : #include "platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_merchant_service.h"
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 : #include <taler/taler_curl_lib.h>
33 :
34 :
35 : /**
36 : * Handle for a (public) POST /orders/ID/refund operation.
37 : */
38 : struct TALER_MERCHANT_WalletOrderRefundHandle
39 : {
40 : /**
41 : * Complete URL where the backend offers /refund
42 : */
43 : char *url;
44 :
45 : /**
46 : * Minor context that holds body and headers.
47 : */
48 : struct TALER_CURL_PostContext post_ctx;
49 :
50 : /**
51 : * The CURL context to connect to the backend
52 : */
53 : struct GNUNET_CURL_Context *ctx;
54 :
55 : /**
56 : * The callback to pass the backend response to
57 : */
58 : TALER_MERCHANT_WalletRefundCallback cb;
59 :
60 : /**
61 : * Clasure to pass to the callback
62 : */
63 : void *cb_cls;
64 :
65 : /**
66 : * Handle for the request
67 : */
68 : struct GNUNET_CURL_Job *job;
69 : };
70 :
71 :
72 : /**
73 : * Convenience function to call the callback in @a owgh with an error code of
74 : * @a ec and the exchange body being set to @a reply.
75 : *
76 : * @param orh handle providing callback
77 : * @param ec error code to return to application
78 : * @param reply JSON reply we got from the exchange, can be NULL
79 : */
80 : static void
81 0 : cb_failure (struct TALER_MERCHANT_WalletOrderRefundHandle *orh,
82 : enum TALER_ErrorCode ec,
83 : const json_t *reply)
84 : {
85 0 : struct TALER_MERCHANT_HttpResponse hr = {
86 : .ec = ec,
87 : .reply = reply
88 : };
89 :
90 0 : orh->cb (orh->cb_cls,
91 : &hr,
92 : NULL,
93 : NULL,
94 : NULL,
95 : 0);
96 0 : }
97 :
98 :
99 : /**
100 : * Callback to process (public) POST /orders/ID/refund response
101 : *
102 : * @param cls the `struct TALER_MERCHANT_OrderRefundHandle`
103 : * @param response_code HTTP response code, 0 on error
104 : * @param response response body, NULL if not JSON
105 : */
106 : static void
107 0 : handle_refund_finished (void *cls,
108 : long response_code,
109 : const void *response)
110 : {
111 0 : struct TALER_MERCHANT_WalletOrderRefundHandle *orh = cls;
112 0 : const json_t *json = response;
113 0 : struct TALER_MERCHANT_HttpResponse hr = {
114 0 : .http_status = (unsigned int) response_code,
115 : .reply = json
116 : };
117 :
118 0 : orh->job = NULL;
119 :
120 0 : switch (response_code)
121 : {
122 0 : case 0:
123 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
124 0 : orh->cb (orh->cb_cls,
125 : &hr,
126 : NULL,
127 : NULL,
128 : NULL,
129 : 0);
130 0 : break;
131 0 : case MHD_HTTP_OK:
132 : {
133 : struct TALER_Amount refund_amount;
134 : json_t *refunds;
135 : struct TALER_MerchantPublicKeyP merchant_pub;
136 : unsigned int refund_len;
137 : struct GNUNET_JSON_Specification spec[] = {
138 0 : TALER_JSON_spec_amount_any ("refund_amount",
139 : &refund_amount),
140 0 : GNUNET_JSON_spec_json ("refunds",
141 : &refunds),
142 0 : GNUNET_JSON_spec_fixed_auto ("merchant_pub",
143 : &merchant_pub),
144 0 : GNUNET_JSON_spec_end ()
145 : };
146 :
147 0 : if (GNUNET_OK !=
148 0 : GNUNET_JSON_parse (json,
149 : spec,
150 : NULL, NULL))
151 : {
152 0 : GNUNET_break_op (0);
153 0 : cb_failure (orh,
154 : TALER_EC_GENERIC_REPLY_MALFORMED,
155 : json);
156 0 : TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
157 0 : return;
158 : }
159 :
160 0 : if (! json_is_array (refunds))
161 : {
162 0 : GNUNET_break_op (0);
163 0 : cb_failure (orh,
164 : TALER_EC_GENERIC_REPLY_MALFORMED,
165 : json);
166 0 : GNUNET_JSON_parse_free (spec);
167 0 : TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
168 0 : return;
169 : }
170 :
171 0 : refund_len = json_array_size (refunds);
172 0 : {
173 0 : struct TALER_MERCHANT_RefundDetail rds[refund_len];
174 :
175 0 : memset (rds,
176 : 0,
177 : sizeof (rds));
178 0 : for (unsigned int i = 0; i<refund_len; i++)
179 : {
180 0 : struct TALER_MERCHANT_RefundDetail *rd = &rds[i];
181 0 : const json_t *jrefund = json_array_get (refunds,
182 : i);
183 : const char *refund_status_type;
184 : uint32_t exchange_status;
185 : int ret;
186 : struct GNUNET_JSON_Specification espec[] = {
187 0 : GNUNET_JSON_spec_uint32 ("exchange_status",
188 : &exchange_status),
189 0 : GNUNET_JSON_spec_end ()
190 : };
191 :
192 0 : if (GNUNET_OK !=
193 0 : GNUNET_JSON_parse (jrefund,
194 : espec,
195 : NULL, NULL))
196 : {
197 0 : GNUNET_break_op (0);
198 0 : cb_failure (orh,
199 : TALER_EC_GENERIC_REPLY_MALFORMED,
200 : json);
201 0 : TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
202 0 : return;
203 : }
204 :
205 0 : if (MHD_HTTP_OK == exchange_status)
206 : {
207 : struct GNUNET_JSON_Specification rspec[] = {
208 0 : GNUNET_JSON_spec_string ("type",
209 : &refund_status_type),
210 0 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
211 : &rd->exchange_sig),
212 0 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
213 : &rd->exchange_pub),
214 0 : GNUNET_JSON_spec_uint64 ("rtransaction_id",
215 : &rd->rtransaction_id),
216 0 : GNUNET_JSON_spec_fixed_auto ("coin_pub",
217 : &rd->coin_pub),
218 0 : TALER_JSON_spec_amount_any ("refund_amount",
219 : &rd->refund_amount),
220 0 : GNUNET_JSON_spec_end ()
221 : };
222 :
223 0 : ret = GNUNET_JSON_parse (jrefund,
224 : rspec,
225 : NULL, NULL);
226 0 : if (GNUNET_OK == ret)
227 : {
228 : /* check that type field is correct */
229 0 : if (0 != strcmp ("success", refund_status_type))
230 : {
231 0 : GNUNET_break_op (0);
232 0 : ret = GNUNET_SYSERR;
233 : }
234 : }
235 : }
236 : else
237 : {
238 : struct GNUNET_JSON_Specification rspec[] = {
239 0 : GNUNET_JSON_spec_string ("type",
240 : &refund_status_type),
241 0 : GNUNET_JSON_spec_fixed_auto ("coin_pub",
242 : &rd->coin_pub),
243 0 : GNUNET_JSON_spec_uint64 ("rtransaction_id",
244 : &rd->rtransaction_id),
245 0 : TALER_JSON_spec_amount_any ("refund_amount",
246 : &rd->refund_amount),
247 0 : GNUNET_JSON_spec_end ()
248 : };
249 :
250 0 : ret = GNUNET_JSON_parse (jrefund,
251 : rspec,
252 : NULL, NULL);
253 0 : if (GNUNET_OK == ret)
254 : {
255 : /* parse optional arguments */
256 : json_t *jec;
257 :
258 0 : jec = json_object_get (jrefund,
259 : "exchange_code");
260 0 : if (NULL != jec)
261 : {
262 0 : if (! json_is_integer (jec))
263 : {
264 0 : GNUNET_break_op (0);
265 0 : ret = GNUNET_SYSERR;
266 : }
267 : else
268 : {
269 0 : rd->hr.ec = (enum TALER_ErrorCode) json_integer_value (jec);
270 : }
271 : }
272 0 : rd->hr.reply = json_object_get (jrefund,
273 : "exchange_reply");
274 : /* check that type field is correct */
275 0 : if (0 != strcmp ("failure", refund_status_type))
276 : {
277 0 : GNUNET_break_op (0);
278 0 : ret = GNUNET_SYSERR;
279 : }
280 : }
281 : }
282 0 : if (GNUNET_OK != ret)
283 : {
284 0 : GNUNET_break_op (0);
285 0 : cb_failure (orh,
286 : TALER_EC_GENERIC_REPLY_MALFORMED,
287 : json);
288 0 : TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
289 0 : return;
290 : }
291 0 : rd->hr.http_status = exchange_status;
292 : }
293 :
294 : {
295 0 : struct TALER_MERCHANT_HttpResponse hr = {
296 : .reply = json,
297 : .http_status = MHD_HTTP_OK
298 : };
299 :
300 0 : orh->cb (orh->cb_cls,
301 : &hr,
302 : &refund_amount,
303 : &merchant_pub,
304 : rds,
305 : refund_len);
306 : }
307 : }
308 0 : GNUNET_JSON_parse_free (spec);
309 : }
310 0 : break;
311 0 : case MHD_HTTP_NO_CONTENT:
312 0 : orh->cb (orh->cb_cls,
313 : &hr,
314 : NULL,
315 : NULL,
316 : NULL,
317 : 0);
318 0 : break;
319 0 : case MHD_HTTP_CONFLICT:
320 : case MHD_HTTP_NOT_FOUND:
321 0 : hr.ec = TALER_JSON_get_error_code (json);
322 0 : hr.hint = TALER_JSON_get_error_hint (json);
323 0 : orh->cb (orh->cb_cls,
324 : &hr,
325 : NULL,
326 : NULL,
327 : NULL,
328 : 0);
329 0 : break;
330 0 : default:
331 0 : GNUNET_break_op (0); /* unexpected status code */
332 0 : TALER_MERCHANT_parse_error_details_ (json,
333 : response_code,
334 : &hr);
335 0 : orh->cb (orh->cb_cls,
336 : &hr,
337 : NULL,
338 : NULL,
339 : NULL,
340 : 0);
341 0 : break;
342 : }
343 0 : TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
344 : }
345 :
346 :
347 : struct TALER_MERCHANT_WalletOrderRefundHandle *
348 0 : TALER_MERCHANT_wallet_post_order_refund (
349 : struct GNUNET_CURL_Context *ctx,
350 : const char *backend_url,
351 : const char *order_id,
352 : const struct TALER_PrivateContractHashP *h_contract_terms,
353 : TALER_MERCHANT_WalletRefundCallback cb,
354 : void *cb_cls)
355 : {
356 : struct TALER_MERCHANT_WalletOrderRefundHandle *orh;
357 : json_t *req;
358 : CURL *eh;
359 :
360 0 : orh = GNUNET_new (struct TALER_MERCHANT_WalletOrderRefundHandle);
361 0 : orh->ctx = ctx;
362 0 : orh->cb = cb;
363 0 : orh->cb_cls = cb_cls;
364 : {
365 : char *path;
366 :
367 0 : GNUNET_asprintf (&path,
368 : "orders/%s/refund",
369 : order_id);
370 0 : orh->url = TALER_url_join (backend_url,
371 : path,
372 : NULL);
373 0 : GNUNET_free (path);
374 : }
375 0 : if (NULL == orh->url)
376 : {
377 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 : "Could not construct request URL.\n");
379 0 : GNUNET_free (orh);
380 0 : return NULL;
381 : }
382 0 : req = GNUNET_JSON_PACK (
383 : GNUNET_JSON_pack_data_auto ("h_contract",
384 : h_contract_terms));
385 0 : eh = TALER_MERCHANT_curl_easy_get_ (orh->url);
386 0 : if (GNUNET_OK !=
387 0 : TALER_curl_easy_post (&orh->post_ctx,
388 : eh,
389 : req))
390 : {
391 0 : GNUNET_break (0);
392 0 : json_decref (req);
393 0 : curl_easy_cleanup (eh);
394 0 : GNUNET_free (orh->url);
395 0 : GNUNET_free (orh);
396 0 : return NULL;
397 : }
398 0 : json_decref (req);
399 0 : orh->job = GNUNET_CURL_job_add2 (ctx,
400 : eh,
401 0 : orh->post_ctx.headers,
402 : &handle_refund_finished,
403 : orh);
404 0 : if (NULL == orh->job)
405 : {
406 0 : GNUNET_free (orh->url);
407 0 : GNUNET_free (orh);
408 0 : return NULL;
409 : }
410 0 : return orh;
411 : }
412 :
413 :
414 : void
415 0 : TALER_MERCHANT_wallet_post_order_refund_cancel (
416 : struct TALER_MERCHANT_WalletOrderRefundHandle *orh)
417 : {
418 0 : if (NULL != orh->job)
419 : {
420 0 : GNUNET_CURL_job_cancel (orh->job);
421 0 : orh->job = NULL;
422 : }
423 0 : TALER_curl_easy_post_finished (&orh->post_ctx);
424 0 : GNUNET_free (orh->url);
425 0 : GNUNET_free (orh);
426 0 : }
427 :
428 :
429 : /* end of merchant_api_wallet_post_order_refund.c */
|