-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from tonkeeper/iap
battery iap
- Loading branch information
Showing
23 changed files
with
608 additions
and
28 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
apps/wallet/api/src/main/java/com/tonapps/wallet/api/entity/IAPPackageEntity.kt
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,19 @@ | ||
package com.tonapps.wallet.api.entity | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
import org.json.JSONObject | ||
|
||
@Parcelize | ||
data class IAPPackageEntity( | ||
val id: IAPPackageId, | ||
val userProceed: Double, | ||
val productId: String, | ||
) : Parcelable { | ||
|
||
constructor(json: JSONObject) : this( | ||
id = IAPPackageId.fromId(json.getString("id")), | ||
userProceed = json.getDouble("user_proceed"), | ||
productId = json.getString("package_id_android"), | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/wallet/api/src/main/java/com/tonapps/wallet/api/entity/IAPPackageId.kt
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,14 @@ | ||
package com.tonapps.wallet.api.entity | ||
|
||
enum class IAPPackageId(val id: String) { | ||
LARGE("large"), | ||
MEDIUM("medium"), | ||
SMALL("small"); | ||
|
||
companion object { | ||
fun fromId(id: String): IAPPackageId { | ||
return values().find { it.id.equals(id, ignoreCase = true) } | ||
?: throw IllegalArgumentException("Invalid IAPPackageId id: $id") | ||
} | ||
} | ||
} |
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
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
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
106 changes: 97 additions & 9 deletions
106
apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/billing/BillingManager.kt
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 |
---|---|---|
@@ -1,21 +1,109 @@ | ||
package com.tonapps.tonkeeper.billing | ||
|
||
import com.android.billingclient.api.Purchase | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import android.app.Activity | ||
import android.content.Context | ||
import com.android.billingclient.api.* | ||
import com.android.billingclient.api.BillingClient.ProductType | ||
import com.tonapps.extensions.MutableEffectFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.filterNotNull | ||
|
||
class BillingManager { | ||
class BillingManager( | ||
context: Context, | ||
) { | ||
|
||
private var billingClient: BillingClient | ||
|
||
// Example | ||
private val _purchasesFlow = MutableSharedFlow<List<Purchase>>() | ||
private val _purchasesFlow = MutableEffectFlow<List<Purchase>?>() | ||
val purchasesFlow = _purchasesFlow.asSharedFlow() | ||
|
||
private val purchasesUpdatedListener = { purchases: List<Purchase> -> | ||
// _purchasesFlow.value = purchases | ||
private val _productsFlow = MutableStateFlow<List<ProductDetails>?>(null) | ||
val productsFlow = _productsFlow.asStateFlow() | ||
|
||
private val purchasesUpdatedListener = PurchasesUpdatedListener { billingResult, purchases -> | ||
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) { | ||
_purchasesFlow.tryEmit(purchases) | ||
} else { | ||
_purchasesFlow.tryEmit(null) | ||
} | ||
} | ||
|
||
private var isInitialized = false | ||
|
||
init { | ||
billingClient = BillingClient.newBuilder(context) | ||
.setListener(purchasesUpdatedListener) | ||
.enablePendingPurchases() | ||
.build() | ||
|
||
billingClient.startConnection(object : BillingClientStateListener { | ||
override fun onBillingSetupFinished(billingResult: BillingResult) { | ||
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) { | ||
isInitialized = true | ||
} | ||
} | ||
|
||
override fun onBillingServiceDisconnected() { | ||
isInitialized = false | ||
} | ||
}) | ||
} | ||
|
||
fun getProducts( | ||
productIds: List<String>, | ||
productType: String = ProductType.INAPP | ||
) { | ||
if (!isInitialized || productIds.isEmpty()) { | ||
return | ||
} | ||
|
||
val productList = productIds.map { productId -> | ||
QueryProductDetailsParams.Product.newBuilder() | ||
.setProductId(productId) | ||
.setProductType(productType) | ||
.build() | ||
} | ||
|
||
val params = QueryProductDetailsParams.newBuilder() | ||
.setProductList(productList) | ||
.build() | ||
|
||
billingClient.queryProductDetailsAsync(params) { billingResult, productDetailsList -> | ||
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) { | ||
_productsFlow.value = productDetailsList | ||
} else { | ||
_productsFlow.value = emptyList() // In case of an error | ||
} | ||
} | ||
} | ||
|
||
// Method to request a purchase | ||
fun requestPurchase(activity: Activity, productDetails: ProductDetails) { | ||
val productDetailsParams = BillingFlowParams.ProductDetailsParams.newBuilder() | ||
.setProductDetails(productDetails) | ||
.build() | ||
|
||
val billingFlowParams = BillingFlowParams.newBuilder() | ||
.setProductDetailsParamsList(listOf(productDetailsParams)) | ||
.build() | ||
|
||
billingClient.launchBillingFlow(activity, billingFlowParams) | ||
} | ||
|
||
suspend fun restorePurchases() { | ||
val params = QueryPurchasesParams.newBuilder() | ||
.setProductType(ProductType.INAPP) | ||
|
||
billingClient.queryPurchasesAsync(params.build()) { billingResult, purchasesList -> | ||
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) { | ||
val pendingPurchases = purchasesList.filter { purchase -> | ||
purchase.purchaseState != Purchase.PurchaseState.PENDING | ||
} | ||
if (pendingPurchases.isNotEmpty()) { | ||
_purchasesFlow.tryEmit(pendingPurchases) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.