Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020, 2021, 2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as
7 : published by the Free Software Foundation; either version 2.1,
8 : or (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General
16 : Public License along with TALER; see the file COPYING.LGPL.
17 : If not, see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file merchant_api_patch_order_forget.c
21 : * @brief Implementation of the PATCH /orders/$ID/forget request
22 : * of the merchant's HTTP API
23 : * @author Jonathan Buchanan
24 : */
25 : #include "platform.h"
26 : #include <curl/curl.h>
27 : #include <jansson.h>
28 : #include <microhttpd.h> /* just for HTTP status codes */
29 : #include <gnunet/gnunet_util_lib.h>
30 : #include "taler_merchant_service.h"
31 : #include "merchant_api_common.h"
32 : #include "merchant_api_curl_defaults.h"
33 : #include <taler/taler_json_lib.h>
34 : #include <taler/taler_curl_lib.h>
35 :
36 :
37 : /**
38 : * Handle for a PATCH /orders/$ORDER_ID/forget operation.
39 : */
40 : struct TALER_MERCHANT_OrderForgetHandle
41 : {
42 :
43 : /**
44 : * The 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_ForgetCallback cb;
57 :
58 : /**
59 : * Closure for @a cb.
60 : */
61 : void *cb_cls;
62 :
63 : /**
64 : * Reference to the execution context.
65 : */
66 : struct GNUNET_CURL_Context *ctx;
67 :
68 : /**
69 : * Minor context that holds body and headers.
70 : */
71 : struct TALER_CURL_PostContext post_ctx;
72 :
73 : };
74 :
75 :
76 : /**
77 : * Function called when we're done processing the
78 : * HTTP PATCH /orders/$ORDER_ID/forget request.
79 : *
80 : * @param cls the `struct TALER_MERCHANT_OrderForgetHandle`
81 : * @param response_code HTTP response code, 0 on error
82 : * @param response response body, NULL if not in JSON
83 : */
84 : static void
85 12 : handle_forget_finished (void *cls,
86 : long response_code,
87 : const void *response)
88 : {
89 12 : struct TALER_MERCHANT_OrderForgetHandle *ofh = cls;
90 12 : const json_t *json = response;
91 12 : struct TALER_MERCHANT_HttpResponse hr = {
92 12 : .http_status = (unsigned int) response_code,
93 : .reply = json
94 : };
95 :
96 12 : ofh->job = NULL;
97 12 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
98 : "PATCH /orders/$ORDER_ID/forget completed with response code %u\n",
99 : (unsigned int) response_code);
100 12 : switch (response_code)
101 : {
102 0 : case 0:
103 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
104 0 : break;
105 0 : case MHD_HTTP_OK:
106 : /* fields were NOW forgotten */
107 0 : break;
108 6 : case MHD_HTTP_NO_CONTENT:
109 : /* fields were already forgotten before */
110 6 : break;
111 2 : case MHD_HTTP_BAD_REQUEST:
112 2 : hr.ec = TALER_JSON_get_error_code (json);
113 2 : hr.hint = TALER_JSON_get_error_hint (json);
114 2 : break;
115 0 : case MHD_HTTP_UNAUTHORIZED:
116 0 : hr.ec = TALER_JSON_get_error_code (json);
117 0 : hr.hint = TALER_JSON_get_error_hint (json);
118 : /* Nothing really to verify, merchant says we need to authenticate. */
119 0 : break;
120 2 : case MHD_HTTP_NOT_FOUND:
121 2 : hr.ec = TALER_JSON_get_error_code (json);
122 2 : hr.hint = TALER_JSON_get_error_hint (json);
123 2 : break;
124 2 : case MHD_HTTP_CONFLICT:
125 2 : hr.ec = TALER_JSON_get_error_code (json);
126 2 : hr.hint = TALER_JSON_get_error_hint (json);
127 2 : break;
128 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
129 0 : hr.ec = TALER_JSON_get_error_code (json);
130 0 : hr.hint = TALER_JSON_get_error_hint (json);
131 : /* Server had an internal issue; we should retry,
132 : but this API leaves this to the application */
133 0 : break;
134 0 : default:
135 0 : TALER_MERCHANT_parse_error_details_ (json,
136 : response_code,
137 : &hr);
138 : /* unexpected response code */
139 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140 : "Unexpected response code %u/%d\n",
141 : (unsigned int) response_code,
142 : (int) hr.ec);
143 0 : GNUNET_break_op (0);
144 0 : break;
145 : }
146 12 : ofh->cb (ofh->cb_cls,
147 : &hr);
148 12 : TALER_MERCHANT_order_forget_cancel (ofh);
149 12 : }
150 :
151 :
152 : struct TALER_MERCHANT_OrderForgetHandle *
153 12 : TALER_MERCHANT_order_forget (
154 : struct GNUNET_CURL_Context *ctx,
155 : const char *merchant_url,
156 : const char *order_id,
157 : unsigned int fields_length,
158 : const char *fields[static fields_length],
159 : TALER_MERCHANT_ForgetCallback cb,
160 : void *cb_cls)
161 12 : {
162 : struct TALER_MERCHANT_OrderForgetHandle *ofh;
163 : json_t *req_fields;
164 : json_t *req_obj;
165 :
166 12 : req_fields = json_array ();
167 12 : if (NULL == req_fields)
168 : {
169 0 : GNUNET_break (0);
170 0 : return NULL;
171 : }
172 24 : for (unsigned int i = 0; i<fields_length; i++)
173 : {
174 12 : if (0 !=
175 12 : json_array_append_new (req_fields,
176 12 : json_string (fields[i])))
177 : {
178 0 : GNUNET_break (0);
179 0 : json_decref (req_fields);
180 0 : return NULL;
181 : }
182 : }
183 12 : req_obj = GNUNET_JSON_PACK (
184 : GNUNET_JSON_pack_array_steal ("fields",
185 : req_fields));
186 12 : ofh = GNUNET_new (struct TALER_MERCHANT_OrderForgetHandle);
187 12 : ofh->ctx = ctx;
188 12 : ofh->cb = cb;
189 12 : ofh->cb_cls = cb_cls;
190 : {
191 : char *path;
192 :
193 12 : GNUNET_asprintf (&path,
194 : "private/orders/%s/forget",
195 : order_id);
196 12 : ofh->url = TALER_url_join (merchant_url,
197 : path,
198 : NULL);
199 12 : GNUNET_free (path);
200 : }
201 12 : if (NULL == ofh->url)
202 : {
203 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 : "Could not construct request URL.\n");
205 0 : json_decref (req_obj);
206 0 : GNUNET_free (ofh);
207 0 : return NULL;
208 : }
209 : {
210 : CURL *eh;
211 :
212 12 : eh = TALER_MERCHANT_curl_easy_get_ (ofh->url);
213 12 : if (GNUNET_OK !=
214 12 : TALER_curl_easy_post (&ofh->post_ctx,
215 : eh,
216 : req_obj))
217 : {
218 0 : GNUNET_break (0);
219 0 : curl_easy_cleanup (eh);
220 0 : json_decref (req_obj);
221 0 : GNUNET_free (ofh);
222 0 : return NULL;
223 : }
224 12 : json_decref (req_obj);
225 12 : GNUNET_assert (CURLE_OK ==
226 : curl_easy_setopt (eh,
227 : CURLOPT_CUSTOMREQUEST,
228 : MHD_HTTP_METHOD_PATCH));
229 24 : ofh->job = GNUNET_CURL_job_add2 (ctx,
230 : eh,
231 12 : ofh->post_ctx.headers,
232 : &handle_forget_finished,
233 : ofh);
234 : }
235 12 : return ofh;
236 : }
237 :
238 :
239 : void
240 12 : TALER_MERCHANT_order_forget_cancel (
241 : struct TALER_MERCHANT_OrderForgetHandle *ofh)
242 : {
243 12 : if (NULL != ofh->job)
244 : {
245 0 : GNUNET_CURL_job_cancel (ofh->job);
246 0 : ofh->job = NULL;
247 : }
248 12 : TALER_curl_easy_post_finished (&ofh->post_ctx);
249 12 : GNUNET_free (ofh->url);
250 12 : GNUNET_free (ofh);
251 12 : }
252 :
253 :
254 : /* end of merchant_api_patch_order_forget.c */
|