Skip to content

Commit a696668

Browse files
authored
Merge pull request #40 from epegasus/master
Google Play Billing
2 parents dba90e0 + 41b617e commit a696668

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ android {
3535

3636
dependencies {
3737

38-
implementation 'androidx.core:core-ktx:1.12.0'
38+
implementation 'androidx.core:core-ktx:1.13.0'
3939
implementation 'androidx.appcompat:appcompat:1.6.1'
4040
implementation 'com.google.android.material:material:1.11.0'
4141
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

billing/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ android {
2929

3030
dependencies {
3131

32-
implementation 'androidx.core:core-ktx:1.12.0'
32+
implementation 'androidx.core:core-ktx:1.13.0'
3333
implementation 'androidx.appcompat:appcompat:1.6.1'
3434

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

3838
// LiveData
3939
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.7.0"

billing/src/main/java/com/hypersoft/billing/asd.kt

Lines changed: 0 additions & 39 deletions
This file was deleted.

billing/src/main/java/com/hypersoft/billing/repository/BillingRepository.kt

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,25 @@ open class BillingRepository(context: Context) {
265265
val resultList = ArrayList<PurchaseDetail>()
266266

267267
val completePurchaseList = purchases.map { purchase ->
268+
Log.d(TAG, "BillingRepository: Purchase: $purchases")
268269
val productParams = queryUtils.getPurchaseParams(userQueryList, purchase.products)
269270
val productDetailsList = queryUtils.queryProductDetailsAsync(productParams)
270271
CompletePurchase(purchase, productDetailsList)
271272
}
272273

273274
completePurchaseList.forEach { completePurchase ->
275+
Log.d(TAG, "BillingRepository: CompletePurchase: $completePurchase")
276+
274277
completePurchase.productDetailList.forEach { productDetails ->
275278
val productType = if (productDetails.productType == BillingClient.ProductType.INAPP) ProductType.inapp else ProductType.subs
276-
val planId = queryUtils.getPlanId(productDetails.subscriptionOfferDetails)
277-
val planTitle = productDetails.subscriptionOfferDetails?.get(0)?.let { queryUtils.getPlanTitle(it) } ?: ""
279+
val splitList = completePurchase.purchase.accountIdentifiers?.obfuscatedAccountId?.split("_") ?: emptyList()
280+
val planId = if (splitList.isNotEmpty() && splitList.size >= 2) {
281+
splitList[1]
282+
} else {
283+
""
284+
}
285+
val offerDetails = productDetails.subscriptionOfferDetails?.find { it.basePlanId == planId }
286+
val planTitle = offerDetails?.let { queryUtils.getPlanTitle(it) } ?: ""
278287

279288
val purchaseDetail = PurchaseDetail(
280289
productId = productDetails.productId,
@@ -431,7 +440,7 @@ open class BillingRepository(context: Context) {
431440
&& it.productDetail.productType == ProductType.inapp
432441
}
433442
queryProductDetail?.let {
434-
launchFlow(activity = activity!!, it.productDetails, offerToken = null)
443+
launchFlow(activity = activity!!, "", it.productDetails, offerToken = null)
435444
} ?: run {
436445
Result.setResultState(ResultState.CONSOLE_PRODUCTS_IN_APP_NOT_EXIST)
437446
onPurchaseListener.onPurchaseResult(false, message = ResultState.CONSOLE_PRODUCTS_IN_APP_NOT_EXIST.message)
@@ -459,16 +468,33 @@ open class BillingRepository(context: Context) {
459468
return
460469
}
461470

462-
launchFlow(activity = activity!!, queryProductDetail.productDetails, offerToken = queryProductDetail.offerDetails.offerToken)
471+
launchFlow(activity = activity!!, planId, queryProductDetail.productDetails, offerToken = queryProductDetail.offerDetails.offerToken)
463472
}
464473

465-
private fun launchFlow(activity: Activity, productDetails: ProductDetails, offerToken: String?) {
474+
private fun launchFlow(activity: Activity, planId: String, productDetails: ProductDetails, offerToken: String?) {
466475
Log.i(TAG, "launchFlow: Product Details about to be purchase: $productDetails")
467476
val paramsList = when (offerToken == null) {
468477
true -> listOf(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).build())
469478
false -> listOf(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).setOfferToken(offerToken).build())
470479
}
471-
val flowParams = BillingFlowParams.newBuilder().setProductDetailsParamsList(paramsList).build()
480+
481+
val timeStamp = "${System.currentTimeMillis()}"
482+
val temp = "${timeStamp}_$planId"
483+
484+
val flowParams = if (planId.isEmpty()) {
485+
BillingFlowParams
486+
.newBuilder()
487+
.setProductDetailsParamsList(paramsList)
488+
.build()
489+
} else {
490+
BillingFlowParams
491+
.newBuilder()
492+
.setProductDetailsParamsList(paramsList)
493+
.setObfuscatedAccountId(temp)
494+
.setObfuscatedProfileId(timeStamp)
495+
.build()
496+
}
497+
472498
billingClient.launchBillingFlow(activity, flowParams)
473499
Result.setResultState(ResultState.LAUNCHING_FLOW_INVOCATION_SUCCESSFULLY)
474500
}
@@ -525,11 +551,24 @@ open class BillingRepository(context: Context) {
525551
.setSubscriptionReplacementMode(BillingFlowParams.SubscriptionUpdateParams.ReplacementMode.CHARGE_FULL_PRICE)
526552
.build()
527553

528-
val flowParams =
529-
BillingFlowParams.newBuilder()
554+
val timeStamp = "${System.currentTimeMillis()}"
555+
val temp = "${timeStamp}_$planId"
556+
557+
val flowParams = if (planId.isEmpty()) {
558+
BillingFlowParams
559+
.newBuilder()
530560
.setProductDetailsParamsList(paramsList)
531561
.setSubscriptionUpdateParams(updateParams)
532562
.build()
563+
} else {
564+
BillingFlowParams
565+
.newBuilder()
566+
.setProductDetailsParamsList(paramsList)
567+
.setSubscriptionUpdateParams(updateParams)
568+
.setObfuscatedAccountId(temp)
569+
.setObfuscatedProfileId(timeStamp)
570+
.build()
571+
}
533572

534573
billingClient.launchBillingFlow(activity!!, flowParams)
535574
Result.setResultState(ResultState.LAUNCHING_FLOW_INVOCATION_SUCCESSFULLY)

billing/src/main/java/com/hypersoft/billing/utils/QueryUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal class QueryUtils(private val billingClient: BillingClient) {
9696
* @param subscriptionOfferDetailList: find reliable base plan id for a product
9797
*
9898
* @return base plan title according to billingPeriod
99-
* @see [com.hypersoft.billing.latest.dataClasses.ProductDetail]
99+
* @see [com.hypersoft.billing.dataClasses.ProductDetail]
100100
*/
101101

102102
fun getPlanTitle(subscriptionOfferDetailList: ProductDetails.SubscriptionOfferDetails): String {

0 commit comments

Comments
 (0)