66 lines
1.9 KiB
Dart
66 lines
1.9 KiB
Dart
|
|
import 'package:mnemo_cards/di/locator.dart';
|
|||
|
|
import 'package:mnemo_cards/domain/router/dialogs.dart';
|
|||
|
|
import 'package:mnemo_cards/features/analytics/analytics.dart';
|
|||
|
|
import 'package:mnemo_cards/features/deeplinks/deeplinks_manager.dart';
|
|||
|
|
import 'package:mnemo_cards/main.dart';
|
|||
|
|
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
|||
|
|
|
|||
|
|
import 'purchase_manager.dart';
|
|||
|
|
|
|||
|
|
class PurchaseDeeplinkHandler implements DeeplinkHandler {
|
|||
|
|
final PurchaseManager _purchaseManager;
|
|||
|
|
|
|||
|
|
PurchaseDeeplinkHandler(this._purchaseManager);
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
bool canHandle(Uri uri) => uri.host == 'purchase';
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
void handle(Uri uri) {
|
|||
|
|
final path = uri.path.replaceFirst('/', '');
|
|||
|
|
switch (path) {
|
|||
|
|
case PurchaseInitDeeplink.deeplinkPath:
|
|||
|
|
return _initPurchase(uri);
|
|||
|
|
case PurchaseCheckDeeplink.deeplinkPath:
|
|||
|
|
return _checkPurchase(uri);
|
|||
|
|
}
|
|||
|
|
Analytics.buyDeeplinkError(uri, message: 'Unknown path: ${path}');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void _initPurchase(Uri uri) {
|
|||
|
|
final model = PurchaseInitDeeplink.parse(uri);
|
|||
|
|
_purchaseManager.initPurchase(
|
|||
|
|
id: model.id,
|
|||
|
|
productType: model.productType,
|
|||
|
|
paymentId: model.paymentId,
|
|||
|
|
paymentSystem: model.paymentSystem,
|
|||
|
|
source: model.source?.notEmptyOrNull,
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void _checkPurchase(Uri uri) async {
|
|||
|
|
final model = PurchaseCheckDeeplink.parse(uri);
|
|||
|
|
final success = await _purchaseManager.checkPurchase(
|
|||
|
|
product: MnemoCardsProduct(
|
|||
|
|
id: model.id,
|
|||
|
|
type: model.productType,
|
|||
|
|
),
|
|||
|
|
token: model.token,
|
|||
|
|
paymentSystem: model.paymentSystem,
|
|||
|
|
);
|
|||
|
|
if (success) {
|
|||
|
|
if (model.productType == MnemoCardsProductType.pack) {
|
|||
|
|
locator.packUpdater.updatePackAndSaveImages(
|
|||
|
|
model.id,
|
|||
|
|
shouldUpdateHolder: true,
|
|||
|
|
);
|
|||
|
|
} else {
|
|||
|
|
locator.userManager.updateUser();
|
|||
|
|
showInfoDialog('Ура! Теперь у вас есть подписка!');
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
// showErrorDialog('Не уда');
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|