util.c (2246B)
1 /* 2 This file is part of TALER 3 (C) 2026 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/util.c 18 * @brief small helpers 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <string.h> 23 #include "taler/taler_merchant_util.h" 24 #include <taler/taler_json_lib.h> 25 #include <gnunet/gnunet_json_lib.h> 26 27 28 bool 29 TALER_MERCHANT_taxes_array_valid (const json_t *taxes) 30 { 31 json_t *tax; 32 size_t idx; 33 34 if (! json_is_array (taxes)) 35 return false; 36 json_array_foreach (taxes, idx, tax) 37 { 38 struct TALER_Amount amount; 39 const char *name; 40 struct GNUNET_JSON_Specification spec[] = { 41 GNUNET_JSON_spec_string ("name", 42 &name), 43 TALER_JSON_spec_amount_any ("tax", 44 &amount), 45 GNUNET_JSON_spec_end () 46 }; 47 enum GNUNET_GenericReturnValue res; 48 const char *ename; 49 unsigned int eline; 50 51 res = GNUNET_JSON_parse (tax, 52 spec, 53 &ename, 54 &eline); 55 if (GNUNET_OK != res) 56 { 57 GNUNET_break_op (0); 58 return false; 59 } 60 } 61 return true; 62 } 63 64 65 enum TALER_MERCHANT_ContractTokenKind 66 TALER_MERCHANT_contract_token_kind_from_string (const char *str) 67 { 68 if (NULL == str) 69 { 70 GNUNET_break (0); 71 return TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID; 72 } 73 if (0 == strcmp ("subscription", 74 str)) 75 return TALER_MERCHANT_CONTRACT_TOKEN_KIND_SUBSCRIPTION; 76 if (0 == strcmp ("discount", 77 str)) 78 return TALER_MERCHANT_CONTRACT_TOKEN_KIND_DISCOUNT; 79 return TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID; 80 }