diff options
| author | levlam <levlam@telegram.org> | 2026-07-05 00:33:40 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2026-07-05 00:33:40 +0300 |
| commit | d6159234823aee742da1ebd31f21d62391439adf (patch) | |
| tree | 3ada3edc75e565e0062912617a6daa2303ab2840 /td | |
| parent | 4466eb8354d88e966bbd55761c4f829732662012 (diff) | |
Add and use LabeledPricePart::get_labeled_price_parts.
Diffstat (limited to 'td')
| -rw-r--r-- | td/telegram/InputInvoice.cpp | 22 | ||||
| -rw-r--r-- | td/telegram/LabeledPricePart.cpp | 35 | ||||
| -rw-r--r-- | td/telegram/LabeledPricePart.h | 5 | ||||
| -rw-r--r-- | td/telegram/Payments.cpp | 25 |
4 files changed, 48 insertions, 39 deletions
diff --git a/td/telegram/InputInvoice.cpp b/td/telegram/InputInvoice.cpp index 9347a7d1e..8aa3b2178 100644 --- a/td/telegram/InputInvoice.cpp +++ b/td/telegram/InputInvoice.cpp @@ -172,25 +172,9 @@ Result<InputInvoice> InputInvoice::process_input_message_invoice( result.start_parameter_ = std::move(input_invoice->start_parameter_); result.invoice_.currency_ = std::move(input_invoice->invoice_->currency_); - result.invoice_.price_parts_.reserve(input_invoice->invoice_->price_parts_.size()); - int64 total_amount = 0; - for (auto &price : input_invoice->invoice_->price_parts_) { - if (!clean_input_string(price->label_)) { - return Status::Error(400, "Invoice price label must be encoded in UTF-8"); - } - if (!check_currency_amount(price->amount_)) { - return Status::Error(400, "Too big amount of the currency specified"); - } - result.invoice_.price_parts_.emplace_back(std::move(price->label_), price->amount_); - total_amount += price->amount_; - } - if (total_amount <= 0) { - return Status::Error(400, "Total price must be positive"); - } - if (!check_currency_amount(total_amount)) { - return Status::Error(400, "Total price is too big"); - } - result.total_amount_ = total_amount; + TRY_RESULT_ASSIGN(result.invoice_.price_parts_, + LabeledPricePart::get_labeled_price_parts(std::move(input_invoice->invoice_->price_parts_), + &result.total_amount_)); result.invoice_.subscription_period_ = max(input_invoice->invoice_->subscription_period_, 0); if (input_invoice->invoice_->max_tip_amount_ < 0 || diff --git a/td/telegram/LabeledPricePart.cpp b/td/telegram/LabeledPricePart.cpp index 375146272..ff2776e1e 100644 --- a/td/telegram/LabeledPricePart.cpp +++ b/td/telegram/LabeledPricePart.cpp @@ -6,6 +6,8 @@ // #include "td/telegram/LabeledPricePart.h" +#include "td/telegram/misc.h" + #include "td/utils/algorithm.h" namespace td { @@ -19,6 +21,39 @@ vector<telegram_api::object_ptr<telegram_api::labeledPrice>> LabeledPricePart::g return transform(parts, [](const LabeledPricePart &price) { return price.get_input_labeled_price(); }); } +Result<vector<LabeledPricePart>> LabeledPricePart::get_labeled_price_parts( + vector<td_api::object_ptr<td_api::labeledPricePart>> &&parts, int64 *result_total_amount) { + if (parts.empty()) { + return Status::Error(400, "There must be at least one price part"); + } + vector<LabeledPricePart> result; + result.reserve(parts.size()); + int64 total_amount = 0; + for (auto &part : parts) { + if (part == nullptr) { + return Status::Error(400, "Price part must be non-empty"); + } + if (!clean_input_string(part->label_)) { + return Status::Error(400, "Price part label must be encoded in UTF-8"); + } + if (!check_currency_amount(part->amount_)) { + return Status::Error(400, "Too big amount of the currency specified"); + } + result.emplace_back(std::move(part->label_), part->amount_); + total_amount += part->amount_; + } + if (total_amount <= 0) { + return Status::Error(400, "Total price must be positive"); + } + if (!check_currency_amount(total_amount)) { + return Status::Error(400, "Total price is too big"); + } + if (result_total_amount != nullptr) { + *result_total_amount = total_amount; + } + return std::move(result); +} + StringBuilder &operator<<(StringBuilder &string_builder, const LabeledPricePart &labeled_price_part) { return string_builder << '[' << labeled_price_part.label << ": " << labeled_price_part.amount << ']'; } diff --git a/td/telegram/LabeledPricePart.h b/td/telegram/LabeledPricePart.h index be97976ea..0c6e8e843 100644 --- a/td/telegram/LabeledPricePart.h +++ b/td/telegram/LabeledPricePart.h @@ -6,9 +6,11 @@ // #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 { @@ -24,6 +26,9 @@ struct LabeledPricePart { LabeledPricePart(string &&label, int64 amount) : label(std::move(label)), amount(amount) { } + static Result<vector<LabeledPricePart>> get_labeled_price_parts( + vector<td_api::object_ptr<td_api::labeledPricePart>> &&parts, int64 *result_total_amount); + static vector<telegram_api::object_ptr<telegram_api::labeledPrice>> get_input_labeled_prices( const vector<LabeledPricePart> &parts); diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index 013482153..872bc83cc 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -1024,26 +1024,11 @@ void answer_shipping_query(Td *td, int64 shipping_query_id, return promise.set_error(400, "Shipping option title must be encoded in UTF-8"); } - vector<tl_object_ptr<telegram_api::labeledPrice>> prices; - for (auto &price_part : option->price_parts_) { - if (price_part == nullptr) { - return promise.set_error(400, "Shipping option price part must be non-empty"); - } - if (!clean_input_string(price_part->label_)) { - return promise.set_error(400, "Shipping option price part label must be encoded in UTF-8"); - } - if (!check_currency_amount(price_part->amount_)) { - return promise.set_error(400, "Too big amount of the currency specified"); - } - - prices.push_back(make_tl_object<telegram_api::labeledPrice>(std::move(price_part->label_), price_part->amount_)); - } - if (prices.empty()) { - return promise.set_error(400, "There must be at least one price"); - } - - options.push_back(make_tl_object<telegram_api::shippingOption>(std::move(option->id_), std::move(option->title_), - std::move(prices))); + TRY_RESULT_PROMISE(promise, labeled_price_parts, + LabeledPricePart::get_labeled_price_parts(std::move(option->price_parts_), nullptr)); + options.push_back(telegram_api::make_object<telegram_api::shippingOption>( + std::move(option->id_), std::move(option->title_), + LabeledPricePart::get_input_labeled_prices(labeled_price_parts))); } if (options.empty()) { return promise.set_error(400, "There must be at least one shipping option"); |
