order_choice_serialize.c (5644B)
1 /* 2 This file is part of GNU Taler 3 Copyright (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 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/order_choice_serialize.c 18 * @brief shared logic for order choice serialization 19 * @author Iván Ávalos 20 * @author Christian Grothoff 21 */ 22 #include "platform.h" 23 #include <gnunet/gnunet_json_lib.h> 24 #include <gnunet/gnunet_common.h> 25 #include <taler/taler_json_lib.h> 26 #include <jansson.h> 27 #include "taler/taler_util.h" 28 #include "taler/taler_merchant_util.h" 29 30 /** 31 * Get JSON representation of order choice input. 32 * 33 * @param[in] input order terms choice input 34 * @return JSON representation of @a input; NULL on error 35 */ 36 static json_t * 37 json_from_order_input ( 38 const struct TALER_MERCHANT_OrderInput *input) 39 { 40 switch (input->type) 41 { 42 case TALER_MERCHANT_CONTRACT_INPUT_TYPE_INVALID: 43 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 44 "invalid order input type"); 45 GNUNET_assert (0); 46 return NULL; 47 case TALER_MERCHANT_CONTRACT_INPUT_TYPE_TOKEN: 48 return GNUNET_JSON_PACK ( 49 GNUNET_JSON_pack_string ("type", 50 "token"), 51 GNUNET_JSON_pack_string ("token_family_slug", 52 input->details.token.token_family_slug), 53 GNUNET_JSON_pack_uint64 ("count", 54 input->details.token.count)); 55 } 56 57 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 58 "unsupported order input type %d", 59 input->type); 60 GNUNET_assert (0); 61 return NULL; 62 } 63 64 65 /** 66 * Get JSON representation of order choice output. 67 * 68 * @param[in] output order terms choice output 69 * @return JSON representation of @a output; NULL on error 70 */ 71 static json_t * 72 json_from_order_output ( 73 const struct TALER_MERCHANT_OrderOutput *output) 74 { 75 switch (output->type) 76 { 77 case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID: 78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 79 "invalid order output type"); 80 GNUNET_assert (0); 81 return NULL; 82 case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN: 83 return GNUNET_JSON_PACK ( 84 GNUNET_JSON_pack_string ("type", 85 "token"), 86 GNUNET_JSON_pack_string ("token_family_slug", 87 output->details.token.token_family_slug), 88 GNUNET_JSON_pack_uint64 ("count", 89 output->details.token.count), 90 GNUNET_TIME_absolute_is_zero (output->details.token.valid_at.abs_time) 91 ? GNUNET_JSON_pack_allow_null ( 92 GNUNET_JSON_pack_string ("dummy", 93 NULL)) 94 : GNUNET_JSON_pack_timestamp ("valid_at", 95 output->details.token.valid_at)); 96 case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT: 97 return GNUNET_JSON_PACK ( 98 GNUNET_JSON_pack_string ("type", 99 "tax-receipt"), 100 GNUNET_JSON_pack_allow_null ( 101 TALER_JSON_pack_amount ("amount", 102 output->details.donation_receipt.no_amount 103 ? NULL 104 : &output->details.donation_receipt.amount))); 105 } 106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 107 "Unsupported order output type %d", 108 output->type); 109 GNUNET_assert (0); 110 return NULL; 111 } 112 113 114 json_t * 115 TALER_MERCHANT_json_from_order_choice ( 116 const struct TALER_MERCHANT_OrderChoice *choice) 117 { 118 json_t *inputs; 119 json_t *outputs; 120 121 inputs = json_array (); 122 GNUNET_assert (NULL != inputs); 123 for (unsigned int i = 0; i < choice->inputs_len; i++) 124 GNUNET_assert (0 == 125 json_array_append_new (inputs, 126 json_from_order_input ( 127 &choice->inputs[i]))); 128 outputs = json_array (); 129 GNUNET_assert (NULL != outputs); 130 for (unsigned int i = 0; i < choice->outputs_len; i++) 131 GNUNET_assert (0 == 132 json_array_append_new (outputs, 133 json_from_order_output ( 134 &choice->outputs[i]))); 135 136 return GNUNET_JSON_PACK ( 137 TALER_JSON_pack_amount ("amount", 138 &choice->amount), 139 GNUNET_JSON_pack_allow_null ( 140 TALER_JSON_pack_amount ("tip", 141 choice->no_tip 142 ? NULL 143 : &choice->tip)), 144 GNUNET_JSON_pack_allow_null ( 145 GNUNET_JSON_pack_string ("description", 146 choice->description)), 147 GNUNET_JSON_pack_allow_null ( 148 GNUNET_JSON_pack_object_incref ("description_i18n", 149 choice->description_i18n)), 150 GNUNET_JSON_pack_allow_null ( 151 TALER_JSON_pack_amount ("max_fee", 152 choice->no_max_fee 153 ? NULL 154 : &choice->max_fee)), 155 GNUNET_JSON_pack_array_steal ("inputs", 156 inputs), 157 GNUNET_JSON_pack_array_steal ("outputs", 158 outputs)); 159 }