import 'dart:developer'; import 'package:auto_route/auto_route.dart'; import 'package:flutter_rustore_billing/flutter_rustore_billing.dart'; import 'package:mnemo_cards/features/analytics/analytics.dart'; import 'package:mnemo_cards/flags.dart'; import 'package:mnemo_cards/main.dart'; import 'package:mnemo_cards_common/mnemo_cards_common.dart'; import '../../../di/locator.dart'; import '../../../domain/router/dialogs.dart'; class RustorePurchaseService { Future init() async { return RustoreBillingClient.initialize( "2063563896", "mnemocards://iamback", true, ).then((value) { print("initialize success: $value"); return true; }, onError: (err) { print("initialize err: $err"); return false; }); } Future buyPack(CardPackBuyDto dto) async { if (dto.rustoreId == null) { return false; } if (!await init()) { return false; } if (!await RustoreBillingClient.available().then((value) { print("available success $value"); return true; }, onError: (err) { print("available err: $err"); return false; })) { return false; } final product = (await RustoreBillingClient.products([dto.rustoreId])) .products .firstOrNull; if (product == null) { return false; } try { final result = await RustoreBillingClient.purchase(dto.rustoreId!, null); try { try { // creating payment to check it later // in case there will be an error after confirmation await locator.repository.checkPayment( itemId: result.successPurchase!.productId, key: result.successPurchase!.subscriptionToken!, paymentSystem: androidPaymentSystem, ); } catch (e, s) { Analytics.buyError( dto, PaymentSystem.rustore, message: 'Error when creating payment', ); log('Error when creating payment', error: e, stackTrace: s); showErrorDialog( 'Технические шоколадки...\n\nПопробуйте позже или обновите приложение до последней версии'); return false; } final p = await RustoreBillingClient.confirm( result.successPurchase!.subscriptionToken!, ); if (p.success) { final r = await locator.repository.checkPayment( itemId: result.successPurchase!.productId, key: result.successPurchase!.subscriptionToken!, paymentSystem: androidPaymentSystem, ); if (r) { return r; } else { Analytics.buyError( dto, PaymentSystem.rustore, message: 'Error when confirming payment', ); showErrorDialog( 'Технические шоколадки...\n\nДоступ откроется в течение 5 мин', ); } } } catch (e, s) { log('Error when confirming purchase', error: e, stackTrace: s); Analytics.crashlyticsError( e: e, s: s, message: 'Error when confirming rustore purchase', ); } } catch (e, s) { log('Error when processing purchase', error: e, stackTrace: s); Analytics.crashlyticsError( e: e, s: s, message: 'Error when processing rustore purchase', ); } return false; } }