alipay.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. } finally {
  32. dispose();
  33. }
  34. }
  35. @override
  36. void pay() {
  37. sendPayBefore();
  38. try {
  39. AlipayKitPlatform.instance.pay(
  40. orderInfo: _aliPayInfo.payInfo,
  41. );
  42. } catch (e) {
  43. sendError(AgilePayCode.payCodePayError,
  44. AgilePayCode.getMessageByCode(AgilePayCode.payCodePayError));
  45. }
  46. }
  47. void dispose() {
  48. _paySubs.cancel();
  49. }
  50. }