mirror of
https://github.com/koloideal/Casha.git
synced 2026-08-08 17:51:14 +03:00
first google play release for internal testing
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
@@ -15,6 +14,9 @@ class PurchaseResult {
|
||||
|
||||
factory PurchaseResult.failed([String? error]) =>
|
||||
PurchaseResult(success: false, error: error);
|
||||
|
||||
factory PurchaseResult.cancelled() =>
|
||||
const PurchaseResult(success: false);
|
||||
}
|
||||
|
||||
abstract class BillingService {
|
||||
@@ -23,24 +25,11 @@ abstract class BillingService {
|
||||
Future<PurchaseResult> purchasePro();
|
||||
Future<PurchaseResult> restorePurchases();
|
||||
Future<PurchaseResult> queryPastPurchase();
|
||||
Future<void> completePurchase(String purchaseToken);
|
||||
Stream<List<PurchaseDetails>> get purchaseStream;
|
||||
Future<void> dispose();
|
||||
}
|
||||
|
||||
class PlayBillingService implements BillingService {
|
||||
final InAppPurchase _inAppPurchase = InAppPurchase.instance;
|
||||
late final StreamSubscription<List<PurchaseDetails>> _sub;
|
||||
final _controller = StreamController<List<PurchaseDetails>>.broadcast();
|
||||
|
||||
PlayBillingService() {
|
||||
_sub = _inAppPurchase.purchaseStream.listen((purchases) {
|
||||
_controller.add(purchases);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<List<PurchaseDetails>> get purchaseStream => _controller.stream;
|
||||
|
||||
@override
|
||||
Future<PurchaseResult> purchasePro() async {
|
||||
@@ -52,51 +41,26 @@ class PlayBillingService implements BillingService {
|
||||
final response = await _inAppPurchase.queryProductDetails(
|
||||
{BillingService.proProductId},
|
||||
);
|
||||
if (response.error != null) {
|
||||
return PurchaseResult.failed(response.error!.message);
|
||||
}
|
||||
if (response.productDetails.isEmpty) {
|
||||
return PurchaseResult.failed('Product not found');
|
||||
}
|
||||
|
||||
final product = response.productDetails.first;
|
||||
final purchaseParam = PurchaseParam(productDetails: product);
|
||||
|
||||
final started = await _inAppPurchase.buyNonConsumable(
|
||||
purchaseParam: purchaseParam,
|
||||
);
|
||||
if (!started) {
|
||||
return PurchaseResult.failed('Could not start purchase');
|
||||
}
|
||||
|
||||
final completer = Completer<PurchaseResult>();
|
||||
late StreamSubscription sub;
|
||||
sub = purchaseStream.timeout(
|
||||
const Duration(seconds: 60),
|
||||
onTimeout: (sink) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(PurchaseResult.failed('Purchase timed out'));
|
||||
return _waitForPurchase(
|
||||
timeout: const Duration(minutes: 2),
|
||||
timeoutError: 'Purchase timed out',
|
||||
start: () async {
|
||||
final started = await _inAppPurchase.buyNonConsumable(
|
||||
purchaseParam: PurchaseParam(productDetails: product),
|
||||
);
|
||||
if (!started) {
|
||||
throw StateError('Could not start purchase');
|
||||
}
|
||||
sub.cancel();
|
||||
},
|
||||
).listen((purchases) {
|
||||
for (final p in purchases) {
|
||||
if (p.productID == BillingService.proProductId &&
|
||||
p.status == PurchaseStatus.purchased) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(PurchaseResult.ok(p.verificationData.serverVerificationData));
|
||||
}
|
||||
sub.cancel();
|
||||
return;
|
||||
}
|
||||
if (p.status == PurchaseStatus.error) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(PurchaseResult.failed(p.error?.message));
|
||||
}
|
||||
sub.cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return completer.future;
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -106,102 +70,101 @@ class PlayBillingService implements BillingService {
|
||||
return PurchaseResult.failed('Billing not available');
|
||||
}
|
||||
|
||||
await _inAppPurchase.restorePurchases();
|
||||
|
||||
final completer = Completer<PurchaseResult>();
|
||||
late StreamSubscription sub;
|
||||
sub = purchaseStream.timeout(
|
||||
const Duration(seconds: 15),
|
||||
onTimeout: (sink) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(PurchaseResult.failed('Restore timed out'));
|
||||
}
|
||||
sub.cancel();
|
||||
},
|
||||
).listen((purchases) {
|
||||
for (final p in purchases) {
|
||||
if (p.productID == BillingService.proProductId &&
|
||||
(p.status == PurchaseStatus.restored ||
|
||||
p.status == PurchaseStatus.purchased)) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(PurchaseResult.ok(p.verificationData.serverVerificationData));
|
||||
}
|
||||
sub.cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PurchaseResult> queryPastPurchase() async {
|
||||
final available = await _inAppPurchase.isAvailable();
|
||||
if (!available) {
|
||||
return const PurchaseResult();
|
||||
}
|
||||
|
||||
final response = await _inAppPurchase.queryProductDetails(
|
||||
{BillingService.proProductId},
|
||||
return _waitForPurchase(
|
||||
timeout: const Duration(seconds: 30),
|
||||
timeoutError: 'No purchases found',
|
||||
start: _inAppPurchase.restorePurchases,
|
||||
);
|
||||
if (response.productDetails.isEmpty) {
|
||||
return const PurchaseResult();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PurchaseResult> queryPastPurchase() => restorePurchases();
|
||||
|
||||
Future<PurchaseResult> _waitForPurchase({
|
||||
required Future<void> Function() start,
|
||||
required Duration timeout,
|
||||
required String timeoutError,
|
||||
}) async {
|
||||
final completer = Completer<PurchaseResult>();
|
||||
late StreamSubscription sub;
|
||||
sub = purchaseStream.timeout(
|
||||
const Duration(seconds: 10),
|
||||
onTimeout: (sink) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(const PurchaseResult());
|
||||
}
|
||||
sub.cancel();
|
||||
},
|
||||
).listen((purchases) {
|
||||
for (final p in purchases) {
|
||||
if (p.productID == BillingService.proProductId &&
|
||||
(p.status == PurchaseStatus.restored ||
|
||||
p.status == PurchaseStatus.purchased)) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(PurchaseResult.ok(p.verificationData.serverVerificationData));
|
||||
}
|
||||
sub.cancel();
|
||||
return;
|
||||
}
|
||||
var handlingPurchase = false;
|
||||
late final StreamSubscription<List<PurchaseDetails>> subscription;
|
||||
late final Timer timeoutTimer;
|
||||
|
||||
void finish(PurchaseResult result) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(result);
|
||||
}
|
||||
});
|
||||
|
||||
await _inAppPurchase.restorePurchases();
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> completePurchase(String purchaseToken) async {
|
||||
if (kDebugMode) {
|
||||
print('completePurchase: $purchaseToken');
|
||||
}
|
||||
|
||||
Future<void> handlePurchase(PurchaseDetails purchase) async {
|
||||
if (purchase.productID != BillingService.proProductId) return;
|
||||
|
||||
switch (purchase.status) {
|
||||
case PurchaseStatus.pending:
|
||||
return;
|
||||
case PurchaseStatus.purchased:
|
||||
case PurchaseStatus.restored:
|
||||
if (handlingPurchase) return;
|
||||
handlingPurchase = true;
|
||||
try {
|
||||
final token = purchase.verificationData.serverVerificationData;
|
||||
if (token.isEmpty) {
|
||||
finish(PurchaseResult.failed('Purchase verification data is empty'));
|
||||
return;
|
||||
}
|
||||
if (purchase.pendingCompletePurchase) {
|
||||
await _inAppPurchase.completePurchase(purchase);
|
||||
}
|
||||
finish(PurchaseResult.ok(token));
|
||||
} catch (error) {
|
||||
finish(PurchaseResult.failed(error.toString()));
|
||||
} finally {
|
||||
handlingPurchase = false;
|
||||
}
|
||||
case PurchaseStatus.error:
|
||||
finish(PurchaseResult.failed(purchase.error?.message));
|
||||
case PurchaseStatus.canceled:
|
||||
finish(PurchaseResult.cancelled());
|
||||
}
|
||||
}
|
||||
|
||||
subscription = _inAppPurchase.purchaseStream.listen(
|
||||
(purchases) {
|
||||
for (final purchase in purchases) {
|
||||
unawaited(handlePurchase(purchase));
|
||||
}
|
||||
},
|
||||
onError: (Object error) {
|
||||
finish(PurchaseResult.failed(error.toString()));
|
||||
},
|
||||
);
|
||||
timeoutTimer = Timer(
|
||||
timeout,
|
||||
() => finish(PurchaseResult.failed(timeoutError)),
|
||||
);
|
||||
|
||||
try {
|
||||
await start();
|
||||
} catch (error) {
|
||||
finish(PurchaseResult.failed(error.toString()));
|
||||
}
|
||||
|
||||
final result = await completer.future;
|
||||
timeoutTimer.cancel();
|
||||
await subscription.cancel();
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await _sub.cancel();
|
||||
await _controller.close();
|
||||
}
|
||||
Future<void> dispose() async {}
|
||||
}
|
||||
|
||||
class DebugBillingService implements BillingService {
|
||||
static const _key = 'debug_purchase_token';
|
||||
final SharedPreferences _prefs;
|
||||
final _controller = StreamController<List<PurchaseDetails>>.broadcast();
|
||||
|
||||
DebugBillingService(this._prefs);
|
||||
|
||||
@override
|
||||
Stream<List<PurchaseDetails>> get purchaseStream => _controller.stream;
|
||||
|
||||
@override
|
||||
Future<PurchaseResult> purchasePro() async {
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
@@ -230,10 +193,5 @@ class DebugBillingService implements BillingService {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> completePurchase(String purchaseToken) async {}
|
||||
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await _controller.close();
|
||||
}
|
||||
Future<void> dispose() async {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user