-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Dibrivnyi
committed
May 30, 2020
1 parent
3c0c34b
commit c2a7b13
Showing
2 changed files
with
75 additions
and
91 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.billing | ||
|
||
import android.app.Activity | ||
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) { | ||
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) { | ||
for (subscriptionServiceListener in subscriptionServiceListeners) { | ||
if (isRestore) { | ||
subscriptionServiceListener.onSubscriptionRestored(sku) | ||
} else { | ||
subscriptionServiceListener.onSubscriptionPurchased(sku) | ||
} | ||
} | ||
} | ||
|
||
fun updatePrices(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() | ||
} | ||
} |