Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ static final class MethodNames {
"BillingClient#launchBillingFlow(Activity, BillingFlowParams)";
static final String ON_PURCHASES_UPDATED =
"PurchasesUpdatedListener#onPurchasesUpdated(int, List<Purchase>)";
static final String QUERY_PURCHASES = "queryPurchases(String)";
static final String QUERY_PURCHASES = "BillingClient#queryPurchases(String)";
static final String QUERY_PURCHASE_HISTORY_ASYNC =
"queryPurchaseHistoryAsync(String, PurchaseHistoryResponseListener)";
"BillingClient#queryPurchaseHistoryAsync(String, PurchaseHistoryResponseListener)";

private MethodNames() {};
}
Expand Down Expand Up @@ -259,7 +259,7 @@ private static BillingClient buildBillingClient(Context context, MethodChannel c
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
final Map<String, Object> callbackArgs = new HashMap<>();
callbackArgs.put("responseCode", responseCode);
callbackArgs.put("purchases", fromPurchasesList(purchases));
callbackArgs.put("purchasesList", fromPurchasesList(purchases));
channel.invokeMethod(MethodNames.ON_PURCHASES_UPDATED, callbackArgs);
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/in_app_purchase/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ try {
}

project.ext {
// TODO(YOU): Set this to match your package ID in the Play Developer Console (see example/README.md).
APP_ID = "io.flutter.plugins.inapppurchaseexample.DEFAULT_DO_NOT_USE"
// TODO(YOU): Create release keys and a `keystore.properties` file. See
// `example/README.md` for more info and `keystore.example.properties` for an
// example.
APP_ID = configured ? keystoreProperties['appId'] : "io.flutter.plugins.inapppurchaseexample.DEFAULT_DO_NOT_USE"
KEYSTORE_STORE_FILE = configured ? rootProject.file(keystoreProperties['storeFile']) : null
KEYSTORE_STORE_PASSWORD = keystoreProperties['storePassword']
KEYSTORE_KEY_ALIAS = keystoreProperties['keyAlias']
KEYSTORE_KEY_PASSWORD = keystoreProperties['keyPassword']
VERSION_CODE = 1
VERSION_NAME = "0.0.1"
VERSION_CODE = configured ? keystoreProperties['versionCode'].toInteger() : 1
VERSION_NAME = configured ? keystoreProperties['versionName'] : "0.0.1"
}

if (project.APP_ID == "io.flutter.plugins.inapppurchaseexample.DEFAULT_DO_NOT_USE") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public void onPurchasesUpdatedListener() {

HashMap<String, Object> resultData = resultCaptor.getValue();
assertEquals(responseCode, resultData.get("responseCode"));
assertEquals(fromPurchasesList(purchasesList), resultData.get("purchases"));
assertEquals(fromPurchasesList(purchasesList), resultData.get("purchasesList"));
}

private void establishConnectedBillingClient(
Expand Down
5 changes: 4 additions & 1 deletion packages/in_app_purchase/example/keystore.example.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
storePassword=???
keyPassword=???
keyAlias=???
storeFile=???
storeFile=???
appId=io.flutter.plugins.inapppurchaseexample.DEFAULT_DO_NOT_USE
versionCode=1
versionName=0.0.1
136 changes: 73 additions & 63 deletions packages/in_app_purchase/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import 'package:in_app_purchase/in_app_purchase_connection.dart';

void main() => runApp(MyApp());

const List<String> _kProductIds = <String>[
'consumable',
'upgrade',
'subscription'
];

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
Expand All @@ -27,103 +33,107 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar(
title: const Text('IAP Example'),
),
body: Column(
body: ListView(
children: [
FutureBuilder(
future: _buildConnectionCheckTile(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.error != null) {
return Column(children: <Widget>[
buildListCard(ListTile(
title: Text(
'Error connecting: ' + snapshot.error.toString())))
]);
return buildListCard(ListTile(
title: Text(
'Error connecting: ' + snapshot.error.toString())));
} else if (!snapshot.hasData) {
return Column(children: <Widget>[
buildListCard(
ListTile(title: const Text('Trying to connect...')))
]);
return Card(
child:
ListTile(title: const Text('Trying to connect...')));
}
return Column(
children: snapshot.data,
);
return snapshot.data;
},
),
Expanded(
child: FutureBuilder(
future: InAppPurchaseConnection.instance.queryProductDetails(
<String>['consumable', 'upgrade', 'subscription'].toSet()),
builder: (BuildContext context,
AsyncSnapshot<ProductDetailsResponse> snapshot) {
if (snapshot.error != null) {
return Center(
child: Text('Error: ' + snapshot.error.toString()),
);
} else if (!snapshot.hasData) {
return Column(children: <Widget>[
buildListCard(ListTile(title: const Text('Loading...')))
]);
}
return Column(
children: <Widget>[
Center(child: Text('Products')),
Expanded(
child: ListView(
children: _buildProductList(snapshot.data),
),
),
],
FutureBuilder(
future: _buildProductList(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.error != null) {
return Center(
child: buildListCard(ListTile(
title: Text('Error fetching products'),
subtitle: snapshot.error)),
);
},
),
} else if (!snapshot.hasData) {
return Card(
child: (ListTile(
leading: CircularProgressIndicator(),
title: Text('Fetching products...'))));
}
return snapshot.data;
},
),
],
),
),
);
}

Future<List<ListTile>> _buildConnectionCheckTile() async {
Future<Card> _buildConnectionCheckTile() async {
final bool available = await InAppPurchaseConnection.instance.isAvailable();
final Widget storeHeader = buildListCard(
ListTile(
leading: Icon(available ? Icons.check : Icons.block),
title: Text(
'The store is ' + (available ? 'available' : 'unavailable') + '.'),
),
final Widget storeHeader = ListTile(
leading: Icon(available ? Icons.check : Icons.block,
color: available ? Colors.green : ThemeData.light().errorColor),
title: Text(
'The store is ' + (available ? 'available' : 'unavailable') + '.'),
);
final List<ListTile> children = <ListTile>[storeHeader];
final List<Widget> children = <Widget>[storeHeader];

if (!available) {
children.add(
buildListCard(
ListTile(
title: Text('Not connected',
style: TextStyle(color: ThemeData.light().errorColor)),
subtitle: const Text(
'Unable to connect to the payments processor. Has this app been configured correctly? See the example README for instructions.'),
),
children.addAll([
Divider(),
ListTile(
title: Text('Not connected',
style: TextStyle(color: ThemeData.light().errorColor)),
subtitle: const Text(
'Unable to connect to the payments processor. Has this app been configured correctly? See the example README for instructions.'),
),
);
]);
}
return children;
return Card(child: Column(children: children));
}

List<ListTile> _buildProductList(ProductDetailsResponse response) {
List<ListTile> productDetailsCards = response.productDetails.map(
Future<Card> _buildProductList() async {
final bool available = await InAppPurchaseConnection.instance.isAvailable();
if (!available) {
return Card();
}
final ListTile productHeader = ListTile(
title: Text('Products for Sale',
style: Theme.of(context).textTheme.headline));
ProductDetailsResponse response = await InAppPurchaseConnection.instance
.queryProductDetails(_kProductIds.toSet());
List<ListTile> productList = <ListTile>[];
if (!response.notFoundIDs.isEmpty) {
productList.add(ListTile(
title: Text('[${response.notFoundIDs.join(", ")}] not found',
style: TextStyle(color: ThemeData.light().errorColor)),
subtitle: Text(
'This app needs special configuration to run. Please see example/README.md for instructions.')));
}

productList.addAll(response.productDetails.map(
(ProductDetails productDetails) {
return buildListCard(ListTile(
return ListTile(
title: Text(
productDetails.title,
),
subtitle: Text(
productDetails.description,
),
trailing: Text(productDetails.price),
));
);
},
).toList();
return productDetailsCards;
));

return Card(
child:
Column(children: <Widget>[productHeader, Divider()] + productList));
}

static ListTile buildListCard(ListTile innerTile) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import '../channel.dart';
import 'sku_details_wrapper.dart';
import 'enum_converters.dart';

const String _kOnBillingServiceDisconnected =
'BillingClientStateListener#onBillingServiceDisconnected()';

/// This class can be used directly instead of [InAppPurchaseConnection] to call
/// Play-specific billing APIs.
///
Expand All @@ -35,7 +38,7 @@ class BillingClient {
// matching callback here to remember, and then once its twin is triggered it
// sends the handle back over the platform channel. We then access that handle
// in this array and call it in Dart code. See also [_callHandler].
List<Map<String, Function>> _callbacks = <Map<String, Function>>[];
Map<String, List<Function>> _callbacks = <String, List<Function>>{};

/// Calls
/// [`BillingClient#isReady()`](https://developer.android.com/reference/com/android/billingclient/api/BillingClient.html#isReady())
Expand All @@ -56,13 +59,12 @@ class BillingClient {
Future<BillingResponse> startConnection(
{@required
OnBillingServiceDisconnected onBillingServiceDisconnected}) async {
final Map<String, Function> callbacks = <String, Function>{
'OnBillingServiceDisconnected': onBillingServiceDisconnected,
};
_callbacks.add(callbacks);
List<Function> disconnectCallbacks =
_callbacks[_kOnBillingServiceDisconnected] ??= [];
disconnectCallbacks.add(onBillingServiceDisconnected);
return BillingResponseConverter().fromJson(await channel.invokeMethod(
"BillingClient#startConnection(BillingClientStateListener)",
<String, dynamic>{'handle': _callbacks.length - 1}));
<String, dynamic>{'handle': disconnectCallbacks.length - 1}));
}

/// Calls
Expand Down Expand Up @@ -134,10 +136,9 @@ class BillingClient {

Future<void> _callHandler(MethodCall call) async {
switch (call.method) {
case 'BillingClientStateListener#onBillingServiceDisconnected()':
case _kOnBillingServiceDisconnected:
final int handle = call.arguments['handle'];
await _callbacks[handle]['OnBillingServiceDisconnected']();
_callbacks.removeAt(handle);
await _callbacks[_kOnBillingServiceDisconnected][handle]();
break;
}
}
Expand All @@ -163,6 +164,9 @@ enum BillingResponse {
@JsonValue(-2)
featureNotSupported,

@JsonValue(-1)
serviceDisconnected,

@JsonValue(0)
ok,

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.