From 20605f590896e9513903cc0714acb642320ed114 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 17 May 2024 13:47:07 +0300 Subject: [PATCH] Allow to pay in Telegram Stars. --- td/generate/scheme/td_api.tl | 2 +- td/telegram/Payments.cpp | 57 +++++++++++++++++++++++++++++++++++- td/telegram/cli.cpp | 6 ++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index f59d010ee..820a5febe 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -10327,7 +10327,7 @@ validateOrderInfo input_invoice:InputInvoice order_info:orderInfo allow_save:Boo //@payment_form_id Payment form identifier returned by getPaymentForm //@order_info_id Identifier returned by validateOrderInfo, or an empty string //@shipping_option_id Identifier of a chosen shipping option, if applicable -//@credentials The credentials chosen by user for payment +//@credentials The credentials chosen by user for payment; pass null for a payment in Telegram stars //@tip_amount Chosen by the user amount of tip in the smallest units of the currency sendPaymentForm input_invoice:InputInvoice payment_form_id:int64 order_info_id:string shipping_option_id:string credentials:InputCredentials tip_amount:int53 = PaymentResult; diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index 0fe3ce39a..e4b77a18f 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -638,6 +638,56 @@ class SendPaymentFormQuery final : public Td::ResultHandler { } }; +class SendStarPaymentFormQuery final : public Td::ResultHandler { + Promise> promise_; + DialogId dialog_id_; + + public: + explicit SendStarPaymentFormQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(InputInvoiceInfo &&input_invoice_info, int64 payment_form_id) { + dialog_id_ = input_invoice_info.dialog_id_; + + send_query(G()->net_query_creator().create( + telegram_api::payments_sendStarsForm(0, payment_form_id, std::move(input_invoice_info.input_invoice_)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto payment_result = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for SendPaymentFormQuery: " << to_string(payment_result); + + switch (payment_result->get_id()) { + case telegram_api::payments_paymentResult::ID: { + auto result = move_tl_object_as(payment_result); + td_->updates_manager_->on_get_updates( + std::move(result->updates_), PromiseCreator::lambda([promise = std::move(promise_)](Unit) mutable { + promise.set_value(make_tl_object(true, string())); + })); + return; + } + case telegram_api::payments_paymentVerificationNeeded::ID: { + auto result = move_tl_object_as(payment_result); + promise_.set_value(make_tl_object(false, std::move(result->url_))); + return; + } + default: + UNREACHABLE(); + } + } + + void on_error(Status status) final { + td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "SendPaymentFormQuery"); + promise_.set_error(std::move(status)); + } +}; + class GetPaymentReceiptQuery final : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; @@ -966,7 +1016,12 @@ void send_payment_form(Td *td, td_api::object_ptr &&input_ TRY_RESULT_PROMISE(promise, input_invoice_info, get_input_invoice_info(td, std::move(input_invoice))); if (credentials == nullptr) { - return promise.set_error(Status::Error(400, "Input payment credentials must be non-empty")); + if (tip_amount != 0 || !order_info_id.empty() || !shipping_option_id.empty()) { + return promise.set_error(Status::Error(400, "Invalid payment form parameters specified")); + } + td->create_handler(std::move(promise)) + ->send(std::move(input_invoice_info), payment_form_id); + return; } tl_object_ptr input_credentials; diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index a4c0af89b..7189fc658 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2739,6 +2739,12 @@ class CliClient final : public Actor { send_request(td_api::make_object( input_invoice, payment_form_id, order_info_id, shipping_option_id, td_api::make_object(data, true), tip_amount)); + } else if (op == "spfstar") { + InputInvoice input_invoice; + int64 payment_form_id; + get_args(args, input_invoice, payment_form_id); + send_request( + td_api::make_object(input_invoice, payment_form_id, string(), string(), nullptr, 0)); } else if (op == "gpre") { ChatId chat_id; MessageId message_id;