From 6f776576ab05dcbbd09a003a990632f0415fd806 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 28 May 2024 19:06:11 +0600 Subject: [PATCH] add support of ommiting provider_token in payment classes --- .../api/send/payments/CreateInvoiceLink.kt | 23 ++++++++ .../api/send/payments/SendInvoice.kt | 59 +++++++++++++++++++ .../abstracts/CommonSendInvoiceData.kt | 2 +- .../send/payments/CreateInvoiceLink.kt | 37 +++++++++++- .../requests/send/payments/SendInvoice.kt | 49 ++++++++++++++- .../InputInvoiceMessageContent.kt | 37 +++++++++++- .../types/payments/abstracts/Currencied.kt | 3 + 7 files changed, 206 insertions(+), 4 deletions(-) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt index 00ae87149c..27b8d1d08c 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt @@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.chat.CommonUser import dev.inmo.tgbotapi.types.payments.LabeledPrice import dev.inmo.tgbotapi.types.payments.abstracts.Currency +import dev.inmo.tgbotapi.types.payments.abstracts.XTR suspend fun TelegramBot.createInvoiceLink( title: String, @@ -29,3 +30,25 @@ suspend fun TelegramBot.createInvoiceLink( ) = execute( CreateInvoiceLink(title, description, payload, providerToken, currency, prices, maxTipAmount, suggestedTipAmounts ?.sorted(), providerData, requireName, requirePhoneNumber, requireEmail, requireShippingAddress, shouldSendPhoneNumberToProvider, shouldSendEmailToProvider, priceDependOnShipAddress) ) + +/** + * For links witn XTR currency and using of Telegram Stars + */ +suspend fun TelegramBot.createInvoiceLink( + title: String, + description: String, + payload: String, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false +) = execute( + CreateInvoiceLink(title, description, payload, null, Currency.XTR, prices, maxTipAmount, suggestedTipAmounts ?.sorted(), providerData, requireName, requirePhoneNumber, requireEmail, requireShippingAddress, shouldSendPhoneNumberToProvider, shouldSendEmailToProvider, priceDependOnShipAddress) +) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/SendInvoice.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/SendInvoice.kt index eb1f7ff458..212d654099 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/SendInvoice.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/SendInvoice.kt @@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.chat.CommonUser import dev.inmo.tgbotapi.types.payments.LabeledPrice import dev.inmo.tgbotapi.types.payments.abstracts.Currency +import dev.inmo.tgbotapi.types.payments.abstracts.XTR /** * @param replyMarkup Some [InlineKeyboardMarkup]. See [dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard] @@ -68,3 +69,61 @@ suspend fun TelegramBot.sendInvoice( replyParameters: ReplyParameters? = null, replyMarkup: InlineKeyboardMarkup? = null ) = sendInvoice(user.id, title, description, payload, providerToken, currency, prices, maxTipAmount, suggestedTipAmounts, startParameter, providerData, requireName, requirePhoneNumber, requireEmail, requireShippingAddress, shouldSendPhoneNumberToProvider, shouldSendEmailToProvider, priceDependOnShipAddress, null, disableNotification, protectContent, replyParameters, replyMarkup) + + +/** + * @param replyMarkup Some [InlineKeyboardMarkup]. See [dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard] + * as a builder for that + */ +suspend fun TelegramBot.sendInvoice( + chatId: IdChatIdentifier, + title: String, + description: String, + payload: String, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + startParameter: StartParameter? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false, + threadId: MessageThreadId? = chatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + replyParameters: ReplyParameters? = null, + replyMarkup: InlineKeyboardMarkup? = null +) = execute( + SendInvoice(chatId, title, description, payload, null, Currency.XTR, prices, maxTipAmount, suggestedTipAmounts ?.sorted(), startParameter, providerData, requireName, requirePhoneNumber, requireEmail, requireShippingAddress, shouldSendPhoneNumberToProvider, shouldSendEmailToProvider, priceDependOnShipAddress, threadId, disableNotification, protectContent, replyParameters, replyMarkup) +) + +/** + * @param replyMarkup Some [InlineKeyboardMarkup]. See [dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard] + * as a builder for that + */ +suspend fun TelegramBot.sendInvoice( + user: CommonUser, + title: String, + description: String, + payload: String, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + startParameter: StartParameter? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false, + disableNotification: Boolean = false, + protectContent: Boolean = false, + replyParameters: ReplyParameters? = null, + replyMarkup: InlineKeyboardMarkup? = null +) = sendInvoice(user.id, title, description, payload, prices, maxTipAmount, suggestedTipAmounts, startParameter, providerData, requireName, requirePhoneNumber, requireEmail, requireShippingAddress, shouldSendPhoneNumberToProvider, shouldSendEmailToProvider, priceDependOnShipAddress, null, disableNotification, protectContent, replyParameters, replyMarkup) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/CommonSendInvoiceData.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/CommonSendInvoiceData.kt index 376e4bad48..ce099f7382 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/CommonSendInvoiceData.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/CommonSendInvoiceData.kt @@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.types.payments.abstracts.Priced interface CommonSendInvoiceData : Titled, Currencied, Priced { val description: String val payload: String - val providerToken: String + val providerToken: String? val maxTipAmount: Int? val suggestedTipAmounts: List? val providerData: String? diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt index 19a932e9d6..598d9a277b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt @@ -11,6 +11,7 @@ import dev.inmo.tgbotapi.types.message.content.InvoiceContent import dev.inmo.tgbotapi.types.payments.LabeledPrice import dev.inmo.tgbotapi.types.payments.LabeledPricesSerializer import dev.inmo.tgbotapi.types.payments.abstracts.Currency +import dev.inmo.tgbotapi.types.payments.abstracts.XTR import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer @@ -26,7 +27,7 @@ data class CreateInvoiceLink( @SerialName(payloadField) override val payload: String, @SerialName(providerTokenField) - override val providerToken: String, + override val providerToken: String?, @SerialName(currencyField) override val currency: Currency, @Serializable(LabeledPricesSerializer::class) @@ -73,6 +74,40 @@ data class CreateInvoiceLink( override var photoHeight: Int? = null private set + constructor( + title: String, + description: String, + payload: String, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false + ) : this( + title = title, + description = description, + payload = payload, + providerToken = null, + currency = Currency.XTR, + prices = prices, + maxTipAmount = maxTipAmount, + suggestedTipAmounts = suggestedTipAmounts, + providerData = providerData, + requireName = requireName, + requirePhoneNumber = requirePhoneNumber, + requireEmail = requireEmail, + requireShippingAddress = requireShippingAddress, + shouldSendPhoneNumberToProvider = shouldSendPhoneNumberToProvider, + shouldSendEmailToProvider = shouldSendEmailToProvider, + priceDependOnShipAddress = priceDependOnShipAddress + ) + init { suggestedTipAmounts ?.let { _ -> require(suggestedTipAmounts.size in suggestedTipAmountsLimit) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/SendInvoice.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/SendInvoice.kt index 2e4effdf1b..ce6261617c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/SendInvoice.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/SendInvoice.kt @@ -12,6 +12,7 @@ import dev.inmo.tgbotapi.types.message.content.InvoiceContent import dev.inmo.tgbotapi.types.payments.LabeledPrice import dev.inmo.tgbotapi.types.payments.LabeledPricesSerializer import dev.inmo.tgbotapi.types.payments.abstracts.Currency +import dev.inmo.tgbotapi.types.payments.abstracts.XTR import kotlinx.serialization.* private val invoiceMessageSerializer: DeserializationStrategy> @@ -31,7 +32,7 @@ data class SendInvoice( @SerialName(payloadField) override val payload: String, @SerialName(providerTokenField) - override val providerToken: String, + override val providerToken: String?, @SerialName(currencyField) override val currency: Currency, @Serializable(LabeledPricesSerializer::class) @@ -95,6 +96,52 @@ data class SendInvoice( override var photoHeight: Int? = null private set + constructor( + chatId: IdChatIdentifier, + title: String, + description: String, + payload: String, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false, + threadId: MessageThreadId? = chatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + replyParameters: ReplyParameters? = null, + replyMarkup: InlineKeyboardMarkup? = null + ) : this( + chatId = chatId, + title = title, + description = description, + payload = payload, + providerToken = null, + currency = Currency.XTR, + prices = prices, + maxTipAmount = maxTipAmount, + suggestedTipAmounts = suggestedTipAmounts, + providerData = providerData, + requireName = requireName, + requirePhoneNumber = requirePhoneNumber, + requireEmail = requireEmail, + requireShippingAddress = requireShippingAddress, + shouldSendPhoneNumberToProvider = shouldSendPhoneNumberToProvider, + shouldSendEmailToProvider = shouldSendEmailToProvider, + priceDependOnShipAddress = priceDependOnShipAddress, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + replyParameters = replyParameters, + replyMarkup = replyMarkup + ) + init { suggestedTipAmounts ?.let { _ -> require(suggestedTipAmounts.size in suggestedTipAmountsLimit) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/InputMessageContent/InputInvoiceMessageContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/InputMessageContent/InputInvoiceMessageContent.kt index b489aaf2e5..712722adb2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/InputMessageContent/InputInvoiceMessageContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/InputMessageContent/InputInvoiceMessageContent.kt @@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.payments.LabeledPrice import dev.inmo.tgbotapi.types.payments.LabeledPricesSerializer import dev.inmo.tgbotapi.types.payments.abstracts.Currency +import dev.inmo.tgbotapi.types.payments.abstracts.XTR import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -17,7 +18,7 @@ class InputInvoiceMessageContent( @SerialName(payloadField) override val payload: String, @SerialName(providerTokenField) - override val providerToken: String, + override val providerToken: String?, @SerialName(currencyField) override val currency: Currency, @Serializable(LabeledPricesSerializer::class) @@ -58,6 +59,40 @@ class InputInvoiceMessageContent( override var photoHeight: Int? = null private set + constructor( + title: String, + description: String, + payload: String, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false + ) : this( + title = title, + description = description, + payload = payload, + providerToken = null, + currency = Currency.XTR, + prices = prices, + maxTipAmount = maxTipAmount, + suggestedTipAmounts = suggestedTipAmounts, + providerData = providerData, + requireName = requireName, + requirePhoneNumber = requirePhoneNumber, + requireEmail = requireEmail, + requireShippingAddress = requireShippingAddress, + shouldSendPhoneNumberToProvider = shouldSendPhoneNumberToProvider, + shouldSendEmailToProvider = shouldSendEmailToProvider, + priceDependOnShipAddress = priceDependOnShipAddress + ) + override fun setPhoto( photoUrl: String, photoSize: Long?, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/abstracts/Currencied.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/abstracts/Currencied.kt index d3c5dd917e..2677da9834 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/abstracts/Currencied.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/abstracts/Currencied.kt @@ -2,6 +2,9 @@ package dev.inmo.tgbotapi.types.payments.abstracts typealias Currency = String +val String.Companion.XTR + get() = "XTR" + interface Currencied { val currency: Currency }