99import com .android .billingclient .api .BillingClientStateListener ;
1010import com .android .billingclient .api .BillingFlowParams ;
1111import com .android .billingclient .api .BillingResult ;
12- import com .android .billingclient .api .SkuDetails ;
13- import com .android .billingclient .api .SkuDetailsParams ;
12+ import com .android .billingclient .api .PendingPurchasesParams ;
13+ import com .android .billingclient .api .ProductDetails ;
14+ import com .android .billingclient .api .QueryProductDetailsParams ;
1415import com .d4rk .androidtutorials .java .databinding .ActivitySupportBinding ;
1516import com .google .android .gms .ads .AdRequest ;
1617import com .google .android .gms .ads .MobileAds ;
2526public class SupportRepository {
2627
2728 private final Context context ;
28- private final Map <String , SkuDetails > skuDetailsMap = new HashMap <>();
29+ private final Map <String , ProductDetails > productDetailsMap = new HashMap <>();
2930 private BillingClient billingClient ;
3031
3132 public SupportRepository (Context context ) {
@@ -41,7 +42,10 @@ public void initBillingClient(Runnable onConnected) {
4142 billingClient = BillingClient .newBuilder (context )
4243 .setListener ((billingResult , purchases ) -> {
4344 })
44- .enablePendingPurchases ()
45+ .enablePendingPurchases (
46+ PendingPurchasesParams .newBuilder ()
47+ .enableOneTimeProducts ()
48+ .build ())
4549 .build ();
4650
4751 billingClient .startConnection (new BillingClientStateListener () {
@@ -63,40 +67,51 @@ public void onBillingServiceDisconnected() {
6367 }
6468
6569 /**
66- * Query your SKU details for in-app items.
70+ * Query your product details for in-app items.
6771 * Typically called after billing client is connected.
6872 */
69- public void querySkuDetails (List <String > skuList , OnSkuDetailsListener listener ) {
73+ public void queryProductDetails (List <String > productIds , OnProductDetailsListener listener ) {
7074 if (billingClient == null || !billingClient .isReady ()) {
7175 return ;
7276 }
73- SkuDetailsParams params = SkuDetailsParams .newBuilder ()
74- .setSkusList (skuList )
75- .setType (BillingClient .SkuType .INAPP )
77+
78+ List <QueryProductDetailsParams .Product > products = productIds .stream ()
79+ .map (id -> QueryProductDetailsParams .Product .newBuilder ()
80+ .setProductId (id )
81+ .setProductType (BillingClient .ProductType .INAPP )
82+ .build ())
83+ .toList ();
84+
85+ QueryProductDetailsParams params = QueryProductDetailsParams .newBuilder ()
86+ .setProductList (products )
7687 .build ();
7788
78- billingClient .querySkuDetailsAsync (params , (billingResult , skuDetailsList ) -> {
89+ billingClient .queryProductDetailsAsync (params , (billingResult , productDetailsList ) -> {
7990 if (billingResult .getResponseCode () == BillingClient .BillingResponseCode .OK
80- && skuDetailsList != null ) {
81- for (SkuDetails skuDetails : skuDetailsList ) {
82- skuDetailsMap .put (skuDetails . getSku (), skuDetails );
91+ && productDetailsList != null ) {
92+ for (ProductDetails productDetails : productDetailsList ) {
93+ productDetailsMap .put (productDetails . getProductId (), productDetails );
8394 }
8495 if (listener != null ) {
85- listener .onSkuDetailsRetrieved ( skuDetailsList );
96+ listener .onProductDetailsRetrieved ( productDetailsList );
8697 }
8798 }
8899 });
89100 }
90101
91102 /**
92- * Launch the billing flow for a particular SKU .
103+ * Launch the billing flow for a particular product .
93104 */
94- public void initiatePurchase (Activity activity , String sku ) {
95- if (skuDetailsMap .containsKey (sku )) {
96- SkuDetails skuDetails = skuDetailsMap .get (sku );
97- if (skuDetails != null ) {
105+ public void initiatePurchase (Activity activity , String productId ) {
106+ if (productDetailsMap .containsKey (productId )) {
107+ ProductDetails details = productDetailsMap .get (productId );
108+ if (details != null ) {
109+ BillingFlowParams .ProductDetailsParams productParams =
110+ BillingFlowParams .ProductDetailsParams .newBuilder ()
111+ .setProductDetails (details )
112+ .build ();
98113 BillingFlowParams flowParams = BillingFlowParams .newBuilder ()
99- .setSkuDetails ( skuDetails )
114+ .setProductDetailsParamsList ( List . of ( productParams ) )
100115 .build ();
101116 billingClient .launchBillingFlow (activity , flowParams );
102117 }
@@ -113,10 +128,10 @@ public void initMobileAds(ActivitySupportBinding binding) {
113128 }
114129
115130 /**
116- * Callback interface for when SKU details are fetched.
131+ * Callback interface for when product details are fetched.
117132 */
118- public interface OnSkuDetailsListener {
119- void onSkuDetailsRetrieved (List <SkuDetails > skuDetailsList );
133+ public interface OnProductDetailsListener {
134+ void onProductDetailsRetrieved (List <ProductDetails > productDetailsList );
120135 }
121136
122137}
0 commit comments