api_premium_option.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "api/api_premium_option.h"
  8. #include "ui/text/format_values.h"
  9. namespace Api {
  10. constexpr auto kDiscountDivider = 1.;
  11. Data::PremiumSubscriptionOption CreateSubscriptionOption(
  12. int months,
  13. int monthlyAmount,
  14. int64 amount,
  15. const QString &currency,
  16. const QString &botUrl) {
  17. const auto discount = [&] {
  18. const auto percent = 1. - float64(amount) / (monthlyAmount * months);
  19. return std::round(percent * 100. / kDiscountDivider)
  20. * kDiscountDivider;
  21. }();
  22. return {
  23. .duration = Ui::FormatTTL(months * 86400 * 31),
  24. .discount = (discount > 0)
  25. ? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount)
  26. : QString(),
  27. .costPerMonth = Ui::FillAmountAndCurrency(
  28. amount / float64(months),
  29. currency),
  30. .costTotal = Ui::FillAmountAndCurrency(amount, currency),
  31. .botUrl = botUrl,
  32. };
  33. }
  34. } // namespace Api