Skip to content

Commit 2145a7a

Browse files
authored
Merge pull request #2 from paytrail/add-decimal-vat-support
Add decimal vat support
2 parents 04a1fb9 + aaf90a4 commit 2145a7a

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ For a deep dive into the SDK's APIs, consult our [Paytrail Android SDK Wiki](htt
3535

3636
```groovy
3737
dependencies {
38-
implementation("com.github.paytrail:paytrail-android-sdk:v0.2.0-beta")
38+
implementation("com.github.paytrail:paytrail-android-sdk:v0.3.0-beta")
3939
}
4040
```
4141

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0-beta
1+
0.3.0-beta
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package fi.paytrail.sdk.apiclient.infrastructure
2+
3+
import kotlinx.serialization.KSerializer
4+
import kotlinx.serialization.SerializationException
5+
import kotlinx.serialization.descriptors.PrimitiveKind
6+
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
7+
import kotlinx.serialization.descriptors.SerialDescriptor
8+
import kotlinx.serialization.encoding.Decoder
9+
import kotlinx.serialization.encoding.Encoder
10+
import kotlinx.serialization.json.JsonDecoder
11+
import kotlinx.serialization.json.JsonEncoder
12+
import kotlinx.serialization.json.JsonPrimitive
13+
import java.math.BigDecimal
14+
15+
object BigDecimalAsNumberAdapter : KSerializer<BigDecimal> {
16+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("BigDecimalAsNumber", PrimitiveKind.STRING)
17+
18+
override fun serialize(encoder: Encoder, value: BigDecimal) {
19+
when (encoder) {
20+
is JsonEncoder -> {
21+
encoder.encodeJsonElement(JsonPrimitive(value))
22+
}
23+
else -> {
24+
// Fallback for non-JSON encoders, encode as string
25+
encoder.encodeString(value.toPlainString())
26+
}
27+
}
28+
}
29+
30+
override fun deserialize(decoder: Decoder): BigDecimal {
31+
return when (decoder) {
32+
is JsonDecoder -> {
33+
val jsonElement = decoder.decodeJsonElement()
34+
if (jsonElement is JsonPrimitive && !jsonElement.isString) {
35+
return BigDecimal(jsonElement.content)
36+
} else {
37+
throw SerializationException("Expected JSON number for BigDecimal")
38+
}
39+
}
40+
else -> {
41+
// Fallback for non-JSON decoders
42+
BigDecimal(decoder.decodeString())
43+
}
44+
}
45+
}
46+
}

api-client-retrofit2/src/main/kotlin/fi/paytrail/sdk/apiclient/models/Item.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515

1616
package fi.paytrail.sdk.apiclient.models
1717

18+
import fi.paytrail.sdk.apiclient.infrastructure.BigDecimalAsNumberAdapter
1819
import kotlinx.serialization.Contextual
1920
import kotlinx.serialization.SerialName
2021
import kotlinx.serialization.Serializable
22+
import java.math.BigDecimal
2123

2224
/**
2325
*
2426
*
2527
* @param unitPrice Unit price of an item in currency minor unit, eg. EUR cents. VAT should be included in amount unless `usePricesWithoutVat` is set to true.
2628
* @param units Number of units
27-
* @param vatPercentage Item VAT percentage
29+
* @param vatPercentage Item VAT percentage. Values between 0 and 100 are allowed with one decimal place.
2830
* @param productCode Merchant specific product code
2931
* @param deliveryDate Estimated delivery date
3032
* @param description Merchant specific product description
@@ -46,9 +48,10 @@ data class Item(
4648
@SerialName(value = "units")
4749
val units: kotlin.Long,
4850

49-
/* Item VAT percentage */
51+
/* Item VAT percentage. Values between 0 and 100 are allowed with one decimal place. */
5052
@SerialName(value = "vatPercentage")
51-
val vatPercentage: kotlin.Long,
53+
@Serializable(with = BigDecimalAsNumberAdapter::class)
54+
val vatPercentage: BigDecimal,
5255

5356
/* Merchant specific product code */
5457
@SerialName(value = "productCode")

demo-app/src/main/java/fi/paytrail/demo/shoppingcart/ShoppingCartRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ val fakeCart = ShoppingCart(
2727
amount = 1,
2828
unitPrice = BigDecimal.valueOf(15),
2929
fakeImage = R.drawable.product_img2,
30-
vatPercentage = 24,
30+
vatPercentage = BigDecimal(25.5),
3131
),
3232
ShoppingCartRow(
3333
name = "Paytrail Drinking Bottle",
3434
id = UUID.fromString("c739864b-0307-4cba-9101-60990c449da0"),
3535
amount = 2,
3636
unitPrice = BigDecimal.valueOf(20),
3737
fakeImage = R.drawable.product_img1,
38-
vatPercentage = 24,
38+
vatPercentage = BigDecimal(14),
3939
),
4040
).associateBy { it.id },
4141
)

demo-app/src/main/java/fi/paytrail/demo/shoppingcart/ShoppingCartRow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ data class ShoppingCartRow(
99
val id: UUID,
1010
val amount: Long,
1111
val unitPrice: BigDecimal,
12-
val vatPercentage: Long,
12+
val vatPercentage: BigDecimal,
1313
val fakeImage: Int, // You can replace it with a string that contain url of the image
1414
) {
1515
val totalPrice by lazy { amount * unitPrice }

demo-app/src/main/java/fi/paytrail/demo/shoppingcart/ShoppingCartScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ fun PreviewShoppingCartItem() {
395395
id = UUID.fromString("6427e0c2-382d-4f03-99e5-413fff4d0afb"),
396396
amount = 2,
397397
unitPrice = BigDecimal("2.99"),
398-
vatPercentage = 24,
398+
vatPercentage = BigDecimal(14),
399399
fakeImage = R.drawable.image_placeholder,
400400
),
401401
)
@@ -411,7 +411,7 @@ fun PreviewShoppingCartItem_LongName() {
411411
id = UUID.fromString("6427e0c2-382d-4f03-99e5-413fff4d0afb"),
412412
amount = 2,
413413
unitPrice = BigDecimal("2345.67"),
414-
vatPercentage = 24,
414+
vatPercentage = BigDecimal(25.5),
415415
fakeImage = R.drawable.image_placeholder,
416416
),
417417
)

0 commit comments

Comments
 (0)