payment_status_manager.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:location/data/bean/goods_bean.dart';
  4. import 'package:location/data/bean/pay_item_bean.dart';
  5. import 'package:location/data/repositories/account_repository.dart';
  6. import 'package:location/data/repositories/member_repository.dart';
  7. import 'package:synchronized/synchronized.dart';
  8. import '../handler/event_handler.dart';
  9. import 'async_util.dart';
  10. @lazySingleton
  11. class PaymentStatusManager {
  12. final MemberRepository memberRepository;
  13. final AccountRepository accountRepository;
  14. PaymentStatusManager(this.memberRepository, this.accountRepository);
  15. //订单状态
  16. //0-查询失败,继续轮询
  17. //1-未支付,继续轮询
  18. //2-支付成功
  19. //3-支付关闭
  20. //4-已退款
  21. static const int payStatusFail = 0;
  22. static const int payStatusUnpaid = 1;
  23. static const int payStatusSuccess = 2;
  24. static const int payStatusClose = 3;
  25. static const int payStatusRefund = 4;
  26. final Map<String, PaymentStatusCallback> callbackMap = {};
  27. final Map<String, CancelableFuture> pollingSubscriptionMap = {};
  28. final _lock = Lock();
  29. void _startCheckPolling(
  30. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean,
  31. {String? receiptData}) async {
  32. await _lock.synchronized(() async {
  33. pollingSubscriptionMap[orderNo]?.cancel();
  34. debugPrint('开始轮询支付状态: orderNo = $orderNo');
  35. CancelableFuture orderFuture = AsyncUtil.retryWithExponentialBackoff(
  36. () {
  37. return memberRepository
  38. .orderStatus(orderNo, receiptData: receiptData)
  39. .then((status) {
  40. if (status == payStatusSuccess) {
  41. return true;
  42. } else {
  43. throw PaymentStatusException(status);
  44. }
  45. });
  46. },
  47. 10,
  48. predicate: (error) {
  49. if (error is PaymentStatusException) {
  50. return error.status == payStatusFail ||
  51. error.status == payStatusUnpaid;
  52. }
  53. return true;
  54. });
  55. orderFuture.then((data) async {
  56. debugPrint('支付成功: orderNo = $orderNo');
  57. accountRepository.refreshMemberStatus();
  58. await _lock.synchronized(() {
  59. callbackMap[orderNo]
  60. ?.onPaymentSuccess(orderNo, paymentWay, storeItemBean);
  61. callbackMap.remove(orderNo);
  62. });
  63. reportPaySuccess(storeItemBean.amount, orderNo, storeItemBean.name,
  64. paymentWay.payMethod);
  65. }).catchError((error) {
  66. debugPrint('支付失败: orderNo = $orderNo, error = $error');
  67. });
  68. pollingSubscriptionMap[orderNo] = orderFuture;
  69. });
  70. }
  71. void reportPaySuccess(
  72. int price, String orderId, String itemName, int paymentWay) {
  73. EventHandler.reportPay(price, orderId, itemName, paymentWay);
  74. }
  75. void checkPaymentStatus(
  76. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean,
  77. {String? receiptData}) {
  78. // recordKeyInfoToDisk(orderNo, paymentWay, storeItemBean);
  79. _startCheckPolling(orderNo, paymentWay, storeItemBean,
  80. receiptData: receiptData);
  81. }
  82. void registerPaymentSuccessCallback(
  83. String orderNo, PaymentStatusCallback callback) async {
  84. await _lock.synchronized(() {
  85. callbackMap[orderNo] = callback;
  86. });
  87. }
  88. void unregisterPaymentSuccessCallback(PaymentStatusCallback callback) async {
  89. await _lock.synchronized(() {
  90. callbackMap.removeWhere((key, value) => value == callback);
  91. });
  92. }
  93. }
  94. class PaymentStatusException implements Exception {
  95. final int status;
  96. PaymentStatusException(this.status);
  97. @override
  98. String toString() {
  99. return '支付状态异常: status = $status';
  100. }
  101. }
  102. abstract class PaymentStatusCallback {
  103. void onPaymentSuccess(
  104. String orderNo, PayItemBean paymentWay, GoodsBean storeItemBean);
  105. }