/* This file is part of TALER (C) 2024 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. TALER is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, see */ /** * @file taler-merchant-httpd_contract.c * @brief shared logic for contract terms handling * @author Christian Blättler */ #include "platform.h" #include #include "taler-merchant-httpd_contract.h" enum GNUNET_GenericReturnValue TMH_serialize_contract (const struct TALER_MerchantContract *contract, const struct TMH_MerchantInstance *instance, json_t *exchanges, json_t **out) { if (TALER_MCV_V0 == contract->version) { return TMH_serialize_contract_v0 (contract, instance, exchanges, out); } if (TALER_MCV_V1 == contract->version) { return TMH_serialize_contract_v1 (contract, instance, exchanges, out); } GNUNET_break (0); return GNUNET_SYSERR; } enum GNUNET_GenericReturnValue TMH_serialize_contract_v0 (const struct TALER_MerchantContract *contract, const struct TMH_MerchantInstance *instance, json_t *exchanges, json_t **out) { json_t *merchant; { merchant = GNUNET_JSON_PACK ( GNUNET_JSON_pack_string ("name", instance->settings.name), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_string ("website", instance->settings.website)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_string ("email", instance->settings.email)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_string ("logo", instance->settings.logo))); GNUNET_assert (NULL != merchant); { json_t *loca; /* Handle merchant address */ loca = instance->settings.address; if (NULL != loca) { loca = json_deep_copy (loca); GNUNET_assert (NULL != loca); GNUNET_assert (0 == json_object_set_new (merchant, "address", loca)); } } { json_t *juri; /* Handle merchant jurisdiction */ juri = instance->settings.jurisdiction; if (NULL != juri) { juri = json_deep_copy (juri); GNUNET_assert (NULL != juri); GNUNET_assert (0 == json_object_set_new (merchant, "jurisdiction", juri)); } } } if (1 != contract->choices_len || NULL == contract->choices) { GNUNET_break (0); return GNUNET_SYSERR; } if (1 != contract->limits_len || NULL == contract->limits) { GNUNET_break (0); return GNUNET_SYSERR; } struct TALER_MerchantContractChoice *choice = contract->choices; *out = GNUNET_JSON_PACK ( GNUNET_JSON_pack_string ("summary", choice->summary), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_object_incref ("summary_i18n", choice->summary_i18n)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_string ("public_reorder_url", contract->public_reorder_url)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_string ("fulfillment_message", choice->fulfillment_message)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_object_incref ("fulfillment_message_i18n", choice->fulfillment_message_i18n)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_string ("fulfillment_url", choice->fulfillment_url)), GNUNET_JSON_pack_array_incref ("products", choice->products), GNUNET_JSON_pack_data_auto ("h_wire", &contract->limits->h_wire), GNUNET_JSON_pack_string ("wire_method", contract->limits->wire_method), GNUNET_JSON_pack_string ("order_id", contract->order_id), GNUNET_JSON_pack_timestamp ("timestamp", contract->timestamp), GNUNET_JSON_pack_timestamp ("pay_deadline", contract->pay_deadline), GNUNET_JSON_pack_timestamp ("wire_transfer_deadline", contract->wire_deadline), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_timestamp ("delivery_date", contract->delivery_date)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_object_incref ("delivery_location", contract->delivery_location)), GNUNET_JSON_pack_string ("merchant_base_url", contract->merchant_base_url), GNUNET_JSON_pack_object_incref ("merchant", merchant), GNUNET_JSON_pack_data_auto ("merchant_pub", &instance->merchant_pub), GNUNET_JSON_pack_array_incref ("exchanges", exchanges), TALER_JSON_pack_amount ("max_fee", &contract->limits->max_fee), TALER_JSON_pack_amount ("amount", &contract->brutto), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_object_incref ("extra", (json_t *) contract->extra)) ); /* Pack does not work here, because it doesn't set zero-values for timestamps */ GNUNET_assert (0 == json_object_set_new (*out, "refund_deadline", GNUNET_JSON_from_timestamp ( contract->refund_deadline))); GNUNET_log ( GNUNET_ERROR_TYPE_INFO, "Refund deadline for contact is %llu\n", (unsigned long long) contract->refund_deadline.abs_time.abs_value_us); GNUNET_log ( GNUNET_ERROR_TYPE_INFO, "Wallet timestamp for contact is %llu\n", (unsigned long long) contract->timestamp.abs_time.abs_value_us); /* Pack does not work here, because it sets zero-values for relative times */ /* auto_refund should only be set if it is not 0 */ if (! GNUNET_TIME_relative_is_zero (contract->auto_refund)) { GNUNET_assert (0 == json_object_set_new (*out, "auto_refund", GNUNET_JSON_from_time_rel ( contract->auto_refund))); } return GNUNET_OK; } enum GNUNET_GenericReturnValue TMH_serialize_contract_v1 (const struct TALER_MerchantContract *contract, const struct TMH_MerchantInstance *instance, json_t *exchanges, json_t **out) { // TODO: Implement v1 serializer return GNUNET_SYSERR; }