55package io .flutter .plugins .inapppurchase ;
66
77import static io .flutter .plugins .inapppurchase .Translator .fromPurchaseHistoryRecordList ;
8- import static io .flutter .plugins .inapppurchase .Translator .fromPurchasesResult ;
8+ import static io .flutter .plugins .inapppurchase .Translator .fromPurchasesList ;
99import static io .flutter .plugins .inapppurchase .Translator .fromSkuDetailsList ;
1010
1111import android .app .Activity ;
2525import com .android .billingclient .api .ConsumeParams ;
2626import com .android .billingclient .api .ConsumeResponseListener ;
2727import com .android .billingclient .api .PriceChangeFlowParams ;
28+ import com .android .billingclient .api .Purchase ;
2829import com .android .billingclient .api .PurchaseHistoryRecord ;
2930import com .android .billingclient .api .PurchaseHistoryResponseListener ;
31+ import com .android .billingclient .api .PurchasesResponseListener ;
32+ import com .android .billingclient .api .QueryPurchaseHistoryParams ;
33+ import com .android .billingclient .api .QueryPurchasesParams ;
3034import com .android .billingclient .api .SkuDetails ;
3135import com .android .billingclient .api .SkuDetailsParams ;
3236import com .android .billingclient .api .SkuDetailsResponseListener ;
@@ -131,10 +135,14 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
131135 : ProrationMode .UNKNOWN_SUBSCRIPTION_UPGRADE_DOWNGRADE_POLICY ,
132136 result );
133137 break ;
134- case InAppPurchasePlugin .MethodNames .QUERY_PURCHASES :
135- queryPurchases ((String ) call .argument ("skuType" ), result );
138+ case InAppPurchasePlugin .MethodNames .QUERY_PURCHASES : // Legacy method name.
139+ queryPurchasesAsync ((String ) call .argument ("skuType" ), result );
140+ break ;
141+ case InAppPurchasePlugin .MethodNames .QUERY_PURCHASES_ASYNC :
142+ queryPurchasesAsync ((String ) call .argument ("skuType" ), result );
136143 break ;
137144 case InAppPurchasePlugin .MethodNames .QUERY_PURCHASE_HISTORY_ASYNC :
145+ Log .e ("flutter" , (String ) call .argument ("skuType" ));
138146 queryPurchaseHistoryAsync ((String ) call .argument ("skuType" ), result );
139147 break ;
140148 case InAppPurchasePlugin .MethodNames .CONSUME_PURCHASE_ASYNC :
@@ -149,6 +157,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
149157 case InAppPurchasePlugin .MethodNames .LAUNCH_PRICE_CHANGE_CONFIRMATION_FLOW :
150158 launchPriceChangeConfirmationFlow ((String ) call .argument ("sku" ), result );
151159 break ;
160+ case InAppPurchasePlugin .MethodNames .GET_CONNECTION_STATE :
161+ getConnectionState (result );
162+ break ;
152163 default :
153164 result .notImplemented ();
154165 }
@@ -174,6 +185,7 @@ private void isReady(MethodChannel.Result result) {
174185 result .success (billingClient .isReady ());
175186 }
176187
188+ // TODO(garyq): Migrate to new subscriptions API: https://developer.android.com/google/play/billing/migrate-gpblv5
177189 private void querySkuDetailsAsync (
178190 final String skuType , final List <String > skusList , final MethodChannel .Result result ) {
179191 if (billingClientError (result )) {
@@ -208,7 +220,6 @@ private void launchBillingFlow(
208220 if (billingClientError (result )) {
209221 return ;
210222 }
211-
212223 SkuDetails skuDetails = cachedSkus .get (sku );
213224 if (skuDetails == null ) {
214225 result .error (
@@ -255,12 +266,15 @@ private void launchBillingFlow(
255266 if (obfuscatedProfileId != null && !obfuscatedProfileId .isEmpty ()) {
256267 paramsBuilder .setObfuscatedProfileId (obfuscatedProfileId );
257268 }
258- if (oldSku != null && !oldSku .isEmpty ()) {
259- paramsBuilder .setOldSku (oldSku , purchaseToken );
269+ BillingFlowParams .SubscriptionUpdateParams .Builder subscriptionUpdateParamsBuilder =
270+ BillingFlowParams .SubscriptionUpdateParams .newBuilder ();
271+ if (oldSku != null && !oldSku .isEmpty () && purchaseToken != null ) {
272+ subscriptionUpdateParamsBuilder .setOldPurchaseToken (purchaseToken );
273+ // The proration mode value has to match one of the following declared in
274+ // https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.ProrationMode
275+ subscriptionUpdateParamsBuilder .setReplaceProrationMode (prorationMode );
276+ paramsBuilder .setSubscriptionUpdateParams (subscriptionUpdateParamsBuilder .build ());
260277 }
261- // The proration mode value has to match one of the following declared in
262- // https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.ProrationMode
263- paramsBuilder .setReplaceSkusProrationMode (prorationMode );
264278 result .success (
265279 Translator .fromBillingResult (
266280 billingClient .launchBillingFlow (activity , paramsBuilder .build ())));
@@ -286,14 +300,30 @@ public void onConsumeResponse(BillingResult billingResult, String outToken) {
286300 billingClient .consumeAsync (params , listener );
287301 }
288302
289- private void queryPurchases (String skuType , MethodChannel .Result result ) {
303+ private void queryPurchasesAsync (String skuType , MethodChannel .Result result ) {
290304 if (billingClientError (result )) {
291305 return ;
292306 }
293307
294308 // Like in our connect call, consider the billing client responding a "success" here regardless
295309 // of status code.
296- result .success (fromPurchasesResult (billingClient .queryPurchases (skuType )));
310+ QueryPurchasesParams .Builder paramsBuilder = QueryPurchasesParams .newBuilder ();
311+ paramsBuilder .setProductType (skuType );
312+ billingClient .queryPurchasesAsync (
313+ paramsBuilder .build (),
314+ new PurchasesResponseListener () {
315+ @ Override
316+ public void onQueryPurchasesResponse (
317+ BillingResult billingResult , List <Purchase > purchasesList ) {
318+ final Map <String , Object > serialized = new HashMap <>();
319+ // The response code is no longer passed, as part of billing 4.0, so we pass OK here
320+ // as success is implied by calling this callback.
321+ serialized .put ("responseCode" , BillingClient .BillingResponseCode .OK );
322+ serialized .put ("billingResult" , Translator .fromBillingResult (billingResult ));
323+ serialized .put ("purchaseList" , fromPurchasesList (purchasesList ));
324+ result .success (serialized );
325+ }
326+ });
297327 }
298328
299329 private void queryPurchaseHistoryAsync (String skuType , final MethodChannel .Result result ) {
@@ -302,7 +332,7 @@ private void queryPurchaseHistoryAsync(String skuType, final MethodChannel.Resul
302332 }
303333
304334 billingClient .queryPurchaseHistoryAsync (
305- skuType ,
335+ QueryPurchaseHistoryParams . newBuilder (). setProductType ( skuType ). build () ,
306336 new PurchaseHistoryResponseListener () {
307337 @ Override
308338 public void onPurchaseHistoryResponse (
@@ -316,6 +346,15 @@ public void onPurchaseHistoryResponse(
316346 });
317347 }
318348
349+ private void getConnectionState (final MethodChannel .Result result ) {
350+ if (billingClientError (result )) {
351+ return ;
352+ }
353+ final Map <String , Object > serialized = new HashMap <>();
354+ serialized .put ("connectionState" , billingClient .getConnectionState ());
355+ result .success (serialized );
356+ }
357+
319358 private void startConnection (final int handle , final MethodChannel .Result result ) {
320359 if (billingClient == null ) {
321360 billingClient = billingClientFactory .createBillingClient (applicationContext , methodChannel );
0 commit comments