payment_status_manager.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import 'dart:ffi';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/data/bean/goods_info.dart';
  5. import 'package:keyboard/data/bean/pay_way_info.dart';
  6. import 'package:keyboard/data/consts/constants.dart';
  7. import 'package:keyboard/data/repository/store_repository.dart';
  8. import 'package:keyboard/utils/method_chanel_ios_util.dart';
  9. import 'package:synchronized/synchronized.dart';
  10. import '../handler/event_handler.dart';
  11. import 'async_util.dart';
  12. class PaymentStatus {
  13. PaymentStatus._();
  14. //订单状态
  15. //0-查询失败,继续轮询
  16. //1-未支付,继续轮询
  17. //2-支付成功
  18. //3-支付关闭
  19. //4-已退款
  20. static const int payStatusFail = 0;
  21. static const int payStatusUnpaid = 1;
  22. static const int payStatusSuccess = 2;
  23. static const int payStatusClose = 3;
  24. static const int payStatusRefund = 4;
  25. }
  26. @lazySingleton
  27. class PaymentStatusManager {
  28. final StoreRepository storeRepository;
  29. PaymentStatusManager(this.storeRepository);
  30. final Map<String, PaymentStatusCallback> callbackMap = {};
  31. final Map<String, CancelableFuture> pollingSubscriptionMap = {};
  32. final _lock = Lock();
  33. void _startCheckPolling(
  34. String orderNo, PayWayInfo payWayInfo, GoodsInfo goodsInfo,
  35. {String? receiptData}) async {
  36. await _lock.synchronized(() async {
  37. pollingSubscriptionMap[orderNo]?.cancel();
  38. debugPrint('开始轮询支付状态: orderNo = $orderNo');
  39. CancelableFuture<int> orderFuture = AsyncUtil.retry(
  40. () => _queryOrderStatus(orderNo, receiptData: receiptData),
  41. Duration(seconds: 2), predicate: (error) {
  42. if (error is PaymentStatusException) {
  43. return error.status == PaymentStatus.payStatusFail ||
  44. error.status == PaymentStatus.payStatusUnpaid;
  45. }
  46. return true;
  47. }, maxRetry: 50);
  48. orderFuture.then((status) async {
  49. debugPrint('支付成功: orderNo = $orderNo');
  50. await _lock.synchronized(() {
  51. callbackMap[orderNo]
  52. ?.onPaymentSuccess(orderNo, payWayInfo, goodsInfo);
  53. callbackMap.remove(orderNo);
  54. });
  55. reportPaySuccess(goodsInfo.realAmount ?? 0, orderNo, goodsInfo.name,
  56. payWayInfo.payMethod);
  57. }).catchError((error) async {
  58. await _lock.synchronized(() {
  59. callbackMap[orderNo]
  60. ?.onPaymentError(error);
  61. callbackMap.remove(orderNo);
  62. });
  63. debugPrint('支付失败: orderNo = $orderNo, error = $error');
  64. });
  65. pollingSubscriptionMap[orderNo] = orderFuture;
  66. });
  67. }
  68. Future<int> _queryOrderStatus(String orderNo, {String? receiptData}) {
  69. return storeRepository
  70. .orderStatus(orderNo, receiptData: receiptData)
  71. .then((status) async {
  72. if (status == PaymentStatus.payStatusSuccess) {
  73. return status;
  74. }
  75. throw PaymentStatusException(status);
  76. });
  77. }
  78. void reportPaySuccess(
  79. int price, String orderId, String itemName, int paymentWay) {
  80. EventHandler.reportPay(price, orderId, itemName, paymentWay);
  81. if (isFirstPaySuccess()) {
  82. double newPrice = price.toDouble() / 100;
  83. MethodChanelIOSUtil.addASAPayReport(newPrice);
  84. setFirstPaySuccess(false);
  85. }
  86. }
  87. void checkPaymentStatus(
  88. String orderNo, PayWayInfo payWayInfo, GoodsInfo goodsInfo,
  89. {String? receiptData}) {
  90. final way = payWayInfo.copyWith();
  91. final goods = goodsInfo.copyWith();
  92. // recordKeyInfoToDisk(orderNo, paymentWay, storeItemBean);
  93. _startCheckPolling(orderNo, way, goods, receiptData: receiptData);
  94. }
  95. void registerPaymentSuccessCallback(
  96. String orderNo, PaymentStatusCallback callback) async {
  97. await _lock.synchronized(() {
  98. callbackMap[orderNo] = callback;
  99. });
  100. }
  101. void unregisterPaymentSuccessCallback(PaymentStatusCallback callback) async {
  102. await _lock.synchronized(() {
  103. callbackMap.removeWhere((key, value) => value == callback);
  104. });
  105. }
  106. void removePollingSubscription(String orderNo) {
  107. pollingSubscriptionMap[orderNo]?.cancel();
  108. pollingSubscriptionMap.remove(orderNo);
  109. callbackMap.remove(orderNo);
  110. }
  111. }
  112. class PaymentStatusException implements Exception {
  113. final int status;
  114. PaymentStatusException(this.status);
  115. @override
  116. String toString() {
  117. return '支付状态异常: status = $status';
  118. }
  119. }
  120. abstract class PaymentStatusCallback {
  121. void onPaymentSuccess(
  122. String orderNo, PayWayInfo payWayInfo, GoodsInfo goodsInfo);
  123. void onPaymentError(Error error);
  124. }