77import android .view .ViewGroup ;
88import android .widget .Button ;
99import android .widget .Toast ;
10+
1011import androidx .annotation .NonNull ;
1112import androidx .annotation .Nullable ;
1213import androidx .fragment .app .Fragment ;
1314import androidx .recyclerview .widget .LinearLayoutManager ;
1415import androidx .recyclerview .widget .RecyclerView ;
16+
1517import java .util .Arrays ;
18+ import java .util .List ;
19+
1620import ru .rustore .example .rustorebillingsample .di .PaymentsModule ;
1721import ru .rustore .sdk .billingclient .RuStoreBillingClient ;
1822import 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 ;
1925import ru .rustore .sdk .billingclient .model .purchase .PurchaseState ;
2026import ru .rustore .sdk .billingclient .usecase .ProductsUseCase ;
2127import ru .rustore .sdk .billingclient .usecase .PurchasesUseCase ;
2228import ru .rustore .sdk .billingclient .utils .BillingRuStoreExceptionExtKt ;
2329import ru .rustore .sdk .billingclient .utils .pub .RuStoreBillingClientExtKt ;
2430import ru .rustore .sdk .core .exception .RuStoreException ;
25- import ru .rustore .sdk .core .feature .model .FeatureAvailabilityResult ;
2631
2732public 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 });
0 commit comments