mnemo_cards/lib/features/purchase/rustore/mnemo_rustore.dart

113 lines
3.5 KiB
Dart
Raw Normal View History

2024-07-22 20:49:30 +00:00
import 'dart:developer';
import 'package:auto_route/auto_route.dart';
2024-07-18 21:45:04 +00:00
import 'package:flutter_rustore_billing/flutter_rustore_billing.dart';
2024-07-22 20:49:30 +00:00
import 'package:mnemo_cards/features/analytics/analytics.dart';
2024-07-18 21:45:04 +00:00
import 'package:mnemo_cards/flags.dart';
2024-07-22 20:49:30 +00:00
import 'package:mnemo_cards/main.dart';
2024-07-18 21:45:04 +00:00
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import '../../../di/locator.dart';
2024-07-22 20:49:30 +00:00
import '../../../domain/router/dialogs.dart';
2024-07-18 21:45:04 +00:00
class RustorePurchaseService {
Future<bool> 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<bool> buyPack(CardPackBuyDto dto) async {
if (dto.rustoreId == null) {
return false;
}
if (!await init()) {
return false;
}
2024-07-22 20:49:30 +00:00
if (!await RustoreBillingClient.available().then((value) {
print("available success $value");
return true;
}, onError: (err) {
print("available err: $err");
return false;
})) {
return false;
}
2024-07-18 21:45:04 +00:00
final product = (await RustoreBillingClient.products([dto.rustoreId]))
.products
.firstOrNull;
if (product == null) {
return false;
}
2024-07-22 20:49:30 +00:00
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(
2024-08-03 22:37:10 +00:00
itemId: result.successPurchase!.productId,
key: result.successPurchase!.subscriptionToken!,
paymentSystem: androidPaymentSystem,
2024-07-22 20:49:30 +00:00
);
} 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(
2024-08-03 22:37:10 +00:00
result.successPurchase!.subscriptionToken!,
2024-07-22 20:49:30 +00:00
);
if (p.success) {
final r = await locator.repository.checkPayment(
2024-08-03 22:37:10 +00:00
itemId: result.successPurchase!.productId,
key: result.successPurchase!.subscriptionToken!,
paymentSystem: androidPaymentSystem,
2024-07-22 20:49:30 +00:00
);
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',
2024-07-18 21:45:04 +00:00
);
}
2024-07-22 20:49:30 +00:00
return false;
2024-07-18 21:45:04 +00:00
}
}