store_controller.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. RxBool isUseFree = false.obs;
  26. @override
  27. Future<void> onInit() async {
  28. initStoreIndexData();
  29. isUseFree.value = await ClassifyPhoto().checkTrialEligibility();
  30. ApplePay().check();
  31. }
  32. void initStoreIndexData() {
  33. storeRepository.storeIndex().then((indexData) {
  34. storeItems.clear();
  35. storeItems.addAll(indexData.items);
  36. currentSelectedStoreItem.value =
  37. storeItems.isNotEmpty ? storeItems.first : null;
  38. paymentWays.clear();
  39. paymentWays.addAll(indexData.paymentWays);
  40. currentSelectedPaymentWay.value =
  41. paymentWays.isNotEmpty ? paymentWays.first : null;
  42. });
  43. }
  44. Future<void> onRestoreClick() async {
  45. PaymentWay? paymentWay = currentSelectedPaymentWay.value;
  46. if (paymentWay == null) {
  47. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  48. return;
  49. }
  50. int payPlatform = paymentWay.payPlatform;
  51. int payMethod = paymentWay.payMethod;
  52. LoadingDialog.show("");
  53. final result = await ApplePay().restore();
  54. if (result["success"] == true) {
  55. // LoadingDialog.hide();
  56. var receipt = result['receipt'];
  57. print('查找恢复记录成功: ${result['receipt']}');
  58. checkRestoreStatus(receipt);
  59. } else {
  60. LoadingDialog.hide();
  61. ToastUtil.show("Pay Error");
  62. print('恢复失败: ${result['error']}');
  63. }
  64. Future.delayed(const Duration(seconds: 20), () {
  65. LoadingDialog.hide();
  66. ToastUtil.show("Restore record not found");
  67. });
  68. // AgilePay.restore(success: (String? result) {
  69. // // LoadingDialog.hide();
  70. // }, payError: (int error, String? errorMessage) {
  71. // debugPrint('zk---payError: $error, $errorMessage');
  72. // // errorPayToast(error);
  73. // LoadingDialog.hide();
  74. // }, error: (int errno, String? error) {
  75. // debugPrint('zk---error: $errno, $error');
  76. // // errorPayToast(errno);
  77. // LoadingDialog.hide();
  78. // }, restore: (String? result) {
  79. // LoadingDialog.show("");
  80. // checkRestoreStatus(result);
  81. // Future.delayed(const Duration(seconds: 30), () {
  82. // LoadingDialog.hide();
  83. // });
  84. // });
  85. }
  86. void onBuyClick() async {
  87. StoreItem? storeItem = currentSelectedStoreItem.value;
  88. if (storeItem == null) {
  89. // ToastUtil.showToast(StringName.storeChoiceGoods.tr);
  90. return;
  91. }
  92. PaymentWay? paymentWay = currentSelectedPaymentWay.value;
  93. if (paymentWay == null) {
  94. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  95. return;
  96. }
  97. int payPlatform = paymentWay.payPlatform;
  98. int payMethod = paymentWay.payMethod;
  99. LoadingDialog.show("");
  100. try {
  101. OrderPayResponse response =
  102. await storeRepository.orderPay(storeItem.id, payPlatform, payMethod);
  103. dynamic payInfo;
  104. String outTradeNo = response.outTradeNo;
  105. if (payPlatform == PayPlatform.apple) {
  106. payInfo = ApplePayInfo(
  107. storeItem.appleGoodsId,
  108. storeItem.subscribable == 1
  109. ? ProductType.nonConsumable
  110. : ProductType.consumable,
  111. response.appAccountToken);
  112. }
  113. Future.delayed(const Duration(seconds: 30), () {
  114. LoadingDialog.hide();
  115. });
  116. final result = await ApplePay().purchase(productId: storeItem.appleGoodsId, appAccountToken: response.appAccountToken);
  117. if (result["success"] == true) {
  118. // LoadingDialog.hide();
  119. var receipt = result['receipt'];
  120. print('购买成功: ${result['receipt']}');
  121. checkPaymentStatus(outTradeNo, paymentWay, storeItem, receiptData: receipt);
  122. } else {
  123. LoadingDialog.hide();
  124. ToastUtil.show("Pay Error");
  125. print('购买失败: ${result['error']}');
  126. }
  127. // AgilePay.startPay(payInfo, success: (String? result) {
  128. // LoadingDialog.show("");
  129. // checkPaymentStatus(outTradeNo, paymentWay, storeItem,
  130. // receiptData: result);
  131. // Future.delayed(const Duration(seconds: 30), () {
  132. // LoadingDialog.hide();
  133. // });
  134. // }, payError: (int error, String? errorMessage) {
  135. // debugPrint('zk---payError: $error, $errorMessage');
  136. // // errorPayToast(error);
  137. // LoadingDialog.hide();
  138. // }, error: (int errno, String? error) {
  139. // debugPrint('zk---error: $errno, $error');
  140. // // errorPayToast(errno);
  141. // LoadingDialog.hide();
  142. // });
  143. } catch (error) {
  144. LoadingDialog.hide();
  145. // ToastUtil.showToast(StringName.storePayError.tr);
  146. }
  147. }
  148. // 检查恢复订阅结果
  149. Future<void> checkRestoreStatus(String? receiptData) async {
  150. PaymentWay? paymentWay = currentSelectedPaymentWay.value;
  151. if (paymentWay == null) {
  152. // ToastUtil.showToast(StringName.storeChoicePayment.tr);
  153. return;
  154. }
  155. if (receiptData == null) {
  156. return;
  157. }
  158. int payPlatform = paymentWay.payPlatform;
  159. int payMethod = paymentWay.payMethod;
  160. // var code = await storeRepository.resume(payPlatform, payMethod, receiptData);
  161. storeRepository.resume(payPlatform, payMethod, receiptData).then((data) {
  162. LoadingDialog.hide();
  163. ToastUtil.show("Restore success");
  164. userRepository.getUserInfo();
  165. Get.back();
  166. }).catchError((error) {
  167. LoadingDialog.hide();
  168. ToastUtil.show("Restore fail");
  169. });
  170. // if (code == 0) {
  171. // LoadingDialog.hide();
  172. // ToastUtil.show("Restore success");
  173. // userRepository.getUserInfo();
  174. // Get.back();
  175. // } else {
  176. // LoadingDialog.hide();
  177. // ToastUtil.show("Restore fail");
  178. // }
  179. }
  180. void checkPaymentStatus(
  181. String orderNo, PaymentWay paymentWay, StoreItem storeItemBean,
  182. {String? receiptData}) {
  183. paymentStatusManager.registerPaymentSuccessCallback(orderNo, this);
  184. paymentStatusManager.checkPaymentStatus(orderNo, paymentWay, storeItemBean,
  185. receiptData: receiptData);
  186. }
  187. @override
  188. void onPaymentSuccess(String orderNo, PaymentWay paymentWay, StoreItem storeItemBean) {
  189. // TODO: implement onPaymentSuccess
  190. LoadingDialog.hide();
  191. ToastUtil.show("Pay success");
  192. userRepository.getUserInfo();
  193. Get.back();
  194. }
  195. }