@@ -11,8 +11,10 @@ import ir.cafebazaar.poolakey.billing.BillingFunction
1111import ir.cafebazaar.poolakey.callback.PurchaseIntentCallback
1212import ir.cafebazaar.poolakey.constant.BazaarIntent
1313import ir.cafebazaar.poolakey.constant.Billing
14+ import ir.cafebazaar.poolakey.exception.DynamicPriceNotSupportedException
1415import ir.cafebazaar.poolakey.exception.ResultNotOkayException
1516import ir.cafebazaar.poolakey.request.PurchaseRequest
17+ import ir.cafebazaar.poolakey.request.purchaseExtraData
1618import ir.cafebazaar.poolakey.takeIf
1719
1820internal class PurchaseFunction (
@@ -34,38 +36,91 @@ internal class PurchaseFunction(
3436 .invoke(IllegalStateException (" Couldn't receive buy intent from Bazaar" ))
3537 }
3638
37- if (doesClientSupportIntentV2(purchaseConfigBundle)) {
38- getBuyIntentV2FromBillingService(
39- billingService,
40- purchaseRequest,
41- purchaseType,
42- callback
43- )?.takeIf (
44- thisIsTrue = { bundle ->
45- bundle.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY ) != null
46- }, andIfNot = intentResponseIsNullError
47- )?.getParcelable<Intent >(INTENT_RESPONSE_BUY )?.also { purchaseIntent ->
48- fireIntentWithIntent.invoke(purchaseIntent)
39+ when {
40+ isBazaarSupportIntentV3(purchaseConfigBundle) -> {
41+ launchBuyIntentV3(billingService, intentResponseIsNullError)
4942 }
50- } else {
51- getBuyIntentFromBillingService(
52- billingService,
53- purchaseRequest,
54- purchaseType,
55- callback
56- )?.takeIf (
57- thisIsTrue = { bundle ->
58- bundle.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY ) != null
59- }, andIfNot = intentResponseIsNullError
60- )?.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY )?.also { purchaseIntent ->
61- fireIntentWithIntentSender.invoke(purchaseIntent.intentSender)
43+ isBazaarSupportIntentV2(purchaseConfigBundle) -> {
44+ launchBuyIntentV2(billingService, intentResponseIsNullError)
45+ }
46+ else -> {
47+ launchBuyIntent(billingService, intentResponseIsNullError)
6248 }
6349 }
6450 } catch (e: RemoteException ) {
6551 PurchaseIntentCallback ().apply (callback).failedToBeginFlow.invoke(e)
6652 }
6753 }
6854
55+ private fun PurchaseFunctionRequest.launchBuyIntentV3 (
56+ billingService : IInAppBillingService ,
57+ intentResponseIsNullError : () -> Unit
58+ ) {
59+ getBuyIntentV3FromBillingService(
60+ billingService,
61+ purchaseRequest,
62+ purchaseRequest.purchaseExtraData(),
63+ callback
64+ )?.takeIf (
65+ thisIsTrue = { bundle ->
66+ bundle.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY ) != null
67+ }, andIfNot = intentResponseIsNullError
68+ )?.getParcelable<Intent >(INTENT_RESPONSE_BUY )?.also { purchaseIntent ->
69+ launchIntent.invoke(purchaseIntent)
70+ }
71+ }
72+
73+ private fun PurchaseFunctionRequest.launchBuyIntentV2 (
74+ billingService : IInAppBillingService ,
75+ intentResponseIsNullError : () -> Unit
76+ ) {
77+
78+ if (purchaseRequest.dynamicPriceToken.isNullOrEmpty().not ()) {
79+ PurchaseIntentCallback ().apply (callback).failedToBeginFlow.invoke(
80+ DynamicPriceNotSupportedException ()
81+ )
82+ return
83+ }
84+
85+ getBuyIntentV2FromBillingService(
86+ billingService,
87+ purchaseRequest,
88+ purchaseType,
89+ callback
90+ )?.takeIf (
91+ thisIsTrue = { bundle ->
92+ bundle.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY ) != null
93+ }, andIfNot = intentResponseIsNullError
94+ )?.getParcelable<Intent >(INTENT_RESPONSE_BUY )?.also { purchaseIntent ->
95+ launchIntent.invoke(purchaseIntent)
96+ }
97+ }
98+
99+ private fun PurchaseFunctionRequest.launchBuyIntent (
100+ billingService : IInAppBillingService ,
101+ intentResponseIsNullError : () -> Unit
102+ ) {
103+ if (purchaseRequest.dynamicPriceToken.isNullOrEmpty().not ()) {
104+ PurchaseIntentCallback ().apply (callback).failedToBeginFlow.invoke(
105+ DynamicPriceNotSupportedException ()
106+ )
107+ return
108+ }
109+
110+ getBuyIntentFromBillingService(
111+ billingService,
112+ purchaseRequest,
113+ purchaseType,
114+ callback
115+ )?.takeIf (
116+ thisIsTrue = { bundle ->
117+ bundle.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY ) != null
118+ }, andIfNot = intentResponseIsNullError
119+ )?.getParcelable<PendingIntent >(INTENT_RESPONSE_BUY )?.also { purchaseIntent ->
120+ launchIntentWithIntentSender.invoke(purchaseIntent.intentSender)
121+ }
122+ }
123+
69124 private inline fun getBuyIntentFromBillingService (
70125 billingService : IInAppBillingService ,
71126 purchaseRequest : PurchaseRequest ,
@@ -92,25 +147,43 @@ internal class PurchaseFunction(
92147 purchaseRequest.payload
93148 )?.takeBundleIfResponseIsOk(callback)
94149
150+ private inline fun getBuyIntentV3FromBillingService (
151+ billingService : IInAppBillingService ,
152+ purchaseRequest : PurchaseRequest ,
153+ extraData : Bundle ,
154+ callback : PurchaseIntentCallback .() -> Unit
155+ ) = billingService.getBuyIntentV3(
156+ Billing .IN_APP_BILLING_VERSION ,
157+ context.packageName,
158+ purchaseRequest.productId,
159+ purchaseRequest.payload,
160+ extraData
161+ )?.takeBundleIfResponseIsOk(callback)
162+
95163 private inline fun Bundle.takeBundleIfResponseIsOk (
96164 callback : PurchaseIntentCallback .() -> Unit
97165 ): Bundle ? = takeIf (
98166 thisIsTrue = { bundle ->
99167 bundle.get(BazaarIntent .RESPONSE_CODE ) == BazaarIntent .RESPONSE_RESULT_OK
100168 }, andIfNot = {
101- PurchaseIntentCallback ().apply (callback)
102- .failedToBeginFlow
103- .invoke(ResultNotOkayException ())
169+ PurchaseIntentCallback ().apply (callback)
170+ .failedToBeginFlow
171+ .invoke(ResultNotOkayException ())
104172 }
105173 )
106174
107- private fun doesClientSupportIntentV2 (purchaseConfigBundle : Bundle ? ): Boolean {
175+ private fun isBazaarSupportIntentV2 (purchaseConfigBundle : Bundle ? ): Boolean {
108176 return purchaseConfigBundle?.getBoolean(INTENT_V2_SUPPORT ) ? : false
109177 }
110178
179+ private fun isBazaarSupportIntentV3 (purchaseConfigBundle : Bundle ? ): Boolean {
180+ return purchaseConfigBundle?.getBoolean(INTENT_V3_SUPPORT ) ? : false
181+ }
182+
111183 companion object {
112184 private const val INTENT_RESPONSE_BUY = " BUY_INTENT"
113185 private const val INTENT_V2_SUPPORT = " INTENT_V2_SUPPORT"
186+ private const val INTENT_V3_SUPPORT = " INTENT_V3_SUPPORT"
114187 }
115188
116189}
0 commit comments