From b20caee5480ea9f5011ea4f68b1aab70d84c38b8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 18 May 2025 14:04:32 +0400 Subject: [PATCH] Check if the resale gift price changed. --- Telegram/Resources/langs/lang.strings | 2 + Telegram/SourceFiles/boxes/star_gift_box.cpp | 110 +++++++++--------- Telegram/SourceFiles/boxes/star_gift_box.h | 13 +++ .../SourceFiles/boxes/transfer_gift_box.cpp | 45 ++++++- 4 files changed, 110 insertions(+), 60 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index f5fbafa589..40c9c2e782 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -3473,6 +3473,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_gift_buy_resale_button" = "Buy for {cost}"; "lng_gift_buy_resale_confirm" = "Do you want to buy {name} for {price} and gift it to {user}?"; "lng_gift_buy_resale_confirm_self" = "Do you want to buy {name} for {price}?"; +"lng_gift_buy_price_change_title" = "Price change!"; +"lng_gift_buy_price_change_text" = "This gift price was changed and now is {price}. Do you still want to buy?"; "lng_gift_sent_title" = "Gift Sent!"; "lng_gift_sent_resale_done" = "{user} has been notified about your gift."; "lng_gift_sent_resale_done_self" = "{gift} is now yours."; diff --git a/Telegram/SourceFiles/boxes/star_gift_box.cpp b/Telegram/SourceFiles/boxes/star_gift_box.cpp index ed44a58653..d021cba69f 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/star_gift_box.cpp @@ -1954,61 +1954,6 @@ void SendStarsFormRequest( } } -void SubmitStarsForm( - std::shared_ptr show, - MTPInputInvoice invoice, - uint64 formId, - uint64 price, - Fn done) { - const auto ready = [=](Settings::SmallBalanceResult result) { - SendStarsFormRequest(show, result, formId, invoice, done); - }; - Settings::MaybeRequestBalanceIncrease( - show, - price, - Settings::SmallBalanceDeepLink{}, - ready); -} - -void RequestStarsForm( - std::shared_ptr show, - MTPInputInvoice invoice, - Fn failure)> done) { - const auto fail = [=](Payments::CheckoutResult failure) { - done(0, 0, failure); - }; - show->session().api().request(MTPpayments_GetPaymentForm( - MTP_flags(0), - invoice, - MTPDataJSON() // theme_params - )).done([=](const MTPpayments_PaymentForm &result) { - result.match([&](const MTPDpayments_paymentFormStarGift &data) { - const auto prices = data.vinvoice().data().vprices().v; - if (show->valid() && !prices.isEmpty()) { - const auto price = prices.front().data().vamount().v; - done(data.vform_id().v, price, std::nullopt); - } else { - fail(Payments::CheckoutResult::Failed); - } - }, [&](const auto &) { - fail(Payments::CheckoutResult::Failed); - }); - }).fail([=](const MTP::Error &error) { - const auto type = error.type(); - if (type == u"STARGIFT_EXPORT_IN_PROGRESS"_q) { - fail(Payments::CheckoutResult::Cancelled); - } else if (type == u"NO_PAYMENT_NEEDED"_q) { - fail(Payments::CheckoutResult::Free); - } else { - show->showToast(type); - fail(Payments::CheckoutResult::Failed); - } - }).send(); -} - void UpgradeGift( not_null window, Data::SavedStarGiftId savedId, @@ -4891,6 +4836,61 @@ void AddUniqueCloseButton( } } +void SubmitStarsForm( + std::shared_ptr show, + MTPInputInvoice invoice, + uint64 formId, + uint64 price, + Fn done) { + const auto ready = [=](Settings::SmallBalanceResult result) { + SendStarsFormRequest(show, result, formId, invoice, done); + }; + Settings::MaybeRequestBalanceIncrease( + show, + price, + Settings::SmallBalanceDeepLink{}, + ready); +} + +void RequestStarsForm( + std::shared_ptr show, + MTPInputInvoice invoice, + Fn failure)> done) { + const auto fail = [=](Payments::CheckoutResult failure) { + done(0, 0, failure); + }; + show->session().api().request(MTPpayments_GetPaymentForm( + MTP_flags(0), + invoice, + MTPDataJSON() // theme_params + )).done([=](const MTPpayments_PaymentForm &result) { + result.match([&](const MTPDpayments_paymentFormStarGift &data) { + const auto prices = data.vinvoice().data().vprices().v; + if (show->valid() && !prices.isEmpty()) { + const auto price = prices.front().data().vamount().v; + done(data.vform_id().v, price, std::nullopt); + } else { + fail(Payments::CheckoutResult::Failed); + } + }, [&](const auto &) { + fail(Payments::CheckoutResult::Failed); + }); + }).fail([=](const MTP::Error &error) { + const auto type = error.type(); + if (type == u"STARGIFT_EXPORT_IN_PROGRESS"_q) { + fail(Payments::CheckoutResult::Cancelled); + } else if (type == u"NO_PAYMENT_NEEDED"_q) { + fail(Payments::CheckoutResult::Free); + } else { + show->showToast(type); + fail(Payments::CheckoutResult::Failed); + } + }).send(); +} + void RequestStarsFormAndSubmit( std::shared_ptr show, MTPInputInvoice invoice, diff --git a/Telegram/SourceFiles/boxes/star_gift_box.h b/Telegram/SourceFiles/boxes/star_gift_box.h index 7137e506d0..d26270d1a4 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.h +++ b/Telegram/SourceFiles/boxes/star_gift_box.h @@ -118,6 +118,19 @@ void AddUniqueCloseButton( Settings::CreditsEntryBoxStyleOverrides st, Fn)> fillMenu = nullptr); +void SubmitStarsForm( + std::shared_ptr show, + MTPInputInvoice invoice, + uint64 formId, + uint64 price, + Fn done); +void RequestStarsForm( + std::shared_ptr show, + MTPInputInvoice invoice, + Fn failure)> done); void RequestStarsFormAndSubmit( std::shared_ptr show, MTPInputInvoice invoice, diff --git a/Telegram/SourceFiles/boxes/transfer_gift_box.cpp b/Telegram/SourceFiles/boxes/transfer_gift_box.cpp index 0d17f6f4fd..6566f68703 100644 --- a/Telegram/SourceFiles/boxes/transfer_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/transfer_gift_box.cpp @@ -489,7 +489,7 @@ void BuyResaleGift( not_null to, std::shared_ptr gift, Fn done) { - auto formDone = [=]( + auto paymentDone = [=]( Payments::CheckoutResult result, const MTPUpdates *updates) { done(result); @@ -502,10 +502,45 @@ void BuyResaleGift( Ui::ShowResaleGiftBoughtToast(show, to, *gift); } }; - Ui::RequestStarsFormAndSubmit( - show, - MTP_inputInvoiceStarGiftResale(MTP_string(gift->slug), to->input), - std::move(formDone)); + + const auto invoice = MTP_inputInvoiceStarGiftResale( + MTP_string(gift->slug), + to->input); + + Ui::RequestStarsForm(show, invoice, [=]( + uint64 formId, + uint64 price, + std::optional failure) { + const auto submit = [=] { + SubmitStarsForm(show, invoice, formId, price, paymentDone); + }; + if (failure) { + paymentDone(*failure, nullptr); + } else if (price != gift->starsForResale) { + const auto cost = Ui::Text::IconEmoji(&st::starIconEmoji).append( + Lang::FormatCountDecimal(price)); + const auto cancelled = [=](Fn close) { + paymentDone(Payments::CheckoutResult::Cancelled, nullptr); + close(); + }; + show->show(Ui::MakeConfirmBox({ + .text = tr::lng_gift_buy_price_change_text( + tr::now, + lt_price, + Ui::Text::Wrapped(cost, EntityType::Bold), + Ui::Text::WithEntities), + .confirmed = [=](Fn close) { close(); submit(); }, + .cancelled = cancelled, + .confirmText = tr::lng_gift_buy_resale_button( + lt_cost, + rpl::single(cost), + Ui::Text::WithEntities), + .title = tr::lng_gift_buy_price_change_title(), + })); + } else { + submit(); + } + }); } } // namespace