Skip to content

Commit adce96a

Browse files
authored
Attempt to fix processing of old, unrelated checkouts (#152)
1 parent bc849cf commit adce96a

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.
66
### Changed
77
### Removed
88
### Fixed
9+
* core: Attempt to fix bug where old checkout processes has been processed
10+
* The saved checkout is only used if it's related to the current cart
911

1012
## [0.71.2]
1113
### Added

core/src/main/java/io/snabble/sdk/checkout/Checkout.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ class Checkout @JvmOverloads constructor(
2626
}
2727

2828
private var persistentState: PersistentState =
29-
PersistentState.restore(File(project.internalStorageDirectory, "checkout.json"))
29+
PersistentState.restore(
30+
file = File(project.internalStorageDirectory, "checkout.json"),
31+
cartId = shoppingCart.id,
32+
projectId = project.id
33+
)
3034

3135
/** The current checkout process response from the backend **/
3236
var checkoutProcess
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package io.snabble.sdk.checkout
22

3-
import io.snabble.sdk.coupons.Coupon
43
import io.snabble.sdk.PaymentMethod
54
import io.snabble.sdk.Product
5+
import io.snabble.sdk.coupons.Coupon
6+
import io.snabble.sdk.events.Events
67
import io.snabble.sdk.utils.Dispatch
78
import io.snabble.sdk.utils.GsonHolder
89
import io.snabble.sdk.utils.Logger
910
import java.io.File
10-
import java.lang.Exception
1111

12-
data class PersistentState (
12+
data class PersistentState(
1313
@Transient
1414
var file: File,
15+
var cartId: String? = null,
1516
var checkoutProcess: CheckoutProcessResponse? = null,
1617
var selectedPaymentMethod: PaymentMethod? = null,
1718
var priceToPay: Int = 0,
@@ -22,6 +23,7 @@ data class PersistentState (
2223
var fulfillmentState: List<Fulfillment>? = null,
2324
var signedCheckoutInfo: SignedCheckoutInfo? = null
2425
) {
26+
2527
fun save() {
2628
val json = GsonHolder.get().toJson(this)
2729

@@ -35,16 +37,20 @@ data class PersistentState (
3537
}
3638

3739
companion object {
38-
fun restore(file: File): PersistentState {
39-
return try {
40-
val text = file.readText()
41-
val persistentState = GsonHolder.get().fromJson(text, PersistentState::class.java)
40+
41+
fun restore(file: File, cartId: String, projectId: String): PersistentState = try {
42+
val persistentState = GsonHolder.get()
43+
.fromJson(file.readText(), PersistentState::class.java)
44+
if (persistentState.cartId == cartId) {
4245
persistentState.file = file
4346
persistentState
44-
} catch (e: Exception) {
45-
Logger.d("read exception [${file.parent}]: $e")
46-
PersistentState(file)
47+
} else {
48+
Events.logErrorEvent(projectId, "Tried to restore a check process w/o a matching cart id.")
49+
PersistentState(file, cartId = cartId)
4750
}
51+
} catch (e: Exception) {
52+
Logger.d("read exception [${file.parent}]: $e")
53+
PersistentState(file, cartId = cartId)
4854
}
4955
}
5056
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
compileSdk = "33"
33
targetSdk = "32"
44
minSdk = "21"
5-
gradlePlugin = "8.0.2"
5+
gradlePlugin = "8.1.1"
66
desugarVersion = "1.1.5"
77
okhttpVersion = "4.10.0"
88
# @pin always, manually updated to ensure overall dependency support

0 commit comments

Comments
 (0)