145 lines
4.1 KiB
Dart
145 lines
4.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:mnemo_cards/features/deeplinks/deeplinks_manager_impl.dart';
|
|
import 'package:mnemo_cards/managers/repository/repository.dart';
|
|
import 'package:mnemo_cards_common/src/dtos/payment/payment_dto.dart';
|
|
|
|
import 'package:mnemo_cards_common/src/dtos/payment/product_type.dart';
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
|
|
import '../../di/locator.dart';
|
|
import '../../main.dart';
|
|
import '../analytics/analytics.dart';
|
|
import 'purchase_handler.dart';
|
|
|
|
class YookassaPurchaseHandler implements PurchaseHandler {
|
|
final Repository _repository;
|
|
|
|
const YookassaPurchaseHandler(this._repository);
|
|
|
|
@override
|
|
Future<bool> checkPurchase({
|
|
required MnemoCardsProduct product,
|
|
required String paymentToken,
|
|
}) async {
|
|
return await _repository.checkPayment(
|
|
product: product,
|
|
key: paymentToken,
|
|
paymentSystem: paymentSystem,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<void> initPurchase({
|
|
required String id,
|
|
required MnemoCardsProductType productType,
|
|
required String paymentId,
|
|
String? source,
|
|
}) async {
|
|
await _buyWithYooMoney(id, productType, scaffoldKey.currentContext);
|
|
}
|
|
|
|
Future<void> _buyWithYooMoney(
|
|
String id,
|
|
MnemoCardsProductType productType,
|
|
BuildContext? context,
|
|
) async {
|
|
final dto = await _repository.createPayment(
|
|
id,
|
|
productType,
|
|
PaymentSystem.yookassa,
|
|
);
|
|
if (dto == null) {
|
|
Analytics.buyError(
|
|
null,
|
|
id: id,
|
|
PaymentSystem.yookassa,
|
|
message: 'payment dto is null',
|
|
);
|
|
return;
|
|
}
|
|
final purchaseUrl = dto.purchaseUrl;
|
|
final checkUrl = dto.checkUrl;
|
|
final completer = Completer();
|
|
bool dialogClosed = false;
|
|
final realContext = (context ?? scaffoldKey.currentContext)!;
|
|
completer.future.then((_) {
|
|
if (!dialogClosed) {
|
|
return Navigator.of(realContext).pop();
|
|
}
|
|
});
|
|
final controller = WebViewController()
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..loadRequest(Uri.parse(purchaseUrl))
|
|
..setNavigationDelegate(
|
|
NavigationDelegate(
|
|
onNavigationRequest: (request) async {
|
|
final url = request.url;
|
|
if (url.startsWith('http')) {
|
|
return NavigationDecision.navigate;
|
|
} else if (url.startsWith('mnemocards')) {
|
|
locator.deeplinkManager.handle(Uri.parse(url));
|
|
if (!completer.isCompleted) {
|
|
completer.complete();
|
|
}
|
|
return NavigationDecision.prevent;
|
|
} else {
|
|
launchUrlString(url);
|
|
return NavigationDecision.prevent;
|
|
}
|
|
},
|
|
),
|
|
);
|
|
final result = showModalBottomSheet(
|
|
context: realContext,
|
|
enableDrag: false,
|
|
showDragHandle: true,
|
|
useSafeArea: true,
|
|
isScrollControlled: true,
|
|
backgroundColor: Colors.white,
|
|
builder: (context) {
|
|
return Container(
|
|
child: WebViewWidget(controller: controller),
|
|
padding: EdgeInsets.only(
|
|
bottom: MediaQuery.of(context).viewInsets.bottom,
|
|
),
|
|
);
|
|
},
|
|
).then((_) async {
|
|
// final success = await checkPurchase(
|
|
// productId: id,
|
|
// productType: MnemoCardsProductType.pack,
|
|
// paymentToken: id,
|
|
// );
|
|
return locator.deeplinkManager.handle(Uri.parse(checkUrl));
|
|
|
|
// if (success) {
|
|
// Analytics.buyComplete(
|
|
// null,
|
|
// id: id,
|
|
// PaymentSystem.yookassa,
|
|
// );
|
|
// locator.packUpdater.updateAvailablePacks();
|
|
// return true;
|
|
// }
|
|
// Analytics.buyError(
|
|
// null,
|
|
// id: id,
|
|
// PaymentSystem.yookassa,
|
|
// message: 'payment not verified',
|
|
// );
|
|
// return false;
|
|
});
|
|
await result.then((_) {
|
|
dialogClosed = true;
|
|
if (!completer.isCompleted) {
|
|
completer.complete();
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
PaymentSystem get paymentSystem => PaymentSystem.yookassa;
|
|
}
|