This commit is contained in:
Phillip Thelen 2022-03-01 10:26:45 +01:00 committed by Phillip Thelen
parent 2712d70868
commit 3ebe6d37c8
4 changed files with 25 additions and 17 deletions

View file

@ -159,8 +159,8 @@ android {
buildConfigField "String", "TESTING_LEVEL", "\"production\""
resConfigs 'en', 'bg', 'de', 'en-rGB', 'es', 'fr', 'hr-rHR', 'in', 'it', 'iw', 'ja', 'ko', 'lt', 'nl', 'pl', 'pt-rBR', 'pt-rPT', 'ru', 'tr', 'zh', 'zh-rTW'
versionCode 3253
versionName "3.5.1"
versionCode 3262
versionName "3.5.1.2"
targetSdkVersion 32

View file

@ -9,6 +9,7 @@ import androidx.core.content.edit
import androidx.core.os.bundleOf
import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient.ConnectionState.DISCONNECTED
import com.google.api.Billing
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.habitrpg.android.habitica.HabiticaBaseApplication
@ -271,6 +272,16 @@ open class PurchaseHandler(
FirebaseCrashlytics.getInstance().recordException(throwable)
}
suspend fun checkForSubscription(): Purchase? {
val result = withContext(Dispatchers.IO) {
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS)
}
if (result.billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
return result.purchasesList.sortedByDescending { it.purchaseTime }.firstOrNull { it.isAcknowledged }
}
return null
}
fun cancelSubscription(): Flowable<User> {
return apiClient.cancelSubscription()
.flatMap { userViewModel.userRepository.retrieveUser(false, true) }

View file

@ -59,7 +59,6 @@ class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
private var user: User? = null
private var hasLoadedSubscriptionOptions: Boolean = false
private var purchasedSubscription: Purchase? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -229,15 +228,18 @@ class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
}
private fun checkIfNeedsCancellation() {
if (user?.purchased?.plan?.paymentMethod == "Google" &&
user?.purchased?.plan?.isActive == true &&
user?.purchased?.plan?.dateTerminated == null &&
(purchasedSubscription?.isAutoRenewing == false || purchasedSubscription == null)
) {
compositeSubscription.add(
purchaseHandler.cancelSubscription()
.subscribe({ }, RxErrorHandler.handleEmptyError())
)
CoroutineScope(Dispatchers.IO).launch {
val newestSubscription = purchaseHandler.checkForSubscription()
if (user?.purchased?.plan?.paymentMethod == "Google" &&
user?.purchased?.plan?.isActive == true &&
user?.purchased?.plan?.dateTerminated == null &&
(newestSubscription?.isAutoRenewing == false)
) {
compositeSubscription.add(
purchaseHandler.cancelSubscription()
.subscribe({ }, RxErrorHandler.handleEmptyError())
)
}
}
}

View file

@ -1,5 +0,0 @@
PORT=3000
BASE_URL=http://localhost:3000
STAGING_KEY=
ANDROID_TESTING_UUID=
APPLE_AUTH_CLIENT_ID=