merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit b2ddf5b3f84fa8b0db7f7a314212b8379c7cf6d0
parent cb73b57e0049d56fd3da69641dc2aad798a7fc3b
Author: Christian Grothoff <grothoff@gnunet.org>
Date:   Sat, 17 Jan 2026 13:01:58 +0100

more cleanup

Diffstat:
Msrc/backend/taler-merchant-httpd_post-using-templates.c | 257+++++++++++++++++++++++++++++++++----------------------------------------------
1 file changed, 106 insertions(+), 151 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_post-using-templates.c b/src/backend/taler-merchant-httpd_post-using-templates.c @@ -1518,42 +1518,51 @@ handle_phase_check_total (struct UseContext *uc) /* ***************** USE_PHASE_CREATE_ORDER **************** */ -// FIXME: review entirely from here... + +/** + * Convert any "tip" specified by the customer into a + * products array for the order. + * + * @param uc request context + * @return NULL if there is no tip + */ +// FIXME: change contract terms spec: a 'tip' is not a product, just put it into a new field 'tip'! +static json_t * +get_tip_product (const struct UseContext *uc) +{ + json_t *tip_products; + + if (uc->parse_request.no_tip) + return NULL; + GNUNET_assert (1 == uc->compute_price.totals_len); + tip_products = json_array (); + GNUNET_assert (NULL != tip_products); + GNUNET_assert (0 == + json_array_append_new ( + tip_products, + GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("description", + "tip"), + GNUNET_JSON_pack_uint64 ("quantity", + 1), + TALER_JSON_pack_amount ( + "price", + &uc->parse_request.tip)))); + return tip_products; +} /** * Create order request for inventory templates. * - * @param uc use context - * @return #GNUNET_OK on success + * @param[in,out] uc use context */ -static enum GNUNET_GenericReturnValue +static void create_using_templates_inventory (struct UseContext *uc) { + json_t *tip_products = get_tip_product (uc); json_t *inventory_products; - json_t *tip_products = NULL; - const bool multi_choices = uc->parse_request.no_amount && - uc->parse_request.no_tip; - - if (NULL != uc->ihc.request_body) - return GNUNET_OK; - - if (! uc->parse_request.no_tip) - { - tip_products = json_array (); - GNUNET_assert (NULL != tip_products); - GNUNET_assert (0 == - json_array_append_new ( - tip_products, - GNUNET_JSON_PACK ( - GNUNET_JSON_pack_string ("description", - "tip"), - GNUNET_JSON_pack_uint64 ("quantity", - 1), - TALER_JSON_pack_amount ( - "price", - &uc->parse_request.tip)))); - } + json_t *choices; inventory_products = json_array (); GNUNET_assert (NULL != inventory_products); @@ -1571,130 +1580,85 @@ create_using_templates_inventory (struct UseContext *uc) GNUNET_JSON_pack_string ("unit_quantity", item->unit_quantity)))); } - + choices = json_array (); + GNUNET_assert (NULL != choices); + for (size_t i = 0; i < uc->compute_price.totals_len; i++) { - json_t *body; - json_t *choices; - - choices = json_array (); - GNUNET_assert (NULL != choices); - if (! multi_choices) - { - GNUNET_assert (0 == - json_array_append_new ( - choices, - GNUNET_JSON_PACK ( - TALER_JSON_pack_amount ("amount", - &uc->compute_price.totals[0]))) - ); - } - else - { - for (size_t i = 0; i < uc->compute_price.totals_len; i++) - { - GNUNET_assert (0 == - json_array_append_new ( - choices, - GNUNET_JSON_PACK ( - TALER_JSON_pack_amount ("amount", - &uc->compute_price.totals[i]) - ))); - } - } + GNUNET_assert (0 == + json_array_append_new ( + choices, + GNUNET_JSON_PACK ( + TALER_JSON_pack_amount ("amount", + &uc->compute_price.totals[i]) + ))); + } - body = GNUNET_JSON_PACK ( - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("otp_id", - uc->lookup_template.etp.otp_id)), - GNUNET_JSON_pack_array_steal ("inventory_products", - inventory_products), - GNUNET_JSON_pack_object_steal ( - "order", - GNUNET_JSON_PACK ( - GNUNET_JSON_pack_uint64 ("version", - 1), - GNUNET_JSON_pack_array_steal ("choices", - choices), - GNUNET_JSON_pack_string ("summary", - NULL == uc->parse_request.summary + uc->ihc.request_body + = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("otp_id", + uc->lookup_template.etp.otp_id)), + GNUNET_JSON_pack_array_steal ("inventory_products", + inventory_products), + GNUNET_JSON_pack_object_steal ( + "order", + GNUNET_JSON_PACK ( + GNUNET_JSON_pack_uint64 ("version", + 1), + GNUNET_JSON_pack_array_steal ("choices", + choices), + GNUNET_JSON_pack_string ("summary", + NULL == uc->parse_request.summary ? uc->parse_template.tsummary : uc->parse_request.summary), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("fulfillment_url", - uc->parse_request.fulfillment_url)), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("fulfillment_message", - uc->parse_request.fulfillment_message)), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_array_steal ("products", - tip_products))))); - GNUNET_assert (NULL != body); - uc->ihc.request_body = body; - } - return GNUNET_OK; + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("fulfillment_url", + uc->parse_request.fulfillment_url)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("fulfillment_message", + uc->parse_request.fulfillment_message)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_array_steal ("products", + tip_products))))); } /** * Create order request for fixed-order templates. * - * @param uc use context - * @return #GNUNET_OK on success + * @param[in,out] uc use context */ -static enum GNUNET_GenericReturnValue +static void create_using_templates_fixed (struct UseContext *uc) { - json_t *tip_products = NULL; + json_t *tip_products = get_tip_product (uc); GNUNET_assert (1 == uc->compute_price.totals_len); - GNUNET_assert (NULL == uc->ihc.request_body); - if (! uc->parse_request.no_tip) - { - tip_products = json_array (); - GNUNET_assert (NULL != tip_products); - GNUNET_assert (0 == - json_array_append_new ( - tip_products, - GNUNET_JSON_PACK ( - GNUNET_JSON_pack_string ("description", - "tip"), - GNUNET_JSON_pack_uint64 ("quantity", - 1), - TALER_JSON_pack_amount ( - "price", - &uc->parse_request.tip)))); - } - { - json_t *body; - - body = GNUNET_JSON_PACK ( - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("otp_id", - uc->lookup_template.etp.otp_id)), - GNUNET_JSON_pack_object_steal ( - "order", - GNUNET_JSON_PACK ( - TALER_JSON_pack_amount ( - "amount", - &uc->compute_price.totals[0]), - GNUNET_JSON_pack_string ( - "summary", - NULL == uc->parse_request.summary + uc->ihc.request_body + = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("otp_id", + uc->lookup_template.etp.otp_id)), + GNUNET_JSON_pack_object_steal ( + "order", + GNUNET_JSON_PACK ( + TALER_JSON_pack_amount ( + "amount", + &uc->compute_price.totals[0]), + GNUNET_JSON_pack_string ( + "summary", + NULL == uc->parse_request.summary ? uc->parse_template.tsummary : uc->parse_request.summary), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("fulfillment_url", - uc->parse_request.fulfillment_url)), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("fulfillment_message", - uc->parse_request.fulfillment_message)), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_array_steal ("products", - tip_products))))); - uc->ihc.request_body = body; - } - - return GNUNET_OK; + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("fulfillment_url", + uc->parse_request.fulfillment_url)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("fulfillment_message", + uc->parse_request.fulfillment_message)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_array_steal ("products", + tip_products))))); } @@ -1702,44 +1666,35 @@ create_using_templates_fixed (struct UseContext *uc) * Create order request for paivana templates. * * @param uc use context - * @return #GNUNET_OK on success */ -static enum GNUNET_GenericReturnValue +static void create_using_templates_paivana (struct UseContext *uc) { /* TODO: PAIVANA include paivana-specific order creation. */ - return create_using_templates_fixed (uc); + create_using_templates_fixed (uc); } static void handle_phase_create_order (struct UseContext *uc) { - enum GNUNET_GenericReturnValue res = GNUNET_SYSERR; - + GNUNET_assert (NULL == uc->ihc.request_body); switch (uc->template_type) { case TALER_MERCHANT_TEMPLATE_TYPE_FIXED_ORDER: - res = create_using_templates_fixed (uc); + create_using_templates_fixed (uc); break; case TALER_MERCHANT_TEMPLATE_TYPE_PAIVANA: /* TODO: PAIVANA handle paivana-specific instantiation. */ - res = create_using_templates_paivana (uc); + create_using_templates_paivana (uc); break; case TALER_MERCHANT_TEMPLATE_TYPE_INVENTORY_CART: - res = create_using_templates_inventory (uc); + create_using_templates_inventory (uc); break; case TALER_MERCHANT_TEMPLATE_TYPE_INVALID: - GNUNET_break (0); - use_reply_with_error ( - uc, - MHD_HTTP_INTERNAL_SERVER_ERROR, - TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, - "template_contract has unexpected type"); - break; + GNUNET_assert (0); } - if (GNUNET_OK == res) - uc->phase++; + uc->phase++; }