From 559e02a7e23a3fac1e4150fbcb947d7738df0c3d Mon Sep 17 00:00:00 2001 From: Mikhail Filimonov Date: Fri, 30 Aug 2024 09:12:21 -0300 Subject: [PATCH] - direct-bot giveaway payment --- .../Payments/BotPaymentForm.swift | 34 +++++++++++++++++++ .../TelegramEngine/Payments/Stars.swift | 8 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/BotPaymentForm.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/BotPaymentForm.swift index 5be3cc803b..6adeaca770 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/BotPaymentForm.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/BotPaymentForm.swift @@ -12,6 +12,7 @@ public enum BotPaymentInvoiceSource { case stars(option: StarsTopUpOption) case starsGift(peerId: EnginePeer.Id, count: Int64, currency: String, amount: Int64) case starsChatSubscription(hash: String) + case starsGiveaway(stars: Int64, boostPeer: EnginePeer.Id, additionalPeerIds: [EnginePeer.Id], countries: [String], onlyNewSubscribers: Bool, showWinners: Bool, prizeDescription: String?, randomId: Int64, untilDate: Int32, currency: String, amount: Int64, users: Int32) } public struct BotPaymentInvoiceFields: OptionSet { @@ -317,6 +318,33 @@ func _internal_parseInputInvoice(transaction: Transaction, source: BotPaymentInv return .inputInvoiceStars(purpose: .inputStorePaymentStarsGift(userId: inputUser, stars: count, currency: currency, amount: amount)) case let .starsChatSubscription(hash): return .inputInvoiceChatInviteSubscription(hash: hash) + case let .starsGiveaway(stars, boostPeerId, additionalPeerIds, countries, onlyNewSubscribers, showWinners, prizeDescription, randomId, untilDate, currency, amount, users): + guard let peer = transaction.getPeer(boostPeerId), let apiBoostPeer = apiInputPeer(peer) else { + return nil + } + var flags: Int32 = 0 + if onlyNewSubscribers { + flags |= (1 << 0) + } + if showWinners { + flags |= (1 << 3) + } + var additionalPeers: [Api.InputPeer] = [] + if !additionalPeerIds.isEmpty { + flags |= (1 << 1) + for peerId in additionalPeerIds { + if let peer = transaction.getPeer(peerId), let inputPeer = apiInputPeer(peer) { + additionalPeers.append(inputPeer) + } + } + } + if !countries.isEmpty { + flags |= (1 << 2) + } + if let _ = prizeDescription { + flags |= (1 << 4) + } + return .inputInvoiceStars(purpose: .inputStorePaymentStarsGiveaway(flags: flags, stars: stars, boostPeer: apiBoostPeer, additionalPeers: additionalPeers, countriesIso2: countries, prizeDescription: prizeDescription, randomId: randomId, untilDate: untilDate, currency: currency, amount: amount, users: users)) } } @@ -626,6 +654,12 @@ func _internal_sendBotPaymentForm(account: Account, formId: Int64, source: BotPa receiptMessageId = id } } + case let .starsGiveaway(_, _, _, _, _, _, _, randomId, _, _, _, _): + if message.globallyUniqueId == randomId { + if case let .Id(id) = message.id { + receiptMessageId = id + } + } case .giftCode, .stars, .starsGift, .starsChatSubscription: receiptMessageId = nil } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift index 33b1b9d536..4036abb1e6 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift @@ -205,7 +205,7 @@ public struct StarsGiveawayOption: Equatable, Codable { self.yearlyBoosts = yearlyBoosts self.currency = currency self.amount = amount - self.storeProductId = storeProductId + self.storeProductId = storeProductId?.replacingOccurrences(of: "telegram_stars.topup", with: "org.telegram.telegramStars.topup") self.winners = winners self.isExtended = isExtended self.isDefault = isDefault @@ -1278,6 +1278,12 @@ func _internal_sendStarsPaymentForm(account: Account, formId: Int64, source: Bot receiptMessageId = id } } + case let .starsGiveaway(_, _, _, _, _, _, _, randomId, _, _, _, _): + if message.globallyUniqueId == randomId { + if case let .Id(id) = message.id { + receiptMessageId = id + } + } case .giftCode, .stars, .starsGift: receiptMessageId = nil case .starsChatSubscription: