base_terms_parse.c (5018B)
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 GNUNET_JSON_spec_string_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_uint8 ("minimum_age", 89 &ct->minimum_age), 90 NULL), 91 GNUNET_JSON_spec_mark_optional ( 92 GNUNET_JSON_spec_uint64 ("order_default_money_pot", 93 &ct->default_money_pot), 94 NULL), 95 GNUNET_JSON_spec_end () 96 }; 97 enum GNUNET_GenericReturnValue res; 98 const char *ename; 99 unsigned int eline; 100 101 GNUNET_assert (NULL != input); 102 ct->max_pickup_time = GNUNET_TIME_UNIT_FOREVER_TS; 103 ct->version = TALER_MERCHANT_CONTRACT_VERSION_0; 104 res = GNUNET_JSON_parse (input, 105 espec, 106 &ename, 107 &eline); 108 if (GNUNET_OK != res) 109 { 110 GNUNET_break (0); 111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 112 "Failed to parse common terms at field %s\n", 113 ename); 114 goto cleanup; 115 } 116 return ct; 117 118 cleanup: 119 TALER_MERCHANT_base_terms_free (ct); 120 return NULL; 121 } 122 123 124 void 125 TALER_MERCHANT_base_terms_free ( 126 struct TALER_MERCHANT_ContractBaseTerms *ct) 127 { 128 if (NULL == ct) 129 return; 130 GNUNET_free (ct->public_reorder_url); 131 GNUNET_free (ct->summary); 132 GNUNET_free (ct->fulfillment_url); 133 GNUNET_free (ct->fulfillment_message); 134 if (NULL != ct->fulfillment_message_i18n) 135 { 136 json_decref (ct->fulfillment_message_i18n); 137 ct->fulfillment_message_i18n = NULL; 138 } 139 if (NULL != ct->delivery_location) 140 { 141 json_decref (ct->delivery_location); 142 ct->delivery_location = NULL; 143 } 144 if (NULL != ct->extra) 145 { 146 json_decref (ct->extra); 147 ct->extra = NULL; 148 } 149 if (NULL != ct->summary_i18n) 150 { 151 json_decref (ct->summary_i18n); 152 ct->summary_i18n = NULL; 153 } 154 GNUNET_free (ct); 155 }