mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added api support of invoice payments for credits.
This commit is contained in:
parent
d0bfee6963
commit
9b11b95c5b
5 changed files with 86 additions and 1 deletions
|
@ -139,6 +139,28 @@ void CheckoutProcess::Start(
|
||||||
j->second->requestActivate();
|
j->second->requestActivate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckoutProcess::Start(
|
||||||
|
InvoiceCredits creditsInvoice,
|
||||||
|
Fn<void(CheckoutResult)> reactivate) {
|
||||||
|
const auto randomId = creditsInvoice.randomId;
|
||||||
|
auto id = InvoiceId{ std::move(creditsInvoice) };
|
||||||
|
auto &processes = LookupSessionProcesses(SessionFromId(id));
|
||||||
|
const auto i = processes.byRandomId.find(randomId);
|
||||||
|
if (i != end(processes.byRandomId)) {
|
||||||
|
i->second->setReactivateCallback(std::move(reactivate));
|
||||||
|
i->second->requestActivate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto j = processes.byRandomId.emplace(
|
||||||
|
randomId,
|
||||||
|
std::make_unique<CheckoutProcess>(
|
||||||
|
std::move(id),
|
||||||
|
Mode::Payment,
|
||||||
|
std::move(reactivate),
|
||||||
|
PrivateTag{})).first;
|
||||||
|
j->second->requestActivate();
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<PaidInvoice> CheckoutProcess::InvoicePaid(
|
std::optional<PaidInvoice> CheckoutProcess::InvoicePaid(
|
||||||
not_null<const HistoryItem*> item) {
|
not_null<const HistoryItem*> item) {
|
||||||
const auto session = &item->history()->session();
|
const auto session = &item->history()->session();
|
||||||
|
|
|
@ -37,6 +37,7 @@ namespace Payments {
|
||||||
class Form;
|
class Form;
|
||||||
struct FormUpdate;
|
struct FormUpdate;
|
||||||
struct Error;
|
struct Error;
|
||||||
|
struct InvoiceCredits;
|
||||||
struct InvoiceId;
|
struct InvoiceId;
|
||||||
struct InvoicePremiumGiftCode;
|
struct InvoicePremiumGiftCode;
|
||||||
|
|
||||||
|
@ -73,6 +74,9 @@ public:
|
||||||
static void Start(
|
static void Start(
|
||||||
InvoicePremiumGiftCode giftCodeInvoice,
|
InvoicePremiumGiftCode giftCodeInvoice,
|
||||||
Fn<void(CheckoutResult)> reactivate);
|
Fn<void(CheckoutResult)> reactivate);
|
||||||
|
static void Start(
|
||||||
|
InvoiceCredits creditsInvoice,
|
||||||
|
Fn<void(CheckoutResult)> reactivate);
|
||||||
[[nodiscard]] static std::optional<PaidInvoice> InvoicePaid(
|
[[nodiscard]] static std::optional<PaidInvoice> InvoicePaid(
|
||||||
not_null<const HistoryItem*> item);
|
not_null<const HistoryItem*> item);
|
||||||
[[nodiscard]] static std::optional<PaidInvoice> InvoicePaid(
|
[[nodiscard]] static std::optional<PaidInvoice> InvoicePaid(
|
||||||
|
|
|
@ -117,6 +117,8 @@ not_null<Main::Session*> SessionFromId(const InvoiceId &id) {
|
||||||
return &message->peer->session();
|
return &message->peer->session();
|
||||||
} else if (const auto slug = std::get_if<InvoiceSlug>(&id.value)) {
|
} else if (const auto slug = std::get_if<InvoiceSlug>(&id.value)) {
|
||||||
return slug->session;
|
return slug->session;
|
||||||
|
} else if (const auto slug = std::get_if<InvoiceCredits>(&id.value)) {
|
||||||
|
return slug->session;
|
||||||
}
|
}
|
||||||
const auto &giftCode = v::get<InvoicePremiumGiftCode>(id.value);
|
const auto &giftCode = v::get<InvoicePremiumGiftCode>(id.value);
|
||||||
const auto users = std::get_if<InvoicePremiumGiftCodeUsers>(
|
const auto users = std::get_if<InvoicePremiumGiftCodeUsers>(
|
||||||
|
@ -314,6 +316,15 @@ MTPInputInvoice Form::inputInvoice() const {
|
||||||
MTP_int(message->itemId.bare));
|
MTP_int(message->itemId.bare));
|
||||||
} else if (const auto slug = std::get_if<InvoiceSlug>(&_id.value)) {
|
} else if (const auto slug = std::get_if<InvoiceSlug>(&_id.value)) {
|
||||||
return MTP_inputInvoiceSlug(MTP_string(slug->slug));
|
return MTP_inputInvoiceSlug(MTP_string(slug->slug));
|
||||||
|
} else if (const auto credits = std::get_if<InvoiceCredits>(&_id.value)) {
|
||||||
|
return MTP_inputInvoiceStars(MTP_starsTopupOption(
|
||||||
|
credits->product.isEmpty()
|
||||||
|
? MTP_flags(0)
|
||||||
|
: MTP_flags(MTPDstarsTopupOption::Flag::f_store_product),
|
||||||
|
MTP_long(credits->credits),
|
||||||
|
MTP_string(credits->product),
|
||||||
|
MTP_string(credits->currency),
|
||||||
|
MTP_long(credits->amount)));
|
||||||
}
|
}
|
||||||
const auto &giftCode = v::get<InvoicePremiumGiftCode>(_id.value);
|
const auto &giftCode = v::get<InvoicePremiumGiftCode>(_id.value);
|
||||||
using Flag = MTPDpremiumGiftCodeOption::Flag;
|
using Flag = MTPDpremiumGiftCodeOption::Flag;
|
||||||
|
|
|
@ -216,8 +216,21 @@ struct InvoicePremiumGiftCode {
|
||||||
int months = 0;
|
int months = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct InvoiceCredits {
|
||||||
|
not_null<Main::Session*> session;
|
||||||
|
uint64 randomId = 0;
|
||||||
|
uint64 credits = 0;
|
||||||
|
QString product;
|
||||||
|
QString currency;
|
||||||
|
uint64 amount = 0;
|
||||||
|
};
|
||||||
|
|
||||||
struct InvoiceId {
|
struct InvoiceId {
|
||||||
std::variant<InvoiceMessage, InvoiceSlug, InvoicePremiumGiftCode> value;
|
std::variant<
|
||||||
|
InvoiceMessage,
|
||||||
|
InvoiceSlug,
|
||||||
|
InvoicePremiumGiftCode,
|
||||||
|
InvoiceCredits> value;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[nodiscard]] not_null<Main::Session*> SessionFromId(const InvoiceId &id);
|
[[nodiscard]] not_null<Main::Session*> SessionFromId(const InvoiceId &id);
|
||||||
|
|
|
@ -13,7 +13,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "info/settings/info_settings_widget.h" // SectionCustomTopBarData.
|
#include "info/settings/info_settings_widget.h" // SectionCustomTopBarData.
|
||||||
#include "lang/lang_keys.h"
|
#include "lang/lang_keys.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
|
#include "payments/payments_checkout_process.h"
|
||||||
|
#include "payments/payments_form.h"
|
||||||
#include "settings/settings_common_session.h"
|
#include "settings/settings_common_session.h"
|
||||||
|
#include "ui/boxes/boost_box.h" // Ui::StartFireworks.
|
||||||
#include "ui/effects/premium_graphics.h"
|
#include "ui/effects/premium_graphics.h"
|
||||||
#include "ui/effects/premium_top_bar.h"
|
#include "ui/effects/premium_top_bar.h"
|
||||||
#include "ui/image/image_prepare.h"
|
#include "ui/image/image_prepare.h"
|
||||||
|
@ -33,6 +36,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "styles/style_layers.h"
|
#include "styles/style_layers.h"
|
||||||
#include "styles/style_settings.h"
|
#include "styles/style_settings.h"
|
||||||
|
|
||||||
|
#include <xxhash.h> // XXH64.
|
||||||
|
|
||||||
#include <QtSvg/QSvgRenderer>
|
#include <QtSvg/QSvgRenderer>
|
||||||
|
|
||||||
namespace Settings {
|
namespace Settings {
|
||||||
|
@ -40,6 +45,16 @@ namespace {
|
||||||
|
|
||||||
using SectionCustomTopBarData = Info::Settings::SectionCustomTopBarData;
|
using SectionCustomTopBarData = Info::Settings::SectionCustomTopBarData;
|
||||||
|
|
||||||
|
[[nodiscard]] uint64 UniqueIdFromOption(
|
||||||
|
const Data::CreditTopupOption &d) {
|
||||||
|
const auto string = QString::number(d.credits)
|
||||||
|
+ d.product
|
||||||
|
+ d.currency
|
||||||
|
+ QString::number(d.amount);
|
||||||
|
|
||||||
|
return XXH64(string.data(), string.size() * sizeof(ushort), 0);
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] QImage GenerateStarForLightTopBar(QRectF rect) {
|
[[nodiscard]] QImage GenerateStarForLightTopBar(QRectF rect) {
|
||||||
const auto strokeWidth = 3;
|
const auto strokeWidth = 3;
|
||||||
|
|
||||||
|
@ -177,6 +192,26 @@ void Credits::setupOptions(not_null<Ui::VerticalLayout*> container) {
|
||||||
icon->moveToLeft(st.iconLeft, st.padding.top());
|
icon->moveToLeft(st.iconLeft, st.padding.top());
|
||||||
}, button->lifetime());
|
}, button->lifetime());
|
||||||
button->setClickedCallback([=] {
|
button->setClickedCallback([=] {
|
||||||
|
const auto invoice = Payments::InvoiceCredits{
|
||||||
|
.session = &_controller->session(),
|
||||||
|
.randomId = UniqueIdFromOption(option),
|
||||||
|
.credits = option.credits,
|
||||||
|
.product = option.product,
|
||||||
|
.currency = option.currency,
|
||||||
|
.amount = option.amount,
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto weak = Ui::MakeWeak(button);
|
||||||
|
const auto done = [=](Payments::CheckoutResult result) {
|
||||||
|
if (const auto strong = weak.data()) {
|
||||||
|
strong->window()->setFocus();
|
||||||
|
if (result == Payments::CheckoutResult::Paid) {
|
||||||
|
Ui::StartFireworks(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Payments::CheckoutProcess::Start(std::move(invoice), done);
|
||||||
});
|
});
|
||||||
Ui::ToggleChildrenVisibility(button, true);
|
Ui::ToggleChildrenVisibility(button, true);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue