Преглед изворни кода

[modify]删除测试文件

zk пре 1 година
родитељ
комит
ce257da5f1

+ 37 - 0
lib/sdk/pay/agile_pay.dart

@@ -0,0 +1,37 @@
+import 'package:electronic_assistant/sdk/pay/alipay/alipay.dart';
+import 'package:electronic_assistant/sdk/pay/listener/i_agile_pay.dart';
+import 'package:electronic_assistant/sdk/pay/wxpay/wechat_pay.dart';
+import 'package:electronic_assistant/sdk/pay/wxpay/wechat_pay_info.dart';
+import 'alipay/ali_pay_info.dart';
+import 'applepay/apple_pay.dart';
+import 'applepay/apple_pay_info.dart';
+import 'code/agile_pay_code.dart';
+import 'listener/agile_pay_state.dart';
+
+class AgilePay {
+  static IAgilePay? realPay;
+
+  static void startPay(dynamic payInfo, AgilePayState payState) {
+    IAgilePay? iAgilePay;
+
+    if (payInfo is AliPayInfo) {
+      iAgilePay = Alipay(payInfo);
+    } else if (payInfo is WechatPayInfo) {
+      iAgilePay = WechatPay(payInfo);
+    } else if (payInfo is ApplePayInfo) {
+      iAgilePay = ApplePay(payInfo);
+    }
+    realPay = iAgilePay;
+    if (iAgilePay != null) {
+      iAgilePay.setPayListener(payState);
+      iAgilePay.pay();
+    } else {
+      payState.payError(AgilePayCode.payCodeNotSupport,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeNotSupport));
+    }
+  }
+
+  static void dispose() {
+    realPay?.dispose();
+  }
+}

+ 7 - 0
lib/sdk/pay/alipay/ali_pay_info.dart

@@ -0,0 +1,7 @@
+class AliPayInfo {
+  final String _payInfo;
+
+  AliPayInfo(this._payInfo);
+
+  String get payInfo => _payInfo;
+}

+ 62 - 0
lib/sdk/pay/alipay/alipay.dart

@@ -0,0 +1,62 @@
+import 'dart:async';
+
+import 'package:flutter/widgets.dart';
+
+import '../assist/agile_pay_state_info.dart';
+import '../code/agile_pay_code.dart';
+import '../listener/i_agile_pay.dart';
+import 'ali_pay_info.dart';
+import 'package:alipay_kit/alipay_kit.dart';
+
+class Alipay extends AgilePayStateInfo implements IAgilePay {
+  final AliPayInfo _aliPayInfo;
+
+  late final StreamSubscription<AlipayResp> _paySubs;
+
+  Alipay(this._aliPayInfo) {
+    _paySubs = AlipayKitPlatform.instance.payResp().listen(_listenPay);
+  }
+
+  void _listenPay(AlipayResp resp) {
+    final String content = 'pay: ${resp.resultStatus} - ${resp.result}';
+    debugPrint('agilePay-alipay---> $content');
+    if (!_paySubs.isPaused) {
+      sendError(AgilePayCode.payCodeCancelError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
+      return;
+    }
+    try {
+      int code = AgilePayCode.payCodeOtherError;
+      if (resp.resultStatus == AgilePayCode.payCodeAlipaySuccess) {
+        sendPaySuccess(resp.result);
+      } else {
+        if (resp.resultStatus != null &&
+            AgilePayCode.resultStatus.containsKey(resp.resultStatus)) {
+          code = resp.resultStatus!;
+        }
+        sendPayError(code, AgilePayCode.getMessageByCode(code));
+      }
+    } catch (e) {
+      sendError(AgilePayCode.payCodeOtherError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+    }
+  }
+
+  @override
+  void pay() {
+    sendPayBefore();
+    try {
+      AlipayKitPlatform.instance.pay(
+        orderInfo: _aliPayInfo.payInfo,
+      );
+    } catch (e) {
+      sendError(AgilePayCode.payCodeOtherError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+    }
+  }
+
+  @override
+  void dispose() {
+    _paySubs.cancel();
+  }
+}

+ 16 - 0
lib/sdk/pay/applepay/apple_pay.dart

@@ -0,0 +1,16 @@
+import 'package:electronic_assistant/sdk/pay/applepay/apple_pay_info.dart';
+
+import '../assist/agile_pay_state_info.dart';
+import '../listener/i_agile_pay.dart';
+
+class ApplePay extends AgilePayStateInfo implements IAgilePay {
+  final ApplePayInfo _payInfo;
+
+  ApplePay(this._payInfo);
+
+  @override
+  void pay() {}
+
+  @override
+  void dispose() {}
+}

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

@@ -0,0 +1 @@
+class ApplePayInfo {}

+ 33 - 0
lib/sdk/pay/assist/agile_pay_state_info.dart

@@ -0,0 +1,33 @@
+import '../listener/agile_pay_state.dart';
+
+abstract class AgilePayStateInfo {
+  AgilePayState? mPay;
+
+  void sendError(int errno, String? error) {
+    if (mPay != null) {
+      mPay?.error(errno, error);
+    }
+  }
+
+  void sendPaySuccess(String? result) {
+    if (mPay != null) {
+      mPay?.paySuccess(result);
+    }
+  }
+
+  void sendPayError(int errno, String? error) {
+    if (mPay != null) {
+      mPay?.payError(errno, error);
+    }
+  }
+
+  void sendPayBefore() {
+    if (mPay != null) {
+      mPay?.payBefore();
+    }
+  }
+
+  void setPayListener(AgilePayState pay) {
+    mPay = pay;
+  }
+}

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

@@ -0,0 +1,46 @@
+class AgilePayCode {
+  AgilePayCode._();
+
+  static const int payCodeQqwalletNotSupport = 50000;
+  static const int payCodeAlipaySuccess = 9000;
+  static const int payCodeOtherError = 99999;
+  static const int payCodeTokenError = 80000;
+  static const int payCodeTokenFormatError = 80001;
+  static const int payCodeWxEnvError = 80002;
+  static const int payCodeWxNoResultError = 80003;
+  static const int payCodeWxNoTokenError = 80004;
+  static const int payCodeWxNoPayperidError = 80005;
+  static const int payCodeParamsError = 80006;
+  static const int payCodeSystemBusy = 4000;
+  static const int payCodeOrderInfoError = 4001;
+  static const int payCodeCancelError = 6001;
+  static const int payCodeNetError = 6002;
+  static const int payCodeShengKeyNotMatch = 70000;
+  static const int payCodePayError = 70001;
+  static const int payCodeSystemError = 70002;
+  static const int payCodeNotSupport = 70003;
+
+  static final Map<int, String> resultStatus = {
+    payCodeSystemError: "系统异常",
+    payCodeNotSupport: "不支持该支付类型",
+    payCodeSystemBusy: "系统繁忙,请稍候再试",
+    payCodeOrderInfoError: "订单参数错误",
+    payCodeCancelError: "取消支付",
+    payCodeNetError: "网络连接异常",
+    payCodeTokenError: "Token获取失败",
+    payCodeTokenFormatError: "Token格式错误",
+    payCodeWxEnvError: "微信未安装或微信版本不支持",
+    payCodeWxNoResultError: "微信支付无返回值",
+    payCodeWxNoTokenError: "微信支付获取access_token错误",
+    payCodeWxNoPayperidError: "微信支付获取payperid错误",
+    payCodeParamsError: "参数错误",
+    payCodePayError: "支付失败",
+    payCodeShengKeyNotMatch: "证书不匹配",
+    payCodeQqwalletNotSupport: "QQ未安装或QQ版本不支持",
+    payCodeOtherError: "其他问题",
+  };
+
+  static String getMessageByCode(int code) {
+    return resultStatus[code] ?? "Unknown code";
+  }
+}

+ 9 - 0
lib/sdk/pay/listener/agile_pay_state.dart

@@ -0,0 +1,9 @@
+abstract class AgilePayState {
+  void error(int errno, String? error);
+
+  void payError(int error, String? errorMessage);
+
+  void paySuccess(String? result);
+
+  void payBefore();
+}

+ 9 - 0
lib/sdk/pay/listener/i_agile_pay.dart

@@ -0,0 +1,9 @@
+import 'agile_pay_state.dart';
+
+abstract class IAgilePay {
+  void pay();
+
+  void dispose();
+
+  void setPayListener(AgilePayState agilePayState);
+}

+ 77 - 0
lib/sdk/pay/wxpay/wechat_pay.dart

@@ -0,0 +1,77 @@
+import 'dart:async';
+
+import 'package:electronic_assistant/sdk/pay/wxpay/wechat_pay_info.dart';
+import 'package:flutter/widgets.dart';
+import 'package:wechat_kit/wechat_kit.dart';
+
+import '../assist/agile_pay_state_info.dart';
+import '../code/agile_pay_code.dart';
+import '../listener/i_agile_pay.dart';
+
+class WechatPay extends AgilePayStateInfo implements IAgilePay {
+  late final StreamSubscription<WechatResp> _respSubs;
+
+  final WechatPayInfo _payInfo;
+
+  WechatPay(this._payInfo) {
+    _respSubs = WechatKitPlatform.instance.respStream().listen(_listenResp);
+  }
+
+  void _listenResp(WechatResp resp) {
+    if (resp is WechatPayResp) {
+      final String content = 'pay: ${resp.errorCode} ${resp.errorMsg}';
+      debugPrint('agilePay-wechat---> $content');
+      if (!_respSubs.isPaused) {
+        sendError(AgilePayCode.payCodeCancelError,
+            AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
+        return;
+      }
+      if (resp.errorCode == WechatResp.kErrorCodeSuccess) {
+        sendPaySuccess(resp.returnKey);
+      } else if (resp.errorCode == WechatResp.kErrorCodeUserCancel) {
+        sendError(AgilePayCode.payCodeCancelError,
+            AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
+      } else {
+        sendError(resp.errorCode, resp.errorMsg);
+      }
+    }
+  }
+
+  Future<bool> isInstalled() {
+    return WechatKitPlatform.instance.isInstalled();
+  }
+
+  @override
+  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));
+        }
+      } else {
+        sendError(AgilePayCode.payCodeWxEnvError,
+            AgilePayCode.getMessageByCode(AgilePayCode.payCodeWxEnvError));
+      }
+    } catch (e) {
+      sendError(AgilePayCode.payCodeOtherError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+    }
+  }
+
+  @override
+  void dispose() {
+    _respSubs.cancel();
+  }
+}

+ 26 - 0
lib/sdk/pay/wxpay/wechat_pay_info.dart

@@ -0,0 +1,26 @@
+class WechatPayInfo {
+  String _appId; // 应用ID
+  String _partnerId; // 商户号
+  String _prepayId; // 预支付交易会话ID
+  String _package; // 扩展字段
+  String _noncestr; // 随机字符串
+  String _timestamp; // 时间戳
+  String _sign; // 签名
+
+  WechatPayInfo(this._appId, this._partnerId, this._prepayId, this._package,
+      this._noncestr, this._timestamp, this._sign);
+
+  String get appId => _appId;
+
+  String get partnerId => _partnerId;
+
+  String get prepayId => _prepayId;
+
+  String get package => _package;
+
+  String get noncestr => _noncestr;
+
+  String get timestamp => _timestamp;
+
+  String get sign => _sign;
+}

+ 0 - 27
plugin/gravity_engine/example/test/widget_test.dart

@@ -1,27 +0,0 @@
-// This is a basic Flutter widget test.
-//
-// To perform an interaction with a widget in your test, use the WidgetTester
-// utility in the flutter_test package. For example, you can send tap and scroll
-// gestures. You can also use WidgetTester to find child widgets in the widget
-// tree, read text, and verify that the values of widget properties are correct.
-
-import 'package:flutter/material.dart';
-import 'package:flutter_test/flutter_test.dart';
-
-import 'package:gravity_engine_example/main.dart';
-
-void main() {
-  testWidgets('Verify Platform version', (WidgetTester tester) async {
-    // Build our app and trigger a frame.
-    await tester.pumpWidget(const MyApp());
-
-    // Verify that platform version is retrieved.
-    expect(
-      find.byWidgetPredicate(
-        (Widget widget) =>
-            widget is Text && widget.data!.startsWith('Running on:'),
-      ),
-      findsOneWidget,
-    );
-  });
-}