Skip to content

Commit

Permalink
Merge pull request #40 from epegasus/master
Browse files Browse the repository at this point in the history
Google Play Billing
  • Loading branch information
hypersoftdev committed Apr 22, 2024
2 parents dba90e0 + 41b617e commit a696668
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.core:core-ktx:1.13.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
Expand Down
4 changes: 2 additions & 2 deletions billing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.core:core-ktx:1.13.0'
implementation 'androidx.appcompat:appcompat:1.6.1'

// Google Play Billing
implementation "com.android.billingclient:billing-ktx:6.1.0"
implementation "com.android.billingclient:billing-ktx:6.2.1"

// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.7.0"
Expand Down
39 changes: 0 additions & 39 deletions billing/src/main/java/com/hypersoft/billing/asd.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,25 @@ open class BillingRepository(context: Context) {
val resultList = ArrayList<PurchaseDetail>()

val completePurchaseList = purchases.map { purchase ->
Log.d(TAG, "BillingRepository: Purchase: $purchases")
val productParams = queryUtils.getPurchaseParams(userQueryList, purchase.products)
val productDetailsList = queryUtils.queryProductDetailsAsync(productParams)
CompletePurchase(purchase, productDetailsList)
}

completePurchaseList.forEach { completePurchase ->
Log.d(TAG, "BillingRepository: CompletePurchase: $completePurchase")

completePurchase.productDetailList.forEach { productDetails ->
val productType = if (productDetails.productType == BillingClient.ProductType.INAPP) ProductType.inapp else ProductType.subs
val planId = queryUtils.getPlanId(productDetails.subscriptionOfferDetails)
val planTitle = productDetails.subscriptionOfferDetails?.get(0)?.let { queryUtils.getPlanTitle(it) } ?: ""
val splitList = completePurchase.purchase.accountIdentifiers?.obfuscatedAccountId?.split("_") ?: emptyList()
val planId = if (splitList.isNotEmpty() && splitList.size >= 2) {
splitList[1]
} else {
""
}
val offerDetails = productDetails.subscriptionOfferDetails?.find { it.basePlanId == planId }
val planTitle = offerDetails?.let { queryUtils.getPlanTitle(it) } ?: ""

val purchaseDetail = PurchaseDetail(
productId = productDetails.productId,
Expand Down Expand Up @@ -431,7 +440,7 @@ open class BillingRepository(context: Context) {
&& it.productDetail.productType == ProductType.inapp
}
queryProductDetail?.let {
launchFlow(activity = activity!!, it.productDetails, offerToken = null)
launchFlow(activity = activity!!, "", it.productDetails, offerToken = null)
} ?: run {
Result.setResultState(ResultState.CONSOLE_PRODUCTS_IN_APP_NOT_EXIST)
onPurchaseListener.onPurchaseResult(false, message = ResultState.CONSOLE_PRODUCTS_IN_APP_NOT_EXIST.message)
Expand Down Expand Up @@ -459,16 +468,33 @@ open class BillingRepository(context: Context) {
return
}

launchFlow(activity = activity!!, queryProductDetail.productDetails, offerToken = queryProductDetail.offerDetails.offerToken)
launchFlow(activity = activity!!, planId, queryProductDetail.productDetails, offerToken = queryProductDetail.offerDetails.offerToken)
}

private fun launchFlow(activity: Activity, productDetails: ProductDetails, offerToken: String?) {
private fun launchFlow(activity: Activity, planId: String, productDetails: ProductDetails, offerToken: String?) {
Log.i(TAG, "launchFlow: Product Details about to be purchase: $productDetails")
val paramsList = when (offerToken == null) {
true -> listOf(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).build())
false -> listOf(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).setOfferToken(offerToken).build())
}
val flowParams = BillingFlowParams.newBuilder().setProductDetailsParamsList(paramsList).build()

val timeStamp = "${System.currentTimeMillis()}"
val temp = "${timeStamp}_$planId"

val flowParams = if (planId.isEmpty()) {
BillingFlowParams
.newBuilder()
.setProductDetailsParamsList(paramsList)
.build()
} else {
BillingFlowParams
.newBuilder()
.setProductDetailsParamsList(paramsList)
.setObfuscatedAccountId(temp)
.setObfuscatedProfileId(timeStamp)
.build()
}

billingClient.launchBillingFlow(activity, flowParams)
Result.setResultState(ResultState.LAUNCHING_FLOW_INVOCATION_SUCCESSFULLY)
}
Expand Down Expand Up @@ -525,11 +551,24 @@ open class BillingRepository(context: Context) {
.setSubscriptionReplacementMode(BillingFlowParams.SubscriptionUpdateParams.ReplacementMode.CHARGE_FULL_PRICE)
.build()

val flowParams =
BillingFlowParams.newBuilder()
val timeStamp = "${System.currentTimeMillis()}"
val temp = "${timeStamp}_$planId"

val flowParams = if (planId.isEmpty()) {
BillingFlowParams
.newBuilder()
.setProductDetailsParamsList(paramsList)
.setSubscriptionUpdateParams(updateParams)
.build()
} else {
BillingFlowParams
.newBuilder()
.setProductDetailsParamsList(paramsList)
.setSubscriptionUpdateParams(updateParams)
.setObfuscatedAccountId(temp)
.setObfuscatedProfileId(timeStamp)
.build()
}

billingClient.launchBillingFlow(activity!!, flowParams)
Result.setResultState(ResultState.LAUNCHING_FLOW_INVOCATION_SUCCESSFULLY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal class QueryUtils(private val billingClient: BillingClient) {
* @param subscriptionOfferDetailList: find reliable base plan id for a product
*
* @return base plan title according to billingPeriod
* @see [com.hypersoft.billing.latest.dataClasses.ProductDetail]
* @see [com.hypersoft.billing.dataClasses.ProductDetail]
*/

fun getPlanTitle(subscriptionOfferDetailList: ProductDetails.SubscriptionOfferDetails): String {
Expand Down

0 comments on commit a696668

Please sign in to comment.