1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 08:37:12 +00:00

add affiliate in TransactionPartner.User

This commit is contained in:
InsanusMokrassar 2024-12-06 12:56:00 +06:00
parent 2d05a334eb
commit 57175b7698
4 changed files with 99 additions and 43 deletions

View File

@ -521,7 +521,10 @@ const val totalAmountField = "total_amount"
const val invoicePayloadField = "invoice_payload" const val invoicePayloadField = "invoice_payload"
const val requestCountField = "request_count" const val requestCountField = "request_count"
const val sponsorUserField = "sponsor_user" const val sponsorUserField = "sponsor_user"
const val commisionPerMilleField = "commission_per_mille" const val affiliateUserField = "affiliate_user"
const val affiliateChatField = "affiliate_chat"
const val commissionPerMilleField = "commission_per_mille"
const val affiliateField = "affiliate"
const val paidMediaPayloadField = "paid_media_payload" const val paidMediaPayloadField = "paid_media_payload"
const val shippingOptionIdField = "shipping_option_id" const val shippingOptionIdField = "shipping_option_id"
const val shippingQueryIdField = "shipping_query_id" const val shippingQueryIdField = "shipping_query_id"

View File

@ -0,0 +1,24 @@
package dev.inmo.tgbotapi.types.payments
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.PreviewBot
import dev.inmo.tgbotapi.types.chat.PreviewChat
import dev.inmo.tgbotapi.types.chat.PreviewUser
import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.payments.abstracts.Amounted
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class AffiliateInfo(
@SerialName(commissionPerMilleField)
val commissionPerMille: Int,
@SerialName(amountField)
override val amount: Long,
@SerialName(nanostarAmountField)
val nanostarAmount: Long,
@SerialName(affiliateUserField)
val affiliateUser: User? = null,
@SerialName(affiliateChatField)
val affiliateChat: PreviewChat? = null,
) : Amounted

View File

@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.payments.stars
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.PreviewUser import dev.inmo.tgbotapi.types.chat.PreviewUser
import dev.inmo.tgbotapi.types.payments.abstracts.Amounted
import dev.inmo.tgbotapi.utils.decodeDataAndJson import dev.inmo.tgbotapi.utils.decodeDataAndJson
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -16,9 +17,8 @@ import kotlinx.serialization.json.JsonElement
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(StarTransaction.Companion::class) @Serializable(StarTransaction.Companion::class)
@ClassCastsIncluded @ClassCastsIncluded
sealed interface StarTransaction { sealed interface StarTransaction : Amounted {
val id: StarTransactionId val id: StarTransactionId
val amount: Int
val nanostarAmount: Int val nanostarAmount: Int
val date: TelegramDate val date: TelegramDate
val partner: TransactionPartner val partner: TransactionPartner
@ -30,7 +30,7 @@ sealed interface StarTransaction {
@SerialName(idField) @SerialName(idField)
override val id: StarTransactionId, override val id: StarTransactionId,
@SerialName(amountField) @SerialName(amountField)
override val amount: Int, override val amount: Long,
@SerialName(nanostarAmountField) @SerialName(nanostarAmountField)
override val nanostarAmount: Int, override val nanostarAmount: Int,
@SerialName(dateField) @SerialName(dateField)
@ -50,7 +50,7 @@ sealed interface StarTransaction {
@SerialName(idField) @SerialName(idField)
override val id: StarTransactionId, override val id: StarTransactionId,
@SerialName(amountField) @SerialName(amountField)
override val amount: Int, override val amount: Long,
@SerialName(nanostarAmountField) @SerialName(nanostarAmountField)
override val nanostarAmount: Int, override val nanostarAmount: Int,
@SerialName(dateField) @SerialName(dateField)
@ -69,7 +69,7 @@ sealed interface StarTransaction {
data class Unknown( data class Unknown(
@SerialName(idField) @SerialName(idField)
override val id: StarTransactionId, override val id: StarTransactionId,
override val amount: Int, override val amount: Long,
@SerialName(nanostarAmountField) @SerialName(nanostarAmountField)
override val nanostarAmount: Int, override val nanostarAmount: Int,
override val date: TelegramDate, override val date: TelegramDate,
@ -85,7 +85,7 @@ sealed interface StarTransaction {
@Serializable @Serializable
private data class Surrogate( private data class Surrogate(
val id: StarTransactionId, val id: StarTransactionId,
val amount: Int, val amount: Long,
@SerialName(nanostarAmountField) @SerialName(nanostarAmountField)
val nanostarAmount: Int, val nanostarAmount: Int,
val date: TelegramDate, val date: TelegramDate,

View File

@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.types.chat.PreviewBot
import dev.inmo.tgbotapi.types.chat.PreviewUser import dev.inmo.tgbotapi.types.chat.PreviewUser
import dev.inmo.tgbotapi.types.gifts.Gift import dev.inmo.tgbotapi.types.gifts.Gift
import dev.inmo.tgbotapi.types.message.payments.PaidMedia import dev.inmo.tgbotapi.types.message.payments.PaidMedia
import dev.inmo.tgbotapi.types.payments.AffiliateInfo
import dev.inmo.tgbotapi.utils.TimeSpanAsSecondsSerializer import dev.inmo.tgbotapi.utils.TimeSpanAsSecondsSerializer
import dev.inmo.tgbotapi.utils.decodeDataAndJson import dev.inmo.tgbotapi.utils.decodeDataAndJson
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
@ -45,6 +46,8 @@ sealed interface TransactionPartner {
data class User( data class User(
@SerialName(userField) @SerialName(userField)
val user: PreviewUser, val user: PreviewUser,
@SerialName(affiliateField)
val affiliate: AffiliateInfo? = null,
@SerialName(invoicePayloadField) @SerialName(invoicePayloadField)
val invoicePayload: InvoicePayload? = null, val invoicePayload: InvoicePayload? = null,
@SerialName(subscriptionPeriodField) @SerialName(subscriptionPeriodField)
@ -88,8 +91,8 @@ sealed interface TransactionPartner {
data class AffiliateProgram( data class AffiliateProgram(
@SerialName(sponsorUserField) @SerialName(sponsorUserField)
val sponsorUser: PreviewBot?, val sponsorUser: PreviewBot?,
@SerialName(commisionPerMilleField) @SerialName(commissionPerMilleField)
val commisionPerMille: Int, val commissionPerMille: Int,
) : TransactionPartner { ) : TransactionPartner {
@EncodeDefault @EncodeDefault
override val type: String = Companion.type override val type: String = Companion.type
@ -123,10 +126,17 @@ sealed interface TransactionPartner {
val type: String, val type: String,
val withdrawal_state: RevenueWithdrawalState? = null, val withdrawal_state: RevenueWithdrawalState? = null,
val user: PreviewUser? = null, val user: PreviewUser? = null,
val affiliate: AffiliateInfo? = null,
val invoice_payload: InvoicePayload? = null, val invoice_payload: InvoicePayload? = null,
@Serializable(TimeSpanAsSecondsSerializer::class)
val subscription_period: TimeSpan? = null,
val paid_media: List<PaidMedia>? = null,
val paid_media_payload: PaidMediaPayload? = null,
val gift: Gift? = null,
val request_count: Int? = null, val request_count: Int? = null,
val sponsor_user: PreviewBot? = null, val sponsor_user: PreviewBot? = null,
val commission_per_mille: Int? = null, val commission_per_mille: Int? = null,
val invoicePayload: InvoicePayload? = null
) )
override val descriptor: SerialDescriptor override val descriptor: SerialDescriptor
@ -138,44 +148,63 @@ sealed interface TransactionPartner {
val unknown by lazy { val unknown by lazy {
Unknown(data.type, json) Unknown(data.type, json)
} }
return when (data.type) { return with(data) {
Other.type -> Other when (data.type) {
User.type -> User( Other.type -> Other
data.user ?: return unknown, User.type -> User(
) user = user ?: return unknown,
TelegramAPI.type -> TelegramAPI( affiliate = affiliate,
data.request_count ?: return unknown, invoicePayload = invoice_payload,
) subscriptionPeriod = subscription_period,
Ads.type -> Ads paidMedia = paid_media,
Fragment.type -> Fragment( paidMediaPayload = paid_media_payload,
data.withdrawal_state ?: return unknown, gift = gift
) )
AffiliateProgram.type -> AffiliateProgram( TelegramAPI.type -> TelegramAPI(
data.sponsor_user, data.request_count ?: return unknown,
data.commission_per_mille ?: return unknown, )
) Ads.type -> Ads
else -> unknown Fragment.type -> Fragment(
data.withdrawal_state ?: return unknown,
)
AffiliateProgram.type -> AffiliateProgram(
data.sponsor_user,
data.commission_per_mille ?: return unknown,
)
else -> unknown
}
} }
} }
override fun serialize(encoder: Encoder, value: TransactionPartner) { override fun serialize(encoder: Encoder, value: TransactionPartner) {
val surrogate = when (value) { val surrogate = with (value) {
Other -> Surrogate(type = value.type) when (this) {
Ads -> Surrogate(type = value.type) Other -> Surrogate(type = value.type)
is User -> Surrogate(type = value.type, user = value.user) Ads -> Surrogate(type = value.type)
is TelegramAPI -> Surrogate(type = value.type, request_count = value.requestCount) is User -> Surrogate(
is Fragment -> Surrogate( type = value.type,
type = value.type, user = user,
withdrawal_state = value.withdrawalState affiliate = affiliate,
) invoice_payload = invoicePayload,
is AffiliateProgram -> Surrogate( subscription_period = subscriptionPeriod,
type = value.type, paid_media = paidMedia,
sponsor_user = value.sponsorUser, paid_media_payload = paidMediaPayload,
commission_per_mille = value.commisionPerMille gift = gift
) )
is Unknown -> value.raw ?.let { is TelegramAPI -> Surrogate(type = value.type, request_count = requestCount)
return JsonElement.serializer().serialize(encoder, it) is Fragment -> Surrogate(
} ?: Surrogate(type = value.type) type = value.type,
withdrawal_state = withdrawalState
)
is AffiliateProgram -> Surrogate(
type = value.type,
sponsor_user = sponsorUser,
commission_per_mille = commissionPerMille
)
is Unknown -> raw ?.let {
return JsonElement.serializer().serialize(encoder, it)
} ?: Surrogate(type = value.type)
}
} }
Surrogate.serializer().serialize(encoder, surrogate) Surrogate.serializer().serialize(encoder, surrogate)