7
7
import android .view .ViewGroup ;
8
8
import android .widget .Button ;
9
9
import android .widget .Toast ;
10
+
10
11
import androidx .annotation .NonNull ;
11
12
import androidx .annotation .Nullable ;
12
13
import androidx .fragment .app .Fragment ;
13
14
import androidx .recyclerview .widget .LinearLayoutManager ;
14
15
import androidx .recyclerview .widget .RecyclerView ;
16
+
15
17
import java .util .Arrays ;
18
+ import java .util .List ;
19
+
16
20
import ru .rustore .example .rustorebillingsample .di .PaymentsModule ;
17
21
import ru .rustore .sdk .billingclient .RuStoreBillingClient ;
18
22
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 ;
19
25
import ru .rustore .sdk .billingclient .model .purchase .PurchaseState ;
20
26
import ru .rustore .sdk .billingclient .usecase .ProductsUseCase ;
21
27
import ru .rustore .sdk .billingclient .usecase .PurchasesUseCase ;
22
28
import ru .rustore .sdk .billingclient .utils .BillingRuStoreExceptionExtKt ;
23
29
import ru .rustore .sdk .billingclient .utils .pub .RuStoreBillingClientExtKt ;
24
30
import ru .rustore .sdk .core .exception .RuStoreException ;
25
- import ru .rustore .sdk .core .feature .model .FeatureAvailabilityResult ;
26
31
27
32
public class StartFragment extends Fragment {
28
33
@@ -31,6 +36,8 @@ public class StartFragment extends Fragment {
31
36
RecyclerView productsList ;
32
37
RecyclerView purchasesList ;
33
38
39
+ private static final String TAG = "RuStoreBillingClient" ;
40
+
34
41
private static final RuStoreBillingClient billingClient =
35
42
PaymentsModule .provideRuStorebillingClient ();
36
43
@@ -54,37 +61,38 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
54
61
productsList = view .findViewById (R .id .productsList );
55
62
purchasesList = view .findViewById (R .id .purchasesList );
56
63
57
- checkPurchaseAvailiability ();
64
+ checkPurchaseAvailability ();
58
65
59
66
productButton .setOnClickListener (v -> getProducts ());
60
67
61
68
purchaseButton .setOnClickListener (v -> getPurchases ());
62
69
}
63
70
64
- public void checkPurchaseAvailiability () {
65
- RuStoreBillingClientExtKt .checkPurchasesAvailability (RuStoreBillingClient .Companion , requireContext () )
71
+ public void checkPurchaseAvailability () {
72
+ RuStoreBillingClientExtKt .checkPurchasesAvailability (RuStoreBillingClient .Companion )
66
73
.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 );
69
78
} else {
70
- RuStoreException exception = ((FeatureAvailabilityResult .Unavailable ) result ).getCause ();
79
+ RuStoreException exception = ((PurchaseAvailabilityResult .Unavailable ) result ).getCause ();
71
80
BillingRuStoreExceptionExtKt .resolveForBilling (exception , getContext ());
72
- Log .w ("RuStoreBillingClient" , "Success calling checkPurchaseAvailiability - Unavailable: " + exception );
81
+ Log .w (TAG , "Success calling checkPurchaseAvailability - Unavailable: " + exception );
73
82
}
74
- }).addOnFailureListener (error -> {
75
- Log .e ("RuStoreBillingClient" , "Error calling checkPurchaseAvailiability: " + error );
76
- });
83
+ }).addOnFailureListener (error -> Log .e (TAG , "Error calling checkPurchaseAvailability: " + error ));
77
84
}
78
85
79
86
public void getProducts () {
80
87
ProductsUseCase productsUseCase = billingClient .getProducts ();
88
+ List <String > productsId = Arrays .asList (
89
+ "productId1" ,
90
+ "productId2" ,
91
+ "productId3"
92
+ );
81
93
82
- productsUseCase .getProducts (
83
- Arrays .asList (
84
- "productId1" ,
85
- "productId2" ,
86
- "productId3"
87
- )).addOnSuccessListener (products -> {
94
+ productsUseCase .getProducts (productsId )
95
+ .addOnSuccessListener (products -> {
88
96
ProductsAdapter productsAdapter = new ProductsAdapter (products );
89
97
90
98
productsList .setAdapter (productsAdapter );
@@ -95,40 +103,45 @@ public void getProducts() {
95
103
purchaseProduct (products .get (position ).getProductId ());
96
104
Toast .makeText (getContext (), "Clicked: " + position , Toast .LENGTH_LONG ).show ();
97
105
});
98
- }).addOnFailureListener (throwable -> Log .e ("RuStoreBillingClient" , "Error calling getProducts cause: " + throwable ));
106
+ })
107
+ .addOnFailureListener (throwable -> Log .e (TAG , "Error calling getProducts cause: " + throwable ));
99
108
}
100
109
101
110
public void getPurchases () {
102
111
PurchasesUseCase purchasesUseCase = billingClient .getPurchases ();
103
112
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 ()));
126
118
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
+ });
132
145
}
133
146
134
147
public void purchaseProduct (String productId ) {
@@ -139,8 +152,8 @@ public void purchaseProduct(String productId) {
139
152
purchasesUseCase .purchaseProduct (productId , null , 1 , developerPayload )
140
153
.addOnSuccessListener (this ::handlePaymentResult )
141
154
.addOnFailureListener (throwable ->
142
- Log .e ("RuStoreBillingClient" , "Error calling purchaseProduct cause: " + throwable )
143
- );
155
+ Log .e ("RuStoreBillingClient" , "Error calling purchaseProduct cause: " + throwable )
156
+ );
144
157
}
145
158
146
159
private void handlePaymentResult (PaymentResult paymentResult ) {
@@ -167,7 +180,8 @@ public void deletePurchase(String purchaseId) {
167
180
PurchasesUseCase purchasesUseCase = billingClient .getPurchases ();
168
181
169
182
purchasesUseCase .deletePurchase (purchaseId )
170
- .addOnSuccessListener (unit -> {})
183
+ .addOnSuccessListener (unit -> {
184
+ })
171
185
.addOnFailureListener (throwable -> {
172
186
Log .e ("RuStoreBillingClient" , "Error calling deletePurchase cause: " + throwable );
173
187
});
0 commit comments