Skip to content

Commit 9299092

Browse files
committed
draft PR of all updates so far
1 parent 6d1a11a commit 9299092

File tree

10 files changed

+317
-27
lines changed

10 files changed

+317
-27
lines changed

Branch-SDK/build.gradle.kts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ jacoco {
1313
toolVersion = "0.8.10"
1414
}
1515

16-
configurations.all {
17-
resolutionStrategy {
18-
force("com.android.billingclient:billing:5.0.0")
19-
}
20-
}
16+
//configurations.all {
17+
// resolutionStrategy {
18+
// force("com.android.billingclient:billing:8.0.0")
19+
// }
20+
//}
21+
2122
dependencies {
2223
implementation(fileTree(mapOf("dir" to "libs", "include" to "*.jar")))
2324
implementation(kotlin("stdlib"))
@@ -140,6 +141,18 @@ android {
140141
signing {
141142
isRequired = isReleaseBuild()
142143
}
144+
145+
flavorDimensions.add("billing")
146+
147+
productFlavors {
148+
create("billing_v6v7") {
149+
dimension = "billing"
150+
}
151+
create("billing_v8") {
152+
dimension = "billing"
153+
}
154+
}
155+
143156
}
144157

145158
fun getRepositoryUsername(): String {
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package io.branch.referral
2+
3+
import android.content.Context
4+
import com.android.billingclient.api.*
5+
import io.branch.indexing.BranchUniversalObject
6+
import io.branch.referral.util.*
7+
import java.math.BigDecimal
8+
9+
class BillingGooglePlayV6V7 : BillingGooglePlayInterface {
10+
lateinit var billingClient: BillingClient
11+
12+
companion object {
13+
@Volatile
14+
private lateinit var instance: BillingGooglePlay
15+
16+
fun getInstance(): BillingGooglePlay {
17+
synchronized(this) {
18+
if (!::instance.isInitialized) {
19+
instance = BillingGooglePlay()
20+
21+
instance.billingClient =
22+
BillingClient.newBuilder(Branch.getInstance().applicationContext)
23+
.setListener(instance.purchasesUpdatedListener)
24+
.enablePendingPurchases()
25+
.build()
26+
}
27+
return instance
28+
}
29+
}
30+
}
31+
32+
33+
override fun startBillingClient(callback: (Boolean) -> Unit) {
34+
if (billingClient.isReady) {
35+
BranchLogger.v("Billing Client has already been started..")
36+
callback(true)
37+
} else {
38+
billingClient.startConnection(object : BillingClientStateListener {
39+
override fun onBillingSetupFinished(billingResult: BillingResult) {
40+
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
41+
BranchLogger.v("Billing Client setup finished.")
42+
callback(true)
43+
} else {
44+
val errorMessage =
45+
"Billing Client setup failed with error: ${billingResult.debugMessage}"
46+
BranchLogger.e(errorMessage)
47+
callback(false)
48+
}
49+
}
50+
51+
override fun onBillingServiceDisconnected() {
52+
BranchLogger.w("Billing Client disconnected")
53+
callback(false)
54+
}
55+
})
56+
}
57+
}
58+
59+
override fun logEventWithPurchase(
60+
context: Context,
61+
purchase: Purchase
62+
) {
63+
val productIds = purchase.products
64+
val productList: MutableList<QueryProductDetailsParams.Product> = ArrayList()
65+
val subsList: MutableList<QueryProductDetailsParams.Product> = ArrayList()
66+
67+
for (productId: String? in productIds) {
68+
val inAppProduct = QueryProductDetailsParams.Product.newBuilder()
69+
.setProductId(productId!!)
70+
.setProductType(BillingClient.ProductType.INAPP)
71+
.build()
72+
productList.add(inAppProduct)
73+
74+
val subsProduct = QueryProductDetailsParams.Product.newBuilder()
75+
.setProductId(productId)
76+
.setProductType(BillingClient.ProductType.SUBS)
77+
.build()
78+
subsList.add(subsProduct)
79+
}
80+
81+
val queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
82+
.setProductList(productList)
83+
.build()
84+
85+
val querySubsProductDetailsParams = QueryProductDetailsParams.newBuilder()
86+
.setProductList(subsList)
87+
.build()
88+
89+
billingClient.queryProductDetailsAsync(
90+
querySubsProductDetailsParams
91+
) { billingResult: BillingResult, subsProductDetailsList: List<ProductDetails?> ->
92+
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
93+
val contentItemBUOs: MutableList<BranchUniversalObject> =
94+
ArrayList()
95+
var currency: CurrencyType = CurrencyType.USD
96+
var revenue = 0.00
97+
98+
for (product: ProductDetails? in subsProductDetailsList) {
99+
val buo: BranchUniversalObject = createBUOWithSubsProductDetails(product)
100+
contentItemBUOs.add(buo)
101+
102+
revenue += buo.contentMetadata.price
103+
currency = buo.contentMetadata.currencyType
104+
}
105+
106+
if (contentItemBUOs.isNotEmpty()) {
107+
createAndLogEventForPurchase(
108+
context,
109+
purchase,
110+
contentItemBUOs,
111+
currency,
112+
revenue,
113+
BillingClient.ProductType.SUBS
114+
)
115+
}
116+
}
117+
else {
118+
BranchLogger.e("Failed to query subscriptions. Error: " + billingResult.debugMessage)
119+
}
120+
}
121+
122+
billingClient.queryProductDetailsAsync(
123+
queryProductDetailsParams
124+
) { billingResult: BillingResult, productDetailsList: List<ProductDetails?> ->
125+
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
126+
127+
val contentItemBUOs: MutableList<BranchUniversalObject> =
128+
ArrayList()
129+
var currency: CurrencyType = CurrencyType.USD
130+
var revenue = 0.00
131+
val quantity: Int = purchase.quantity
132+
133+
for (product: ProductDetails? in productDetailsList) {
134+
val buo: BranchUniversalObject =
135+
createBUOWithInAppProductDetails(product, quantity)
136+
contentItemBUOs.add(buo)
137+
138+
revenue += (BigDecimal(buo.contentMetadata.price.toString()) * BigDecimal(
139+
quantity.toString()
140+
)).toDouble()
141+
currency = buo.contentMetadata.currencyType
142+
}
143+
144+
if (contentItemBUOs.isNotEmpty()) {
145+
createAndLogEventForPurchase(
146+
context,
147+
purchase,
148+
contentItemBUOs,
149+
currency,
150+
revenue,
151+
BillingClient.ProductType.INAPP
152+
)
153+
}
154+
}
155+
else {
156+
BranchLogger.e("Failed to query subscriptions. Error: " + billingResult.debugMessage)
157+
}
158+
}
159+
}
160+
161+
162+
163+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
compileSdk ANDROID_BUILD_SDK_VERSION_COMPILE.toInt() // Or your project's compile SDK version
8+
9+
defaultConfig {
10+
minSdk = ANDROID_BUILD_SDK_VERSION_MINIMUM.toInt()
11+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
12+
consumerProguardFiles("proguard-consumer.txt")
13+
}
14+
15+
// This section is for library-specific configurations
16+
buildTypes {
17+
fun String.wrapInQuotes(): String {
18+
return "\"$this\""
19+
}
20+
21+
debug {
22+
enableUnitTestCoverage = true
23+
enableAndroidTestCoverage = true
24+
buildConfigField("long", "VERSION_CODE", VERSION_CODE)
25+
buildConfigField("String", "VERSION_NAME", VERSION_NAME.wrapInQuotes())
26+
}
27+
release {
28+
buildConfigField("long", "VERSION_CODE", VERSION_CODE)
29+
buildConfigField("String", "VERSION_NAME", VERSION_NAME.wrapInQuotes())
30+
}
31+
}
32+
}
33+
34+
dependencies {
35+
// This is the older V6 version of the billing library
36+
implementation 'com.android.billingclient:billing:6.0.1'
37+
38+
// Add any other dependencies specific to this module
39+
implementation 'androidx.core:core-ktx:1.9.0'
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.branch.referral
2+
3+
import android.content.Context
4+
import com.android.billingclient.api.*
5+
6+
class BillingGooglePlayV8 : BillingGooglePlayInterface{
7+
override fun startBillingClient(context: Context?) {
8+
TODO("Not yet implemented")
9+
}
10+
11+
override fun logEventWithPurchase(
12+
context: Context?,
13+
purchase: Purchase?
14+
) {
15+
TODO("Not yet implemented")
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
compileSdk ANDROID_BUILD_SDK_VERSION_COMPILE.toInt() // Or your project's compile SDK version
8+
9+
defaultConfig {
10+
minSdk = ANDROID_BUILD_SDK_VERSION_MINIMUM.toInt()
11+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
12+
consumerProguardFiles("proguard-consumer.txt")
13+
}
14+
15+
// This section is for library-specific configurations
16+
buildTypes {
17+
fun String.wrapInQuotes(): String {
18+
return "\"$this\""
19+
}
20+
21+
debug {
22+
enableUnitTestCoverage = true
23+
enableAndroidTestCoverage = true
24+
buildConfigField("long", "VERSION_CODE", VERSION_CODE)
25+
buildConfigField("String", "VERSION_NAME", VERSION_NAME.wrapInQuotes())
26+
}
27+
release {
28+
buildConfigField("long", "VERSION_CODE", VERSION_CODE)
29+
buildConfigField("String", "VERSION_NAME", VERSION_NAME.wrapInQuotes())
30+
}
31+
}
32+
}
33+
34+
dependencies {
35+
// This is the older V6 version of the billing library
36+
implementation 'com.android.billingclient:billing:8.0.0'
37+
38+
// Add any other dependencies specific to this module
39+
implementation 'androidx.core:core-ktx:1.9.0'
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.branch.referral;
2+
3+
import io.branch.referral.BillingGooglePlayV6V7;
4+
import java.lang.ClassNotFoundException;
5+
6+
public class BillingGooglePlayReflection {
7+
public static BillingGooglePlayInterface getBillingLibraryVersion() {
8+
return new BillingGooglePlayV6V7();
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.branch.referral;
2+
3+
import io.branch.referral.BillingGooglePlayV8;
4+
import java.lang.ClassNotFoundException;
5+
6+
public class BillingGooglePlayReflection {
7+
public static BillingGooglePlayInterface getBillingLibraryVersion() {
8+
return new BillingGooglePlayV8();
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.branch.referral;
2+
3+
import android.content.Context;
4+
import io.branch.referral.util.*;
5+
6+
import com.android.billingclient.api.Purchase;
7+
8+
public interface BillingGooglePlayInterface {
9+
fun logEventWithPurchase(context: Context, purchase: Purchase);
10+
fun startBillingClient(callback: (Boolean) -> Unit);
11+
}

Branch-SDK/src/main/java/io/branch/referral/BillingGooglePlayReflection.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,27 @@
33
import java.lang.ClassNotFoundException;
44

55
public class BillingGooglePlayReflection {
6-
public static String getBillingLibraryVersion() {
6+
public static BillingGooglePlayInterface getBillingLibraryVersion() {
77
try {
88
// Check for a class added in version 8.0 or higher
99
Class billingClientBuilderClass = Class.forName("com.android.billingclient.api.BillingClient$Builder");
1010
billingClientBuilderClass.getMethod("enableAutoServiceReconnection");
11-
return "Version 8.0 or higher";
11+
return new BillingGooglePlayV8();
1212
} catch (NoSuchMethodException | ClassNotFoundException version8CheckFailed) {
1313
try {
1414
// Check for a class added in version 7.0 or higher
1515
Class.forName("com.android.billingclient.api.ProductDetails$InstallmentPlanDetails");
16-
return "Version 7.0 or higher";
16+
17+
return new BillingGooglePlayV6V7();
1718
} catch (ClassNotFoundException version7CheckFailed) {
1819
try {
1920
// Check for a class added in version 6.0 or higher
2021
Class.forName("com.android.billingclient.api.BillingFlowParams$SubscriptionUpdateParams$ReplacementMode");
21-
return "Version 6.0 or higher";
22+
return new BillingGooglePlayV6V7();
2223
} catch (ClassNotFoundException version6CheckFailed) {
23-
try {
24-
// Check for the ProductDetails class, introduced in version 5.0
25-
Class.forName("com.android.billingclient.api.ProductDetails");
26-
return "Version 5.0 or higher";
27-
} catch (ClassNotFoundException version5CheckFailed) {
28-
try {
29-
// If ProductDetails is not found, check for the older SkuDetails class
30-
Class.forName("com.android.billingclient.api.SkuDetails");
31-
return "Version 4.0 or older";
32-
} catch (ClassNotFoundException version4CheckFailed) {
33-
// If neither class is found, the library is not present or is a very old version
34-
return "Not found or very old version";
35-
}
36-
}
24+
return new BillingGooglePlayV6V7();
3725
}
3826
}
3927
}
4028
}
41-
public static void main(String[] args) {
42-
String version = getBillingLibraryVersion();
43-
System.out.println("Detected Google Billing Library Version: " + version);
44-
}
4529
}

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
include(":Branch-SDK")
22
include(":Branch-SDK-TestBed")
3+
include (":BillingGooglePlayModules:BillingV6V7", ":BillingGooglePlayModules:BillingV8")
4+
35

46
pluginManagement {
57
repositories {

0 commit comments

Comments
 (0)