Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020-2022 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_delete_order.c
19 : * @brief Implementation of the DELETE /orders/$ORDER_ID request of the merchant's HTTP API
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 :
33 : /**
34 : * Handle for a DELETE /orders/$ID operation.
35 : */
36 : struct TALER_MERCHANT_OrderDeleteHandle
37 : {
38 : /**
39 : * The url for this request.
40 : */
41 : char *url;
42 :
43 : /**
44 : * Handle for the request.
45 : */
46 : struct GNUNET_CURL_Job *job;
47 :
48 : /**
49 : * Function to call with the result.
50 : */
51 : TALER_MERCHANT_OrderDeleteCallback cb;
52 :
53 : /**
54 : * Closure for @a cb.
55 : */
56 : void *cb_cls;
57 :
58 : /**
59 : * Reference to the execution context.
60 : */
61 : struct GNUNET_CURL_Context *ctx;
62 : };
63 :
64 :
65 : /**
66 : * Function called when we're done processing the
67 : * HTTP DELETE /orders/$ORDER_ID request.
68 : *
69 : * @param cls the `struct TALER_MERCHANT_OrderDeleteHandle`
70 : * @param response_code HTTP response code, 0 on error
71 : * @param response response body, NULL if not in JSON
72 : */
73 : static void
74 6 : handle_delete_order_finished (void *cls,
75 : long response_code,
76 : const void *response)
77 : {
78 6 : struct TALER_MERCHANT_OrderDeleteHandle *odh = cls;
79 6 : struct TALER_MERCHANT_HttpResponse hr = {
80 6 : .http_status = (unsigned int) response_code,
81 : .reply = NULL,
82 : };
83 :
84 6 : odh->job = NULL;
85 6 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 : "Got /orders/$ID response with status code %u\n",
87 : (unsigned int) response_code);
88 6 : switch (response_code)
89 : {
90 2 : case MHD_HTTP_NO_CONTENT:
91 2 : break;
92 0 : case MHD_HTTP_UNAUTHORIZED:
93 0 : hr.ec = TALER_JSON_get_error_code (response);
94 0 : hr.hint = TALER_JSON_get_error_hint (response);
95 : /* Nothing really to verify, merchant says we need to authenticate. */
96 0 : break;
97 2 : case MHD_HTTP_NOT_FOUND:
98 2 : break;
99 2 : case MHD_HTTP_CONFLICT:
100 2 : break;
101 0 : default:
102 : /* unexpected response code */
103 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
104 : "Unexpected response code %u\n",
105 : (unsigned int) response_code);
106 0 : break;
107 : }
108 6 : odh->cb (odh->cb_cls,
109 : &hr);
110 6 : TALER_MERCHANT_order_delete_cancel (odh);
111 6 : }
112 :
113 :
114 : struct TALER_MERCHANT_OrderDeleteHandle *
115 6 : TALER_MERCHANT_order_delete (
116 : struct GNUNET_CURL_Context *ctx,
117 : const char *backend_url,
118 : const char *order_id,
119 : bool force,
120 : TALER_MERCHANT_OrderDeleteCallback cb,
121 : void *cb_cls)
122 : {
123 : struct TALER_MERCHANT_OrderDeleteHandle *odh;
124 :
125 6 : odh = GNUNET_new (struct TALER_MERCHANT_OrderDeleteHandle);
126 6 : odh->ctx = ctx;
127 6 : odh->cb = cb;
128 6 : odh->cb_cls = cb_cls;
129 : {
130 : char *path;
131 :
132 6 : GNUNET_asprintf (&path,
133 : "private/orders/%s%s",
134 : order_id,
135 : force
136 : ? "?force=yes"
137 : : "");
138 :
139 6 : odh->url = TALER_url_join (backend_url,
140 : path,
141 : NULL);
142 6 : GNUNET_free (path);
143 : }
144 6 : if (NULL == odh->url)
145 : {
146 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 : "Could not construct request url.\n");
148 0 : GNUNET_free (odh);
149 0 : return NULL;
150 : }
151 :
152 : {
153 : CURL *eh;
154 :
155 6 : eh = TALER_MERCHANT_curl_easy_get_ (odh->url);
156 6 : GNUNET_assert (CURLE_OK ==
157 : curl_easy_setopt (eh,
158 : CURLOPT_CUSTOMREQUEST,
159 : MHD_HTTP_METHOD_DELETE));
160 6 : odh->job = GNUNET_CURL_job_add (ctx,
161 : eh,
162 : &handle_delete_order_finished,
163 : odh);
164 : }
165 6 : return odh;
166 : }
167 :
168 :
169 : void
170 6 : TALER_MERCHANT_order_delete_cancel (
171 : struct TALER_MERCHANT_OrderDeleteHandle *odh)
172 : {
173 6 : if (NULL != odh->job)
174 0 : GNUNET_CURL_job_cancel (odh->job);
175 6 : GNUNET_free (odh->url);
176 6 : GNUNET_free (odh);
177 6 : }
|