Skip to content

Commit 2977506

Browse files
author
i.rybakov
committed
update example to sdk v8.0.0
1 parent c79bf52 commit 2977506

File tree

7 files changed

+91
-82
lines changed

7 files changed

+91
-82
lines changed

app/build.gradle.kts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import com.android.build.api.dsl.ApkSigningConfig
22
import java.util.Properties
33

4-
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
54
plugins {
6-
alias(libs.plugins.androidApplication)
5+
id("com.android.application")
6+
id("org.jetbrains.kotlin.android")
77
}
88

99
android {
@@ -53,13 +53,13 @@ android {
5353

5454
dependencies {
5555

56-
implementation(libs.appcompat)
56+
implementation(libs.kotlin.stdlib)
57+
implementation(libs.androidx.core)
5758
implementation(libs.material)
5859
implementation(libs.constraintlayout)
5960
testImplementation(libs.junit)
60-
androidTestImplementation(libs.androidx.test.ext.junit)
61-
androidTestImplementation(libs.espresso.core)
6261

6362
// RuStore Implementation
64-
implementation(libs.billingclient)
63+
implementation(platform(libs.rustore.sdk.bom))
64+
implementation(libs.rustore.sdk.billingclient)
6565
}

app/src/main/java/ru/rustore/example/rustorebillingsample/ItemClickSupport.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import androidx.recyclerview.widget.RecyclerView;
77

88
public class ItemClickSupport {
9+
910
private final RecyclerView mRecyclerView;
1011
private OnItemClickListener mOnItemClickListener;
1112
private OnItemLongClickListener mOnItemLongClickListener;
13+
1214
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
1315
@Override
1416
public void onClick(View v) {
@@ -18,6 +20,7 @@ public void onClick(View v) {
1820
}
1921
}
2022
};
23+
2124
private final View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
2225
@Override
2326
public boolean onLongClick(View v) {
@@ -28,6 +31,7 @@ public boolean onLongClick(View v) {
2831
return false;
2932
}
3033
};
34+
3135
private final RecyclerView.OnChildAttachStateChangeListener mAttachListener
3236
= new RecyclerView.OnChildAttachStateChangeListener() {
3337
@Override

app/src/main/java/ru/rustore/example/rustorebillingsample/StartFragment.java

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@
77
import android.view.ViewGroup;
88
import android.widget.Button;
99
import android.widget.Toast;
10+
1011
import androidx.annotation.NonNull;
1112
import androidx.annotation.Nullable;
1213
import androidx.fragment.app.Fragment;
1314
import androidx.recyclerview.widget.LinearLayoutManager;
1415
import androidx.recyclerview.widget.RecyclerView;
16+
1517
import java.util.Arrays;
18+
import java.util.List;
19+
1620
import ru.rustore.example.rustorebillingsample.di.PaymentsModule;
1721
import ru.rustore.sdk.billingclient.RuStoreBillingClient;
1822
import ru.rustore.sdk.billingclient.model.purchase.PaymentResult;
23+
import ru.rustore.sdk.billingclient.model.purchase.Purchase;
24+
import ru.rustore.sdk.billingclient.model.purchase.PurchaseAvailabilityResult;
1925
import ru.rustore.sdk.billingclient.model.purchase.PurchaseState;
2026
import ru.rustore.sdk.billingclient.usecase.ProductsUseCase;
2127
import ru.rustore.sdk.billingclient.usecase.PurchasesUseCase;
2228
import ru.rustore.sdk.billingclient.utils.BillingRuStoreExceptionExtKt;
2329
import ru.rustore.sdk.billingclient.utils.pub.RuStoreBillingClientExtKt;
2430
import ru.rustore.sdk.core.exception.RuStoreException;
25-
import ru.rustore.sdk.core.feature.model.FeatureAvailabilityResult;
2631

2732
public class StartFragment extends Fragment {
2833

@@ -31,6 +36,8 @@ public class StartFragment extends Fragment {
3136
RecyclerView productsList;
3237
RecyclerView purchasesList;
3338

39+
private static final String TAG = "RuStoreBillingClient";
40+
3441
private static final RuStoreBillingClient billingClient =
3542
PaymentsModule.provideRuStorebillingClient();
3643

@@ -54,37 +61,38 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
5461
productsList = view.findViewById(R.id.productsList);
5562
purchasesList = view.findViewById(R.id.purchasesList);
5663

57-
checkPurchaseAvailiability();
64+
checkPurchaseAvailability();
5865

5966
productButton.setOnClickListener(v -> getProducts());
6067

6168
purchaseButton.setOnClickListener(v -> getPurchases());
6269
}
6370

64-
public void checkPurchaseAvailiability() {
65-
RuStoreBillingClientExtKt.checkPurchasesAvailability(RuStoreBillingClient.Companion, requireContext())
71+
public void checkPurchaseAvailability() {
72+
RuStoreBillingClientExtKt.checkPurchasesAvailability(RuStoreBillingClient.Companion)
6673
.addOnSuccessListener(result -> {
67-
if (result instanceof FeatureAvailabilityResult.Available) {
68-
Log.w("RuStoreBillingClient", "Success calling checkPurchaseAvailiability - Available: " + result);
74+
if (result instanceof PurchaseAvailabilityResult.Available) {
75+
Log.w(TAG, "Success calling checkPurchaseAvailability - Available: " + result);
76+
} else if (result instanceof PurchaseAvailabilityResult.Unavailable) {
77+
Log.w(TAG, "Success calling checkPurchaseAvailability - Unavailable: " + result);
6978
} else {
70-
RuStoreException exception = ((FeatureAvailabilityResult.Unavailable) result).getCause();
79+
RuStoreException exception = ((PurchaseAvailabilityResult.Unavailable) result).getCause();
7180
BillingRuStoreExceptionExtKt.resolveForBilling(exception, getContext());
72-
Log.w("RuStoreBillingClient", "Success calling checkPurchaseAvailiability - Unavailable: " + exception);
81+
Log.w(TAG, "Success calling checkPurchaseAvailability - Unavailable: " + exception);
7382
}
74-
}).addOnFailureListener(error -> {
75-
Log.e("RuStoreBillingClient", "Error calling checkPurchaseAvailiability: " + error);
76-
});
83+
}).addOnFailureListener(error -> Log.e(TAG, "Error calling checkPurchaseAvailability: " + error));
7784
}
7885

7986
public void getProducts() {
8087
ProductsUseCase productsUseCase = billingClient.getProducts();
88+
List<String> productsId = Arrays.asList(
89+
"productId1",
90+
"productId2",
91+
"productId3"
92+
);
8193

82-
productsUseCase.getProducts(
83-
Arrays.asList(
84-
"productId1",
85-
"productId2",
86-
"productId3"
87-
)).addOnSuccessListener(products -> {
94+
productsUseCase.getProducts(productsId)
95+
.addOnSuccessListener(products -> {
8896
ProductsAdapter productsAdapter = new ProductsAdapter(products);
8997

9098
productsList.setAdapter(productsAdapter);
@@ -95,40 +103,45 @@ public void getProducts() {
95103
purchaseProduct(products.get(position).getProductId());
96104
Toast.makeText(getContext(), "Clicked: " + position, Toast.LENGTH_LONG).show();
97105
});
98-
}).addOnFailureListener(throwable -> Log.e("RuStoreBillingClient", "Error calling getProducts cause: " + throwable));
106+
})
107+
.addOnFailureListener(throwable -> Log.e(TAG, "Error calling getProducts cause: " + throwable));
99108
}
100109

101110
public void getPurchases() {
102111
PurchasesUseCase purchasesUseCase = billingClient.getPurchases();
103112

104-
purchasesUseCase.getPurchases().addOnSuccessListener(purchases -> {
105-
PurchaseAdapter purchaseAdapter = new PurchaseAdapter(purchases);
106-
107-
purchasesList.setAdapter(purchaseAdapter);
108-
purchasesList.setLayoutManager(new LinearLayoutManager(getContext()));
109-
110-
purchases.forEach(purchase -> {
111-
String purchaseId = purchase.getPurchaseId();
112-
if (purchaseId != null) {
113-
assert purchase.getDeveloperPayload() != null;
114-
Log.w("HOHOHO", purchase.getDeveloperPayload());
115-
if (purchase.getPurchaseState() != null) {
116-
if (purchase.getPurchaseState() == PurchaseState.CREATED ||
117-
purchase.getPurchaseState() == PurchaseState.INVOICE_CREATED )
118-
{
119-
deletePurchase(purchaseId);
120-
} else if (purchase.getPurchaseState() == PurchaseState.PAID) {
121-
confirmPurchase(purchaseId);
122-
}
123-
} else {
124-
Log.e("HOHOHO", "PurchaseState is null");
125-
}
113+
purchasesUseCase.getPurchases()
114+
.addOnSuccessListener(purchases -> {
115+
PurchaseAdapter purchaseAdapter = new PurchaseAdapter(purchases);
116+
purchasesList.setAdapter(purchaseAdapter);
117+
purchasesList.setLayoutManager(new LinearLayoutManager(getContext()));
126118

127-
}
128-
});
129-
}).addOnFailureListener(throwable ->
130-
Log.e("RuStoreBillingClient", "Error calling getPurchases cause: " + throwable)
131-
);
119+
proceedUnfinishedPurchases(purchases);
120+
})
121+
.addOnFailureListener(throwable -> Log.e("RuStoreBillingClient", "Error calling getPurchases cause: " + throwable));
122+
}
123+
124+
public void proceedUnfinishedPurchases(List<Purchase> purchases) {
125+
purchases.forEach(purchase -> {
126+
String purchaseId = purchase.getPurchaseId();
127+
PurchaseState purchaseState = purchase.getPurchaseState();
128+
129+
if (purchaseId == null) {
130+
return;
131+
} else if (purchaseState == null) {
132+
Log.e(TAG, "PurchaseState is null");
133+
return;
134+
}
135+
136+
boolean needDeletePurchase = purchaseState == PurchaseState.CREATED || purchaseState == PurchaseState.INVOICE_CREATED;
137+
boolean needConfirmPurchase = purchaseState == PurchaseState.PAID;
138+
139+
if (needDeletePurchase) {
140+
deletePurchase(purchaseId);
141+
} else if (needConfirmPurchase) {
142+
confirmPurchase(purchaseId);
143+
}
144+
});
132145
}
133146

134147
public void purchaseProduct(String productId) {
@@ -139,8 +152,8 @@ public void purchaseProduct(String productId) {
139152
purchasesUseCase.purchaseProduct(productId, null, 1, developerPayload)
140153
.addOnSuccessListener(this::handlePaymentResult)
141154
.addOnFailureListener(throwable ->
142-
Log.e("RuStoreBillingClient", "Error calling purchaseProduct cause: " + throwable)
143-
);
155+
Log.e("RuStoreBillingClient", "Error calling purchaseProduct cause: " + throwable)
156+
);
144157
}
145158

146159
private void handlePaymentResult(PaymentResult paymentResult) {
@@ -167,7 +180,8 @@ public void deletePurchase(String purchaseId) {
167180
PurchasesUseCase purchasesUseCase = billingClient.getPurchases();
168181

169182
purchasesUseCase.deletePurchase(purchaseId)
170-
.addOnSuccessListener(unit -> {})
183+
.addOnSuccessListener(unit -> {
184+
})
171185
.addOnFailureListener(throwable -> {
172186
Log.e("RuStoreBillingClient", "Error calling deletePurchase cause: " + throwable);
173187
});

build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
32
plugins {
4-
alias(libs.plugins.androidApplication) apply false
5-
}
6-
true // Needed to make the Suppress annotation work for the plugins block
3+
id("com.android.application") version "7.2.2" apply false
4+
id("com.android.library") version "7.2.2" apply false
5+
id("org.jetbrains.kotlin.android") version "1.8.0" apply false
6+
}

gradle/libs.versions.toml

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
[versions]
2-
agp = "8.0.0"
3-
billingclient = "6.0.0"
4-
fragment = "1.6.1"
5-
junit = "4.13.2"
6-
androidx-test-ext-junit = "1.1.5"
7-
espresso-core = "3.5.1"
8-
appcompat = "1.6.1"
9-
material = "1.9.0"
10-
constraintlayout = "2.1.4"
2+
junitVersion = "4.13.2"
3+
coreVersion = "1.9.0"
4+
materialVersion = "1.3.0"
5+
constraintlayoutVersion = "1.7.0"
6+
kotlinStdlibVersion = "1.8.22"
7+
rustoreSdkBomVersion = "2025.02.01"
118

129
[libraries]
13-
androidx-fragment = { module = "androidx.fragment:fragment", version.ref = "fragment" }
14-
billingclient = { module = "ru.rustore.sdk:billingclient", version.ref = "billingclient" }
15-
junit = { group = "junit", name = "junit", version.ref = "junit" }
16-
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
17-
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
18-
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
19-
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
20-
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
21-
22-
[plugins]
23-
androidApplication = { id = "com.android.application", version.ref = "agp" }
24-
10+
junit = { group = "junit", name = "junit", version.ref = "junitVersion" }
11+
rustore-sdk-bom = { group = "ru.rustore.sdk", name = "bom", version.ref = "rustoreSdkBomVersion" }
12+
rustore-sdk-billingclient = { module = "ru.rustore.sdk:billingclient" }
13+
androidx-core = { group = "androidx.core", name = "core", version.ref = "coreVersion" }
14+
material = { group = "com.google.android.material", name = "material", version.ref = "materialVersion" }
15+
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayoutVersion" }
16+
kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlinStdlibVersion" }
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Tue Sep 19 08:35:13 MSK 2023
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
54
zipStoreBase=GRADLE_USER_HOME
65
zipStorePath=wrapper/dists

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ dependencyResolutionManagement {
1919

2020
rootProject.name = "RustoreBillingSample"
2121
include(":app")
22-
22+
enableFeaturePreview("VERSION_CATALOGS")

0 commit comments

Comments
 (0)