| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import 'dart:async';
- import 'package:electronic_assistant/sdk/pay/assist/apple_or_google_pay_info.dart';
- import 'package:electronic_assistant/sdk/pay/assist/product_type.dart';
- import 'package:in_app_purchase/in_app_purchase.dart';
- import '../code/agile_pay_code.dart';
- import 'agile_pay_state_info.dart';
- abstract class AppleOrGooglePay extends AgilePayStateInfo {
- final AppleOrGooglePayInfo _payInfo;
- late final StreamSubscription<List<PurchaseDetails>>
- _purchaseUpdatedSubscription;
- late final InAppPurchase _iap;
- final Duration timeout = const Duration(seconds: 10);
- AppleOrGooglePay(this._payInfo) {
- _iap = InAppPurchase.instance;
- _purchaseUpdatedSubscription =
- _iap.purchaseStream.listen((purchaseDetailsList) {
- listenToPurchaseUpdated(purchaseDetailsList);
- }, onDone: () {
- _purchaseUpdatedSubscription.cancel();
- }, onError: (error) {});
- }
- void pay() async {
- sendPayBefore();
- try {
- final bool isAvailable = await _iap.isAvailable().timeout(timeout);
- if (!isAvailable) {
- sendError(AgilePayCode.payCodeNotConnectStore,
- AgilePayCode.getMessageByCode(AgilePayCode.payCodeNotConnectStore));
- return;
- }
- final ProductDetailsResponse response = await InAppPurchase.instance
- .queryProductDetails({_payInfo.productId});
- if (response.notFoundIDs.isNotEmpty || response.productDetails.isEmpty) {
- sendError(
- AgilePayCode.payCodeProductNotFindStore,
- AgilePayCode.getMessageByCode(
- AgilePayCode.payCodeProductNotFindStore));
- return;
- }
- List<ProductDetails> products = response.productDetails;
- for (var element in products) {
- bool isSend;
- if (_payInfo.type == ProductType.consumable) {
- isSend = await _iap.buyConsumable(
- purchaseParam: PurchaseParam(productDetails: element));
- } else {
- isSend = await _iap.buyNonConsumable(
- purchaseParam: PurchaseParam(productDetails: element));
- }
- if (!isSend) {
- sendError(
- AgilePayCode.payCodeRequestSendError,
- AgilePayCode.getMessageByCode(
- AgilePayCode.payCodeRequestSendError));
- return;
- }
- return;
- }
- } catch (e) {
- if (e is TimeoutException) {
- sendError(AgilePayCode.payCodeNotConnectStore,
- AgilePayCode.getMessageByCode(AgilePayCode.payCodeNotConnectStore));
- return;
- }
- sendError(AgilePayCode.payCodeOtherError,
- AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
- return;
- }
- }
- void dispose() {
- _purchaseUpdatedSubscription.cancel();
- }
- void listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList);
- }
|