Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014, 2015, 2016, 2017, 2019, 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_post_order_refund.c
19 : * @brief Implementation of the POST /orders/ID/refund request
20 : * @author Christian Grothoff
21 : * @author Marcello Stanisci
22 : */
23 : #include "platform.h"
24 : #include <curl/curl.h>
25 : #include <jansson.h>
26 : #include <microhttpd.h> /* just for HTTP status codes */
27 : #include <gnunet/gnunet_util_lib.h>
28 : #include <gnunet/gnunet_curl_lib.h>
29 : #include "taler_merchant_service.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 POST /orders/ID/refund operation.
37 : */
38 : struct TALER_MERCHANT_OrderRefundHandle
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_RefundCallback 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 : * Callback to process POST /orders/ID/refund response
74 : *
75 : * @param cls the `struct TALER_MERCHANT_OrderRefundHandle`
76 : * @param response_code HTTP response code, 0 on error
77 : * @param response response body, NULL if not JSON
78 : */
79 : static void
80 0 : handle_refund_finished (void *cls,
81 : long response_code,
82 : const void *response)
83 : {
84 0 : struct TALER_MERCHANT_OrderRefundHandle *orh = cls;
85 0 : const json_t *json = response;
86 0 : struct TALER_MERCHANT_HttpResponse hr = {
87 0 : .http_status = (unsigned int) response_code,
88 : .reply = json
89 : };
90 :
91 0 : orh->job = NULL;
92 0 : switch (response_code)
93 : {
94 0 : case 0:
95 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
96 0 : orh->cb (orh->cb_cls,
97 : &hr,
98 : NULL,
99 : NULL);
100 0 : break;
101 0 : case MHD_HTTP_OK:
102 : {
103 : const char *taler_refund_uri;
104 : struct GNUNET_HashCode h_contract;
105 : struct GNUNET_JSON_Specification spec[] = {
106 0 : GNUNET_JSON_spec_string ("taler_refund_uri",
107 : &taler_refund_uri),
108 0 : GNUNET_JSON_spec_fixed_auto ("h_contract",
109 : &h_contract),
110 0 : GNUNET_JSON_spec_end ()
111 : };
112 :
113 0 : if (GNUNET_OK !=
114 0 : GNUNET_JSON_parse (json,
115 : spec,
116 : NULL, NULL))
117 : {
118 0 : GNUNET_break_op (0);
119 0 : hr.http_status = 0;
120 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
121 0 : orh->cb (orh->cb_cls,
122 : &hr,
123 : NULL,
124 : NULL);
125 0 : break;
126 : }
127 0 : orh->cb (orh->cb_cls,
128 : &hr,
129 : taler_refund_uri,
130 : &h_contract);
131 0 : GNUNET_JSON_parse_free (spec);
132 : }
133 0 : break;
134 0 : case MHD_HTTP_CONFLICT:
135 : case MHD_HTTP_NOT_FOUND:
136 0 : hr.ec = TALER_JSON_get_error_code (json);
137 0 : hr.hint = TALER_JSON_get_error_hint (json);
138 0 : orh->cb (orh->cb_cls,
139 : &hr,
140 : NULL,
141 : NULL);
142 0 : break;
143 0 : default:
144 0 : GNUNET_break_op (0); /* unexpected status code */
145 0 : TALER_MERCHANT_parse_error_details_ (json,
146 : response_code,
147 : &hr);
148 0 : orh->cb (orh->cb_cls,
149 : &hr,
150 : NULL,
151 : NULL);
152 0 : break;
153 : }
154 0 : TALER_MERCHANT_post_order_refund_cancel (orh);
155 0 : }
156 :
157 :
158 : void
159 0 : TALER_MERCHANT_post_order_refund_cancel (
160 : struct TALER_MERCHANT_OrderRefundHandle *orh)
161 : {
162 0 : if (NULL != orh->job)
163 : {
164 0 : GNUNET_CURL_job_cancel (orh->job);
165 0 : orh->job = NULL;
166 : }
167 0 : TALER_curl_easy_post_finished (&orh->post_ctx);
168 0 : GNUNET_free (orh->url);
169 0 : GNUNET_free (orh);
170 0 : }
171 :
172 :
173 : struct TALER_MERCHANT_OrderRefundHandle *
174 0 : TALER_MERCHANT_post_order_refund (struct GNUNET_CURL_Context *ctx,
175 : const char *backend_url,
176 : const char *order_id,
177 : const struct TALER_Amount *refund,
178 : const char *reason,
179 : TALER_MERCHANT_RefundCallback cb,
180 : void *cb_cls)
181 : {
182 : struct TALER_MERCHANT_OrderRefundHandle *orh;
183 : json_t *req;
184 : CURL *eh;
185 :
186 0 : orh = GNUNET_new (struct TALER_MERCHANT_OrderRefundHandle);
187 0 : orh->ctx = ctx;
188 0 : orh->cb = cb;
189 0 : orh->cb_cls = cb_cls;
190 : {
191 : char *path;
192 :
193 0 : GNUNET_asprintf (&path,
194 : "private/orders/%s/refund",
195 : order_id);
196 0 : orh->url = TALER_url_join (backend_url,
197 : path,
198 : NULL);
199 0 : GNUNET_free (path);
200 : }
201 0 : if (NULL == orh->url)
202 : {
203 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 : "Could not construct request URL.\n");
205 0 : GNUNET_free (orh);
206 0 : return NULL;
207 : }
208 0 : req = json_pack ("{s:o, s:s}",
209 : "refund", TALER_JSON_from_amount (refund),
210 : "reason", reason);
211 0 : GNUNET_assert (NULL != req);
212 0 : eh = curl_easy_init ();
213 0 : GNUNET_assert (NULL != eh);
214 0 : if (GNUNET_OK !=
215 0 : TALER_curl_easy_post (&orh->post_ctx,
216 : eh,
217 : req))
218 : {
219 0 : GNUNET_break (0);
220 0 : json_decref (req);
221 0 : GNUNET_free (orh->url);
222 0 : GNUNET_free (orh);
223 0 : return NULL;
224 : }
225 0 : json_decref (req);
226 0 : GNUNET_assert (CURLE_OK ==
227 : curl_easy_setopt (eh,
228 : CURLOPT_URL,
229 : orh->url));
230 0 : orh->job = GNUNET_CURL_job_add2 (ctx,
231 : eh,
232 0 : orh->post_ctx.headers,
233 : &handle_refund_finished,
234 : orh);
235 0 : if (NULL == orh->job)
236 : {
237 0 : GNUNET_free (orh->url);
238 0 : GNUNET_free (orh);
239 0 : return NULL;
240 : }
241 0 : return orh;
242 : }
243 :
244 :
245 : /* end of merchant_api_post_order_refund.c */
|