[go: up one dir, main page]

Skip to content

Commit

Permalink
Fix issue with billing upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
b-lam committed Feb 13, 2023
1 parent 4b71c87 commit 483ab9c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,34 @@ class BillingRepository(
localCacheBillingClient.entitlementsDao().getResplashPro()
}

fun launchBillingFlow(activity: Activity, productDetails: ProductDetails) {
val params = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(
listOf(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build()
)
)
.build()
billingClient.launchBillingFlow(activity, params)
init {
startDataSourceConnections()
}

fun startDataSourceConnections() {
private fun startDataSourceConnections() {
debug("startDataSourceConnections")
instantiateAndConnectToPlayBillingService()
localCacheBillingClient = LocalBillingDatabase.getInstance(application)
}

fun endDataSourceConnections() {
// Not needed since BillingRepository should be a singleton
private fun endDataSourceConnections() {
debug("endDataSourceConnections")
billingClient.endConnection()
}

private fun instantiateAndConnectToPlayBillingService() {
billingClient = BillingClient.newBuilder(application.applicationContext)
.enablePendingPurchases()
.setListener(this).build()
connectToPlayBillingService()
}

private fun connectToPlayBillingService(): Boolean {
.setListener(this)
.build()
if (!billingClient.isReady) {
billingClient.startConnection(this)
return true
}
return false
}

override fun onBillingSetupFinished(billingResult: BillingResult) {
debug("onBillingSetupFinished")
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
queryProductDetailsAsync()
Expand All @@ -112,6 +102,7 @@ class BillingRepository(
* self-upgrades or is force closed.
*/
override fun onBillingServiceDisconnected() {
debug("onBillingServiceDisconnected")
retryBillingServiceConnectionWithExponentialBackoff()
}

Expand All @@ -134,22 +125,23 @@ class BillingRepository(
billingResult: BillingResult,
purchases: MutableList<Purchase>?
) {
debug("onPurchasesUpdated: ${billingResult.responseCode} ${billingResult.debugMessage}")
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK ->
purchases?.apply { processPurchases(this.toSet()) }
BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED -> queryPurchasesAsync()
else -> debug("${billingResult.responseCode}: ${billingResult.debugMessage}")
}
}

fun queryPurchasesAsync(restore: Boolean = false) {
debug("queryPurchasesAsync")
val params = QueryPurchasesParams.newBuilder()
.setProductType(BillingClient.ProductType.INAPP)
.build()
billingClient.queryPurchasesAsync(params) { billingResult, purchasesList ->
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
debug("queryPurchasesAsync INAPP results: ${purchasesList.size}")
debug("Inapp purchases: $purchasesList")
if (restore && purchasesList.isEmpty()) {
_billingMessageLiveData.postValue(Event("No purchases found"))
}
Expand All @@ -161,6 +153,7 @@ class BillingRepository(
}

private fun queryProductDetailsAsync() {
debug("queryProductDetailsAsync")
val productList = INAPP_PRODUCTS.map {
QueryProductDetailsParams.Product.newBuilder()
.setProductId(it)
Expand Down Expand Up @@ -221,6 +214,7 @@ class BillingRepository(
}

private fun handleConsumablePurchasesAsync(consumables: List<Purchase>) {
debug("handleConsumablePurchasesAsync")
consumables.forEach {
val params = ConsumeParams.newBuilder().setPurchaseToken(it.purchaseToken).build()
billingClient.consumeAsync(params) { billingResult, _ ->
Expand All @@ -238,17 +232,20 @@ class BillingRepository(
}

private fun acknowledgeNonConsumablePurchasesAsync(nonConsumables: List<Purchase>) {
debug("acknowledgeNonConsumablePurchasesAsync")
nonConsumables.forEach { purchase ->
if (!purchase.isAcknowledged) {
val params = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient.acknowledgePurchase(params) { billingResult ->
val message = "Response code ${billingResult.responseCode}: ${billingResult.debugMessage}"
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK ->
BillingClient.BillingResponseCode.OK -> {
debug(message)
disburseNonConsumableEntitlement(purchase)
}
else -> {
val message = "${billingResult.responseCode}: ${billingResult.debugMessage}"
warn(message)
_billingMessageLiveData.postValue(Event(message))
_billingErrorLiveData.postValue(Event(billingResult))
Expand All @@ -263,6 +260,7 @@ class BillingRepository(

private fun disburseConsumableEntitlements(purchase: Purchase) =
CoroutineScope(Job() + Dispatchers.IO).launch {
debug("disburseConsumableEntitlements: ${purchase.products}")
for (product in purchase.products) {
if (Sku.CONSUMABLE_PRODUCTS.contains(product)) {
when (product) {
Expand All @@ -276,15 +274,15 @@ class BillingRepository(
_purchaseCompleteLiveData.postValue(Event(purchase))
}

private fun disburseNonConsumableEntitlement(purchase: Purchase) {
private fun disburseNonConsumableEntitlement(purchase: Purchase) =
CoroutineScope(Job() + Dispatchers.IO).launch {
debug("disburseNonConsumableEntitlement: ${purchase.products}")
for (product in purchase.products) {
when (product) {
Sku.RESPLASH_PRO -> insert(ResplashPro(true))
}
}
}
}

@WorkerThread
suspend fun updateDonations(donation: Donation) = withContext(Dispatchers.IO) {
Expand All @@ -310,6 +308,19 @@ class BillingRepository(
localCacheBillingClient.entitlementsDao().insert(entitlement)
}

fun launchBillingFlow(activity: Activity, productDetails: ProductDetails) {
val params = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(
listOf(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build()
)
)
.build()
billingClient.launchBillingFlow(activity, params)
}

companion object {
private val handler = Handler(Looper.getMainLooper())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@ import androidx.lifecycle.ViewModel
import com.b_lam.resplash.domain.billing.BillingRepository

class AutoWallpaperSettingsViewModel(
private val billingRepository: BillingRepository
billingRepository: BillingRepository
) : ViewModel() {

init {
billingRepository.startDataSourceConnections()
}

val resplashProLiveData = billingRepository.resplashProLiveData

override fun onCleared() {
super.onCleared()
billingRepository.endDataSourceConnections()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class DonationViewModel(
private val photoRepository: PhotoRepository
) : ViewModel() {

init {
billingRepository.startDataSourceConnections()
}

val productDetailsLiveData = billingRepository.productsWithProductDetails

val purchaseCompleteLiveData = billingRepository.purchaseCompleteLiveData
Expand All @@ -38,9 +34,4 @@ class DonationViewModel(
fun makePurchase(activity: Activity, productDetails: ProductDetails) {
billingRepository.launchBillingFlow(activity, productDetails)
}

override fun onCleared() {
super.onCleared()
billingRepository.endDataSourceConnections()
}
}
11 changes: 1 addition & 10 deletions app/src/main/java/com/b_lam/resplash/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ class MainViewModel(
private val photoRepository: PhotoRepository,
private val collectionRepository: CollectionRepository,
private val loginRepository: LoginRepository,
private val billingRepository: BillingRepository
billingRepository: BillingRepository
) : ViewModel() {

init {
billingRepository.startDataSourceConnections()
}

private val _navigationItemSelectedLiveData = MutableLiveData<Event<Int>>()
val navigationItemSelectedLiveData: LiveData<Event<Int>> = _navigationItemSelectedLiveData

Expand Down Expand Up @@ -110,9 +106,4 @@ class MainViewModel(
_emailLiveData.postValue(email)
_profilePictureLiveData.postValue(profilePicture)
}

override fun onCleared() {
super.onCleared()
billingRepository.endDataSourceConnections()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@ import androidx.lifecycle.ViewModel
import com.b_lam.resplash.domain.billing.BillingRepository

class MuzeiSettingsViewModel(
private val billingRepository: BillingRepository
billingRepository: BillingRepository
) : ViewModel() {

init {
billingRepository.startDataSourceConnections()
}

val resplashProLiveData = billingRepository.resplashProLiveData

override fun onCleared() {
super.onCleared()
billingRepository.endDataSourceConnections()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class UpgradeViewModel(
private val photoRepository: PhotoRepository
) : ViewModel() {

init {
billingRepository.startDataSourceConnections()
}

private val productDetailsLiveData = billingRepository.productsWithProductDetails

val resplashProLiveData = billingRepository.resplashProLiveData
Expand Down Expand Up @@ -51,11 +47,6 @@ class UpgradeViewModel(

fun restorePurchase() = billingRepository.queryPurchasesAsync(restore = true)

override fun onCleared() {
super.onCleared()
billingRepository.endDataSourceConnections()
}

companion object {

private const val RESPLASH_COLLECTION_ID = "10548247"
Expand Down

0 comments on commit 483ab9c

Please sign in to comment.