alipay.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if (!_paySubs.isPaused) {
  18. sendError(AgilePayCode.payCodeCancelError,
  19. AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
  20. return;
  21. }
  22. try {
  23. int code = AgilePayCode.payCodeOtherError;
  24. if (resp.resultStatus == AgilePayCode.payCodeAlipaySuccess) {
  25. sendPaySuccess(resp.result);
  26. } else {
  27. if (resp.resultStatus != null &&
  28. AgilePayCode.resultStatus.containsKey(resp.resultStatus)) {
  29. code = resp.resultStatus!;
  30. }
  31. sendPayError(code, AgilePayCode.getMessageByCode(code));
  32. }
  33. } catch (e) {
  34. sendError(AgilePayCode.payCodeOtherError,
  35. AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
  36. }
  37. }
  38. @override
  39. void pay() {
  40. sendPayBefore();
  41. try {
  42. AlipayKitPlatform.instance.pay(
  43. orderInfo: _aliPayInfo.payInfo,
  44. );
  45. } catch (e) {
  46. sendError(AgilePayCode.payCodePayError,
  47. AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
  48. }
  49. }
  50. @override
  51. void dispose() {
  52. _paySubs.cancel();
  53. }
  54. }