wechat_pay.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import 'dart:async';
  2. import 'package:electronic_assistant/sdk/pay/wxpay/wechat_pay_info.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:wechat_kit/wechat_kit.dart';
  5. import '../assist/agile_pay_state_info.dart';
  6. import '../code/agile_pay_code.dart';
  7. import '../listener/i_agile_pay.dart';
  8. class WechatPay extends AgilePayStateInfo implements IAgilePay {
  9. late final StreamSubscription<WechatResp> _respSubs;
  10. final WechatPayInfo _payInfo;
  11. WechatPay(this._payInfo) {
  12. _respSubs = WechatKitPlatform.instance.respStream().listen(_listenResp);
  13. }
  14. void _listenResp(WechatResp resp) {
  15. if (resp is WechatPayResp) {
  16. final String content = 'pay: ${resp.errorCode} ${resp.errorMsg}';
  17. debugPrint('agilePay-wechat---> $content');
  18. if (!_respSubs.isPaused) {
  19. sendError(AgilePayCode.payCodeCancelError,
  20. AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
  21. return;
  22. }
  23. if (resp.errorCode == WechatResp.kErrorCodeSuccess) {
  24. sendPaySuccess(resp.returnKey);
  25. } else if (resp.errorCode == WechatResp.kErrorCodeUserCancel) {
  26. sendError(AgilePayCode.payCodeCancelError,
  27. AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
  28. } else {
  29. sendError(resp.errorCode, resp.errorMsg);
  30. }
  31. }
  32. }
  33. Future<bool> isInstalled() {
  34. return WechatKitPlatform.instance.isInstalled();
  35. }
  36. @override
  37. void pay() async {
  38. sendPayBefore();
  39. try {
  40. bool isInstall = await isInstalled();
  41. if (isInstall) {
  42. try {
  43. WechatKitPlatform.instance.pay(
  44. appId: _payInfo.appId,
  45. partnerId: _payInfo.partnerId,
  46. prepayId: _payInfo.prepayId,
  47. package: _payInfo.package,
  48. nonceStr: _payInfo.noncestr,
  49. timeStamp: _payInfo.timestamp,
  50. sign: _payInfo.sign);
  51. } catch (e) {
  52. sendError(AgilePayCode.payCodePayError,
  53. AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
  54. }
  55. } else {
  56. sendError(AgilePayCode.payCodeWxEnvError,
  57. AgilePayCode.getMessageByCode(AgilePayCode.payCodeWxEnvError));
  58. }
  59. } catch (e) {
  60. sendError(AgilePayCode.payCodeOtherError,
  61. AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
  62. }
  63. }
  64. @override
  65. void dispose() {
  66. _respSubs.cancel();
  67. }
  68. }