Skip to content

Commit

Permalink
Merge pull request #26 from eggheadgames/improve-library-usability
Browse files Browse the repository at this point in the history
Improve library usability
  • Loading branch information
mikemee authored May 31, 2020
2 parents 0d563c5 + d6adca2 commit 7d40078
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 195 deletions.
91 changes: 0 additions & 91 deletions library/src/main/java/com/billing/BillingService.java

This file was deleted.

99 changes: 99 additions & 0 deletions library/src/main/java/com/billing/BillingService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.billing

import android.app.Activity
import android.os.Handler
import android.os.Looper
import androidx.annotation.CallSuper

abstract class BillingService {

private val purchaseServiceListeners: MutableList<PurchaseServiceListener> = mutableListOf()
private val subscriptionServiceListeners: MutableList<SubscriptionServiceListener> = mutableListOf()

fun addPurchaseListener(purchaseServiceListener: PurchaseServiceListener) {
purchaseServiceListeners.add(purchaseServiceListener)
}

fun removePurchaseListener(purchaseServiceListener: PurchaseServiceListener) {
purchaseServiceListeners.remove(purchaseServiceListener)
}

fun addSubscriptionListener(subscriptionServiceListener: SubscriptionServiceListener) {
subscriptionServiceListeners.add(subscriptionServiceListener)
}

fun removeSubscriptionListener(subscriptionServiceListener: SubscriptionServiceListener) {
subscriptionServiceListeners.remove(subscriptionServiceListener)
}

/**
* @param sku - product specificator
* @param isRestore - a flag indicating whether it's a fresh purchase or restored product
*/
fun productOwned(sku: String?, isRestore: Boolean) {
findUiHandler().post {
productOwnedInternal(sku, isRestore)
}
}

fun productOwnedInternal(sku: String?, isRestore: Boolean) {
for (purchaseServiceListener in purchaseServiceListeners) {
if (isRestore) {
purchaseServiceListener.onProductRestored(sku)
} else {
purchaseServiceListener.onProductPurchased(sku)
}
}
}

/**
* @param sku - subscription specificator
* @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription
*/
fun subscriptionOwned(sku: String, isRestore: Boolean) {
findUiHandler().post {
subscriptionOwnedInternal(sku, isRestore)
}
}

fun subscriptionOwnedInternal(sku: String, isRestore: Boolean) {
for (subscriptionServiceListener in subscriptionServiceListeners) {
if (isRestore) {
subscriptionServiceListener.onSubscriptionRestored(sku)
} else {
subscriptionServiceListener.onSubscriptionPurchased(sku)
}
}
}

fun updatePrices(iapkeyPrices: Map<String, String>) {
findUiHandler().post {
updatePricesInternal(iapkeyPrices)
}
}

fun updatePricesInternal(iapkeyPrices: Map<String, String>) {
for (billingServiceListener in purchaseServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
}
for (billingServiceListener in subscriptionServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
}
}

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

@CallSuper
open fun close() {
subscriptionServiceListeners.clear()
purchaseServiceListeners.clear()
}
}

fun findUiHandler(): Handler {
return Handler(Looper.getMainLooper())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AmazonBillingService(val context: Context, private val iapKeys: List<Strin

private var mAmazonBillingListener: AmazonBillingListener? = null

override fun init(key: String) {
override fun init(key: String?) {
mAmazonBillingListener = AmazonBillingListener(this)
PurchasingService.registerListener(context, mAmazonBillingListener)

Expand All @@ -22,15 +22,15 @@ class AmazonBillingService(val context: Context, private val iapKeys: List<Strin
PurchasingService.getPurchaseUpdates(true)
}

override fun buy(activity: Activity, sku: String, id: Int) {
override fun buy(activity: Activity, sku: String) {
PurchasingService.purchase(sku)
}

override fun subscribe(activity: Activity, sku: String, id: Int) {
override fun subscribe(activity: Activity, sku: String) {
PurchasingService.purchase(sku)
}

override fun unsubscribe(activity: Activity, sku: String, id: Int) {
override fun unsubscribe(activity: Activity, sku: String) {
try {
val intent = Intent()
intent.action = Intent.ACTION_VIEW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class GoogleBillingService2(val context: Context, private val inAppSkuKeys: List
: BillingService(), PurchasesUpdatedListener, BillingClientStateListener, AcknowledgePurchaseResponseListener {

private lateinit var mBillingClient: BillingClient
private lateinit var decodedKey: String
private var decodedKey: String? = null

private var enableDebug: Boolean = false

private val skusDetails = mutableMapOf<String, SkuDetails?>()

override fun init(key: String) {
override fun init(key: String?) {
decodedKey = key

mBillingClient = BillingClient.newBuilder(context).setListener(this).enablePendingPurchases().build()
Expand Down Expand Up @@ -51,7 +51,7 @@ class GoogleBillingService2(val context: Context, private val inAppSkuKeys: List
}
}

override fun buy(activity: Activity, sku: String, id: Int) {
override fun buy(activity: Activity, sku: String) {
if (!sku.isSkuReady()) {
log("buy. Google billing service is not ready yet.")
return
Expand All @@ -60,7 +60,7 @@ class GoogleBillingService2(val context: Context, private val inAppSkuKeys: List
launchBillingFlow(activity, sku, BillingClient.SkuType.INAPP)
}

override fun subscribe(activity: Activity, sku: String, id: Int) {
override fun subscribe(activity: Activity, sku: String) {
if (!sku.isSkuReady()) {
log("buy. Google billing service is not ready yet.")
return
Expand All @@ -77,7 +77,7 @@ class GoogleBillingService2(val context: Context, private val inAppSkuKeys: List
}
}

override fun unsubscribe(activity: Activity, sku: String, id: Int) {
override fun unsubscribe(activity: Activity, sku: String) {
try {
val intent = Intent()
intent.action = Intent.ACTION_VIEW
Expand Down Expand Up @@ -165,7 +165,8 @@ class GoogleBillingService2(val context: Context, private val inAppSkuKeys: List
}

private fun isSignatureValid(purchase: Purchase): Boolean {
return Security.verifyPurchase(decodedKey, purchase.originalJson, purchase.signature)
val key = decodedKey ?: return true
return Security.verifyPurchase(key, purchase.originalJson, purchase.signature)
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit 7d40078

Please sign in to comment.