Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2014-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 Affero General Public License as published by the Free Software
7 : Foundation; either version 3, 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 General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c
18 : * @brief implement API for authorizing tips to be paid to visitors
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <jansson.h>
23 : #include <taler/taler_util.h>
24 : #include <taler/taler_json_lib.h>
25 : #include "taler-merchant-httpd.h"
26 : #include "taler-merchant-httpd_mhd.h"
27 : #include "taler-merchant-httpd_get-tips-ID.h"
28 : #include "taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h"
29 :
30 :
31 : /**
32 : * Handle a "tip-authorize" request.
33 : *
34 : * @param rh context of the handler
35 : * @param connection the MHD connection to handle
36 : * @param[in,out] hc context with further information about the request
37 : * @param reserve_pub reserve to use, or NULL for "any"
38 : * @return MHD result code
39 : */
40 : static MHD_RESULT
41 0 : authorize_tip (const struct TMH_RequestHandler *rh,
42 : struct MHD_Connection *connection,
43 : struct TMH_HandlerContext *hc,
44 : const struct TALER_ReservePublicKeyP *reserve_pub)
45 : {
46 : enum TALER_ErrorCode ec;
47 : struct GNUNET_TIME_Timestamp expiration;
48 : struct TALER_TipIdentifierP tip_id;
49 : const char *justification;
50 : const char *next_url;
51 : struct TALER_Amount amount;
52 : {
53 : struct GNUNET_JSON_Specification spec[] = {
54 0 : TALER_JSON_spec_amount ("amount",
55 : TMH_currency,
56 : &amount),
57 0 : GNUNET_JSON_spec_string ("justification",
58 : &justification),
59 0 : GNUNET_JSON_spec_string ("next_url",
60 : &next_url),
61 0 : GNUNET_JSON_spec_end ()
62 : };
63 : enum GNUNET_GenericReturnValue res;
64 :
65 0 : res = TALER_MHD_parse_json_data (connection,
66 0 : hc->request_body,
67 : spec);
68 0 : if (GNUNET_YES != res)
69 : {
70 0 : GNUNET_break_op (0);
71 : return (GNUNET_NO == res)
72 : ? MHD_YES
73 0 : : MHD_NO;
74 : }
75 : }
76 0 : TMH_db->preflight (TMH_db->cls);
77 0 : ec = TMH_db->authorize_tip (TMH_db->cls,
78 0 : hc->instance->settings.id,
79 : reserve_pub,
80 : &amount,
81 : justification,
82 : next_url,
83 : &tip_id,
84 : &expiration);
85 : /* handle errors */
86 0 : if (TALER_EC_NONE != ec)
87 : {
88 : unsigned int http_status;
89 :
90 0 : switch (ec)
91 : {
92 0 : case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_INSUFFICIENT_FUNDS:
93 0 : http_status = MHD_HTTP_PRECONDITION_FAILED;
94 0 : break;
95 0 : case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_RESERVE_EXPIRED:
96 0 : http_status = MHD_HTTP_GONE;
97 0 : break;
98 0 : case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_RESERVE_UNKNOWN:
99 0 : http_status = MHD_HTTP_SERVICE_UNAVAILABLE;
100 0 : break;
101 0 : case TALER_EC_MERCHANT_PRIVATE_POST_TIP_AUTHORIZE_RESERVE_NOT_FOUND:
102 0 : http_status = MHD_HTTP_NOT_FOUND;
103 0 : break;
104 0 : default:
105 0 : http_status = MHD_HTTP_INTERNAL_SERVER_ERROR;
106 0 : break;
107 : }
108 :
109 0 : return TALER_MHD_reply_with_error (connection,
110 : http_status,
111 : ec,
112 : NULL);
113 : }
114 :
115 : /* generate success response */
116 : {
117 : char *taler_tip_uri;
118 : char *tip_status_url;
119 : struct GNUNET_CRYPTO_HashAsciiEncoded hash_enc;
120 : MHD_RESULT res;
121 :
122 0 : GNUNET_CRYPTO_hash_to_enc (&tip_id.hash,
123 : &hash_enc);
124 0 : taler_tip_uri = TMH_make_taler_tip_uri (connection,
125 : &tip_id,
126 0 : hc->instance->settings.id);
127 0 : tip_status_url = TMH_make_tip_status_url (connection,
128 : &tip_id,
129 0 : hc->instance->settings.id);
130 0 : res = TALER_MHD_REPLY_JSON_PACK (
131 : connection,
132 : MHD_HTTP_OK,
133 : GNUNET_JSON_pack_string ("tip_id",
134 : (const char *) hash_enc.encoding),
135 : GNUNET_JSON_pack_string ("taler_tip_uri",
136 : taler_tip_uri),
137 : GNUNET_JSON_pack_string ("tip_status_url",
138 : tip_status_url),
139 : GNUNET_JSON_pack_timestamp ("tip_expiration",
140 : expiration));
141 0 : GNUNET_free (taler_tip_uri);
142 0 : GNUNET_free (tip_status_url);
143 0 : return res;
144 : }
145 : }
146 :
147 :
148 : MHD_RESULT
149 0 : TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh,
150 : struct MHD_Connection *connection,
151 : struct TMH_HandlerContext *hc)
152 : {
153 : struct TALER_ReservePublicKeyP reserve_pub;
154 :
155 0 : if (GNUNET_OK !=
156 0 : GNUNET_STRINGS_string_to_data (hc->infix,
157 0 : strlen (hc->infix),
158 : &reserve_pub,
159 : sizeof (reserve_pub)))
160 : {
161 0 : GNUNET_break_op (0);
162 0 : return TALER_MHD_reply_with_error (connection,
163 : MHD_HTTP_BAD_REQUEST,
164 : TALER_EC_MERCHANT_GENERIC_RESERVE_PUB_MALFORMED,
165 0 : hc->infix);
166 : }
167 0 : return authorize_tip (rh,
168 : connection,
169 : hc,
170 : &reserve_pub);
171 : }
172 :
173 :
174 : MHD_RESULT
175 0 : TMH_private_post_tips (const struct TMH_RequestHandler *rh,
176 : struct MHD_Connection *connection,
177 : struct TMH_HandlerContext *hc)
178 : {
179 0 : return authorize_tip (rh,
180 : connection,
181 : hc,
182 : NULL);
183 : }
184 :
185 :
186 : /* end of taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c */
|