base_terms_parse.c (5294B)
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 TALER_MERCHANT_base_terms_parse ( 35 json_t *input) 36 { 37 struct TALER_MERCHANT_ContractBaseTerms *ct 38 = GNUNET_new (struct TALER_MERCHANT_ContractBaseTerms); 39 struct GNUNET_JSON_Specification espec[] = { 40 GNUNET_JSON_spec_mark_optional ( 41 TALER_MERCHANT_spec_contract_version ("version", 42 &ct->version), 43 NULL), 44 GNUNET_JSON_spec_string_copy ("summary", 45 &ct->summary), 46 /* FIXME: do i18n_str validation in the future */ 47 GNUNET_JSON_spec_mark_optional ( 48 GNUNET_JSON_spec_object_copy ("summary_i18n", 49 &ct->summary_i18n), 50 NULL), 51 GNUNET_JSON_spec_mark_optional ( 52 TALER_JSON_spec_web_url_copy ("public_reorder_url", 53 &ct->public_reorder_url), 54 NULL), 55 GNUNET_JSON_spec_mark_optional ( 56 GNUNET_JSON_spec_string_copy ("fulfillment_url", 57 &ct->fulfillment_url), 58 NULL), 59 GNUNET_JSON_spec_mark_optional ( 60 GNUNET_JSON_spec_string_copy ("fulfillment_message", 61 &ct->fulfillment_message), 62 NULL), 63 GNUNET_JSON_spec_mark_optional ( 64 GNUNET_JSON_spec_object_copy ("fulfillment_message_i18n", 65 &ct->fulfillment_message_i18n), 66 NULL), 67 GNUNET_JSON_spec_mark_optional ( 68 GNUNET_JSON_spec_object_copy ("delivery_location", 69 &ct->delivery_location), 70 NULL), 71 GNUNET_JSON_spec_mark_optional ( 72 GNUNET_JSON_spec_timestamp ("delivery_date", 73 &ct->delivery_date), 74 NULL), 75 GNUNET_JSON_spec_mark_optional ( 76 GNUNET_JSON_spec_timestamp ("max_pickup_time", 77 &ct->max_pickup_time), 78 NULL), 79 GNUNET_JSON_spec_mark_optional ( 80 GNUNET_JSON_spec_relative_time ("auto_refund", 81 &ct->auto_refund), 82 NULL), 83 GNUNET_JSON_spec_mark_optional ( 84 GNUNET_JSON_spec_object_copy ("extra", 85 &ct->extra), 86 NULL), 87 GNUNET_JSON_spec_mark_optional ( 88 GNUNET_JSON_spec_array_copy ("amount_external", 89 &ct->amount_external), 90 NULL), 91 GNUNET_JSON_spec_mark_optional ( 92 GNUNET_JSON_spec_uint8 ("minimum_age", 93 &ct->minimum_age), 94 NULL), 95 GNUNET_JSON_spec_mark_optional ( 96 GNUNET_JSON_spec_uint64 ("order_default_money_pot", 97 &ct->default_money_pot), 98 NULL), 99 GNUNET_JSON_spec_end () 100 }; 101 enum GNUNET_GenericReturnValue res; 102 const char *ename; 103 unsigned int eline; 104 105 GNUNET_assert (NULL != input); 106 ct->max_pickup_time = GNUNET_TIME_UNIT_FOREVER_TS; 107 ct->version = TALER_MERCHANT_CONTRACT_VERSION_0; 108 res = GNUNET_JSON_parse (input, 109 espec, 110 &ename, 111 &eline); 112 if (GNUNET_OK != res) 113 { 114 GNUNET_break (0); 115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 116 "Failed to parse common terms at field %s\n", 117 ename); 118 goto cleanup; 119 } 120 return ct; 121 122 cleanup: 123 TALER_MERCHANT_base_terms_free (ct); 124 return NULL; 125 } 126 127 128 void 129 TALER_MERCHANT_base_terms_free ( 130 struct TALER_MERCHANT_ContractBaseTerms *ct) 131 { 132 if (NULL == ct) 133 return; 134 GNUNET_free (ct->public_reorder_url); 135 GNUNET_free (ct->summary); 136 GNUNET_free (ct->fulfillment_url); 137 GNUNET_free (ct->fulfillment_message); 138 if (NULL != ct->fulfillment_message_i18n) 139 { 140 json_decref (ct->fulfillment_message_i18n); 141 ct->fulfillment_message_i18n = NULL; 142 } 143 if (NULL != ct->delivery_location) 144 { 145 json_decref (ct->delivery_location); 146 ct->delivery_location = NULL; 147 } 148 if (NULL != ct->extra) 149 { 150 json_decref (ct->extra); 151 ct->extra = NULL; 152 } 153 if (NULL != ct->amount_external) 154 { 155 json_decref (ct->amount_external); 156 ct->amount_external = NULL; 157 } 158 if (NULL != ct->summary_i18n) 159 { 160 json_decref (ct->summary_i18n); 161 ct->summary_i18n = NULL; 162 } 163 GNUNET_free (ct); 164 }