Fix retry issue with payment processing.

This commit is contained in:
Alex Hart
2021-11-01 09:42:50 -03:00
committed by Greyson Parrelli
parent cf9b91ebd4
commit b0788f7307
7 changed files with 51 additions and 42 deletions

View File

@@ -12,7 +12,6 @@ import org.thoughtcrime.securesms.payments.currency.CurrencyUtil
import org.thoughtcrime.securesms.subscription.LevelUpdateOperation
import org.thoughtcrime.securesms.subscription.Subscriber
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.util.guava.Optional
import org.whispersystems.signalservice.api.subscriptions.IdempotencyKey
import org.whispersystems.signalservice.api.subscriptions.SubscriberId
import java.util.Currency
@@ -45,9 +44,6 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
private val boostCurrencyPublisher: Subject<Currency> by lazy { BehaviorSubject.createDefault(getBoostCurrency()) }
val observableBoostCurrency: Observable<Currency> by lazy { boostCurrencyPublisher }
private val levelUpdateOperationPublisher: Subject<Optional<LevelUpdateOperation>> by lazy { BehaviorSubject.createDefault(Optional.fromNullable(getLevelOperation())) }
val levelUpdateOperationObservable: Observable<Optional<LevelUpdateOperation>> by lazy { levelUpdateOperationPublisher }
fun getSubscriptionCurrency(): Currency {
val currencyCode = getString(KEY_SUBSCRIPTION_CURRENCY_CODE, null)
val currency: Currency? = if (currencyCode == null) {
@@ -120,7 +116,7 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
fun getLevelOperation(): LevelUpdateOperation? {
val level = getString(KEY_LEVEL, null)
val idempotencyKey = getIdempotencyKey()
val idempotencyKey = getBlob(KEY_IDEMPOTENCY, null)?.let { IdempotencyKey.fromBytes(it) }
return if (level == null || idempotencyKey == null) {
null
@@ -130,19 +126,17 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
}
fun setLevelOperation(levelUpdateOperation: LevelUpdateOperation) {
putString(KEY_LEVEL, levelUpdateOperation.level)
setIdempotencyKey(levelUpdateOperation.idempotencyKey)
dispatchLevelOperation()
store.beginWrite()
.putString(KEY_LEVEL, levelUpdateOperation.level)
.putBlob(KEY_IDEMPOTENCY, levelUpdateOperation.idempotencyKey.bytes)
.apply()
}
fun clearLevelOperation(levelUpdateOperation: LevelUpdateOperation): Boolean {
val currentKey = getIdempotencyKey()
return if (currentKey == levelUpdateOperation.idempotencyKey) {
clearLevelOperation()
true
} else {
false
}
fun clearLevelOperation() {
store.beginWrite()
.remove(KEY_LEVEL)
.remove(KEY_IDEMPOTENCY)
.apply()
}
fun setExpiredBadge(badge: Badge?) {
@@ -159,20 +153,6 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
return Badges.fromDatabaseBadge(BadgeList.Badge.parseFrom(badgeBytes))
}
private fun clearLevelOperation() {
remove(KEY_IDEMPOTENCY)
remove(KEY_LEVEL)
dispatchLevelOperation()
}
private fun getIdempotencyKey(): IdempotencyKey? {
return getBlob(KEY_IDEMPOTENCY, null)?.let { IdempotencyKey.fromBytes(it) }
}
private fun setIdempotencyKey(key: IdempotencyKey) {
putBlob(KEY_IDEMPOTENCY, key.bytes)
}
fun getLastKeepAliveLaunchTime(): Long {
return getLong(KEY_LAST_KEEP_ALIVE_LAUNCH, 0L)
}
@@ -200,8 +180,4 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
fun clearUserManuallyCancelled() {
remove(USER_MANUALLY_CANCELLED)
}
private fun dispatchLevelOperation() {
levelUpdateOperationPublisher.onNext(Optional.fromNullable(getLevelOperation()))
}
}