Skip to content

Commit

Permalink
fix(masamune_purchase): Throw Exception when cancelled.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Sep 15, 2022
1 parent d957524 commit e96645e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
46 changes: 26 additions & 20 deletions packages/masamune_purchase/lib/src/purchase_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class PurchaseModel extends ValueModel<List<MobilePurchaseProduct>> {
"There is no method for purchase. Set up a method for purchasing.",
);
}
} else if (purchase.status == PurchaseStatus.canceled) {
throw Exception("Your purchase has been canceled.");
}
if (Config.isAndroid) {
if (!_autoConsumeOnAndroid &&
Expand All @@ -195,30 +197,24 @@ class PurchaseModel extends ValueModel<List<MobilePurchaseProduct>> {
}
}
if (done) {
if (_purchaseCompleter != null &&
!_purchaseCompleter!.isCompleted) {
_purchaseCompleter!.complete();
}
_purchaseCompleter?.complete();
_purchaseCompleter = null;
notifyListeners();
}
} catch (e) {
if (_purchaseCompleter != null &&
!_purchaseCompleter!.isCompleted) {
_purchaseCompleter!.completeError(e);
}
_purchaseCompleter?.completeError(e);
_purchaseCompleter = null;
rethrow;
}
},
onDone: () {
if (_purchaseCompleter != null && !_purchaseCompleter!.isCompleted) {
_purchaseCompleter!.complete();
}
_purchaseCompleter?.complete();
_purchaseCompleter = null;
dispose();
},
onError: (error) {
if (_purchaseCompleter != null && !_purchaseCompleter!.isCompleted) {
_purchaseCompleter!.completeError(error);
}
_purchaseCompleter?.completeError(error);
_purchaseCompleter = null;
throw Exception(error.toString());
},
);
Expand Down Expand Up @@ -411,23 +407,33 @@ class PurchaseModel extends ValueModel<List<MobilePurchaseProduct>> {
);
_purchaseCompleter = Completer<void>();
if (product.type == ProductType.consumable) {
await connection
final success = await connection
.buyConsumable(
purchaseParam: purchaseParam,
autoConsume: _autoConsumeOnAndroid || Config.isIOS,
)
.timeout(timeout);
if (!success) {
throw Exception("Purchase failed or was canceled.");
}
} else {
await connection
final success = await connection
.buyNonConsumable(purchaseParam: purchaseParam)
.timeout(timeout);
if (!success) {
throw Exception("Purchase failed or was canceled.");
}
}
await _purchaseCompleter!.future;
await _purchaseCompleter?.future;
_purchaseCompleter?.complete();
_purchaseCompleter = null;
} catch (e) {
if (_purchaseCompleter != null && !_purchaseCompleter!.isCompleted) {
_purchaseCompleter!.complete();
}
_purchaseCompleter?.completeError(e);
_purchaseCompleter = null;
rethrow;
} finally {
_purchaseCompleter?.complete();
_purchaseCompleter = null;
}
}

Expand Down
23 changes: 15 additions & 8 deletions packages/masamune_purchase/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ packages:
name: in_app_purchase_android
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.3+3"
version: "0.2.3+4"
in_app_purchase_platform_interface:
dependency: "direct main"
description:
Expand Down Expand Up @@ -323,35 +323,35 @@ packages:
name: katana
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+1"
version: "0.14.1"
katana_annotation:
dependency: transitive
description:
name: katana_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.2"
version: "0.14.2+1"
katana_flutter:
dependency: transitive
description:
name: katana_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+1"
version: "0.14.0+2"
katana_module:
dependency: transitive
description:
name: katana_module
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+11"
version: "0.14.0+12"
katana_routing:
dependency: transitive
description:
name: katana_routing
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+1"
version: "0.14.0+2"
loading_animations:
dependency: transitive
description:
Expand All @@ -365,7 +365,7 @@ packages:
name: masamune
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.1+6"
version: "0.14.1+7"
material_color_utilities:
dependency: transitive
description:
Expand Down Expand Up @@ -393,7 +393,7 @@ packages:
name: model_notifier
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+5"
version: "0.14.0+6"
nm:
dependency: transitive
description:
Expand Down Expand Up @@ -548,6 +548,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
recase:
dependency: transitive
description:
name: recase
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
riverpod:
dependency: transitive
description:
Expand Down

0 comments on commit e96645e

Please sign in to comment.