From f08ff9247083c72de12fbcf7a9c6039673054fb5 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 24 May 2024 20:54:13 +0300 Subject: [PATCH] Added initial ability to provide data for non-panel payment forms. --- .../payments/payments_checkout_process.cpp | 1 + .../payments/payments_checkout_process.h | 5 + .../SourceFiles/payments/payments_form.cpp | 37 ++++- Telegram/SourceFiles/payments/payments_form.h | 128 ++++++++++-------- 4 files changed, 113 insertions(+), 58 deletions(-) diff --git a/Telegram/SourceFiles/payments/payments_checkout_process.cpp b/Telegram/SourceFiles/payments/payments_checkout_process.cpp index 108376084..14e7d695d 100644 --- a/Telegram/SourceFiles/payments/payments_checkout_process.cpp +++ b/Telegram/SourceFiles/payments/payments_checkout_process.cpp @@ -381,6 +381,7 @@ void CheckoutProcess::handleFormUpdate(const FormUpdate &update) { if (weak) { closeAndReactivate(CheckoutResult::Paid); } + }, [&](const CreditsPaymentStarted &data) { }, [&](const Error &error) { handleError(error); }); diff --git a/Telegram/SourceFiles/payments/payments_checkout_process.h b/Telegram/SourceFiles/payments/payments_checkout_process.h index 9a8703927..e26fff3cd 100644 --- a/Telegram/SourceFiles/payments/payments_checkout_process.h +++ b/Telegram/SourceFiles/payments/payments_checkout_process.h @@ -40,6 +40,7 @@ struct Error; struct InvoiceCredits; struct InvoiceId; struct InvoicePremiumGiftCode; +struct CreditsFormData; enum class Mode { Payment, @@ -53,6 +54,10 @@ enum class CheckoutResult { Failed, }; +struct NonPanelPaymentForm : std::variant> { + using variant::variant; +}; + struct PaidInvoice { QString title; }; diff --git a/Telegram/SourceFiles/payments/payments_form.cpp b/Telegram/SourceFiles/payments/payments_form.cpp index b6512d532..f3283b570 100644 --- a/Telegram/SourceFiles/payments/payments_form.cpp +++ b/Telegram/SourceFiles/payments/payments_form.cpp @@ -376,7 +376,42 @@ void Form::requestForm() { MTP_dataJSON(MTP_bytes(Window::Theme::WebViewParams().json)) )).done([=](const MTPpayments_PaymentForm &result) { hideProgress(); - processForm(result); + result.match([&](const MTPDpayments_paymentForm &data) { + processForm(result); + }, [&](const MTPDpayments_paymentFormStars &data) { + _session->data().processUsers(data.vusers()); + const auto currency = qs(data.vinvoice().data().vcurrency()); + const auto &tlPrices = data.vinvoice().data().vprices().v; + const auto amount = tlPrices.empty() + ? 0 + : tlPrices.front().data().vamount().v; + if (currency != "XTR" || !amount) { + using Type = Error::Type; + _updates.fire(Error{ Type::Form, u"Bad Stars Form."_q }); + return; + } + const auto invoice = InvoiceCredits{ + .session = _session, + .randomId = 0, + .credits = amount, + .currency = currency, + .amount = amount, + }; + const auto formData = CreditsFormData{ + .formId = data.vform_id().v, + .botId = data.vbot_id().v, + .title = qs(data.vtitle()), + .description = qs(data.vdescription()), + .photo = data.vphoto() + ? _session->data().photoFromWeb( + *data.vphoto(), + ImageLocation()) + : nullptr, + .invoice = invoice, + .inputInvoice = inputInvoice(), + }; + _updates.fire(CreditsPaymentStarted{ .data = formData }); + }); }).fail([=](const MTP::Error &error) { hideProgress(); _updates.fire(Error{ Error::Type::Form, error.type() }); diff --git a/Telegram/SourceFiles/payments/payments_form.h b/Telegram/SourceFiles/payments/payments_form.h index ffa6b17d7..609af8213 100644 --- a/Telegram/SourceFiles/payments/payments_form.h +++ b/Telegram/SourceFiles/payments/payments_form.h @@ -120,63 +120,6 @@ struct PaymentMethod { Ui::PaymentMethodDetails ui; }; -struct ToggleProgress { - bool shown = true; -}; -struct FormReady {}; -struct ThumbnailUpdated { - QImage thumbnail; -}; -struct ValidateFinished {}; -struct PaymentMethodUpdate { - bool requestNewPassword = false; -}; -struct VerificationNeeded { - QString url; -}; -struct TmpPasswordRequired {}; -struct BotTrustRequired { - not_null bot; - not_null provider; -}; -struct PaymentFinished { - MTPUpdates updates; -}; -struct Error { - enum class Type { - None, - Form, - Validate, - Stripe, - SmartGlocal, - TmpPassword, - Send, - }; - Type type = Type::None; - QString id; - - [[nodiscard]] bool empty() const { - return (type == Type::None); - } - [[nodiscard]] explicit operator bool() const { - return !empty(); - } -}; - -struct FormUpdate : std::variant< - ToggleProgress, - FormReady, - ThumbnailUpdated, - ValidateFinished, - PaymentMethodUpdate, - VerificationNeeded, - TmpPasswordRequired, - BotTrustRequired, - PaymentFinished, - Error> { - using variant::variant; -}; - struct InvoiceMessage { not_null peer; MsgId itemId = 0; @@ -234,6 +177,77 @@ struct InvoiceId { InvoiceCredits> value; }; +struct CreditsFormData { + uint64 formId = 0; + uint64 botId = 0; + QString title; + QString description; + PhotoData *photo = nullptr; + InvoiceCredits invoice; + MTPInputInvoice inputInvoice; +}; + +struct ToggleProgress { + bool shown = true; +}; +struct FormReady {}; +struct ThumbnailUpdated { + QImage thumbnail; +}; +struct ValidateFinished {}; +struct PaymentMethodUpdate { + bool requestNewPassword = false; +}; +struct VerificationNeeded { + QString url; +}; +struct TmpPasswordRequired {}; +struct BotTrustRequired { + not_null bot; + not_null provider; +}; +struct PaymentFinished { + MTPUpdates updates; +}; +struct CreditsPaymentStarted { + CreditsFormData data; +}; +struct Error { + enum class Type { + None, + Form, + Validate, + Stripe, + SmartGlocal, + TmpPassword, + Send, + }; + Type type = Type::None; + QString id; + + [[nodiscard]] bool empty() const { + return (type == Type::None); + } + [[nodiscard]] explicit operator bool() const { + return !empty(); + } +}; + +struct FormUpdate : std::variant< + ToggleProgress, + FormReady, + ThumbnailUpdated, + ValidateFinished, + PaymentMethodUpdate, + VerificationNeeded, + TmpPasswordRequired, + BotTrustRequired, + PaymentFinished, + CreditsPaymentStarted, + Error> { + using variant::variant; +}; + [[nodiscard]] not_null SessionFromId(const InvoiceId &id); [[nodiscard]] MTPinputStorePaymentPurpose InvoicePremiumGiftCodeGiveawayToTL(