From a98a9fb971e67f546e0acac97e0cb13b869df1a7 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 3 Oct 2023 18:27:56 +0300 Subject: [PATCH] Add td_api::inputInvoiceTelegram. --- td/generate/scheme/td_api.tl | 39 ++++++++++++++---- td/telegram/Payments.cpp | 79 ++++++++++++++++++++++++++++++++++++ td/telegram/Premium.cpp | 2 +- td/telegram/Premium.h | 4 ++ td/telegram/cli.cpp | 4 +- 5 files changed, 118 insertions(+), 10 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2a23ef165..a4fa25536 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2228,6 +2228,9 @@ inputInvoiceMessage chat_id:int53 message_id:int53 = InputInvoice; //@description An invoice from a link of the type internalLinkTypeInvoice @name Name of the invoice inputInvoiceName name:string = InputInvoice; +//@description An invoice for a payment toward Telegram; must not be used in the in-store apps @purpose Transaction purpose +inputInvoiceTelegram purpose:TelegramPaymentPurpose = InputInvoice; + //@class MessageExtendedMedia @description Describes a media, which is attached to an invoice @@ -4409,22 +4412,22 @@ premiumState state:formattedText payment_options:vector boosted_chat_id:int53 currency:string amount:int53 = StorePaymentPurpose; +//@user_ids Identifiers of the users which can activate the gift codes +storePaymentPurposePremiumGiftCodes boosted_chat_id:int53 currency:string amount:int53 user_ids:vector = StorePaymentPurpose; -//@description The user created a Telegram Premium giveaway for subscribers of a channel chat +//@description The user creating a Telegram Premium giveaway for subscribers of a channel chat //@boosted_chat_id Identifier of the channel chat, which will be automatically boosted by the users for duration of the Premium subscription and which is administered by the user -//@additional_chat_ids Identifiers of other channel chats that must be subscribed by the users to be eligible +//@additional_chat_ids Identifiers of other channel chats that must be subscribed by the users to be eligible for the giveaway //@date Point in time (Unix timestamp) when the giveaway will be performed; must be from 1 minute to 365 days in the future //@only_new_subscribers Pass true if only new subscribers of the chats will be eligible for the giveaway //@currency ISO 4217 currency code of the payment currency @@ -4432,6 +4435,28 @@ storePaymentPurposePremiumGiftCodes user_ids:vector boosted_chat_id:int53 storePaymentPurposePremiumGiveaway boosted_chat_id:int53 additional_chat_ids:vector date:int32 only_new_subscribers:Bool currency:string amount:int53 = StorePaymentPurpose; +//@class TelegramPaymentPurpose @description Describes a purpose of a payment toward Telegram + +//@description The user creating Telegram Premium gift codes for other users +//@boosted_chat_id Identifier of the channel chat, which will be automatically boosted by the users for duration of the Premium subscription and which is administered by the user; 0 if none +//@currency ISO 4217 currency code of the payment currency +//@amount Paid amount, in the smallest units of the currency +//@user_ids Identifiers of the users which can activate the gift codes +//@month_count Number of month the Telegram Premium subscription will be active for the users +telegramPaymentPurposePremiumGiftCodes boosted_chat_id:int53 currency:string amount:int53 user_ids:vector month_count:int32 = TelegramPaymentPurpose; + +//@description The user creating a Telegram Premium giveaway for subscribers of a channel chat +//@boosted_chat_id Identifier of the channel chat, which will be automatically boosted by the users for duration of the Premium subscription and which is administered by the user +//@additional_chat_ids Identifiers of other channel chats that must be subscribed by the users to be eligible for the giveaway +//@date Point in time (Unix timestamp) when the giveaway will be performed; must be from 1 minute to 365 days in the future +//@only_new_subscribers Pass true, if only new subscribers of the chats will be eligible for the giveaway +//@currency ISO 4217 currency code of the payment currency +//@amount Paid amount, in the smallest units of the currency +//@user_count Number of users which will be able to activate the gift codes +//@month_count Number of month the Telegram Premium subscription will be active for the users +telegramPaymentPurposePremiumGiveaway boosted_chat_id:int53 additional_chat_ids:vector date:int32 only_new_subscribers:Bool currency:string amount:int53 user_count:int32 month_count:int32 = TelegramPaymentPurpose; + + //@class DeviceToken @description Represents a data needed to subscribe for push notifications through registerDevice method. //-To use specific push notification service, the correct application platform must be specified and a valid server authentication data must be uploaded at https://my.telegram.org diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index a51458cce..2a8afcee4 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -17,6 +17,7 @@ #include "td/telegram/misc.h" #include "td/telegram/PasswordManager.h" #include "td/telegram/Photo.h" +#include "td/telegram/Premium.h" #include "td/telegram/ServerMessageId.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" @@ -29,6 +30,7 @@ #include "td/utils/common.h" #include "td/utils/JsonBuilder.h" #include "td/utils/logging.h" +#include "td/utils/Random.h" #include "td/utils/Status.h" namespace td { @@ -68,6 +70,83 @@ Result get_input_invoice_info(Td *td, td_api::object_ptr(invoice->name_); break; } + case td_api::inputInvoiceTelegram::ID: { + auto invoice = td_api::move_object_as(input_invoice); + if (invoice->purpose_ == nullptr) { + return Status::Error(400, "Purpose must not be empty"); + } + switch (invoice->purpose_->get_id()) { + case td_api::telegramPaymentPurposePremiumGiftCodes::ID: { + auto p = static_cast(invoice->purpose_.get()); + vector> input_users; + for (auto user_id : p->user_ids_) { + TRY_RESULT(input_user, td->contacts_manager_->get_input_user(UserId(user_id))); + input_users.push_back(std::move(input_user)); + } + if (p->amount_ <= 0 || !check_currency_amount(p->amount_)) { + return Status::Error(400, "Invalid amount of the currency specified"); + } + DialogId boosted_dialog_id(p->boosted_chat_id_); + TRY_RESULT(boost_input_peer, get_boost_input_peer(td, boosted_dialog_id)); + int32 flags = 0; + if (boost_input_peer != nullptr) { + flags |= telegram_api::inputStorePaymentPremiumGiftCode::BOOST_PEER_MASK; + } + auto option = telegram_api::make_object( + 0, input_users.size(), p->month_count_, string(), 0, p->currency_, p->amount_); + auto purpose = telegram_api::make_object( + flags, std::move(input_users), std::move(boost_input_peer), p->currency_, p->amount_); + + result.dialog_id_ = boosted_dialog_id; + result.input_invoice_ = telegram_api::make_object( + std::move(purpose), std::move(option)); + break; + } + case td_api::telegramPaymentPurposePremiumGiveaway::ID: { + auto p = static_cast(invoice->purpose_.get()); + if (p->amount_ <= 0 || !check_currency_amount(p->amount_)) { + return Status::Error(400, "Invalid amount of the currency specified"); + } + DialogId boosted_dialog_id(p->boosted_chat_id_); + TRY_RESULT(boost_input_peer, get_boost_input_peer(td, boosted_dialog_id)); + if (boost_input_peer == nullptr) { + return Status::Error(400, "Boosted chat can't be empty"); + } + vector> additional_input_peers; + for (auto additional_chat_id : p->additional_chat_ids_) { + TRY_RESULT(input_peer, get_boost_input_peer(td, DialogId(additional_chat_id))); + if (input_peer == nullptr) { + return Status::Error(400, "Additional chat can't be empty"); + } + additional_input_peers.push_back(std::move(input_peer)); + } + int64 random_id; + do { + random_id = Random::secure_int64(); + } while (random_id == 0); + + int32 flags = 0; + if (p->only_new_subscribers_) { + flags |= telegram_api::inputStorePaymentPremiumGiveaway::ONLY_NEW_SUBSCRIBERS_MASK; + } + if (!additional_input_peers.empty()) { + flags |= telegram_api::inputStorePaymentPremiumGiveaway::ADDITIONAL_PEERS_MASK; + } + auto option = telegram_api::make_object( + 0, p->user_count_, p->month_count_, string(), 0, p->currency_, p->amount_); + auto purpose = telegram_api::make_object( + flags, false /*ignored*/, std::move(boost_input_peer), std::move(additional_input_peers), + vector(), random_id, p->date_, p->currency_, p->amount_); + + result.input_invoice_ = telegram_api::make_object( + std::move(purpose), std::move(option)); + break; + } + default: + UNREACHABLE(); + } + break; + } default: UNREACHABLE(); } diff --git a/td/telegram/Premium.cpp b/td/telegram/Premium.cpp index 59ad1dba0..13a08b09c 100644 --- a/td/telegram/Premium.cpp +++ b/td/telegram/Premium.cpp @@ -90,7 +90,7 @@ static td_api::object_ptr get_premium_feature_object(Sli return nullptr; } -static Result> get_boost_input_peer(Td *td, DialogId dialog_id) { +Result> get_boost_input_peer(Td *td, DialogId dialog_id) { if (dialog_id == DialogId()) { return nullptr; } diff --git a/td/telegram/Premium.h b/td/telegram/Premium.h index 02ab09394..90b692506 100644 --- a/td/telegram/Premium.h +++ b/td/telegram/Premium.h @@ -8,15 +8,19 @@ #include "td/telegram/DialogId.h" #include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" #include "td/utils/common.h" #include "td/utils/Promise.h" #include "td/utils/Slice.h" +#include "td/utils/Status.h" namespace td { class Td; +Result> get_boost_input_peer(Td *td, DialogId dialog_id); + const vector &get_premium_limit_keys(); void get_premium_limit(const td_api::object_ptr &limit_type, diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index b0d10ccf7..55bfca3fb 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3119,8 +3119,8 @@ class CliClient final : public Actor { boosted_chat_id, Auto(), static_cast(user_id) /*date*/, rand_bool(), currency, amount))); } else { send_request(td_api::make_object( - td_api::make_object(vector{user_id}, boosted_chat_id, - currency, amount))); + td_api::make_object(boosted_chat_id, currency, amount, + vector{user_id}))); } } else if (op == "atos") { send_request(td_api::make_object(args));