Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2024, 2025 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 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 src/util/base_terms_parse.c
18 : * @brief shared logic for order and contract terms parsing
19 : * @author Iván Ávalos
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <gnunet/gnunet_common.h>
24 : #include <gnunet/gnunet_json_lib.h>
25 : #include <jansson.h>
26 : #include <stdbool.h>
27 : #include <stdint.h>
28 : #include <taler/taler_json_lib.h>
29 : #include <taler/taler_util.h>
30 : #include "taler/taler_merchant_util.h"
31 :
32 :
33 : struct TALER_MERCHANT_ContractBaseTerms *
34 338 : TALER_MERCHANT_base_terms_parse (
35 : json_t *input)
36 : {
37 : struct TALER_MERCHANT_ContractBaseTerms *ct
38 338 : = GNUNET_new (struct TALER_MERCHANT_ContractBaseTerms);
39 : struct GNUNET_JSON_Specification espec[] = {
40 338 : GNUNET_JSON_spec_mark_optional (
41 : TALER_MERCHANT_spec_contract_version ("version",
42 : &ct->version),
43 : NULL),
44 338 : GNUNET_JSON_spec_string_copy ("summary",
45 : &ct->summary),
46 : /* FIXME: do i18n_str validation in the future */
47 338 : GNUNET_JSON_spec_mark_optional (
48 : GNUNET_JSON_spec_object_copy ("summary_i18n",
49 : &ct->summary_i18n),
50 : NULL),
51 338 : GNUNET_JSON_spec_mark_optional (
52 : TALER_JSON_spec_web_url_copy ("public_reorder_url",
53 : &ct->public_reorder_url),
54 : NULL),
55 338 : GNUNET_JSON_spec_mark_optional (
56 : GNUNET_JSON_spec_string_copy ("fulfillment_url",
57 : &ct->fulfillment_url),
58 : NULL),
59 338 : GNUNET_JSON_spec_mark_optional (
60 : GNUNET_JSON_spec_string_copy ("fulfillment_message",
61 : &ct->fulfillment_message),
62 : NULL),
63 338 : GNUNET_JSON_spec_mark_optional (
64 : GNUNET_JSON_spec_object_copy ("fulfillment_message_i18n",
65 : &ct->fulfillment_message_i18n),
66 : NULL),
67 338 : GNUNET_JSON_spec_mark_optional (
68 : GNUNET_JSON_spec_object_copy ("delivery_location",
69 : &ct->delivery_location),
70 : NULL),
71 338 : GNUNET_JSON_spec_mark_optional (
72 : GNUNET_JSON_spec_timestamp ("delivery_date",
73 : &ct->delivery_date),
74 : NULL),
75 338 : GNUNET_JSON_spec_mark_optional (
76 : GNUNET_JSON_spec_timestamp ("max_pickup_time",
77 : &ct->max_pickup_time),
78 : NULL),
79 338 : GNUNET_JSON_spec_mark_optional (
80 : GNUNET_JSON_spec_relative_time ("auto_refund",
81 : &ct->auto_refund),
82 : NULL),
83 338 : GNUNET_JSON_spec_mark_optional (
84 : GNUNET_JSON_spec_object_copy ("extra",
85 : &ct->extra),
86 : NULL),
87 338 : GNUNET_JSON_spec_mark_optional (
88 : GNUNET_JSON_spec_array_copy ("amount_external",
89 : &ct->amount_external),
90 : NULL),
91 338 : GNUNET_JSON_spec_mark_optional (
92 : GNUNET_JSON_spec_uint8 ("minimum_age",
93 : &ct->minimum_age),
94 : NULL),
95 338 : GNUNET_JSON_spec_mark_optional (
96 : GNUNET_JSON_spec_uint64 ("order_default_money_pot",
97 : &ct->default_money_pot),
98 : NULL),
99 338 : GNUNET_JSON_spec_end ()
100 : };
101 : enum GNUNET_GenericReturnValue res;
102 : const char *ename;
103 : unsigned int eline;
104 :
105 338 : GNUNET_assert (NULL != input);
106 338 : ct->max_pickup_time = GNUNET_TIME_UNIT_FOREVER_TS;
107 338 : ct->version = TALER_MERCHANT_CONTRACT_VERSION_0;
108 338 : res = GNUNET_JSON_parse (input,
109 : espec,
110 : &ename,
111 : &eline);
112 338 : if (GNUNET_OK != res)
113 : {
114 0 : GNUNET_break (0);
115 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 : "Failed to parse common terms at field %s\n",
117 : ename);
118 0 : goto cleanup;
119 : }
120 338 : return ct;
121 :
122 0 : cleanup:
123 0 : TALER_MERCHANT_base_terms_free (ct);
124 0 : return NULL;
125 : }
126 :
127 :
128 : void
129 337 : TALER_MERCHANT_base_terms_free (
130 : struct TALER_MERCHANT_ContractBaseTerms *ct)
131 : {
132 337 : if (NULL == ct)
133 0 : return;
134 337 : GNUNET_free (ct->public_reorder_url);
135 337 : GNUNET_free (ct->summary);
136 337 : GNUNET_free (ct->fulfillment_url);
137 337 : GNUNET_free (ct->fulfillment_message);
138 337 : if (NULL != ct->fulfillment_message_i18n)
139 : {
140 3 : json_decref (ct->fulfillment_message_i18n);
141 3 : ct->fulfillment_message_i18n = NULL;
142 : }
143 337 : if (NULL != ct->delivery_location)
144 : {
145 3 : json_decref (ct->delivery_location);
146 3 : ct->delivery_location = NULL;
147 : }
148 337 : if (NULL != ct->extra)
149 : {
150 3 : json_decref (ct->extra);
151 3 : ct->extra = NULL;
152 : }
153 337 : if (NULL != ct->amount_external)
154 : {
155 23 : json_decref (ct->amount_external);
156 23 : ct->amount_external = NULL;
157 : }
158 337 : if (NULL != ct->summary_i18n)
159 : {
160 3 : json_decref (ct->summary_i18n);
161 3 : ct->summary_i18n = NULL;
162 : }
163 337 : GNUNET_free (ct);
164 : }
|