Skip to content

Commit

Permalink
Move obfuscatedAccountId and obfuscatedProfileId from IapConnector's …
Browse files Browse the repository at this point in the history
…constructor to purchase and subscribe to add flexibility
  • Loading branch information
kf99916 committed Jul 16, 2023
1 parent 97c1abe commit 394ba64
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
14 changes: 6 additions & 8 deletions app/src/main/java/com/limurse/iapsample/JavaSampleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
consumablesList,
subsList,
getString(R.string.licenseKey),
null,
null,
true
);

Expand Down Expand Up @@ -90,27 +88,27 @@ public void onPricesUpdated(@NotNull Map iapKeyPrices) {
});

binding.btPurchaseCons.setOnClickListener(it ->
iapConnector.purchase(this, "base")
iapConnector.purchase(this, "base", null, null)
);

binding.btnMonthly.setOnClickListener(it ->
iapConnector.subscribe(this, "subscription")
iapConnector.subscribe(this, "subscription", null, null)
);

binding.btnYearly.setOnClickListener(it ->
iapConnector.subscribe(this, "yearly")
iapConnector.subscribe(this, "yearly", null, null)
);

binding.btnQuite.setOnClickListener(it ->
iapConnector.purchase(this, "quite")
iapConnector.purchase(this, "quite", null, null)
);

binding.btnModerate.setOnClickListener(it ->
iapConnector.purchase(this, "moderate")
iapConnector.purchase(this, "moderate", null, null)
);

binding.btnUltimate.setOnClickListener(it ->
iapConnector.purchase(this, "plenty")
iapConnector.purchase(this, "plenty", null, null)
);
}
}
12 changes: 5 additions & 7 deletions iap/src/main/java/com/limurse/iap/BillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class BillingService(
private val nonConsumableKeys: List<String>,
private val consumableKeys: List<String>,
private val subscriptionSkuKeys: List<String>,
private val obfuscatedAccountId: String?,
private val obfuscatedProfileId: String?
) : IBillingService(), PurchasesUpdatedListener, AcknowledgePurchaseResponseListener {

private lateinit var mBillingClient: BillingClient
Expand Down Expand Up @@ -80,25 +78,25 @@ class BillingService(
processPurchases(subsResult.purchasesList, isRestore = true)
}

override fun buy(activity: Activity, sku: String) {
override fun buy(activity: Activity, sku: String, obfuscatedAccountId: String?, obfuscatedProfileId: String?) {
if (!sku.isProductReady()) {
log("buy. Google billing service is not ready yet. (SKU is not ready yet -1)")
return
}

launchBillingFlow(activity, sku, BillingClient.ProductType.INAPP)
launchBillingFlow(activity, sku, BillingClient.ProductType.INAPP, obfuscatedAccountId, obfuscatedProfileId)
}

override fun subscribe(activity: Activity, sku: String) {
override fun subscribe(activity: Activity, sku: String, obfuscatedAccountId: String?, obfuscatedProfileId: String?) {
if (!sku.isProductReady()) {
log("buy. Google billing service is not ready yet. (SKU is not ready yet -2)")
return
}

launchBillingFlow(activity, sku, BillingClient.ProductType.SUBS)
launchBillingFlow(activity, sku, BillingClient.ProductType.SUBS, obfuscatedAccountId, obfuscatedProfileId)
}

private fun launchBillingFlow(activity: Activity, sku: String, type: String) {
private fun launchBillingFlow(activity: Activity, sku: String, type: String, obfuscatedAccountId: String?, obfuscatedProfileId: String?) {
sku.toProductDetails(type) { productDetails ->
if (productDetails != null) {

Expand Down
4 changes: 2 additions & 2 deletions iap/src/main/java/com/limurse/iap/IBillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ abstract class IBillingService {
}

abstract fun init(key: String?)
abstract fun buy(activity: Activity, sku: String)
abstract fun subscribe(activity: Activity, sku: String)
abstract fun buy(activity: Activity, sku: String, obfuscatedAccountId: String?, obfuscatedProfileId: String?)
abstract fun subscribe(activity: Activity, sku: String, obfuscatedAccountId: String?, obfuscatedProfileId: String?)
abstract fun unsubscribe(activity: Activity, sku: String)
abstract fun enableDebugLogging(enable: Boolean)

Expand Down
14 changes: 5 additions & 9 deletions iap/src/main/java/com/limurse/iap/IapConnector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import kotlinx.coroutines.DelicateCoroutinesApi
* @param consumableKeys SKU list for consumable one-time products.
* @param subscriptionKeys SKU list for subscriptions.
* @param key Key to verify purchase messages. Leave it empty if you want to skip verification.
* @param obfuscatedAccountId Specifies an optional obfuscated string that is uniquely associated with the user's account in your app.
* @param obfuscatedProfileId Specifies an optional obfuscated string that is uniquely associated with the user's profile in your app.
* @param enableLogging Log operations/errors to the logcat for debugging purposes.
*/
@OptIn(DelicateCoroutinesApi::class)
Expand All @@ -23,16 +21,14 @@ class IapConnector @JvmOverloads constructor(
consumableKeys: List<String> = emptyList(),
subscriptionKeys: List<String> = emptyList(),
key: String? = null,
obfuscatedAccountId: String? = null,
obfuscatedProfileId: String? = null,
enableLogging: Boolean = false
) {

private var mBillingService: IBillingService? = null

init {
val contextLocal = context.applicationContext ?: context
mBillingService = BillingService(contextLocal, nonConsumableKeys, consumableKeys, subscriptionKeys, obfuscatedAccountId, obfuscatedProfileId)
mBillingService = BillingService(contextLocal, nonConsumableKeys, consumableKeys, subscriptionKeys)
getBillingService().init(key)
getBillingService().enableDebugLogging(enableLogging)
}
Expand Down Expand Up @@ -61,12 +57,12 @@ class IapConnector @JvmOverloads constructor(
getBillingService().removeSubscriptionListener(subscriptionServiceListener)
}

fun purchase(activity: Activity, sku: String) {
getBillingService().buy(activity, sku)
fun purchase(activity: Activity, sku: String, obfuscatedAccountId: String? = null, obfuscatedProfileId: String? = null) {
getBillingService().buy(activity, sku, obfuscatedAccountId, obfuscatedProfileId)
}

fun subscribe(activity: Activity, sku: String) {
getBillingService().subscribe(activity, sku)
fun subscribe(activity: Activity, sku: String, obfuscatedAccountId: String? = null, obfuscatedProfileId: String? = null) {
getBillingService().subscribe(activity, sku, obfuscatedAccountId, obfuscatedProfileId)
}

fun unsubscribe(activity: Activity, sku: String) {
Expand Down

0 comments on commit 394ba64

Please sign in to comment.