merchant

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

commit 145ed59d9aff58e705790f9bda7e1ef80b069d4e
parent 2466c779f3524f2f1227e77519d33e0708e369cb
Author: Christian Grothoff <christian@grothoff.org>
Date:   Mon, 11 Jul 2022 12:45:16 +0200

-add product validation logic

Diffstat:
Msrc/backend/taler-merchant-httpd_private-post-orders.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 58 insertions(+), 6 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_private-post-orders.c b/src/backend/taler-merchant-httpd_private-post-orders.c @@ -71,14 +71,48 @@ check_products (const json_t *products) } json_array_foreach (products, index, value) { const char *description; + const char *product_id = NULL; + uint64_t quantity; + const char *unit = NULL; + struct TALER_Amount price; + const char *image = NULL; + json_t *taxes = NULL; + struct GNUNET_TIME_Timestamp delivery_date; const char *error_name; unsigned int error_line; enum GNUNET_GenericReturnValue res; struct GNUNET_JSON_Specification spec[] = { - // FIXME: parse and format-validate all - // optional fields of a product and check validity - GNUNET_JSON_spec_string ("description", - &description), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_string ("product_id", + &product_id), + NULL), + TALER_JSON_spec_i18n_str ("description", + &description), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_uint64 ("quantity", + &quantity), + NULL), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_string ("unit", + &unit), + NULL), + GNUNET_JSON_spec_mark_optional ( + TALER_JSON_spec_amount ("price", + TMH_currency, + &price), + NULL), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_string ("image", + &image), + NULL), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_json ("taxes", + &taxes), + NULL), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_timestamp ("delivery_date", + &delivery_date), + NULL), GNUNET_JSON_spec_end () }; @@ -90,13 +124,31 @@ check_products (const json_t *products) if (GNUNET_OK != res) { GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Product parsing failed at #%u: %s:%u\n", (unsigned int) index, error_name, error_line); return GNUNET_SYSERR; } + if ( (NULL != taxes) && + (! TMH_taxes_array_valid (taxes) ) ) + { + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Product parsing failed for taxes\n"); + GNUNET_JSON_parse_free (spec); + return GNUNET_SYSERR; + } + if ( (NULL != image) && + (! TMH_image_data_url_valid (image) ) ) + { + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Product parsing failed for image\n"); + GNUNET_JSON_parse_free (spec); + return GNUNET_SYSERR; + } GNUNET_JSON_parse_free (spec); } return GNUNET_OK; @@ -1099,7 +1151,7 @@ patch_order (struct MHD_Connection *connection, { case GNUNET_SYSERR: GNUNET_break (0); - GNUNET_JSON_parse_free (spec); + GNUNET_JSON_parse_free (spec); return TALER_MHD_reply_with_error ( connection, MHD_HTTP_INTERNAL_SERVER_ERROR,