@@ -265,16 +265,25 @@ open class BillingRepository(context: Context) {
265
265
val resultList = ArrayList <PurchaseDetail >()
266
266
267
267
val completePurchaseList = purchases.map { purchase ->
268
+ Log .d(TAG , " BillingRepository: Purchase: $purchases " )
268
269
val productParams = queryUtils.getPurchaseParams(userQueryList, purchase.products)
269
270
val productDetailsList = queryUtils.queryProductDetailsAsync(productParams)
270
271
CompletePurchase (purchase, productDetailsList)
271
272
}
272
273
273
274
completePurchaseList.forEach { completePurchase ->
275
+ Log .d(TAG , " BillingRepository: CompletePurchase: $completePurchase " )
276
+
274
277
completePurchase.productDetailList.forEach { productDetails ->
275
278
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) } ? : " "
278
287
279
288
val purchaseDetail = PurchaseDetail (
280
289
productId = productDetails.productId,
@@ -431,7 +440,7 @@ open class BillingRepository(context: Context) {
431
440
&& it.productDetail.productType == ProductType .inapp
432
441
}
433
442
queryProductDetail?.let {
434
- launchFlow(activity = activity!! , it.productDetails, offerToken = null )
443
+ launchFlow(activity = activity!! , " " , it.productDetails, offerToken = null )
435
444
} ? : run {
436
445
Result .setResultState(ResultState .CONSOLE_PRODUCTS_IN_APP_NOT_EXIST )
437
446
onPurchaseListener.onPurchaseResult(false , message = ResultState .CONSOLE_PRODUCTS_IN_APP_NOT_EXIST .message)
@@ -459,16 +468,33 @@ open class BillingRepository(context: Context) {
459
468
return
460
469
}
461
470
462
- launchFlow(activity = activity!! , queryProductDetail.productDetails, offerToken = queryProductDetail.offerDetails.offerToken)
471
+ launchFlow(activity = activity!! , planId, queryProductDetail.productDetails, offerToken = queryProductDetail.offerDetails.offerToken)
463
472
}
464
473
465
- private fun launchFlow (activity : Activity , productDetails : ProductDetails , offerToken : String? ) {
474
+ private fun launchFlow (activity : Activity , planId : String , productDetails : ProductDetails , offerToken : String? ) {
466
475
Log .i(TAG , " launchFlow: Product Details about to be purchase: $productDetails " )
467
476
val paramsList = when (offerToken == null ) {
468
477
true -> listOf (BillingFlowParams .ProductDetailsParams .newBuilder().setProductDetails(productDetails).build())
469
478
false -> listOf (BillingFlowParams .ProductDetailsParams .newBuilder().setProductDetails(productDetails).setOfferToken(offerToken).build())
470
479
}
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
+
472
498
billingClient.launchBillingFlow(activity, flowParams)
473
499
Result .setResultState(ResultState .LAUNCHING_FLOW_INVOCATION_SUCCESSFULLY )
474
500
}
@@ -525,11 +551,24 @@ open class BillingRepository(context: Context) {
525
551
.setSubscriptionReplacementMode(BillingFlowParams .SubscriptionUpdateParams .ReplacementMode .CHARGE_FULL_PRICE )
526
552
.build()
527
553
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()
530
560
.setProductDetailsParamsList(paramsList)
531
561
.setSubscriptionUpdateParams(updateParams)
532
562
.build()
563
+ } else {
564
+ BillingFlowParams
565
+ .newBuilder()
566
+ .setProductDetailsParamsList(paramsList)
567
+ .setSubscriptionUpdateParams(updateParams)
568
+ .setObfuscatedAccountId(temp)
569
+ .setObfuscatedProfileId(timeStamp)
570
+ .build()
571
+ }
533
572
534
573
billingClient.launchBillingFlow(activity!! , flowParams)
535
574
Result .setResultState(ResultState .LAUNCHING_FLOW_INVOCATION_SUCCESSFULLY )
0 commit comments