Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020-2026 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-private-orders-ORDER_ID.c
19 : * @brief Implementation of the DELETE /private/orders/$ORDER_ID request
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/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/delete-private-orders-ORDER_ID.h>
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 :
32 :
33 : /**
34 : * Handle for a DELETE /private/orders/$ORDER_ID operation.
35 : */
36 : struct TALER_MERCHANT_DeletePrivateOrderHandle
37 : {
38 : /**
39 : * Base URL of the merchant backend.
40 : */
41 : char *base_url;
42 :
43 : /**
44 : * The full URL for this request.
45 : */
46 : char *url;
47 :
48 : /**
49 : * Handle for the request.
50 : */
51 : struct GNUNET_CURL_Job *job;
52 :
53 : /**
54 : * Function to call with the result.
55 : */
56 : TALER_MERCHANT_DeletePrivateOrderCallback cb;
57 :
58 : /**
59 : * Closure for @a cb.
60 : */
61 : TALER_MERCHANT_DELETE_PRIVATE_ORDER_RESULT_CLOSURE *cb_cls;
62 :
63 : /**
64 : * Reference to the execution context.
65 : */
66 : struct GNUNET_CURL_Context *ctx;
67 :
68 : /**
69 : * Identifier of the order to delete.
70 : */
71 : char *order_id;
72 :
73 : /**
74 : * Whether to force deletion even if order has payments.
75 : */
76 : bool force;
77 : };
78 :
79 :
80 : /**
81 : * Function called when we're done processing the
82 : * HTTP DELETE /private/orders/$ORDER_ID request.
83 : *
84 : * @param cls the `struct TALER_MERCHANT_DeletePrivateOrderHandle`
85 : * @param response_code HTTP response code, 0 on error
86 : * @param response response body, NULL if not in JSON
87 : */
88 : static void
89 0 : handle_delete_order_finished (void *cls,
90 : long response_code,
91 : const void *response)
92 : {
93 0 : struct TALER_MERCHANT_DeletePrivateOrderHandle *doh = cls;
94 0 : const json_t *json = response;
95 0 : struct TALER_MERCHANT_DeletePrivateOrderResponse dor = {
96 0 : .hr.http_status = (unsigned int) response_code,
97 : .hr.reply = json
98 : };
99 :
100 0 : doh->job = NULL;
101 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
102 : "Got /private/orders/$ORDER_ID DELETE response with status code %u\n",
103 : (unsigned int) response_code);
104 0 : switch (response_code)
105 : {
106 0 : case MHD_HTTP_NO_CONTENT:
107 0 : break;
108 0 : case MHD_HTTP_UNAUTHORIZED:
109 0 : dor.hr.ec = TALER_JSON_get_error_code (json);
110 0 : dor.hr.hint = TALER_JSON_get_error_hint (json);
111 0 : break;
112 0 : case MHD_HTTP_NOT_FOUND:
113 0 : dor.hr.ec = TALER_JSON_get_error_code (json);
114 0 : dor.hr.hint = TALER_JSON_get_error_hint (json);
115 0 : break;
116 0 : case MHD_HTTP_CONFLICT:
117 0 : dor.hr.ec = TALER_JSON_get_error_code (json);
118 0 : dor.hr.hint = TALER_JSON_get_error_hint (json);
119 0 : break;
120 0 : default:
121 0 : dor.hr.ec = TALER_JSON_get_error_code (json);
122 0 : dor.hr.hint = TALER_JSON_get_error_hint (json);
123 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124 : "Unexpected response code %u/%d for DELETE /private/orders/$ORDER_ID\n",
125 : (unsigned int) response_code,
126 : (int) dor.hr.ec);
127 0 : break;
128 : }
129 0 : doh->cb (doh->cb_cls,
130 : &dor);
131 0 : TALER_MERCHANT_delete_private_order_cancel (doh);
132 0 : }
133 :
134 :
135 : struct TALER_MERCHANT_DeletePrivateOrderHandle *
136 0 : TALER_MERCHANT_delete_private_order_create (
137 : struct GNUNET_CURL_Context *ctx,
138 : const char *url,
139 : const char *order_id)
140 : {
141 : struct TALER_MERCHANT_DeletePrivateOrderHandle *doh;
142 :
143 0 : doh = GNUNET_new (struct TALER_MERCHANT_DeletePrivateOrderHandle);
144 0 : doh->ctx = ctx;
145 0 : doh->base_url = GNUNET_strdup (url);
146 0 : doh->order_id = GNUNET_strdup (order_id);
147 0 : doh->force = false;
148 0 : return doh;
149 : }
150 :
151 :
152 : enum GNUNET_GenericReturnValue
153 0 : TALER_MERCHANT_delete_private_order_set_options_ (
154 : struct TALER_MERCHANT_DeletePrivateOrderHandle *handle,
155 : unsigned int num_options,
156 : const struct TALER_MERCHANT_DeletePrivateOrderOptionValue options[])
157 : {
158 0 : for (unsigned int i = 0; i < num_options; i++)
159 : {
160 0 : switch (options[i].option)
161 : {
162 0 : case TALER_MERCHANT_DELETE_PRIVATE_ORDER_OPTION_END:
163 0 : return GNUNET_OK;
164 0 : case TALER_MERCHANT_DELETE_PRIVATE_ORDER_OPTION_FORCE:
165 0 : handle->force = true;
166 0 : continue;
167 : }
168 0 : GNUNET_break (0);
169 0 : return GNUNET_SYSERR;
170 : }
171 0 : return GNUNET_OK;
172 : }
173 :
174 :
175 : enum TALER_ErrorCode
176 0 : TALER_MERCHANT_delete_private_order_start (
177 : struct TALER_MERCHANT_DeletePrivateOrderHandle *doh,
178 : TALER_MERCHANT_DeletePrivateOrderCallback cb,
179 : TALER_MERCHANT_DELETE_PRIVATE_ORDER_RESULT_CLOSURE *cb_cls)
180 : {
181 : CURL *eh;
182 :
183 0 : doh->cb = cb;
184 0 : doh->cb_cls = cb_cls;
185 : {
186 : char *path;
187 :
188 0 : GNUNET_asprintf (&path,
189 : "private/orders/%s",
190 : doh->order_id);
191 0 : doh->url = TALER_url_join (doh->base_url,
192 : path,
193 : "force",
194 0 : (doh->force) ? "yes" : NULL,
195 : NULL);
196 0 : GNUNET_free (path);
197 : }
198 0 : if (NULL == doh->url)
199 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
200 0 : eh = TALER_MERCHANT_curl_easy_get_ (doh->url);
201 0 : if (NULL == eh)
202 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
203 0 : GNUNET_assert (CURLE_OK ==
204 : curl_easy_setopt (eh,
205 : CURLOPT_CUSTOMREQUEST,
206 : MHD_HTTP_METHOD_DELETE));
207 0 : doh->job = GNUNET_CURL_job_add (doh->ctx,
208 : eh,
209 : &handle_delete_order_finished,
210 : doh);
211 0 : if (NULL == doh->job)
212 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
213 0 : return TALER_EC_NONE;
214 : }
215 :
216 :
217 : void
218 0 : TALER_MERCHANT_delete_private_order_cancel (
219 : struct TALER_MERCHANT_DeletePrivateOrderHandle *doh)
220 : {
221 0 : if (NULL != doh->job)
222 : {
223 0 : GNUNET_CURL_job_cancel (doh->job);
224 0 : doh->job = NULL;
225 : }
226 0 : GNUNET_free (doh->url);
227 0 : GNUNET_free (doh->order_id);
228 0 : GNUNET_free (doh->base_url);
229 0 : GNUNET_free (doh);
230 0 : }
231 :
232 :
233 : /* end of merchant_api_delete-private-orders-ORDER_ID.c */
|