order_choice_serialize.c (5567B)
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_JSON_pack_conditional ( 91 ! GNUNET_TIME_absolute_is_zero (output->details.token.valid_at.abs_time), 92 GNUNET_JSON_pack_timestamp ("valid_at", 93 output->details.token.valid_at))); 94 case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT: 95 return GNUNET_JSON_PACK ( 96 GNUNET_JSON_pack_string ("type", 97 "tax-receipt"), 98 GNUNET_JSON_pack_allow_null ( 99 TALER_JSON_pack_amount ("amount", 100 output->details.donation_receipt.no_amount 101 ? NULL 102 : &output->details.donation_receipt.amount))); 103 } 104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 105 "Unsupported order output type %d", 106 output->type); 107 GNUNET_assert (0); 108 return NULL; 109 } 110 111 112 json_t * 113 TALER_MERCHANT_json_from_order_choice ( 114 const struct TALER_MERCHANT_OrderChoice *choice) 115 { 116 json_t *inputs; 117 json_t *outputs; 118 119 inputs = json_array (); 120 GNUNET_assert (NULL != inputs); 121 for (unsigned int i = 0; i < choice->inputs_len; i++) 122 GNUNET_assert (0 == 123 json_array_append_new (inputs, 124 json_from_order_input ( 125 &choice->inputs[i]))); 126 outputs = json_array (); 127 GNUNET_assert (NULL != outputs); 128 for (unsigned int i = 0; i < choice->outputs_len; i++) 129 GNUNET_assert (0 == 130 json_array_append_new (outputs, 131 json_from_order_output ( 132 &choice->outputs[i]))); 133 134 return GNUNET_JSON_PACK ( 135 TALER_JSON_pack_amount ("amount", 136 &choice->amount), 137 GNUNET_JSON_pack_allow_null ( 138 TALER_JSON_pack_amount ("tip", 139 choice->no_tip 140 ? NULL 141 : &choice->tip)), 142 GNUNET_JSON_pack_allow_null ( 143 GNUNET_JSON_pack_string ("description", 144 choice->description)), 145 GNUNET_JSON_pack_allow_null ( 146 GNUNET_JSON_pack_object_incref ("description_i18n", 147 choice->description_i18n)), 148 GNUNET_JSON_pack_allow_null ( 149 TALER_JSON_pack_amount ("max_fee", 150 choice->no_max_fee 151 ? NULL 152 : &choice->max_fee)), 153 GNUNET_JSON_pack_array_steal ("inputs", 154 inputs), 155 GNUNET_JSON_pack_array_steal ("outputs", 156 outputs)); 157 }