store_controller.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import 'dart:io';
  2. import 'package:apple_pay/apple_pay.dart';
  3. import 'package:classify_photo/classify_photo.dart';
  4. import 'package:clean/data/repositories/user_repository.dart';
  5. import 'package:clean/module/store/payment_status_manager.dart';
  6. import 'package:flutter/Material.dart';
  7. import 'package:get/get.dart';
  8. import 'package:in_app_purchase/in_app_purchase.dart';
  9. import '../../base/base_controller.dart';
  10. import '../../data/api/response/order_pay_response.dart';
  11. import '../../data/bean/payment_way.dart';
  12. import '../../data/bean/store_item.dart';
  13. import '../../data/consts/constants.dart';
  14. import '../../data/repositories/store_repository.dart';
  15. import '../../dialog/loading_dialog.dart';
  16. import '../../sdk/pay/agile_pay.dart';
  17. import '../../sdk/pay/applepay/apple_pay_info.dart';
  18. import '../../sdk/pay/assist/product_type.dart';
  19. import '../../utils/toast_util.dart';
  20. class StoreController extends BaseController implements PaymentStatusCallback {
  21. final RxList<StoreItem> storeItems = <StoreItem>[].obs;
  22. final RxList<PaymentWay> paymentWays = <PaymentWay>[].obs;
  23. final Rxn<StoreItem> currentSelectedStoreItem = Rxn<StoreItem>();
  24. final Rxn<PaymentWay> currentSelectedPaymentWay = Rxn<PaymentWay>();
  25. var freeAppleId = "";
  26. RxBool isFree = false.obs;
  27. @override
  28. Future<void> onInit() async {
  29. initStoreIndexData();
  30. }
  31. void initStoreIndexData() {
  32. storeRepository.storeIndex().then((indexData) async {
  33. storeItems.clear();
  34. storeItems.addAll(indexData.items);
  35. currentSelectedStoreItem.value =
  36. storeItems.isNotEmpty ? storeItems.first : null;
  37. paymentWays.clear();
  38. paymentWays.addAll(indexData.paymentWays);
  39. currentSelectedPaymentWay.value =
  40. paymentWays.isNotEmpty ? paymentWays.first : null;
  41. var freeAppleId = "";
  42. for (var item in storeItems) {
  43. if (item.freeTrialName != null) {
  44. freeAppleId = item.appleGoodsId;
  45. }
  46. }
  47. isFree.value = await ApplePay().check(freeAppleId);
  48. });
  49. }
  50. Future<void> onRestoreClick() async {
  51. PaymentWay? paymentWay = currentSelectedPaymentWay.value;
  52. if (paymentWay == null) {
  53. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  54. return;
  55. }
  56. int payPlatform = paymentWay.payPlatform;
  57. int payMethod = paymentWay.payMethod;
  58. LoadingDialog.show("");
  59. final result = await ApplePay().restore();
  60. if (result["success"] == true) {
  61. // LoadingDialog.hide();
  62. var receipt = result['receipt'];
  63. print('查找恢复记录成功: ${result['receipt']}');
  64. checkRestoreStatus(receipt);
  65. } else {
  66. LoadingDialog.hide();
  67. ToastUtil.show("Pay Error");
  68. print('恢复失败: ${result['error']}');
  69. }
  70. Future.delayed(const Duration(seconds: 20), () {
  71. LoadingDialog.hide();
  72. ToastUtil.show("Restore record not found");
  73. });
  74. // AgilePay.restore(success: (String? result) {
  75. // // LoadingDialog.hide();
  76. // }, payError: (int error, String? errorMessage) {
  77. // debugPrint('zk---payError: $error, $errorMessage');
  78. // // errorPayToast(error);
  79. // LoadingDialog.hide();
  80. // }, error: (int errno, String? error) {
  81. // debugPrint('zk---error: $errno, $error');
  82. // // errorPayToast(errno);
  83. // LoadingDialog.hide();
  84. // }, restore: (String? result) {
  85. // LoadingDialog.show("");
  86. // checkRestoreStatus(result);
  87. // Future.delayed(const Duration(seconds: 30), () {
  88. // LoadingDialog.hide();
  89. // });
  90. // });
  91. }
  92. void onBuyClick() async {
  93. StoreItem? storeItem = currentSelectedStoreItem.value;
  94. if (storeItem == null) {
  95. // ToastUtil.showToast(StringName.storeChoiceGoods.tr);
  96. return;
  97. }
  98. PaymentWay? paymentWay = currentSelectedPaymentWay.value;
  99. if (paymentWay == null) {
  100. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  101. return;
  102. }
  103. int payPlatform = paymentWay.payPlatform;
  104. int payMethod = paymentWay.payMethod;
  105. LoadingDialog.show("");
  106. try {
  107. OrderPayResponse response =
  108. await storeRepository.orderPay(storeItem.id, payPlatform, payMethod);
  109. dynamic payInfo;
  110. String outTradeNo = response.outTradeNo;
  111. if (payPlatform == PayPlatform.apple) {
  112. payInfo = ApplePayInfo(
  113. storeItem.appleGoodsId,
  114. storeItem.subscribable == 1
  115. ? ProductType.nonConsumable
  116. : ProductType.consumable,
  117. response.appAccountToken);
  118. }
  119. Future.delayed(const Duration(seconds: 30), () {
  120. LoadingDialog.hide();
  121. });
  122. final result = await ApplePay().purchase(productId: storeItem.appleGoodsId, appAccountToken: response.appAccountToken);
  123. if (result["success"] == true) {
  124. // LoadingDialog.hide();
  125. var receipt = result['receipt'];
  126. print('购买成功: ${result['receipt']}');
  127. checkPaymentStatus(outTradeNo, paymentWay, storeItem, receiptData: receipt);
  128. } else {
  129. LoadingDialog.hide();
  130. ToastUtil.show("Pay Error");
  131. print('购买失败: ${result['error']}');
  132. }
  133. // AgilePay.startPay(payInfo, success: (String? result) {
  134. // LoadingDialog.show("");
  135. // checkPaymentStatus(outTradeNo, paymentWay, storeItem,
  136. // receiptData: result);
  137. // Future.delayed(const Duration(seconds: 30), () {
  138. // LoadingDialog.hide();
  139. // });
  140. // }, payError: (int error, String? errorMessage) {
  141. // debugPrint('zk---payError: $error, $errorMessage');
  142. // // errorPayToast(error);
  143. // LoadingDialog.hide();
  144. // }, error: (int errno, String? error) {
  145. // debugPrint('zk---error: $errno, $error');
  146. // // errorPayToast(errno);
  147. // LoadingDialog.hide();
  148. // });
  149. } catch (error) {
  150. LoadingDialog.hide();
  151. // ToastUtil.showToast(StringName.storePayError.tr);
  152. }
  153. }
  154. // 检查恢复订阅结果
  155. Future<void> checkRestoreStatus(String? receiptData) async {
  156. PaymentWay? paymentWay = currentSelectedPaymentWay.value;
  157. if (paymentWay == null) {
  158. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  159. return;
  160. }
  161. if (receiptData == null) {
  162. return;
  163. }
  164. int payPlatform = paymentWay.payPlatform;
  165. int payMethod = paymentWay.payMethod;
  166. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  167. storeRepository.resume(payPlatform, payMethod, receiptData).then((data) {
  168. LoadingDialog.hide();
  169. ToastUtil.show("Restore success");
  170. userRepository.getUserInfo();
  171. Get.back();
  172. }).catchError((error) {
  173. LoadingDialog.hide();
  174. ToastUtil.show("Restore fail");
  175. });
  176. // if (code == 0) {
  177. // LoadingDialog.hide();
  178. // ToastUtil.show("Restore success");
  179. // userRepository.getUserInfo();
  180. // Get.back();
  181. // } else {
  182. // LoadingDialog.hide();
  183. // ToastUtil.show("Restore fail");
  184. // }
  185. }
  186. void checkPaymentStatus(
  187. String orderNo, PaymentWay paymentWay, StoreItem storeItemBean,
  188. {String? receiptData}) {
  189. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  190. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, storeItemBean,
  191. receiptData: receiptData);
  192. }
  193. @override
  194. void onPaymentSuccess(String orderNo, PaymentWay paymentWay, StoreItem storeItemBean) {
  195. // TODO: implement onPaymentSuccess
  196. LoadingDialog.hide();
  197. ToastUtil.show("Pay success");
  198. userRepository.getUserInfo();
  199. Get.back();
  200. }
  201. }