membership_offer_controller.dart
7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import 'dart:math';
import 'package:doublefeel_flutter/core/constants/membership.dart';
import 'package:doublefeel_flutter/core/logging/app_logger.dart';
import 'package:doublefeel_flutter/core/network/api/pay_api.dart';
import 'package:doublefeel_flutter/core/network/api/vip_api.dart';
import 'package:doublefeel_flutter/core/result/app_result.dart';
import 'package:doublefeel_flutter/core/services/thinking_data_service.dart';
import 'package:doublefeel_flutter/core/util/app_toast.dart';
import 'package:doublefeel_flutter/data/local/user_preferences_storage.dart';
import 'package:doublefeel_flutter/data/models/local/user_preferences.dart';
import 'package:doublefeel_flutter/data/models/pay/apple_pay_order_response.dart';
import 'package:doublefeel_flutter/data/models/pay/pay_models.dart';
import 'package:doublefeel_flutter/l10n/l10n_extensions.dart';
import 'package:doublefeel_flutter/pigeon/platform_api.g.dart';
import 'package:get/get.dart';
import '../../../routes/app_pages.dart';
class MembershipOfferController extends GetxController {
MembershipOfferController(this._vipApi, this._payApi);
final VipApi _vipApi;
final PayApi _payApi;
final UserPreferencesStorage _userPreferences =
Get.find<UserPreferencesStorage>();
final PlatformHostApi _platformHostApi = PlatformHostApi();
final isUnlocking = false.obs;
Future<void> unlock() async {
if (isUnlocking.value) return;
final productId = yearlyProduct.value?.id;
final appleProductId = yearlyProduct.value?.appleId;
if (productId == null || appleProductId == null || appleProductId.isEmpty) {
AppToast.show(l10n.purchaseProductInfoUnavailable);
return;
}
isUnlocking.value = true;
try {
final orderResult = await _payApi.createOrderByApple(productId);
if (orderResult is! AppSuccess<ApplePayOrderResponse>) {
if (isFromOnboard) {
Get.offAllNamed(AppRoutes.home, arguments: {'isFromOnboard': true});
}
return;
}
final orderUuid = orderResult.data.orderInfo?.orderUuid?.trim();
if (orderUuid == null || orderUuid.isEmpty) {
AppToast.show(l10n.purchaseOrderInfoUnavailable);
if (isFromOnboard) {
Get.offAllNamed(AppRoutes.home, arguments: {'isFromOnboard': true});
}
return;
}
final paymentResult = await _performApplePayment(
productId: appleProductId,
orderUuid: orderUuid,
);
await _handleApplePaymentResult(paymentResult);
} finally {
isUnlocking.value = false;
}
}
Future<void> _handleApplePaymentResult(
AppleProductPaymentResult? result,
) async {
if (result?.success == true) {
_trackSuccessPayOrder();
await _completeSuccessfulPurchase();
return;
}
if (result?.success != false) return;
AppToast.show(_applePaymentFailureMessage(result!));
if (isFromOnboard) {
Get.offAllNamed(AppRoutes.home, arguments: {'isFromOnboard': true});
}
}
void _trackSuccessPayOrder() {
final trackMap = <String, dynamic>{};
if (Get.arguments?['from'] == 'onboarding') {
trackMap['channel_type'] = '新手引导';
} else {
trackMap['channel_type'] = Get.arguments?['channel_type'] ?? '';
}
trackMap['product_type'] = yearlyProduct.value?.appleId;
trackMap['price'] = yearlyProduct.value?.price;
ta.track(
'success_doublefeel_pay_order',
properties: trackMap,
);
}
String _applePaymentFailureMessage(AppleProductPaymentResult result) {
final errorCode = result.errorCode;
if (errorCode != null) {
return _localizedApplePaymentErrorCode(errorCode);
}
final errorMessage = _nonEmpty(result.errorMessage);
if (errorMessage == null) return l10n.purchaseApplePaymentFailed;
return _localizedApplePaymentErrorMessage(errorMessage);
}
String _localizedApplePaymentErrorCode(int errorCode) {
return switch (errorCode) {
-1 => l10n.purchaseApplePaymentInvalidOrder,
-2 => l10n.purchaseApplePaymentProductNotFound,
-3 => l10n.purchaseApplePaymentCancelled,
-4 => l10n.purchaseApplePaymentVerificationFailed,
-5 => l10n.purchaseApplePaymentFailed,
_ => l10n.purchaseApplePaymentFailed,
};
}
String _localizedApplePaymentErrorMessage(String errorMessage) {
if (errorMessage == 'missingUUID') {
return l10n.purchaseApplePaymentInvalidOrder;
}
if (errorMessage == 'productNotFound') {
return l10n.purchaseApplePaymentProductNotFound;
}
if (errorMessage == 'userCancelled') {
return l10n.purchaseApplePaymentCancelled;
}
if (errorMessage == 'failedVerification') {
return l10n.purchaseApplePaymentVerificationFailed;
}
if (errorMessage == 'unknown') {
return l10n.purchaseApplePaymentFailed;
}
return errorMessage;
}
Future<void> _completeSuccessfulPurchase() async {
try {
await _refreshVipInfo();
} finally {
if (isFromOnboard) {
Get.offAllNamed(AppRoutes.home, arguments: {'isFromOnboard': true});
} else {
Get.offNamed(Routes.PREMIUM_ACTIVATED);
}
}
}
Future<void> _refreshVipInfo() async {
final result = await _vipApi.getVipInfo();
if (result case AppSuccess(data: final vipInfo)) {
await _userPreferences.updateVipInfo(
UserPreferencesVipInfo.fromVipInfo(vipInfo),
);
}
}
Future<AppleProductPaymentResult?> _performApplePayment({
required String productId,
required String orderUuid,
}) async {
try {
return await _platformHostApi.performApplePayment(productId, orderUuid);
} catch (_) {
return null;
}
}
@override
void onInit() {
super.onInit();
isFromOnboard = Get.arguments?['from'] == 'onboarding';
// getProductList();
_refreshVipInfo();
getProductList();
}
var isFromOnboard = false;
final yearlyProduct = Rxn<PayProduct>();
final yearlyProductAppleInfo = Rxn<AppleProductInfo>();
Future<void> getProductList() async {
final result = await _payApi.getProductList();
if (result is! AppSuccess<PayProductListResponse>) return;
final products = (result.data.productList ?? const <PayProduct>[])
.where((product) => _nonEmpty(product.appleId) != null)
.toList();
yearlyProduct.value = products
.firstWhereOrNull((p) => p.appleId == Membership.yearlyProductId);
if (yearlyProduct.value != null) {
final appleProductId = _nonEmpty(yearlyProduct.value!.appleId);
if (appleProductId == null) return;
yearlyProductAppleInfo.value = await _requestAppleProductInfo(
productId: appleProductId,
baseUnit: yearlyProduct.value!.content?.baseUnit() ?? 1);
}
}
Future<AppleProductInfo?> _requestAppleProductInfo({
required String productId,
required int baseUnit,
}) async {
try {
return await _platformHostApi.requestAppleProductInfo(
productId,
baseUnit,
);
} catch (_) {
return null;
}
}
String? _nonEmpty(String? value) {
final trimmed = value?.trim();
if (trimmed == null || trimmed.isEmpty) return null;
return trimmed;
}
void onBackPressed() {
if (isFromOnboard) {
Get.offAllNamed(AppRoutes.home, arguments: {'isFromOnboard': true});
} else {
Get.back();
}
}
@override
void onReady() {
super.onReady();
ta.track('enter_doublefeel_vip_discount_page');
}
}