Procházet zdrojové kódy

[modify]调整支付代码

zk před 1 rokem
rodič
revize
1cddcc9964

+ 10 - 78
lib/sdk/pay/applepay/apple_pay.dart

@@ -4,48 +4,36 @@ import 'package:electronic_assistant/sdk/pay/applepay/apple_pay_info.dart';
 import 'package:flutter/widgets.dart';
 import 'package:in_app_purchase/in_app_purchase.dart';
 
-import '../assist/agile_pay_state_info.dart';
-import '../assist/product_type.dart';
+import '../assist/apple_or_google_pay.dart';
 import '../code/agile_pay_code.dart';
 import '../listener/i_agile_pay.dart';
 
-class ApplePay extends AgilePayStateInfo implements IAgilePay {
+class ApplePay extends AppleOrGooglePay 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) : super(_payInfo);
 
-  ApplePay(this._payInfo) {
-    _iap = InAppPurchase.instance;
-    _purchaseUpdatedSubscription =
-        _iap.purchaseStream.listen((purchaseDetailsList) {
-      _listenToPurchaseUpdated(purchaseDetailsList);
-    }, onDone: () {
-      _purchaseUpdatedSubscription.cancel();
-    }, onError: (error) {});
-  }
-
-  void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
+  @override
+  void listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
     for (var purchaseDetails in purchaseDetailsList) {
+      debugPrint(
+          'agilePay-applePay--PurchaseUpdated-> ${purchaseDetails.toString()}');
       if (purchaseDetails.status == PurchaseStatus.pending) {
       } else if (purchaseDetails.status == PurchaseStatus.error) {
         _verifyErrorPurchase(purchaseDetails);
       } else if (purchaseDetails.status == PurchaseStatus.purchased) {
         _verifySuccessPurchase(purchaseDetails);
+      } else if (purchaseDetails.status == PurchaseStatus.canceled) {
+        sendError(AgilePayCode.payCodeCancelError,
+            AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
       }
     }
   }
 
   void _verifyErrorPurchase(PurchaseDetails purchaseDetails) {
-    debugPrint('agilePay-applePay--error-> ${purchaseDetails.toString()}');
     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));
@@ -53,62 +41,6 @@ class ApplePay extends AgilePayStateInfo implements IAgilePay {
   }
 
   void _verifySuccessPurchase(PurchaseDetails purchaseDetails) {
-    debugPrint('agilePay-applePay--success-> ${purchaseDetails.toString()}');
     sendPaySuccess(purchaseDetails.purchaseID);
   }
-
-  @override
-  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() {
-    _purchaseUpdatedSubscription.cancel();
-  }
 }

+ 3 - 10
lib/sdk/pay/applepay/apple_pay_info.dart

@@ -1,13 +1,6 @@
+import '../assist/apple_or_google_pay_info.dart';
 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;
+class ApplePayInfo extends AppleOrGooglePayInfo {
+  ApplePayInfo(super.productId, super.type);
 }

+ 83 - 0
lib/sdk/pay/assist/apple_or_google_pay.dart

@@ -0,0 +1,83 @@
+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);
+}

+ 13 - 0
lib/sdk/pay/assist/apple_or_google_pay_info.dart

@@ -0,0 +1,13 @@
+import 'package:electronic_assistant/sdk/pay/assist/product_type.dart';
+
+class AppleOrGooglePayInfo {
+  final String _productId;
+
+  final ProductType _type;
+
+  AppleOrGooglePayInfo(this._productId, this._type);
+
+  String get productId => _productId;
+
+  ProductType get type => _type;
+}

+ 24 - 14
lib/sdk/pay/wxpay/wechat_pay.dart

@@ -45,20 +45,26 @@ class WechatPay extends AgilePayStateInfo implements IAgilePay {
   void pay() async {
     sendPayBefore();
     try {
-      bool isInstall = await isInstalled();
-      if (isInstall) {
-        try {
-          WechatKitPlatform.instance.pay(
-              appId: _payInfo.appId,
-              partnerId: _payInfo.partnerId,
-              prepayId: _payInfo.prepayId,
-              package: _payInfo.package,
-              nonceStr: _payInfo.noncestr,
-              timeStamp: _payInfo.timestamp,
-              sign: _payInfo.sign);
-        } catch (e) {
-          sendError(AgilePayCode.payCodePayError,
-              AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
+      if (await check()) {
+        if (await isInstalled()) {
+          await WechatKitPlatform.instance.registerApp(
+              appId: _payInfo.appId, universalLink: _payInfo.universalLink);
+          try {
+            WechatKitPlatform.instance.pay(
+                appId: _payInfo.appId,
+                partnerId: _payInfo.partnerId,
+                prepayId: _payInfo.prepayId,
+                package: _payInfo.package,
+                nonceStr: _payInfo.noncestr,
+                timeStamp: _payInfo.timestamp,
+                sign: _payInfo.sign);
+          } catch (e) {
+            sendError(AgilePayCode.payCodePayError,
+                AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
+          }
+        } else {
+          sendError(AgilePayCode.payCodeWxEnvError,
+              AgilePayCode.getMessageByCode(AgilePayCode.payCodeWxEnvError));
         }
       } else {
         sendError(AgilePayCode.payCodeWxEnvError,
@@ -70,6 +76,10 @@ class WechatPay extends AgilePayStateInfo implements IAgilePay {
     }
   }
 
+  Future<bool> check() async {
+    return WechatKitPlatform.instance.isSupportApi();
+  }
+
   @override
   void dispose() {
     _respSubs.cancel();

+ 7 - 1
lib/sdk/pay/wxpay/wechat_pay_info.dart

@@ -6,9 +6,15 @@ class WechatPayInfo {
   String _noncestr; // 随机字符串
   String _timestamp; // 时间戳
   String _sign; // 签名
+  String? _universalLink;
 
   WechatPayInfo(this._appId, this._partnerId, this._prepayId, this._package,
-      this._noncestr, this._timestamp, this._sign);
+      this._noncestr, this._timestamp, this._sign,
+      {String? universalLink}) {
+    _universalLink = universalLink;
+  }
+
+  String? get universalLink => _universalLink;
 
   String get appId => _appId;