5
5
package io .flutter .plugins .inapppurchase ;
6
6
7
7
import 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 ;
9
9
import static io .flutter .plugins .inapppurchase .Translator .fromSkuDetailsList ;
10
10
11
11
import android .app .Activity ;
25
25
import com .android .billingclient .api .ConsumeParams ;
26
26
import com .android .billingclient .api .ConsumeResponseListener ;
27
27
import com .android .billingclient .api .PriceChangeFlowParams ;
28
+ import com .android .billingclient .api .Purchase ;
28
29
import com .android .billingclient .api .PurchaseHistoryRecord ;
29
30
import 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 ;
30
34
import com .android .billingclient .api .SkuDetails ;
31
35
import com .android .billingclient .api .SkuDetailsParams ;
32
36
import com .android .billingclient .api .SkuDetailsResponseListener ;
@@ -131,10 +135,14 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
131
135
: ProrationMode .UNKNOWN_SUBSCRIPTION_UPGRADE_DOWNGRADE_POLICY ,
132
136
result );
133
137
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 );
136
143
break ;
137
144
case InAppPurchasePlugin .MethodNames .QUERY_PURCHASE_HISTORY_ASYNC :
145
+ Log .e ("flutter" , (String ) call .argument ("skuType" ));
138
146
queryPurchaseHistoryAsync ((String ) call .argument ("skuType" ), result );
139
147
break ;
140
148
case InAppPurchasePlugin .MethodNames .CONSUME_PURCHASE_ASYNC :
@@ -149,6 +157,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
149
157
case InAppPurchasePlugin .MethodNames .LAUNCH_PRICE_CHANGE_CONFIRMATION_FLOW :
150
158
launchPriceChangeConfirmationFlow ((String ) call .argument ("sku" ), result );
151
159
break ;
160
+ case InAppPurchasePlugin .MethodNames .GET_CONNECTION_STATE :
161
+ getConnectionState (result );
162
+ break ;
152
163
default :
153
164
result .notImplemented ();
154
165
}
@@ -174,6 +185,7 @@ private void isReady(MethodChannel.Result result) {
174
185
result .success (billingClient .isReady ());
175
186
}
176
187
188
+ // TODO(garyq): Migrate to new subscriptions API: https://developer.android.com/google/play/billing/migrate-gpblv5
177
189
private void querySkuDetailsAsync (
178
190
final String skuType , final List <String > skusList , final MethodChannel .Result result ) {
179
191
if (billingClientError (result )) {
@@ -208,7 +220,6 @@ private void launchBillingFlow(
208
220
if (billingClientError (result )) {
209
221
return ;
210
222
}
211
-
212
223
SkuDetails skuDetails = cachedSkus .get (sku );
213
224
if (skuDetails == null ) {
214
225
result .error (
@@ -255,12 +266,15 @@ private void launchBillingFlow(
255
266
if (obfuscatedProfileId != null && !obfuscatedProfileId .isEmpty ()) {
256
267
paramsBuilder .setObfuscatedProfileId (obfuscatedProfileId );
257
268
}
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 ());
260
277
}
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 );
264
278
result .success (
265
279
Translator .fromBillingResult (
266
280
billingClient .launchBillingFlow (activity , paramsBuilder .build ())));
@@ -286,14 +300,30 @@ public void onConsumeResponse(BillingResult billingResult, String outToken) {
286
300
billingClient .consumeAsync (params , listener );
287
301
}
288
302
289
- private void queryPurchases (String skuType , MethodChannel .Result result ) {
303
+ private void queryPurchasesAsync (String skuType , MethodChannel .Result result ) {
290
304
if (billingClientError (result )) {
291
305
return ;
292
306
}
293
307
294
308
// Like in our connect call, consider the billing client responding a "success" here regardless
295
309
// 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
+ });
297
327
}
298
328
299
329
private void queryPurchaseHistoryAsync (String skuType , final MethodChannel .Result result ) {
@@ -302,7 +332,7 @@ private void queryPurchaseHistoryAsync(String skuType, final MethodChannel.Resul
302
332
}
303
333
304
334
billingClient .queryPurchaseHistoryAsync (
305
- skuType ,
335
+ QueryPurchaseHistoryParams . newBuilder (). setProductType ( skuType ). build () ,
306
336
new PurchaseHistoryResponseListener () {
307
337
@ Override
308
338
public void onPurchaseHistoryResponse (
@@ -316,6 +346,15 @@ public void onPurchaseHistoryResponse(
316
346
});
317
347
}
318
348
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
+
319
358
private void startConnection (final int handle , final MethodChannel .Result result ) {
320
359
if (billingClient == null ) {
321
360
billingClient = billingClientFactory .createBillingClient (applicationContext , methodChannel );
0 commit comments