Skip to content

Commit

Permalink
Merge pull request #39 from epegasus/master
Browse files Browse the repository at this point in the history
Billing
  • Loading branch information
hypersoftdev authored Mar 14, 2024
2 parents 77c1c16 + 523fa5e commit dba90e0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
39 changes: 39 additions & 0 deletions billing/src/main/java/com/hypersoft/billing/asd.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hypersoft.billing

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

/**
* @Author: SOHAIB AHMED
* @Date: 14/03/2024
* @Accounts
* -> https://github.com/epegasus
* -> https://stackoverflow.com/users/20440272/sohaib-ahmed
*/


fun main() {
runBlocking {
for (i in 0..50) {
val data = fetchData(i)
println(data)
}
}
}

suspend fun fetchData(index: Int): String {
println("Processing : $index")
return suspendCancellableCoroutine { continuation ->
CoroutineScope(Dispatchers.IO).launch {
delay(1000)
if (continuation.isActive) {
continuation.resume("Result # $index")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ enum class ResultState(val message: String) {
CONNECTION_ESTABLISHED("Connection has been established to Google Play Console"),
CONNECTION_FAILED("Failed to connect Google Play Console"),

// Purchases
USER_QUERY_LIST_EMPTY("User query list is empty"),

// Purchases
CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING("InApp -> Fetching purchased products from google play console."),
CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING_FAILED("InApp -> Failed to fetch purchased products from google play console."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ open class BillingRepository(context: Context) {
} else {
productDetail.planTitle = queryUtils.getPlanTitle(pricingPhase.billingPeriod)
productDetail.currencyCode = pricingPhase.priceCurrencyCode
productDetail.price = pricingPhase.formattedPrice
productDetail.price = pricingPhase.formattedPrice.removeSuffix(".00")
productDetail.priceAmountMicros = pricingPhase.priceAmountMicros
productDetail.billingPeriod = pricingPhase.billingPeriod
}
Expand Down
34 changes: 14 additions & 20 deletions billing/src/main/java/com/hypersoft/billing/utils/QueryUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,25 @@ internal class QueryUtils(private val billingClient: BillingClient) {
}

suspend fun queryProductDetailsAsync(params: List<QueryProductDetailsParams.Product>): List<ProductDetails> {
if (billingClient.isReady.not() || params.isEmpty()) {
if (billingClient.isReady.not()) {
Result.setResultState(ResultState.CONNECTION_INVALID)
return emptyList()
}
if (params.isEmpty()) {
Result.setResultState(ResultState.USER_QUERY_LIST_EMPTY)
return emptyList()
}
val queryParams = QueryProductDetailsParams.newBuilder().setProductList(params).build()
return suspendCancellableCoroutine { continuation ->
if (continuation.isActive) {
billingClient.queryProductDetailsAsync(queryParams) { billingResult, productDetailsList ->
if (BillingResponse(billingResult.responseCode).isOk) {
if (continuation.isActive){
try {
continuation.resume(productDetailsList)
}catch (ex:Exception){
Log.e(TAG, "${ex.message}")
}
}
} else {
if (continuation.isActive){
try {
continuation.resume(emptyList())
}catch (ex:Exception){
Log.e(TAG, "${ex.message}")
}
}
Log.e(TAG, "queryProductDetailsAsync: Failed to query product details. Response code: ${billingResult.responseCode}")
billingClient.queryProductDetailsAsync(queryParams) { billingResult, productDetailsList ->
if (BillingResponse(billingResult.responseCode).isOk) {
if (continuation.isActive) {
continuation.resume(productDetailsList)
}
} else {
Log.e(TAG, "queryProductDetailsAsync: Failed to query product details. Response code: ${billingResult.responseCode}")
if (continuation.isActive) {
continuation.resume(emptyList())
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions billing/src/main/java/com/hypersoft/billing/utils/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ object Result {
ResultState.CONNECTION_FAILED -> ResultState.CONNECTION_FAILED.message
ResultState.ACTIVITY_REFERENCE_NOT_FOUND -> ResultState.ACTIVITY_REFERENCE_NOT_FOUND.message

ResultState.USER_QUERY_LIST_EMPTY -> ResultState.USER_QUERY_LIST_EMPTY.message

ResultState.CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING -> ResultState.CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING.message
ResultState.CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING_FAILED -> ResultState.CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING_FAILED.message
ResultState.CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING_SUCCESS -> ResultState.CONSOLE_PURCHASE_PRODUCTS_INAPP_FETCHING_SUCCESS.message
Expand Down

0 comments on commit dba90e0

Please sign in to comment.