Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[in_app_purchase] Implement registerPlatform method for iOS implementation #3851

Merged
merged 33 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
652e5d1
Draft implementation of platform interface
mvanbeusekom Apr 2, 2021
bcb9d38
Added finishPurchase and restorePurchase definitions
mvanbeusekom Apr 7, 2021
23230c9
Fix formatting and analysis warnings
mvanbeusekom Apr 7, 2021
8f17a42
Added missing license header
mvanbeusekom Apr 7, 2021
164700d
Fix analysis warnings
mvanbeusekom Apr 7, 2021
af8a388
Fix typo
mvanbeusekom Apr 7, 2021
a7693bd
Remove NoopInAppPurchase implementation
mvanbeusekom Apr 7, 2021
b80a3c6
Apply feedback from PR
mvanbeusekom Apr 8, 2021
12943f4
Removed obsolete PurchaseException
mvanbeusekom Apr 9, 2021
7ec5de6
Updated documentation per feedback
mvanbeusekom Apr 14, 2021
3ceef85
Fixed formatting
mvanbeusekom Apr 14, 2021
efc6120
Allow nullable instance
mvanbeusekom Apr 14, 2021
e7f6216
Updated readme to reflect setInstance method
mvanbeusekom Apr 14, 2021
24d305b
Removed platform specific comments
mvanbeusekom Apr 21, 2021
d86ac55
Add interfaces to support InAppPurchaseAddition
mvanbeusekom Apr 21, 2021
9eb358f
Document the addition functionality in README
mvanbeusekom Apr 22, 2021
33d1118
Added example code and documentation
mvanbeusekom Apr 22, 2021
a7cea45
Reference the platform_interface
mvanbeusekom Apr 22, 2021
06dd755
Initial iOS specific implementation
mvanbeusekom Apr 26, 2021
21eb28d
Added StoreKit wrapper tests
mvanbeusekom Apr 26, 2021
0cfc84d
Added unit-tests
mvanbeusekom Apr 28, 2021
fa22504
Merged with master
mvanbeusekom Apr 28, 2021
2f115b9
Clean up code
mvanbeusekom Apr 28, 2021
d4e9165
Added initial version of README.md
mvanbeusekom Apr 28, 2021
61dc0be
Fixed typo
mvanbeusekom Apr 28, 2021
a33f312
Added support for iOS specific features
mvanbeusekom Apr 28, 2021
415ac5e
Add missing license headers
mvanbeusekom Apr 28, 2021
70e89cd
Added feedback from PR
mvanbeusekom Apr 29, 2021
99fde90
Merge remote-tracking branch 'upstream/master' into iap_federated_ios…
mvanbeusekom Apr 29, 2021
0474545
Unblock tree by solving lint error
mvanbeusekom Apr 30, 2021
4f723de
Merge remote-tracking branch 'upstream/master' into iap_federated_ios…
mvanbeusekom May 5, 2021
125c490
Implement registerPlatform method for iOS implementation
mvanbeusekom May 5, 2021
67a491f
Added TODO comment to add example
mvanbeusekom May 5, 2021
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
3 changes: 3 additions & 0 deletions packages/in_app_purchase/in_app_purchase_ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ dependencies:
...
```

## TODO
- [ ] Add an example application demonstrating the use of the [in_app_purchase_ios] package (see also issue [flutter/flutter#81695](https://github.com/flutter/flutter/issues/81695)).

[1]: ../in_app_purchase/in_app_purchase
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ const String kIAPSource = 'app_store';
/// This translates various `StoreKit` calls and responses into the
/// generic plugin API.
class InAppPurchaseIosPlatform extends InAppPurchasePlatform {
/// Returns the singleton instance of the [InAppPurchaseIosPlatform] that should be
/// used across the app.
static InAppPurchaseIosPlatform get instance => _getOrCreateInstance();
static InAppPurchaseIosPlatform? _instance;
static late SKPaymentQueueWrapper _skPaymentQueueWrapper;
static late _TransactionObserver _observer;

Expand All @@ -44,22 +40,19 @@ class InAppPurchaseIosPlatform extends InAppPurchasePlatform {
@visibleForTesting
static SKTransactionObserverWrapper get observer => _observer;

static InAppPurchaseIosPlatform _getOrCreateInstance() {
if (_instance != null) {
return _instance!;
}

/// Registers this class as the default instance of [InAppPurchasePlatform].
static void registerPlatform() {
// Register the [InAppPurchaseIosPlatformAddition] containing iOS
// platform-specific functionality.
InAppPurchasePlatformAddition.instance = InAppPurchaseIosPlatformAddition();

// Register the platform-specific implementation of the idiomatic
// InAppPurchase API.
_instance = InAppPurchaseIosPlatform();
InAppPurchasePlatform.setInstance(InAppPurchaseIosPlatform());

_skPaymentQueueWrapper = SKPaymentQueueWrapper();
_observer = _TransactionObserver(StreamController.broadcast());
_skPaymentQueueWrapper.setTransactionObserver(observer);
return _instance!;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();

final FakeIOSPlatform fakeIOSPlatform = FakeIOSPlatform();
late InAppPurchaseIosPlatform iapIosPlatform;

setUpAll(() {
SystemChannels.platform
.setMockMethodCallHandler(fakeIOSPlatform.onMethodCall);
});

setUp(() => fakeIOSPlatform.reset());
setUp(() {
InAppPurchaseIosPlatform.registerPlatform();
iapIosPlatform = InAppPurchasePlatform.instance as InAppPurchaseIosPlatform;
fakeIOSPlatform.reset();
});

tearDown(() => fakeIOSPlatform.reset());

group('isAvailable', () {
test('true', () async {
expect(await InAppPurchaseIosPlatform.instance.isAvailable(), isTrue);
expect(await iapIosPlatform.isAvailable(), isTrue);
});
});

Expand Down Expand Up @@ -69,8 +74,7 @@ void main() {
group('restore purchases', () {
test('should emit restored transactions on purchase stream', () async {
Completer completer = Completer();
Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;

late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
Expand All @@ -80,7 +84,7 @@ void main() {
}
});

await InAppPurchaseIosPlatform.instance.restorePurchases();
await iapIosPlatform.restorePurchases();
List<PurchaseDetails> details = await completer.future;

expect(details.length, 2);
Expand All @@ -103,8 +107,7 @@ void main() {
fakeIOSPlatform.transactions
.insert(0, fakeIOSPlatform.createPurchasedTransaction('foo', 'bar'));
Completer completer = Completer();
Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;

late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
Expand All @@ -113,7 +116,7 @@ void main() {
subscription.cancel();
}
});
await InAppPurchaseIosPlatform.instance.restorePurchases();
await iapIosPlatform.restorePurchases();
List<PurchaseDetails> details = await completer.future;
expect(details.length, 3);
for (int i = 0; i < fakeIOSPlatform.transactions.length; i++) {
Expand All @@ -139,8 +142,7 @@ void main() {
() async {
fakeIOSPlatform.receiptData = null;
Completer completer = Completer();
Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;

late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
Expand All @@ -150,7 +152,7 @@ void main() {
}
});

await InAppPurchaseIosPlatform.instance.restorePurchases();
await iapIosPlatform.restorePurchases();
List<PurchaseDetails> details = await completer.future;

for (PurchaseDetails purchase in details) {
Expand All @@ -166,7 +168,7 @@ void main() {
userInfo: {'message': 'errorMessage'});

expect(
() => InAppPurchaseIosPlatform.instance.restorePurchases(),
() => iapIosPlatform.restorePurchases(),
throwsA(
isA<SKError>()
.having((error) => error.code, 'code', 123)
Expand All @@ -183,8 +185,7 @@ void main() {
() async {
List<PurchaseDetails> details = [];
Completer completer = Completer();
Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;

late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
Expand All @@ -198,8 +199,7 @@ void main() {
productDetails:
AppStoreProductDetails.fromSKProduct(dummyProductWrapper),
applicationUserName: 'appName');
await InAppPurchaseIosPlatform.instance
.buyNonConsumable(purchaseParam: purchaseParam);
await iapIosPlatform.buyNonConsumable(purchaseParam: purchaseParam);

List<PurchaseDetails> result = await completer.future;
expect(result.length, 2);
Expand All @@ -211,8 +211,7 @@ void main() {
() async {
List<PurchaseDetails> details = [];
Completer completer = Completer();
Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;

late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
Expand All @@ -226,8 +225,7 @@ void main() {
productDetails:
AppStoreProductDetails.fromSKProduct(dummyProductWrapper),
applicationUserName: 'appName');
await InAppPurchaseIosPlatform.instance
.buyConsumable(purchaseParam: purchaseParam);
await iapIosPlatform.buyConsumable(purchaseParam: purchaseParam);

List<PurchaseDetails> result = await completer.future;
expect(result.length, 2);
Expand All @@ -240,8 +238,8 @@ void main() {
AppStoreProductDetails.fromSKProduct(dummyProductWrapper),
applicationUserName: 'appName');
expect(
() => InAppPurchaseIosPlatform.instance
.buyConsumable(purchaseParam: purchaseParam, autoConsume: false),
() => iapIosPlatform.buyConsumable(
purchaseParam: purchaseParam, autoConsume: false),
throwsA(isInstanceOf<AssertionError>()));
});

Expand All @@ -251,8 +249,7 @@ void main() {
Completer completer = Completer();
late IAPError error;

Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;
late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
details.addAll(purchaseDetailsList);
Expand All @@ -268,8 +265,7 @@ void main() {
productDetails:
AppStoreProductDetails.fromSKProduct(dummyProductWrapper),
applicationUserName: 'appName');
await InAppPurchaseIosPlatform.instance
.buyNonConsumable(purchaseParam: purchaseParam);
await iapIosPlatform.buyNonConsumable(purchaseParam: purchaseParam);

IAPError completerError = await completer.future;
expect(completerError.code, 'purchase_error');
Expand All @@ -283,14 +279,13 @@ void main() {
test('should complete purchase', () async {
List<PurchaseDetails> details = [];
Completer completer = Completer();
Stream<List<PurchaseDetails>> stream =
InAppPurchaseIosPlatform.instance.purchaseStream;
Stream<List<PurchaseDetails>> stream = iapIosPlatform.purchaseStream;
late StreamSubscription subscription;
subscription = stream.listen((purchaseDetailsList) {
details.addAll(purchaseDetailsList);
purchaseDetailsList.forEach((purchaseDetails) {
if (purchaseDetails.pendingCompletePurchase) {
InAppPurchaseIosPlatform.instance.completePurchase(purchaseDetails);
iapIosPlatform.completePurchase(purchaseDetails);
completer.complete(details);
subscription.cancel();
}
Expand All @@ -300,8 +295,7 @@ void main() {
productDetails:
AppStoreProductDetails.fromSKProduct(dummyProductWrapper),
applicationUserName: 'appName');
await InAppPurchaseIosPlatform.instance
.buyNonConsumable(purchaseParam: purchaseParam);
await iapIosPlatform.buyNonConsumable(purchaseParam: purchaseParam);
List<PurchaseDetails> result = await completer.future;
expect(result.length, 2);
expect(result.first.productID, dummyProductWrapper.productIdentifier);
Expand Down