Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2017-2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU 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_get-tips-ID.c
18 : * @brief implementation of a GET /tips/ID handler
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <microhttpd.h>
23 : #include <jansson.h>
24 : #include <taler/taler_json_lib.h>
25 : #include <taler/taler_signatures.h>
26 : #include "taler-merchant-httpd.h"
27 : #include "taler-merchant-httpd_mhd.h"
28 : #include "taler-merchant-httpd_exchanges.h"
29 :
30 :
31 : MHD_RESULT
32 0 : TMH_private_get_tips_ID (const struct TMH_RequestHandler *rh,
33 : struct MHD_Connection *connection,
34 : struct TMH_HandlerContext *hc)
35 : {
36 : struct TALER_TipIdentifierP tip_id;
37 : struct TALER_Amount total_authorized;
38 : struct TALER_Amount total_picked_up;
39 : char *reason;
40 : struct GNUNET_TIME_Timestamp expiration;
41 : struct TALER_ReservePublicKeyP reserve_pub;
42 0 : unsigned int pickups_length = 0;
43 0 : struct TALER_MERCHANTDB_PickupDetails *pickups = NULL;
44 : enum GNUNET_DB_QueryStatus qs;
45 : bool fpu;
46 0 : json_t *pickups_json = NULL;
47 :
48 0 : GNUNET_assert (NULL != hc->infix);
49 0 : if (GNUNET_OK !=
50 0 : GNUNET_CRYPTO_hash_from_string (hc->infix,
51 : &tip_id.hash))
52 : {
53 : /* tip_id has wrong encoding */
54 0 : GNUNET_break_op (0);
55 0 : return TALER_MHD_reply_with_error (connection,
56 : MHD_HTTP_BAD_REQUEST,
57 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
58 0 : hc->infix);
59 : }
60 : {
61 : const char *pstr;
62 :
63 0 : pstr = MHD_lookup_connection_value (connection,
64 : MHD_GET_ARGUMENT_KIND,
65 : "pickups");
66 0 : fpu = (NULL != pstr)
67 0 : ? 0 == strcasecmp (pstr, "yes")
68 0 : : false;
69 : }
70 0 : TMH_db->preflight (TMH_db->cls);
71 0 : qs = TMH_db->lookup_tip_details (TMH_db->cls,
72 0 : hc->instance->settings.id,
73 : &tip_id,
74 : fpu,
75 : &total_authorized,
76 : &total_picked_up,
77 : &reason,
78 : &expiration,
79 : &reserve_pub,
80 : &pickups_length,
81 : &pickups);
82 0 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
83 : {
84 : unsigned int response_code;
85 : enum TALER_ErrorCode ec;
86 :
87 0 : switch (qs)
88 : {
89 0 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
90 0 : ec = TALER_EC_MERCHANT_GENERIC_TIP_ID_UNKNOWN;
91 0 : response_code = MHD_HTTP_NOT_FOUND;
92 0 : break;
93 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
94 0 : ec = TALER_EC_GENERIC_DB_SOFT_FAILURE;
95 0 : response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
96 0 : break;
97 0 : case GNUNET_DB_STATUS_HARD_ERROR:
98 0 : ec = TALER_EC_GENERIC_DB_COMMIT_FAILED;
99 0 : response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
100 0 : break;
101 0 : default:
102 0 : GNUNET_break (0);
103 0 : ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
104 0 : response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
105 0 : break;
106 : }
107 0 : return TALER_MHD_reply_with_error (connection,
108 : response_code,
109 : ec,
110 : NULL);
111 : }
112 0 : if (fpu)
113 : {
114 0 : pickups_json = json_array ();
115 0 : GNUNET_assert (NULL != pickups_json);
116 0 : for (unsigned int i = 0; i<pickups_length; i++)
117 : {
118 0 : GNUNET_assert (0 ==
119 : json_array_append_new (
120 : pickups_json,
121 : GNUNET_JSON_PACK (
122 : GNUNET_JSON_pack_data_auto ("pickup_id",
123 : &pickups[i].pickup_id),
124 : GNUNET_JSON_pack_uint64 ("num_planchets",
125 : pickups[i].num_planchets),
126 : TALER_JSON_pack_amount ("requested_amount",
127 : &pickups[i].requested_amount))));
128 : }
129 : }
130 0 : GNUNET_array_grow (pickups,
131 : pickups_length,
132 : 0);
133 : {
134 : MHD_RESULT ret;
135 :
136 0 : ret = TALER_MHD_REPLY_JSON_PACK (
137 : connection,
138 : MHD_HTTP_OK,
139 : TALER_JSON_pack_amount ("total_authorized",
140 : &total_authorized),
141 : TALER_JSON_pack_amount ("total_picked_up",
142 : &total_picked_up),
143 : GNUNET_JSON_pack_string ("reason",
144 : reason),
145 : GNUNET_JSON_pack_timestamp ("expiration",
146 : expiration),
147 : GNUNET_JSON_pack_data_auto ("reserve_pub",
148 : &reserve_pub),
149 : GNUNET_JSON_pack_allow_null (
150 : GNUNET_JSON_pack_array_steal ("pickups",
151 : pickups_json)));
152 0 : GNUNET_free (reason);
153 0 : return ret;
154 : }
155 : }
|