From 54c052adce955894805094f3391be87c0d037656 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 1 Jul 2022 19:27:59 +0300 Subject: [PATCH] Add td_api::StorePaymentPurpose. --- td/generate/scheme/td_api.tl | 17 +++++++--- td/telegram/Premium.cpp | 63 ++++++++++++++++++++++++++++-------- td/telegram/Premium.h | 6 ++-- td/telegram/Td.cpp | 8 ++--- td/telegram/Td.h | 2 +- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 85534c05a..c18a3dccb 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3018,6 +3018,15 @@ premiumFeaturePromotionAnimation feature:PremiumFeature animation:animation = Pr premiumState state:formattedText currency:string monthly_amount:int53 animations:vector = PremiumState; +//@class StorePaymentPurpose @description Describes a purpose of an in-store payment + +//@description The user subscribed to Telegram Premium @is_restore Pass true if this is a restore of a Telegram Premium purchase; only for App Store +storePaymentPurposePremiumSubscription is_restore:Bool = StorePaymentPurpose; + +//@description The user gifted Telegram Premium to another user @user_id Identifier of the user for which Premium was gifted @currency ISO 4217 currency code of the payment currency @amount Paid amount, in the smallest units of the currency +storePaymentPurposeGiftedPremium user_id:int53 currency:string amount:int64 = StorePaymentPurpose; + + //@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 //@description A token for Firebase Cloud Messaging @token Device registration token; may be empty to deregister a device @encrypt True, if push notifications must be additionally encrypted @@ -6420,11 +6429,11 @@ getPremiumState = PremiumState; //@description Checks whether Telegram Premium purchase is possible. Must be called before in-store Premium purchase canPurchasePremium = Ok; -//@description Informs server about a Telegram Premium purchase through App Store. For official applications only @receipt App Store receipt @is_restore Pass true if this is a restore of a Telegram Premium purchase -assignAppStoreTransaction receipt:bytes is_restore:Bool = Ok; +//@description Informs server about a purchase through App Store. For official applications only @receipt App Store receipt @purpose Transaction purpose +assignAppStoreTransaction receipt:bytes purpose:StorePaymentPurpose = Ok; -//@description Informs server about a Telegram Premium purchase through Google Play. For official applications only @purchase_token Google Play purchase token -assignGooglePlayTransaction purchase_token:string = Ok; +//@description Informs server about a purchase through Google Play. For official applications only @purchase_token Google Play purchase token @purpose Transaction purpose +assignGooglePlayTransaction purchase_token:string purpose:StorePaymentPurpose = Ok; //@description Accepts Telegram terms of services @terms_of_service_id Terms of service identifier diff --git a/td/telegram/Premium.cpp b/td/telegram/Premium.cpp index feb598ebc..a1d08b48c 100644 --- a/td/telegram/Premium.cpp +++ b/td/telegram/Premium.cpp @@ -67,6 +67,34 @@ static td_api::object_ptr get_premium_feature_object(Sli return nullptr; } +static Result> get_input_store_payment_purpose( + Td *td, const td_api::object_ptr &purpose) { + if (purpose == nullptr) { + return Status::Error(400, "Purchase purpose must be non-empty"); + } + + switch (purpose->get_id()) { + case td_api::storePaymentPurposePremiumSubscription::ID: { + auto p = static_cast(purpose.get()); + int32 flags = 0; + if (p->is_restore_) { + flags |= telegram_api::inputStorePaymentPremiumSubscription::RESTORE_MASK; + } + return make_tl_object(flags, false /*ignored*/); + } + case td_api::storePaymentPurposeGiftedPremium::ID: { + auto p = static_cast(purpose.get()); + UserId user_id(p->user_id_); + TRY_RESULT(input_user, td->contacts_manager_->get_input_user(user_id)); + return make_tl_object(std::move(input_user), p->currency_, + p->amount_); + } + default: + UNREACHABLE(); + return nullptr; + } +} + class GetPremiumPromoQuery final : public Td::ResultHandler { Promise> promise_; @@ -178,14 +206,14 @@ class AssignAppStoreTransactionQuery final : public Td::ResultHandler { explicit AssignAppStoreTransactionQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(const string &receipt, bool is_restore) { - int32 flags = 0; - if (is_restore) { - flags |= telegram_api::inputStorePaymentPremiumSubscription::RESTORE_MASK; + void send(const string &receipt, td_api::object_ptr &&purpose) { + auto r_input_purpose = get_input_store_payment_purpose(td_, purpose); + if (r_input_purpose.is_error()) { + return on_error(r_input_purpose.move_as_error()); } - send_query(G()->net_query_creator().create(telegram_api::payments_assignAppStoreTransaction( - BufferSlice(receipt), - make_tl_object(flags, false /*ignored*/)))); + + send_query(G()->net_query_creator().create( + telegram_api::payments_assignAppStoreTransaction(BufferSlice(receipt), r_input_purpose.move_as_ok()))); } void on_result(BufferSlice packet) final { @@ -211,12 +239,16 @@ class AssignPlayMarketTransactionQuery final : public Td::ResultHandler { explicit AssignPlayMarketTransactionQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(const string &purchase_token) { + void send(const string &purchase_token, td_api::object_ptr &&purpose) { + auto r_input_purpose = get_input_store_payment_purpose(td_, purpose); + if (r_input_purpose.is_error()) { + return on_error(r_input_purpose.move_as_error()); + } auto receipt = make_tl_object(string()); receipt->data_ = json_encode(json_object([&purchase_token](auto &o) { o("purchase_token", purchase_token); })); - send_query(G()->net_query_creator().create(telegram_api::payments_assignPlayMarketTransaction( - std::move(receipt), make_tl_object(0, false /*ignored*/)))); + send_query(G()->net_query_creator().create( + telegram_api::payments_assignPlayMarketTransaction(std::move(receipt), r_input_purpose.move_as_ok()))); } void on_result(BufferSlice packet) final { @@ -478,12 +510,15 @@ void can_purchase_premium(Td *td, Promise &&promise) { td->create_handler(std::move(promise))->send(); } -void assign_app_store_transaction(Td *td, const string &receipt, bool is_restore, Promise &&promise) { - td->create_handler(std::move(promise))->send(receipt, is_restore); +void assign_app_store_transaction(Td *td, const string &receipt, + td_api::object_ptr &&purpose, Promise &&promise) { + td->create_handler(std::move(promise))->send(receipt, std::move(purpose)); } -void assign_play_market_transaction(Td *td, const string &purchase_token, Promise &&promise) { - td->create_handler(std::move(promise))->send(purchase_token); +void assign_play_market_transaction(Td *td, const string &purchase_token, + td_api::object_ptr &&purpose, + Promise &&promise) { + td->create_handler(std::move(promise))->send(purchase_token, std::move(purpose)); } } // namespace td diff --git a/td/telegram/Premium.h b/td/telegram/Premium.h index fa97a471d..aa16927ec 100644 --- a/td/telegram/Premium.h +++ b/td/telegram/Premium.h @@ -32,8 +32,10 @@ void get_premium_state(Td *td, Promise> void can_purchase_premium(Td *td, Promise &&promise); -void assign_app_store_transaction(Td *td, const string &receipt, bool is_restore, Promise &&promise); +void assign_app_store_transaction(Td *td, const string &receipt, + td_api::object_ptr &&purpose, Promise &&promise); -void assign_play_market_transaction(Td *td, const string &purchase_token, Promise &&promise); +void assign_play_market_transaction(Td *td, const string &purchase_token, + td_api::object_ptr &&purpose, Promise &&promise); } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 1b0d8627e..e92c07837 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5196,7 +5196,7 @@ void Td::on_request(uint64 id, const td_api::clickAnimatedEmojiMessage &request) } void Td::on_request(uint64 id, const td_api::getInternalLinkType &request) { - auto type = link_manager_->parse_internal_link(request.link_); + auto type = LinkManager::parse_internal_link(request.link_); send_closure(actor_id(this), &Td::send_result, id, type == nullptr ? nullptr : type->get_internal_link_type_object()); } @@ -7918,17 +7918,17 @@ void Td::on_request(uint64 id, const td_api::canPurchasePremium &request) { can_purchase_premium(this, std::move(promise)); } -void Td::on_request(uint64 id, const td_api::assignAppStoreTransaction &request) { +void Td::on_request(uint64 id, td_api::assignAppStoreTransaction &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - assign_app_store_transaction(this, request.receipt_, request.is_restore_, std::move(promise)); + assign_app_store_transaction(this, request.receipt_, std::move(request.purpose_), std::move(promise)); } void Td::on_request(uint64 id, td_api::assignGooglePlayTransaction &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.purchase_token_); CREATE_OK_REQUEST_PROMISE(); - assign_play_market_transaction(this, request.purchase_token_, std::move(promise)); + assign_play_market_transaction(this, request.purchase_token_, std::move(request.purpose_), std::move(promise)); } void Td::on_request(uint64 id, td_api::acceptTermsOfService &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index e67af1ae9..5abc5b7f9 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1309,7 +1309,7 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::canPurchasePremium &request); - void on_request(uint64 id, const td_api::assignAppStoreTransaction &request); + void on_request(uint64 id, td_api::assignAppStoreTransaction &request); void on_request(uint64 id, td_api::assignGooglePlayTransaction &request);