From 7f433c7e181728f9159f38a4c4f9297dbb1cd35a Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 22 Sep 2022 19:28:39 +0300 Subject: [PATCH] Add OrderInfo.h. --- CMakeLists.txt | 4 +- td/telegram/MessageContent.cpp | 4 +- td/telegram/OrderInfo.cpp | 190 ++++++++++++++++++++ td/telegram/OrderInfo.h | 81 +++++++++ td/telegram/{Payments.hpp => OrderInfo.hpp} | 0 td/telegram/Payments.cpp | 174 ------------------ td/telegram/Payments.h | 63 ------- td/telegram/SecureValue.cpp | 2 +- td/telegram/UpdatesManager.cpp | 1 + 9 files changed, 278 insertions(+), 241 deletions(-) create mode 100644 td/telegram/OrderInfo.cpp create mode 100644 td/telegram/OrderInfo.h rename td/telegram/{Payments.hpp => OrderInfo.hpp} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc1971495..830574eb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -408,6 +408,7 @@ set(TDLIB_SOURCE td/telegram/NotificationSound.cpp td/telegram/NotificationType.cpp td/telegram/OptionManager.cpp + td/telegram/OrderInfo.cpp td/telegram/Payments.cpp td/telegram/PasswordManager.cpp td/telegram/PhoneNumberManager.cpp @@ -651,6 +652,7 @@ set(TDLIB_SOURCE td/telegram/NotificationSoundType.h td/telegram/NotificationType.h td/telegram/OptionManager.h + td/telegram/OrderInfo.h td/telegram/PasswordManager.h td/telegram/Payments.h td/telegram/PhoneNumberManager.h @@ -740,7 +742,7 @@ set(TDLIB_SOURCE td/telegram/MessageReplyInfo.hpp td/telegram/MinChannel.hpp td/telegram/NotificationSettings.hpp - td/telegram/Payments.hpp + td/telegram/OrderInfo.hpp td/telegram/Photo.hpp td/telegram/PhotoSize.hpp td/telegram/PhotoSizeSource.hpp diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 6c434a9c8..6155ffead 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -46,8 +46,8 @@ #include "td/telegram/misc.h" #include "td/telegram/net/DcId.h" #include "td/telegram/OptionManager.h" -#include "td/telegram/Payments.h" -#include "td/telegram/Payments.hpp" +#include "td/telegram/OrderInfo.h" +#include "td/telegram/OrderInfo.hpp" #include "td/telegram/Photo.h" #include "td/telegram/Photo.hpp" #include "td/telegram/PhotoFormat.h" diff --git a/td/telegram/OrderInfo.cpp b/td/telegram/OrderInfo.cpp new file mode 100644 index 000000000..cd00061f4 --- /dev/null +++ b/td/telegram/OrderInfo.cpp @@ -0,0 +1,190 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/OrderInfo.h" + +#include "td/telegram/misc.h" + +#include "td/utils/format.h" +#include "td/utils/JsonBuilder.h" + +namespace td { + +bool operator==(const Address &lhs, const Address &rhs) { + return lhs.country_code == rhs.country_code && lhs.state == rhs.state && lhs.city == rhs.city && + lhs.street_line1 == rhs.street_line1 && lhs.street_line2 == rhs.street_line2 && + lhs.postal_code == rhs.postal_code; +} + +bool operator!=(const Address &lhs, const Address &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const Address &address) { + return string_builder << "[Address " << tag("country_code", address.country_code) << tag("state", address.state) + << tag("city", address.city) << tag("street_line1", address.street_line1) + << tag("street_line2", address.street_line2) << tag("postal_code", address.postal_code) << "]"; +} + +unique_ptr
get_address(tl_object_ptr &&address) { + if (address == nullptr) { + return nullptr; + } + return td::make_unique
(std::move(address->country_iso2_), std::move(address->state_), + std::move(address->city_), std::move(address->street_line1_), + std::move(address->street_line2_), std::move(address->post_code_)); +} + +static bool is_capital_alpha(char c) { + return 'A' <= c && c <= 'Z'; +} + +Status check_country_code(string &country_code) { + if (!clean_input_string(country_code)) { + return Status::Error(400, "Country code must be encoded in UTF-8"); + } + if (country_code.size() != 2 || !is_capital_alpha(country_code[0]) || !is_capital_alpha(country_code[1])) { + return Status::Error(400, "Wrong country code specified"); + } + return Status::OK(); +} + +static Status check_state(string &state) { + if (!clean_input_string(state)) { + return Status::Error(400, "State must be encoded in UTF-8"); + } + return Status::OK(); +} + +static Status check_city(string &city) { + if (!clean_input_string(city)) { + return Status::Error(400, "City must be encoded in UTF-8"); + } + return Status::OK(); +} + +static Status check_street_line(string &street_line) { + if (!clean_input_string(street_line)) { + return Status::Error(400, "Street line must be encoded in UTF-8"); + } + return Status::OK(); +} + +static Status check_postal_code(string &postal_code) { + if (!clean_input_string(postal_code)) { + return Status::Error(400, "Postal code must be encoded in UTF-8"); + } + return Status::OK(); +} + +Result
get_address(td_api::object_ptr &&address) { + if (address == nullptr) { + return Status::Error(400, "Address must be non-empty"); + } + TRY_STATUS(check_country_code(address->country_code_)); + TRY_STATUS(check_state(address->state_)); + TRY_STATUS(check_city(address->city_)); + TRY_STATUS(check_street_line(address->street_line1_)); + TRY_STATUS(check_street_line(address->street_line2_)); + TRY_STATUS(check_postal_code(address->postal_code_)); + + return Address(std::move(address->country_code_), std::move(address->state_), std::move(address->city_), + std::move(address->street_line1_), std::move(address->street_line2_), + std::move(address->postal_code_)); +} + +tl_object_ptr get_address_object(const unique_ptr
&address) { + if (address == nullptr) { + return nullptr; + } + return get_address_object(*address); +} + +tl_object_ptr get_address_object(const Address &address) { + return make_tl_object(address.country_code, address.state, address.city, address.street_line1, + address.street_line2, address.postal_code); +} + +string address_to_json(const Address &address) { + return json_encode(json_object([&](auto &o) { + o("country_code", address.country_code); + o("state", address.state); + o("city", address.city); + o("street_line1", address.street_line1); + o("street_line2", address.street_line2); + o("post_code", address.postal_code); + })); +} + +Result
address_from_json(Slice json) { + auto json_copy = json.str(); + auto r_value = json_decode(json_copy); + if (r_value.is_error()) { + return Status::Error(400, "Can't parse address JSON object"); + } + + auto value = r_value.move_as_ok(); + if (value.type() != JsonValue::Type::Object) { + return Status::Error(400, "Address must be an Object"); + } + + auto &object = value.get_object(); + TRY_RESULT(country_code, get_json_object_string_field(object, "country_code", true)); + TRY_RESULT(state, get_json_object_string_field(object, "state", true)); + TRY_RESULT(city, get_json_object_string_field(object, "city", true)); + TRY_RESULT(street_line1, get_json_object_string_field(object, "street_line1", true)); + TRY_RESULT(street_line2, get_json_object_string_field(object, "street_line2", true)); + TRY_RESULT(postal_code, get_json_object_string_field(object, "post_code", true)); + + TRY_STATUS(check_country_code(country_code)); + TRY_STATUS(check_state(state)); + TRY_STATUS(check_city(city)); + TRY_STATUS(check_street_line(street_line1)); + TRY_STATUS(check_street_line(street_line2)); + TRY_STATUS(check_postal_code(postal_code)); + + return Address(std::move(country_code), std::move(state), std::move(city), std::move(street_line1), + std::move(street_line2), std::move(postal_code)); +} + +bool operator==(const OrderInfo &lhs, const OrderInfo &rhs) { + return lhs.name == rhs.name && lhs.phone_number == rhs.phone_number && lhs.email_address == rhs.email_address && + ((lhs.shipping_address == nullptr && rhs.shipping_address == nullptr) || + (lhs.shipping_address != nullptr && rhs.shipping_address != nullptr && + *lhs.shipping_address == *rhs.shipping_address)); +} + +bool operator!=(const OrderInfo &lhs, const OrderInfo &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const OrderInfo &order_info) { + string_builder << "[OrderInfo " << tag("name", order_info.name) << tag("phone_number", order_info.phone_number) + << tag("email_address", order_info.email_address); + if (order_info.shipping_address != nullptr) { + string_builder << *order_info.shipping_address; + } + return string_builder << "]"; +} + +unique_ptr get_order_info(tl_object_ptr order_info) { + if (order_info == nullptr || order_info->flags_ == 0) { + return nullptr; + } + return td::make_unique(std::move(order_info->name_), std::move(order_info->phone_), + std::move(order_info->email_), + get_address(std::move(order_info->shipping_address_))); +} + +tl_object_ptr get_order_info_object(const unique_ptr &order_info) { + if (order_info == nullptr) { + return nullptr; + } + return make_tl_object(order_info->name, order_info->phone_number, order_info->email_address, + get_address_object(order_info->shipping_address)); +} + +} // namespace td diff --git a/td/telegram/OrderInfo.h b/td/telegram/OrderInfo.h new file mode 100644 index 000000000..0026b1e50 --- /dev/null +++ b/td/telegram/OrderInfo.h @@ -0,0 +1,81 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/Status.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +struct Address { + string country_code; + string state; + string city; + string street_line1; + string street_line2; + string postal_code; + + Address() = default; + Address(string &&country_code, string &&state, string &&city, string &&street_line1, string &&street_line2, + string &&postal_code) + : country_code(std::move(country_code)) + , state(std::move(state)) + , city(std::move(city)) + , street_line1(std::move(street_line1)) + , street_line2(std::move(street_line2)) + , postal_code(std::move(postal_code)) { + } +}; + +struct OrderInfo { + string name; + string phone_number; + string email_address; + unique_ptr
shipping_address; + + OrderInfo() = default; + OrderInfo(string &&name, string &&phone_number, string &&email_address, unique_ptr
&&shipping_address) + : name(std::move(name)) + , phone_number(std::move(phone_number)) + , email_address(std::move(email_address)) + , shipping_address(std::move(shipping_address)) { + } +}; + +bool operator==(const Address &lhs, const Address &rhs); +bool operator!=(const Address &lhs, const Address &rhs); + +StringBuilder &operator<<(StringBuilder &string_builder, const Address &address); + +unique_ptr
get_address(tl_object_ptr &&address); + +Result
get_address(td_api::object_ptr &&address); + +tl_object_ptr get_address_object(const unique_ptr
&address); + +tl_object_ptr get_address_object(const Address &address); + +string address_to_json(const Address &address); + +Result
address_from_json(Slice json); + +Status check_country_code(string &country_code); + +bool operator==(const OrderInfo &lhs, const OrderInfo &rhs); +bool operator!=(const OrderInfo &lhs, const OrderInfo &rhs); + +StringBuilder &operator<<(StringBuilder &string_builder, const OrderInfo &order_info); + +unique_ptr get_order_info(tl_object_ptr order_info); + +tl_object_ptr get_order_info_object(const unique_ptr &order_info); + +} // namespace td diff --git a/td/telegram/Payments.hpp b/td/telegram/OrderInfo.hpp similarity index 100% rename from td/telegram/Payments.hpp rename to td/telegram/OrderInfo.hpp diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index f68074f2c..3971c78b4 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -705,180 +705,6 @@ class GetBankCardInfoQuery final : public Td::ResultHandler { } }; -bool operator==(const Address &lhs, const Address &rhs) { - return lhs.country_code == rhs.country_code && lhs.state == rhs.state && lhs.city == rhs.city && - lhs.street_line1 == rhs.street_line1 && lhs.street_line2 == rhs.street_line2 && - lhs.postal_code == rhs.postal_code; -} - -bool operator!=(const Address &lhs, const Address &rhs) { - return !(lhs == rhs); -} - -StringBuilder &operator<<(StringBuilder &string_builder, const Address &address) { - return string_builder << "[Address " << tag("country_code", address.country_code) << tag("state", address.state) - << tag("city", address.city) << tag("street_line1", address.street_line1) - << tag("street_line2", address.street_line2) << tag("postal_code", address.postal_code) << "]"; -} - -unique_ptr
get_address(tl_object_ptr &&address) { - if (address == nullptr) { - return nullptr; - } - return td::make_unique
(std::move(address->country_iso2_), std::move(address->state_), - std::move(address->city_), std::move(address->street_line1_), - std::move(address->street_line2_), std::move(address->post_code_)); -} - -static bool is_capital_alpha(char c) { - return 'A' <= c && c <= 'Z'; -} - -Status check_country_code(string &country_code) { - if (!clean_input_string(country_code)) { - return Status::Error(400, "Country code must be encoded in UTF-8"); - } - if (country_code.size() != 2 || !is_capital_alpha(country_code[0]) || !is_capital_alpha(country_code[1])) { - return Status::Error(400, "Wrong country code specified"); - } - return Status::OK(); -} - -static Status check_state(string &state) { - if (!clean_input_string(state)) { - return Status::Error(400, "State must be encoded in UTF-8"); - } - return Status::OK(); -} - -static Status check_city(string &city) { - if (!clean_input_string(city)) { - return Status::Error(400, "City must be encoded in UTF-8"); - } - return Status::OK(); -} - -static Status check_street_line(string &street_line) { - if (!clean_input_string(street_line)) { - return Status::Error(400, "Street line must be encoded in UTF-8"); - } - return Status::OK(); -} - -static Status check_postal_code(string &postal_code) { - if (!clean_input_string(postal_code)) { - return Status::Error(400, "Postal code must be encoded in UTF-8"); - } - return Status::OK(); -} - -Result
get_address(td_api::object_ptr &&address) { - if (address == nullptr) { - return Status::Error(400, "Address must be non-empty"); - } - TRY_STATUS(check_country_code(address->country_code_)); - TRY_STATUS(check_state(address->state_)); - TRY_STATUS(check_city(address->city_)); - TRY_STATUS(check_street_line(address->street_line1_)); - TRY_STATUS(check_street_line(address->street_line2_)); - TRY_STATUS(check_postal_code(address->postal_code_)); - - return Address(std::move(address->country_code_), std::move(address->state_), std::move(address->city_), - std::move(address->street_line1_), std::move(address->street_line2_), - std::move(address->postal_code_)); -} - -tl_object_ptr get_address_object(const unique_ptr
&address) { - if (address == nullptr) { - return nullptr; - } - return get_address_object(*address); -} - -tl_object_ptr get_address_object(const Address &address) { - return make_tl_object(address.country_code, address.state, address.city, address.street_line1, - address.street_line2, address.postal_code); -} - -string address_to_json(const Address &address) { - return json_encode(json_object([&](auto &o) { - o("country_code", address.country_code); - o("state", address.state); - o("city", address.city); - o("street_line1", address.street_line1); - o("street_line2", address.street_line2); - o("post_code", address.postal_code); - })); -} - -Result
address_from_json(Slice json) { - auto json_copy = json.str(); - auto r_value = json_decode(json_copy); - if (r_value.is_error()) { - return Status::Error(400, "Can't parse address JSON object"); - } - - auto value = r_value.move_as_ok(); - if (value.type() != JsonValue::Type::Object) { - return Status::Error(400, "Address must be an Object"); - } - - auto &object = value.get_object(); - TRY_RESULT(country_code, get_json_object_string_field(object, "country_code", true)); - TRY_RESULT(state, get_json_object_string_field(object, "state", true)); - TRY_RESULT(city, get_json_object_string_field(object, "city", true)); - TRY_RESULT(street_line1, get_json_object_string_field(object, "street_line1", true)); - TRY_RESULT(street_line2, get_json_object_string_field(object, "street_line2", true)); - TRY_RESULT(postal_code, get_json_object_string_field(object, "post_code", true)); - - TRY_STATUS(check_country_code(country_code)); - TRY_STATUS(check_state(state)); - TRY_STATUS(check_city(city)); - TRY_STATUS(check_street_line(street_line1)); - TRY_STATUS(check_street_line(street_line2)); - TRY_STATUS(check_postal_code(postal_code)); - - return Address(std::move(country_code), std::move(state), std::move(city), std::move(street_line1), - std::move(street_line2), std::move(postal_code)); -} - -bool operator==(const OrderInfo &lhs, const OrderInfo &rhs) { - return lhs.name == rhs.name && lhs.phone_number == rhs.phone_number && lhs.email_address == rhs.email_address && - ((lhs.shipping_address == nullptr && rhs.shipping_address == nullptr) || - (lhs.shipping_address != nullptr && rhs.shipping_address != nullptr && - *lhs.shipping_address == *rhs.shipping_address)); -} - -bool operator!=(const OrderInfo &lhs, const OrderInfo &rhs) { - return !(lhs == rhs); -} - -StringBuilder &operator<<(StringBuilder &string_builder, const OrderInfo &order_info) { - string_builder << "[OrderInfo " << tag("name", order_info.name) << tag("phone_number", order_info.phone_number) - << tag("email_address", order_info.email_address); - if (order_info.shipping_address != nullptr) { - string_builder << *order_info.shipping_address; - } - return string_builder << "]"; -} - -unique_ptr get_order_info(tl_object_ptr order_info) { - if (order_info == nullptr || order_info->flags_ == 0) { - return nullptr; - } - return td::make_unique(std::move(order_info->name_), std::move(order_info->phone_), - std::move(order_info->email_), - get_address(std::move(order_info->shipping_address_))); -} - -tl_object_ptr get_order_info_object(const unique_ptr &order_info) { - if (order_info == nullptr) { - return nullptr; - } - return make_tl_object(order_info->name, order_info->phone_number, order_info->email_address, - get_address_object(order_info->shipping_address)); -} - bool operator==(const ShippingOption &lhs, const ShippingOption &rhs) { return lhs.id == rhs.id && lhs.title == rhs.title && lhs.price_parts == rhs.price_parts; } diff --git a/td/telegram/Payments.h b/td/telegram/Payments.h index bdb29444f..6374e3eb4 100644 --- a/td/telegram/Payments.h +++ b/td/telegram/Payments.h @@ -22,75 +22,12 @@ namespace td { class Td; -struct Address { - string country_code; - string state; - string city; - string street_line1; - string street_line2; - string postal_code; - - Address() = default; - Address(string &&country_code, string &&state, string &&city, string &&street_line1, string &&street_line2, - string &&postal_code) - : country_code(std::move(country_code)) - , state(std::move(state)) - , city(std::move(city)) - , street_line1(std::move(street_line1)) - , street_line2(std::move(street_line2)) - , postal_code(std::move(postal_code)) { - } -}; - -struct OrderInfo { - string name; - string phone_number; - string email_address; - unique_ptr
shipping_address; - - OrderInfo() = default; - OrderInfo(string &&name, string &&phone_number, string &&email_address, unique_ptr
&&shipping_address) - : name(std::move(name)) - , phone_number(std::move(phone_number)) - , email_address(std::move(email_address)) - , shipping_address(std::move(shipping_address)) { - } -}; - struct ShippingOption { string id; string title; vector price_parts; }; -bool operator==(const Address &lhs, const Address &rhs); -bool operator!=(const Address &lhs, const Address &rhs); - -StringBuilder &operator<<(StringBuilder &string_builder, const Address &address); - -unique_ptr
get_address(tl_object_ptr &&address); - -Result
get_address(td_api::object_ptr &&address); - -tl_object_ptr get_address_object(const unique_ptr
&address); - -tl_object_ptr get_address_object(const Address &address); - -string address_to_json(const Address &address); - -Result
address_from_json(Slice json); - -Status check_country_code(string &country_code); - -bool operator==(const OrderInfo &lhs, const OrderInfo &rhs); -bool operator!=(const OrderInfo &lhs, const OrderInfo &rhs); - -StringBuilder &operator<<(StringBuilder &string_builder, const OrderInfo &order_info); - -unique_ptr get_order_info(tl_object_ptr order_info); - -tl_object_ptr get_order_info_object(const unique_ptr &order_info); - bool operator==(const ShippingOption &lhs, const ShippingOption &rhs); bool operator!=(const ShippingOption &lhs, const ShippingOption &rhs); diff --git a/td/telegram/SecureValue.cpp b/td/telegram/SecureValue.cpp index 380aa875d..2128c7593 100644 --- a/td/telegram/SecureValue.cpp +++ b/td/telegram/SecureValue.cpp @@ -14,7 +14,7 @@ #include "td/telegram/Global.h" #include "td/telegram/misc.h" #include "td/telegram/net/DcId.h" -#include "td/telegram/Payments.h" +#include "td/telegram/OrderInfo.h" #include "td/telegram/telegram_api.hpp" #include "td/utils/algorithm.h" diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 888c6f782..0d98d5ba8 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -36,6 +36,7 @@ #include "td/telegram/NotificationSettings.h" #include "td/telegram/NotificationSettingsManager.h" #include "td/telegram/OptionManager.h" +#include "td/telegram/OrderInfo.h" #include "td/telegram/Payments.h" #include "td/telegram/PollId.h" #include "td/telegram/PollManager.h"