Skip to content

Commit

Permalink
Merge pull request #637 from qonversion/feature/sc-35377/multiQuantit…
Browse files Browse the repository at this point in the history
…yInApps

Added purchase callback with quantity for inapp purchases.
  • Loading branch information
SpertsyanKM authored Oct 14, 2024
2 parents 10c2399 + c1902eb commit f49f150
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
8 changes: 6 additions & 2 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
<ID>MaxLineLength:QonversionConfig.kt$QonversionConfig.Builder$*</ID>
<ID>MaxLineLength:QonversionError.kt$QonversionErrorCode$*</ID>
<ID>MaxLineLength:QonversionError.kt$QonversionErrorCode$RemoteConfigurationNotAvailable : QonversionErrorCode</ID>
<ID>MaxLineLength:QonversionInternal.kt$QonversionInternal$@Deprecated("Use the new purchase() method", replaceWith = ReplaceWith("purchase(context, TODO(\"pass product here\"), callback)"))</ID>
<ID>MaxLineLength:QonversionInternal.kt$QonversionInternal$@Deprecated("Use the new updatePurchase() method", replaceWith = ReplaceWith("updatePurchase(context, TODO(\"pass product here\"), TODO(\"pass purchase options here\"), callback)"))</ID>
<ID>MaxLineLength:QonversionRepositoryIntegrationTest.kt$QonversionRepositoryIntegrationTest$"""HTTP status code=400, data={"message":"Invalid access token received","code":10003,"status":400,"extra":[]}. """</ID>
<ID>MaxLineLength:QonversionRepositoryIntegrationTest.kt$QonversionRepositoryIntegrationTest$"lcbfeigohklhpdgmpildjabg.AO-J1OyV-EE2bKGqDcRCvqjZ2NI1uHDRuvonRn5RorP6LNsyK7yHK8FaFlXp6bjTEX3-4JvZKtbY_bpquKBfux09Mfkx05M9YGZsfsr5BJk74r719m77Oyo"</ID>
<ID>MaxLineLength:QonversionRepositoryIntegrationTest.kt$QonversionRepositoryIntegrationTest$"lgeigljfpmeoddkcebkcepjc.AO-J1Oy305qZj99jXTPEVBN8UZGoYAtjDLj4uTjRQvUFaG0vie-nr6VBlN0qnNDMU8eJR-sI7o3CwQyMOEHKl8eJsoQ86KSFzxKBR07PSpHLI_o7agXhNKY"</ID>
Expand Down Expand Up @@ -154,11 +156,13 @@
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:214</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:371</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:90</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManager.kt:332</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManager.kt:333</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManagerTest.kt:152</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManagerTest.kt:153</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QRemoteConfigManager.kt:225</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QUserPropertiesManagerTest.kt:178</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:162</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:213</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:287</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:356</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:878</ID>
Expand Down Expand Up @@ -243,7 +247,7 @@
<ID>ReturnCount:QExceptionManager.kt$QExceptionManager$private fun getContentOfCrashReport(filename: String): CrashRequest.ExceptionInfo?</ID>
<ID>ReturnCount:QProductCenterManager.kt$QProductCenterManager$@Synchronized private fun executeProductsBlocks(loadStoreProductsError: QonversionError? = null)</ID>
<ID>ReturnCount:QProductCenterManager.kt$QProductCenterManager$fun identify(identityId: String, callback: QonversionUserCallback? = null)</ID>
<ID>ReturnCount:QProductCenterManager.kt$QProductCenterManager$private fun calculatePurchasePermissionsLocally( purchase: Purchase, purchaseCallback: QonversionEntitlementsCallback?, purchaseError: QonversionError )</ID>
<ID>ReturnCount:QProductCenterManager.kt$QProductCenterManager$private fun calculatePurchasePermissionsLocally( purchase: Purchase, purchaseCallback: QonversionPurchaseCallback?, purchaseError: QonversionError )</ID>
<ID>ReturnCount:ScreenPresenter.kt$ScreenPresenter$override fun shouldOverrideUrlLoading(url: String?): Boolean</ID>
<ID>SpacingAroundColon:com.qonversion.android.sdk.internal.requests.ProviderDataRequestTest.kt:45</ID>
<ID>SpacingAroundCurly:com.qonversion.android.sdk.automations.internal.QAutomationsManagerTest.kt:254</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.qonversion.android.sdk.internal.services.QUserInfoService
import com.qonversion.android.sdk.internal.storage.LaunchResultCacheWrapper
import com.qonversion.android.sdk.internal.storage.PurchasesCache
import com.qonversion.android.sdk.listeners.QEntitlementsUpdateListener
import com.qonversion.android.sdk.listeners.QonversionPurchaseCallback
import com.qonversion.android.sdk.listeners.QonversionUserCallback
import kotlin.math.min
import java.util.Date
Expand Down Expand Up @@ -79,7 +80,7 @@ internal class QProductCenterManager internal constructor(

private var productsCallbacks = mutableListOf<QonversionProductsCallback>()
private var entitlementCallbacks = mutableListOf<QonversionEntitlementsCallback>()
private var purchasingCallbacks = mutableMapOf<String, QonversionEntitlementsCallback>()
private var purchasingCallbacks = mutableMapOf<String, QonversionPurchaseCallback>()
private var restoreCallbacks = mutableListOf<QonversionEntitlementsCallback>()

private var processingPartnersIdentityId: String? = null
Expand Down Expand Up @@ -292,7 +293,7 @@ internal class QProductCenterManager internal constructor(
fun purchaseProduct(
context: Activity,
purchaseModel: PurchaseModelInternal,
callback: QonversionEntitlementsCallback
callback: QonversionPurchaseCallback
) {
if (internalConfig.isAnalyticsMode) {
logger.warn(
Expand All @@ -318,7 +319,7 @@ internal class QProductCenterManager internal constructor(
private fun tryToPurchase(
context: Activity,
purchaseModel: PurchaseModelInternal,
callback: QonversionEntitlementsCallback
callback: QonversionPurchaseCallback
) {
val products = launchResultCache.getActualProducts() ?: run {
callback.onError(launchError ?: QonversionError(QonversionErrorCode.LaunchError))
Expand All @@ -337,7 +338,7 @@ internal class QProductCenterManager internal constructor(
private fun processPurchase(
context: Activity,
purchaseModel: PurchaseModelInternalEnriched,
callback: QonversionEntitlementsCallback
callback: QonversionPurchaseCallback
) {
if (purchaseModel.product.storeID == null) {
callback.onError(QonversionError(QonversionErrorCode.ProductNotFound))
Expand Down Expand Up @@ -477,7 +478,7 @@ internal class QProductCenterManager internal constructor(

private fun calculatePurchasePermissionsLocally(
purchase: Purchase,
purchaseCallback: QonversionEntitlementsCallback?,
purchaseCallback: QonversionPurchaseCallback?,
purchaseError: QonversionError
) {
val products = launchResultCache.getActualProducts() ?: run {
Expand Down Expand Up @@ -505,11 +506,11 @@ internal class QProductCenterManager internal constructor(
purchasedProduct,
productPermissions
)
purchaseCallback?.onSuccess(permissions.toEntitlementsMap())
purchaseCallback?.onSuccess(permissions.toEntitlementsMap(), purchase)
}

private fun failLocallyGrantingPurchasePermissionsWithError(
callback: QonversionEntitlementsCallback?,
callback: QonversionPurchaseCallback?,
error: QonversionError
) {
launchResultCache.clearPermissionsCache()
Expand Down Expand Up @@ -999,7 +1000,7 @@ internal class QProductCenterManager internal constructor(
val entitlements = launchResult.permissions.toEntitlementsMap()

removePurchaseOptions(product?.storeID)
purchaseCallback?.onSuccess(entitlements) ?: run {
purchaseCallback?.onSuccess(entitlements, purchase) ?: run {
internalConfig.entitlementsUpdateListener?.onEntitlementsUpdated(
entitlements
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Application
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.ProcessLifecycleOwner
import com.android.billingclient.api.Purchase
import com.qonversion.android.sdk.Qonversion
import com.qonversion.android.sdk.automations.internal.QAutomationsManager
import com.qonversion.android.sdk.dto.QAttributionProvider
Expand Down Expand Up @@ -37,6 +38,7 @@ import com.qonversion.android.sdk.listeners.QonversionRemoteConfigCallback
import com.qonversion.android.sdk.listeners.QonversionEligibilityCallback
import com.qonversion.android.sdk.listeners.QonversionUserCallback
import com.qonversion.android.sdk.listeners.QEntitlementsUpdateListener
import com.qonversion.android.sdk.listeners.QonversionPurchaseCallback
import com.qonversion.android.sdk.listeners.QonversionRemoteConfigListCallback
import com.qonversion.android.sdk.listeners.QonversionRemoteConfigurationAttachCallback
import com.qonversion.android.sdk.listeners.QonversionUserPropertiesCallback
Expand Down Expand Up @@ -157,6 +159,7 @@ internal class QonversionInternal(
})
}

@Deprecated("Use the new purchase() method", replaceWith = ReplaceWith("purchase(context, TODO(\"pass product here\"), callback)"))
override fun purchase(
context: Activity,
purchaseModel: QPurchaseModel,
Expand All @@ -165,7 +168,7 @@ internal class QonversionInternal(
productCenterManager.purchaseProduct(
context,
PurchaseModelInternal(purchaseModel),
mainEntitlementsCallback(callback)
mainPurchaseCallback(callback)
)
}

Expand All @@ -178,7 +181,7 @@ internal class QonversionInternal(
productCenterManager.purchaseProduct(
context,
PurchaseModelInternal(product, options),
mainEntitlementsCallback(callback)
mainPurchaseCallback(callback)
)
}

Expand All @@ -190,7 +193,7 @@ internal class QonversionInternal(
productCenterManager.purchaseProduct(
context,
PurchaseModelInternal(product),
mainEntitlementsCallback(callback)
mainPurchaseCallback(callback)
)
}

Expand All @@ -203,10 +206,11 @@ internal class QonversionInternal(
productCenterManager.purchaseProduct(
context,
PurchaseModelInternal(product, options),
mainEntitlementsCallback(callback)
mainPurchaseCallback(callback)
)
}

@Deprecated("Use the new updatePurchase() method", replaceWith = ReplaceWith("updatePurchase(context, TODO(\"pass product here\"), TODO(\"pass purchase options here\"), callback)"))
override fun updatePurchase(
context: Activity,
purchaseUpdateModel: QPurchaseUpdateModel,
Expand All @@ -215,7 +219,7 @@ internal class QonversionInternal(
productCenterManager.purchaseProduct(
context,
PurchaseModelInternal(purchaseUpdateModel),
mainEntitlementsCallback(callback)
mainPurchaseCallback(callback)
)
}

Expand Down Expand Up @@ -395,6 +399,34 @@ internal class QonversionInternal(
postToMainThread { callback.onError(error) }
}

private fun mainPurchaseCallback(callback: QonversionEntitlementsCallback): QonversionPurchaseCallback {
val purchaseCallback = if (callback is QonversionPurchaseCallback) {
callback
} else {
object : QonversionPurchaseCallback {
override fun onSuccess(entitlements: Map<String, QEntitlement>) {
callback.onSuccess(entitlements)
}

override fun onError(error: QonversionError) {
callback.onError(error)
}
}
}

return object : QonversionPurchaseCallback {
override fun onSuccess(entitlements: Map<String, QEntitlement>, purchase: Purchase) {
postToMainThread { purchaseCallback.onSuccess(entitlements, purchase) }
}

override fun onSuccess(entitlements: Map<String, QEntitlement>) =
postToMainThread { purchaseCallback.onSuccess(entitlements) }

override fun onError(error: QonversionError) =
postToMainThread { purchaseCallback.onError(error) }
}
}

private fun mainUserCallback(callback: QonversionUserCallback): QonversionUserCallback =
object : QonversionUserCallback {
override fun onSuccess(user: QUser) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qonversion.android.sdk.listeners

import com.android.billingclient.api.Purchase
import com.qonversion.android.sdk.dto.entitlements.QEntitlement
import com.qonversion.android.sdk.dto.QRemoteConfig
import com.qonversion.android.sdk.dto.QRemoteConfigList
Expand Down Expand Up @@ -46,6 +47,10 @@ interface QonversionRemoteConfigurationAttachCallback {
fun onError(error: QonversionError)
}

interface QonversionPurchaseCallback : QonversionEntitlementsCallback {
fun onSuccess(entitlements: Map<String, QEntitlement>, purchase: Purchase) = onSuccess(entitlements)
}

interface QonversionEntitlementsCallback {
fun onSuccess(entitlements: Map<String, QEntitlement>)
fun onError(error: QonversionError)
Expand Down

0 comments on commit f49f150

Please sign in to comment.