Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2018, 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_get_tip.c
19 : * @brief Implementation of the GET /tips/$TIP_ID request of the merchant's HTTP API
20 : * @author Florian Dold
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 <taler/taler_json_lib.h>
30 : #include <taler/taler_signatures.h>
31 :
32 :
33 : /**
34 : * @brief A handle for tracking /tip-get operations
35 : */
36 : struct TALER_MERCHANT_TipWalletGetHandle
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_TipWalletGetCallback 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 : /**
67 : * Function called when we're done processing the
68 : * HTTP /track/transaction request.
69 : *
70 : * @param cls the `struct TALER_MERCHANT_TipGetHandle`
71 : * @param response_code HTTP response code, 0 on error
72 : * @param response response body, NULL if not in JSON
73 : */
74 : static void
75 0 : handle_wallet_tip_get_finished (void *cls,
76 : long response_code,
77 : const void *response)
78 : {
79 0 : struct TALER_MERCHANT_TipWalletGetHandle *tgh = cls;
80 0 : const json_t *json = response;
81 0 : struct TALER_MERCHANT_HttpResponse hr = {
82 0 : .http_status = (unsigned int) response_code,
83 : .reply = json
84 : };
85 :
86 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87 : "Got /tip/$TIP_ID response with status code %u\n",
88 : (unsigned int) response_code);
89 :
90 0 : tgh->job = NULL;
91 0 : switch (response_code)
92 : {
93 0 : case MHD_HTTP_OK:
94 : {
95 : const char *exchange_url;
96 : struct TALER_Amount amount_remaining;
97 : struct GNUNET_TIME_Absolute expiration;
98 : struct GNUNET_JSON_Specification spec[] = {
99 0 : TALER_JSON_spec_absolute_time ("expiration",
100 : &expiration),
101 0 : GNUNET_JSON_spec_string ("exchange_url",
102 : &exchange_url),
103 0 : TALER_JSON_spec_amount ("tip_amount",
104 : &amount_remaining),
105 0 : GNUNET_JSON_spec_end ()
106 : };
107 :
108 0 : if (GNUNET_OK !=
109 0 : GNUNET_JSON_parse (json,
110 : spec,
111 : NULL, NULL))
112 : {
113 0 : GNUNET_break_op (0);
114 0 : hr.http_status = 0;
115 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
116 0 : break;
117 : }
118 0 : tgh->cb (tgh->cb_cls,
119 : &hr,
120 : expiration,
121 : exchange_url,
122 : &amount_remaining);
123 0 : TALER_MERCHANT_wallet_tip_get_cancel (tgh);
124 0 : return;
125 : }
126 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
127 : /* Server had an internal issue; we should retry, but this API
128 : leaves this to the application */
129 0 : hr.ec = TALER_JSON_get_error_code (json);
130 0 : hr.hint = TALER_JSON_get_error_hint (json);
131 0 : break;
132 0 : case MHD_HTTP_NOT_FOUND:
133 : /* legal, can happen if instance or tip reserve is unknown */
134 0 : hr.ec = TALER_JSON_get_error_code (json);
135 0 : hr.hint = TALER_JSON_get_error_hint (json);
136 0 : break;
137 0 : default:
138 : /* unexpected response code */
139 0 : GNUNET_break_op (0);
140 0 : TALER_MERCHANT_parse_error_details_ (json,
141 : response_code,
142 : &hr);
143 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 : "Unexpected response code %u/%d\n",
145 : (unsigned int) response_code,
146 : (int) hr.ec);
147 0 : break;
148 : }
149 0 : tgh->cb (tgh->cb_cls,
150 : &hr,
151 : GNUNET_TIME_UNIT_ZERO_ABS,
152 : NULL,
153 : NULL);
154 0 : TALER_MERCHANT_wallet_tip_get_cancel (tgh);
155 : }
156 :
157 :
158 : struct TALER_MERCHANT_TipWalletGetHandle *
159 0 : TALER_MERCHANT_wallet_tip_get (struct GNUNET_CURL_Context *ctx,
160 : const char *backend_url,
161 : const struct GNUNET_HashCode *tip_id,
162 : TALER_MERCHANT_TipWalletGetCallback cb,
163 : void *cb_cls)
164 : {
165 : struct TALER_MERCHANT_TipWalletGetHandle *tgh;
166 : CURL *eh;
167 :
168 0 : tgh = GNUNET_new (struct TALER_MERCHANT_TipWalletGetHandle);
169 0 : tgh->ctx = ctx;
170 0 : tgh->cb = cb;
171 0 : tgh->cb_cls = cb_cls;
172 : {
173 : char res_str[sizeof (*tip_id) * 2];
174 : char arg_str[sizeof (res_str) + 48];
175 : char *end;
176 :
177 0 : end = GNUNET_STRINGS_data_to_string (tip_id,
178 : sizeof (*tip_id),
179 : res_str,
180 : sizeof (res_str));
181 0 : *end = '\0';
182 0 : GNUNET_snprintf (arg_str,
183 : sizeof (arg_str),
184 : "tips/%s",
185 : res_str);
186 0 : tgh->url = TALER_url_join (backend_url,
187 : arg_str,
188 : NULL);
189 : }
190 :
191 0 : if (NULL == tgh->url)
192 : {
193 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 : "Could not construct request URL.\n");
195 0 : GNUNET_free (tgh);
196 0 : return NULL;
197 : }
198 :
199 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 : "Requesting URL '%s'\n",
201 : tgh->url);
202 :
203 0 : eh = curl_easy_init ();
204 0 : GNUNET_assert (CURLE_OK ==
205 : curl_easy_setopt (eh,
206 : CURLOPT_URL,
207 : tgh->url));
208 :
209 0 : tgh->job = GNUNET_CURL_job_add (ctx,
210 : eh,
211 : &handle_wallet_tip_get_finished,
212 : tgh);
213 0 : return tgh;
214 : }
215 :
216 :
217 : void
218 0 : TALER_MERCHANT_wallet_tip_get_cancel (
219 : struct TALER_MERCHANT_TipWalletGetHandle *tgh)
220 : {
221 0 : if (NULL != tgh->job)
222 : {
223 0 : GNUNET_CURL_job_cancel (tgh->job);
224 0 : tgh->job = NULL;
225 : }
226 0 : GNUNET_free (tgh->url);
227 0 : GNUNET_free (tgh);
228 0 : }
229 :
230 :
231 : /* end of merchant_api_wallet_get_tip.c */
|