apple_or_google_pay.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'dart:async';
  2. import 'package:electronic_assistant/sdk/pay/assist/apple_or_google_pay_info.dart';
  3. import 'package:electronic_assistant/sdk/pay/assist/product_type.dart';
  4. import 'package:in_app_purchase/in_app_purchase.dart';
  5. import '../code/agile_pay_code.dart';
  6. import 'agile_pay_state_info.dart';
  7. abstract class AppleOrGooglePay extends AgilePayStateInfo {
  8. final AppleOrGooglePayInfo _payInfo;
  9. late final StreamSubscription<List<PurchaseDetails>>
  10. _purchaseUpdatedSubscription;
  11. late final InAppPurchase _iap;
  12. final Duration timeout = const Duration(seconds: 10);
  13. AppleOrGooglePay(this._payInfo) {
  14. _iap = InAppPurchase.instance;
  15. _purchaseUpdatedSubscription =
  16. _iap.purchaseStream.listen((purchaseDetailsList) {
  17. listenToPurchaseUpdated(purchaseDetailsList);
  18. }, onDone: () {
  19. _purchaseUpdatedSubscription.cancel();
  20. }, onError: (error) {});
  21. }
  22. void pay() async {
  23. sendPayBefore();
  24. try {
  25. final bool isAvailable = await _iap.isAvailable().timeout(timeout);
  26. if (!isAvailable) {
  27. sendError(AgilePayCode.payCodeNotConnectStore,
  28. AgilePayCode.getMessageByCode(AgilePayCode.payCodeNotConnectStore));
  29. return;
  30. }
  31. final ProductDetailsResponse response = await InAppPurchase.instance
  32. .queryProductDetails({_payInfo.productId});
  33. if (response.notFoundIDs.isNotEmpty || response.productDetails.isEmpty) {
  34. sendError(
  35. AgilePayCode.payCodeProductNotFindStore,
  36. AgilePayCode.getMessageByCode(
  37. AgilePayCode.payCodeProductNotFindStore));
  38. return;
  39. }
  40. List<ProductDetails> products = response.productDetails;
  41. for (var element in products) {
  42. bool isSend;
  43. if (_payInfo.type == ProductType.consumable) {
  44. isSend = await _iap.buyConsumable(
  45. purchaseParam: PurchaseParam(
  46. productDetails: element,
  47. applicationUserName: _payInfo.accountToken));
  48. } else {
  49. isSend = await _iap.buyNonConsumable(
  50. purchaseParam: PurchaseParam(
  51. productDetails: element,
  52. applicationUserName: _payInfo.accountToken));
  53. }
  54. if (!isSend) {
  55. sendError(
  56. AgilePayCode.payCodeRequestSendError,
  57. AgilePayCode.getMessageByCode(
  58. AgilePayCode.payCodeRequestSendError));
  59. return;
  60. }
  61. return;
  62. }
  63. } catch (e) {
  64. if (e is TimeoutException) {
  65. sendError(AgilePayCode.payCodeNotConnectStore,
  66. AgilePayCode.getMessageByCode(AgilePayCode.payCodeNotConnectStore));
  67. return;
  68. }
  69. sendError(AgilePayCode.payCodeOtherError,
  70. AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
  71. return;
  72. }
  73. }
  74. void dispose() {
  75. _purchaseUpdatedSubscription.cancel();
  76. }
  77. void listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList);
  78. }