alipay.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'dart:async';
  2. import 'package:flutter/widgets.dart';
  3. import '../assist/agile_pay_state_info.dart';
  4. import '../code/agile_pay_code.dart';
  5. import '../listener/i_agile_pay.dart';
  6. import 'ali_pay_info.dart';
  7. import 'package:alipay_kit/alipay_kit.dart';
  8. class Alipay extends AgilePayStateInfo implements IAgilePay {
  9. final AliPayInfo _aliPayInfo;
  10. late final StreamSubscription<AlipayResp> _paySubs;
  11. Alipay(this._aliPayInfo) {
  12. _paySubs = AlipayKitPlatform.instance.payResp().listen(_listenPay);
  13. }
  14. void _listenPay(AlipayResp resp) {
  15. final String content = 'pay: ${resp.resultStatus} - ${resp.result}';
  16. debugPrint('agilePay-alipay---> $content');
  17. try {
  18. int code = AgilePayCode.payCodeOtherError;
  19. if (resp.resultStatus == AgilePayCode.payCodeAlipaySuccess) {
  20. sendPaySuccess(resp.result);
  21. } else {
  22. if (resp.resultStatus != null &&
  23. AgilePayCode.resultStatus.containsKey(resp.resultStatus)) {
  24. code = resp.resultStatus!;
  25. }
  26. sendPayError(code, AgilePayCode.getMessageByCode(code));
  27. }
  28. } catch (e) {
  29. sendError(AgilePayCode.payCodeOtherError,
  30. AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
  31. }
  32. }
  33. @override
  34. void pay() {
  35. sendPayBefore();
  36. try {
  37. AlipayKitPlatform.instance.pay(
  38. orderInfo: _aliPayInfo.payInfo,
  39. );
  40. } catch (e) {
  41. sendError(AgilePayCode.payCodePayError,
  42. AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
  43. }
  44. }
  45. @override
  46. void dispose() {
  47. _paySubs.cancel();
  48. }
  49. }