Browse Source

[new]增加支付

zk 1 year ago
parent
commit
f40a046c83

+ 2 - 2
lib/sdk/pay/alipay/alipay.dart

@@ -50,8 +50,8 @@ class Alipay extends AgilePayStateInfo implements IAgilePay {
         orderInfo: _aliPayInfo.payInfo,
       );
     } catch (e) {
-      sendError(AgilePayCode.payCodeOtherError,
-          AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+      sendError(AgilePayCode.payCodePayError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
     }
   }
 

+ 98 - 3
lib/sdk/pay/applepay/apple_pay.dart

@@ -1,16 +1,111 @@
+import 'dart:async';
+
 import 'package:electronic_assistant/sdk/pay/applepay/apple_pay_info.dart';
+import 'package:in_app_purchase/in_app_purchase.dart';
 
 import '../assist/agile_pay_state_info.dart';
+import '../assist/product_type.dart';
+import '../code/agile_pay_code.dart';
 import '../listener/i_agile_pay.dart';
 
 class ApplePay extends AgilePayStateInfo implements IAgilePay {
   final ApplePayInfo _payInfo;
+  late final StreamSubscription<List<PurchaseDetails>>
+      _purchaseUpdatedSubscription;
+  late final InAppPurchase _iap;
+
+  final Duration timeout = const Duration(seconds: 15);
+
+  ApplePay(this._payInfo) {
+    _iap = InAppPurchase.instance;
+    _purchaseUpdatedSubscription =
+        _iap.purchaseStream.listen((purchaseDetailsList) {
+      _listenToPurchaseUpdated(purchaseDetailsList);
+    }, onDone: () {
+      _purchaseUpdatedSubscription.cancel();
+    }, onError: (error) {});
+  }
+
+  void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
+    for (var purchaseDetails in purchaseDetailsList) {
+      if (purchaseDetails.status == PurchaseStatus.pending) {
+      } else if (purchaseDetails.status == PurchaseStatus.error) {
+        _verifyErrorPurchase(purchaseDetails);
+      } else if (purchaseDetails.status == PurchaseStatus.purchased) {
+        _verifySuccessPurchase(purchaseDetails);
+      }
+    }
+  }
+
+  void _verifyErrorPurchase(PurchaseDetails purchaseDetails) {
+    if (purchaseDetails.error?.code == 'store_kit_network_error') {
+      sendError(AgilePayCode.payCodeNetError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeNetError));
+    } else if (purchaseDetails.error?.code == 'store_kit_payment_cancelled') {
+      sendError(AgilePayCode.payCodeCancelError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
+    } else {
+      sendError(AgilePayCode.payCodeOtherError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+    }
+  }
 
-  ApplePay(this._payInfo);
+  void _verifySuccessPurchase(PurchaseDetails purchaseDetails) {
+    sendPaySuccess(purchaseDetails.purchaseID);
+  }
 
   @override
-  void pay() {}
+  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;
+    }
+  }
 
   @override
-  void dispose() {}
+  void dispose() {
+    _purchaseUpdatedSubscription.cancel();
+  }
 }

+ 13 - 1
lib/sdk/pay/applepay/apple_pay_info.dart

@@ -1 +1,13 @@
-class ApplePayInfo {}
+import '../assist/product_type.dart';
+
+class ApplePayInfo {
+  final String _productId;
+
+  final ProductType _type;
+
+  ApplePayInfo(this._productId, this._type);
+
+  String get productId => _productId;
+
+  ProductType get type => _type;
+}

+ 4 - 0
lib/sdk/pay/assist/product_type.dart

@@ -0,0 +1,4 @@
+enum ProductType {
+  consumable,
+  nonConsumable,
+}

+ 6 - 0
lib/sdk/pay/code/agile_pay_code.dart

@@ -19,6 +19,9 @@ class AgilePayCode {
   static const int payCodePayError = 70001;
   static const int payCodeSystemError = 70002;
   static const int payCodeNotSupport = 70003;
+  static const int payCodeNotConnectStore = 70004;
+  static const int payCodeProductNotFindStore = 70005;
+  static const int payCodeRequestSendError = 70006;
 
   static final Map<int, String> resultStatus = {
     payCodeSystemError: "系统异常",
@@ -38,6 +41,9 @@ class AgilePayCode {
     payCodeShengKeyNotMatch: "证书不匹配",
     payCodeQqwalletNotSupport: "QQ未安装或QQ版本不支持",
     payCodeOtherError: "其他问题",
+    payCodeNotConnectStore: "无法连接到商店",
+    payCodeProductNotFindStore: "商品未找到",
+    payCodeRequestSendError: "请求支付发送失败",
   };
 
   static String getMessageByCode(int code) {

+ 28 - 41
pubspec.lock

@@ -443,11 +443,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "3.0.2"
-  flutter_localizations:
-    dependency: transitive
-    description: flutter
-    source: sdk
-    version: "0.0.0"
   flutter_plugin_android_lifecycle:
     dependency: transitive
     description:
@@ -649,14 +644,38 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.1.3"
-  intl:
+  in_app_purchase:
+    dependency: "direct main"
+    description:
+      name: in_app_purchase
+      sha256: "960f26a08d9351fb8f89f08901f8a829d41b04d45a694b8f776121d9e41dcad6"
+      url: "https://pub.dev"
+    source: hosted
+    version: "3.2.0"
+  in_app_purchase_android:
     dependency: transitive
     description:
-      name: intl
-      sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
+      name: in_app_purchase_android
+      sha256: "25eb8694819caca282a527c26d5a1775164965c554ee99b9c10f3a0e44675c75"
       url: "https://pub.dev"
     source: hosted
-    version: "0.19.0"
+    version: "0.3.6+8"
+  in_app_purchase_platform_interface:
+    dependency: transitive
+    description:
+      name: in_app_purchase_platform_interface
+      sha256: "1d353d38251da5b9fea6635c0ebfc6bb17a2d28d0e86ea5e083bf64244f1fb4c"
+      url: "https://pub.dev"
+    source: hosted
+    version: "1.4.0"
+  in_app_purchase_storekit:
+    dependency: transitive
+    description:
+      name: in_app_purchase_storekit
+      sha256: e9a408da11d055f9af9849859e1251b2b0a2f84f256fa3bf1285c5614a397079
+      url: "https://pub.dev"
+    source: hosted
+    version: "0.3.18+1"
   io:
     dependency: transitive
     description:
@@ -961,38 +980,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.3.0"
-  pay:
-    dependency: "direct main"
-    description:
-      name: pay
-      sha256: ad904db0e06848cade6990a3ce1e10e921ae48f7ee06447873e07b9688ac1fc5
-      url: "https://pub.dev"
-    source: hosted
-    version: "2.0.0"
-  pay_android:
-    dependency: transitive
-    description:
-      name: pay_android
-      sha256: aa46cd0ece1807d3fa293113fdb84afb5fc4b6ed60cf09a4886b753acb300859
-      url: "https://pub.dev"
-    source: hosted
-    version: "2.0.0"
-  pay_ios:
-    dependency: transitive
-    description:
-      name: pay_ios
-      sha256: "75ccb285d03f22b136c58ab8e8e0c4b614ee52a8b67e6ccfb680d4d8c04a70f6"
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.0.11"
-  pay_platform_interface:
-    dependency: transitive
-    description:
-      name: pay_platform_interface
-      sha256: "26a379e33c46508987c7afee8cde6f4aca5b5ab0afc697c27efbd33a9c2ea82a"
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.0.4"
   petitparser:
     dependency: transitive
     description:

+ 1 - 1
pubspec.yaml

@@ -98,7 +98,7 @@ dependencies:
   #支付
   alipay_kit: ^6.0.0
   wechat_kit: ^6.0.1
-  pay: ^2.0.0
+  in_app_purchase: ^3.2.0
 
 
 dev_dependencies: