giftcodes for invoice

This commit is contained in:
Mike Renoir 2023-10-17 13:15:19 +04:00
parent e48319be5c
commit 1b50b16bee
3 changed files with 64 additions and 52 deletions

View File

@ -7,8 +7,10 @@ import TelegramApi
public enum BotPaymentInvoiceSource {
case message(MessageId)
case slug(String)
case premiumGiftCode(peerIds: [EnginePeer.Id], boostPeer: EnginePeer.Id?, quantity: Int32, option: PremiumGiftCodeOption)
}
public struct BotPaymentInvoiceFields: OptionSet {
public var rawValue: Int32
@ -203,17 +205,51 @@ extension BotPaymentRequestedInfo {
}
}
private func _internal_parseInputInvoice(transaction: Transaction, source: BotPaymentInvoiceSource) -> Api.InputInvoice? {
switch source {
case let .message(messageId):
guard let inputPeer = transaction.getPeer(messageId.peerId).flatMap(apiInputPeer) else {
return nil
}
return .inputInvoiceMessage(peer: inputPeer, msgId: messageId.id)
case let .slug(slug):
return .inputInvoiceSlug(slug: slug)
case let .premiumGiftCode(peerIds, boostPeerId, quantity, option):
var flags: Int32 = 0
var apiBoostPeer: Api.InputPeer?
var apiInputUsers: [Api.InputUser] = []
for peerId in peerIds {
if let user = transaction.getPeer(peerId), let apiUser = apiInputUser(user) {
apiInputUsers.append(apiUser)
}
}
if let boostPeerId = boostPeerId, let boostPeer = transaction.getPeer(boostPeerId), let apiPeer = apiInputPeer(boostPeer) {
apiBoostPeer = apiPeer
flags |= (1 << 0)
}
let input: Api.InputStorePaymentPurpose = .inputStorePaymentPremiumGiftCode(flags: flags, users: apiInputUsers, boostPeer: apiBoostPeer, currency: option.currency, amount: option.amount)
flags = 0
if let _ = option.storeProductId {
flags |= (1 << 0)
}
if option.storeQuantity > 0 {
flags |= (1 << 1)
}
let option: Api.PremiumGiftCodeOption = .premiumGiftCodeOption(flags: flags, users: quantity, months: option.months, storeProduct: option.storeProductId, storeQuantity: option.storeQuantity, currency: option.currency, amount: option.amount)
return .inputInvoicePremiumGiftCode(purpose: input, option: option)
}
}
func _internal_fetchBotPaymentInvoice(postbox: Postbox, network: Network, source: BotPaymentInvoiceSource) -> Signal<TelegramMediaInvoice, BotPaymentFormRequestError> {
return postbox.transaction { transaction -> Api.InputInvoice? in
switch source {
case let .message(messageId):
guard let inputPeer = transaction.getPeer(messageId.peerId).flatMap(apiInputPeer) else {
return nil
}
return .inputInvoiceMessage(peer: inputPeer, msgId: messageId.id)
case let .slug(slug):
return .inputInvoiceSlug(slug: slug)
}
return _internal_parseInputInvoice(transaction: transaction, source: source)
}
|> castError(BotPaymentFormRequestError.self)
|> mapToSignal { invoice -> Signal<TelegramMediaInvoice, BotPaymentFormRequestError> in
@ -251,15 +287,7 @@ func _internal_fetchBotPaymentInvoice(postbox: Postbox, network: Network, source
func _internal_fetchBotPaymentForm(accountPeerId: PeerId, postbox: Postbox, network: Network, source: BotPaymentInvoiceSource, themeParams: [String: Any]?) -> Signal<BotPaymentForm, BotPaymentFormRequestError> {
return postbox.transaction { transaction -> Api.InputInvoice? in
switch source {
case let .message(messageId):
guard let inputPeer = transaction.getPeer(messageId.peerId).flatMap(apiInputPeer) else {
return nil
}
return .inputInvoiceMessage(peer: inputPeer, msgId: messageId.id)
case let .slug(slug):
return .inputInvoiceSlug(slug: slug)
}
return _internal_parseInputInvoice(transaction: transaction, source: source)
}
|> castError(BotPaymentFormRequestError.self)
|> mapToSignal { invoice -> Signal<BotPaymentForm, BotPaymentFormRequestError> in
@ -354,15 +382,7 @@ extension BotPaymentShippingOption {
func _internal_validateBotPaymentForm(account: Account, saveInfo: Bool, source: BotPaymentInvoiceSource, formInfo: BotPaymentRequestedInfo) -> Signal<BotPaymentValidatedFormInfo, ValidateBotPaymentFormError> {
return account.postbox.transaction { transaction -> Api.InputInvoice? in
switch source {
case let .message(messageId):
guard let inputPeer = transaction.getPeer(messageId.peerId).flatMap(apiInputPeer) else {
return nil
}
return .inputInvoiceMessage(peer: inputPeer, msgId: messageId.id)
case let .slug(slug):
return .inputInvoiceSlug(slug: slug)
}
return _internal_parseInputInvoice(transaction: transaction, source: source)
}
|> castError(ValidateBotPaymentFormError.self)
|> mapToSignal { invoice -> Signal<BotPaymentValidatedFormInfo, ValidateBotPaymentFormError> in
@ -440,15 +460,7 @@ public enum SendBotPaymentResult {
func _internal_sendBotPaymentForm(account: Account, formId: Int64, source: BotPaymentInvoiceSource, validatedInfoId: String?, shippingOptionId: String?, tipAmount: Int64?, credentials: BotPaymentCredentials) -> Signal<SendBotPaymentResult, SendBotPaymentFormError> {
return account.postbox.transaction { transaction -> Api.InputInvoice? in
switch source {
case let .message(messageId):
guard let inputPeer = transaction.getPeer(messageId.peerId).flatMap(apiInputPeer) else {
return nil
}
return .inputInvoiceMessage(peer: inputPeer, msgId: messageId.id)
case let .slug(slug):
return .inputInvoiceSlug(slug: slug)
}
return _internal_parseInputInvoice(transaction: transaction, source: source)
}
|> castError(SendBotPaymentFormError.self)
|> mapToSignal { invoice -> Signal<SendBotPaymentResult, SendBotPaymentFormError> in
@ -510,6 +522,8 @@ func _internal_sendBotPaymentForm(account: Account, formId: Int64, source: BotPa
}
}
}
case let .premiumGiftCode(peerIds, boostPeer, quantity, option):
receiptMessageId = nil
}
}
}

View File

@ -20,18 +20,23 @@ public struct PremiumGiftCodeOption: Codable, Equatable {
case months
case storeProductId
case storeQuantity
case currency
case amount
}
public let users: Int32
public let months: Int32
public let storeProductId: String?
public let storeQuantity: Int32
public init(users: Int32, months: Int32, storeProductId: String?, storeQuantity: Int32) {
public let currency: String
public let amount: Int64
public init(users: Int32, months: Int32, storeProductId: String?, storeQuantity: Int32, currency: String, amount: Int64) {
self.users = users
self.months = months
self.storeProductId = storeProductId
self.storeQuantity = storeQuantity
self.currency = currency
self.amount = amount
}
public init(from decoder: Decoder) throws {
@ -40,6 +45,9 @@ public struct PremiumGiftCodeOption: Codable, Equatable {
self.months = try container.decode(Int32.self, forKey: .months)
self.storeProductId = try container.decodeIfPresent(String.self, forKey: .storeProductId)
self.storeQuantity = try container.decodeIfPresent(Int32.self, forKey: .storeQuantity) ?? 1
self.currency = try container.decode(String.self, forKey: .currency)
self.amount = try container.decode(Int64.self, forKey: .amount)
}
public func encode(to encoder: Encoder) throws {
@ -48,6 +56,8 @@ public struct PremiumGiftCodeOption: Codable, Equatable {
try container.encode(self.months, forKey: .months)
try container.encodeIfPresent(self.storeProductId, forKey: .storeProductId)
try container.encode(self.storeQuantity, forKey: .storeQuantity)
try container.encode(self.currency, forKey: .currency)
try container.encode(self.amount, forKey: .amount)
}
}
@ -234,8 +244,8 @@ func _internal_launchPrepaidGiveaway(account: Account, peerId: EnginePeer.Id, id
extension PremiumGiftCodeOption {
init(apiGiftCodeOption: Api.PremiumGiftCodeOption) {
switch apiGiftCodeOption {
case let .premiumGiftCodeOption(_, users, months, storeProduct, storeQuantity, _, _):
self.init(users: users, months: months, storeProductId: storeProduct, storeQuantity: storeQuantity ?? 1)
case let .premiumGiftCodeOption(_, users, months, storeProduct, storeQuantity, curreny, amount):
self.init(users: users, months: months, storeProductId: storeProduct, storeQuantity: storeQuantity ?? 1, currency: curreny, amount: amount)
}
}
}

View File

@ -52,18 +52,6 @@ public extension Peer {
return nil
}
}
var nameColor: PeerNameColor? {
switch self {
case let user as TelegramUser:
return user.nameColor
case let group as TelegramGroup:
return group.nameColor
case let channel as TelegramChannel:
return channel.nameColor
default:
return nil
}
}
var usernames: [TelegramPeerUsername] {
switch self {