summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_helper.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-08-04 18:11:35 +0200
committerChristian Grothoff <christian@grothoff.org>2021-08-04 18:11:35 +0200
commit99eb36e490f5f0ca16cb451941ca873b03016b27 (patch)
tree9da20e4ef9dd7b5b1e9a860afc92c55b431df648 /src/backend/taler-merchant-httpd_helper.c
parent977c3d47538d071e2b37599bb71c0fae3ad36d44 (diff)
downloadmerchant-99eb36e490f5f0ca16cb451941ca873b03016b27.tar.gz
merchant-99eb36e490f5f0ca16cb451941ca873b03016b27.tar.bz2
merchant-99eb36e490f5f0ca16cb451941ca873b03016b27.zip
-implement #6953 + #6948
Diffstat (limited to 'src/backend/taler-merchant-httpd_helper.c')
-rw-r--r--src/backend/taler-merchant-httpd_helper.c166
1 files changed, 164 insertions, 2 deletions
diff --git a/src/backend/taler-merchant-httpd_helper.c b/src/backend/taler-merchant-httpd_helper.c
index 374751d1..585a491a 100644
--- a/src/backend/taler-merchant-httpd_helper.c
+++ b/src/backend/taler-merchant-httpd_helper.c
@@ -92,11 +92,173 @@ TMH_payto_uri_array_valid (const json_t *payto_uris)
bool
TMH_location_object_valid (const json_t *location)
{
- return true; // FIXME
+ const char *country;
+ const char *subdivision;
+ const char *district;
+ const char *town;
+ const char *town_loc;
+ const char *postcode;
+ const char *street;
+ const char *building;
+ const char *building_no;
+ json_t *lines = NULL;
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("country",
+ &country)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("country_subdivision",
+ &subdivision)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("district",
+ &district)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("town",
+ &town)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("town_location",
+ &town_loc)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("post_code",
+ &postcode)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("street",
+ &street)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("building_name",
+ &building)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("building_number",
+ &building_no)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_json ("address_lines",
+ &lines)),
+ GNUNET_JSON_spec_end ()
+ };
+ const char *ename;
+ unsigned int eline;
+
+ if (GNUNET_OK !=
+ GNUNET_JSON_parse (location,
+ spec,
+ &ename,
+ &eline))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Invalid location for field %s\n",
+ ename);
+ return false;
+ }
+ if (NULL != lines)
+ {
+ size_t idx;
+ json_t *line;
+
+ if (! json_is_array (lines))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Invalid location for field %s\n",
+ "lines");
+ return false;
+ }
+ json_array_foreach (lines, idx, line)
+ {
+ if (! json_is_string (line))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Invalid address line #%u in location\n",
+ (unsigned int) idx);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+
+bool
+TMH_products_array_valid (const json_t *products)
+{
+ const json_t *product;
+ size_t idx;
+ bool valid = true;
+
+ if (! json_is_array (products))
+ return false;
+ json_array_foreach ((json_t *) products, idx, product)
+ {
+ const char *product_id;
+ const char *description;
+ json_t *description_i18n = NULL;
+ uint64_t quantity;
+ const char *unit;
+ struct TALER_Amount price;
+ const char *image_data_url = NULL;
+ json_t *taxes = NULL;
+ struct GNUNET_TIME_Absolute delivery_date;
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("product_id",
+ &product_id)),
+ GNUNET_JSON_spec_string ("description",
+ &description),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_json ("description_i18n",
+ &description_i18n)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_uint64 ("quantity",
+ &quantity)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("unit",
+ &unit)),
+ GNUNET_JSON_spec_mark_optional (
+ TALER_JSON_spec_amount ("price",
+ TMH_currency,
+ &price)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_string ("image",
+ &image_data_url)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_json ("taxes",
+ &taxes)),
+ GNUNET_JSON_spec_mark_optional (
+ GNUNET_JSON_spec_absolute_time ("delivery_date",
+ &delivery_date)),
+ GNUNET_JSON_spec_end ()
+ };
+ const char *ename;
+ unsigned int eline;
+
+ if (GNUNET_OK !=
+ GNUNET_JSON_parse (product,
+ spec,
+ &ename,
+ &eline))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Invalid product #%u for field %s\n",
+ (unsigned int) idx,
+ ename);
+ return false;
+ }
+ if ( (NULL != image_data_url) &&
+ (! TMH_image_data_url_valid (image_data_url)) )
+ valid = false;
+ if ( (NULL != taxes) &&
+ (! TMH_taxes_array_valid (taxes)) )
+ valid = false;
+ if ( (NULL != description_i18n) &&
+ (! TALER_JSON_check_i18n (description_i18n)) )
+ valid = false;
+ GNUNET_JSON_parse_free (spec);
+ if (! valid)
+ break;
+ }
+
+ return valid;
}
-// FIXME
bool
TMH_image_data_url_valid (const char *image_data_url)
{