-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Expand file tree
/
Copy pathplan_data.dart
More file actions
215 lines (184 loc) · 6.21 KB
/
Copy pathplan_data.dart
File metadata and controls
215 lines (184 loc) · 6.21 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
import 'package:lantern/core/common/common.dart';
class PlansData {
Providers providers;
List<Plan> plans;
PlansData({required this.providers, required this.plans});
/// The payment methods offered on the current platform. Matches the
/// selection in ChoosePaymentMethod: only Android uses the android list;
/// iOS pays via IAP and falls back to the desktop list like everyone else.
List<Android> get platformProviders =>
PlatformUtils.isAndroid ? providers.android : providers.desktop;
/// Sorts plans (best-value first, then by descending price) and orders the
/// platform's payment providers so subscription-capable ones come first.
/// Applied after fetching/attaching plans (including referral V2).
void sortPlansAndProviders() {
plans.sort((a, b) {
if (a.bestValue != b.bestValue) {
return a.bestValue ? -1 : 1;
}
// Then: sort by usdPrice descending
return b.usdPrice.compareTo(a.usdPrice);
});
platformProviders.sort(
(a, b) =>
(b.providers.supportSubscription ? 1 : 0) -
(a.providers.supportSubscription ? 1 : 0),
);
}
/// Publishable key advertised by the Stripe provider for this platform,
/// or null when Stripe isn't offered or no key was sent.
String? get stripePubKey {
for (final method in platformProviders) {
if (method.providers.name == 'stripe') {
return method.providers.data.pubKey;
}
}
return null;
}
factory PlansData.fromJson(Map<String, dynamic> json) => PlansData(
providers: Providers.fromJson(json["providers"]),
plans: List<Plan>.from(json["plans"].map((x) => Plan.fromJson(x))),
);
PlansData copyWith({
Providers? providers,
List<Plan>? plans,
Map<String, List<String>>? icons,
}) {
return PlansData(
providers: providers ?? this.providers,
plans: plans ?? this.plans,
);
}
Map<String, dynamic> toJson() => {
"providers": providers.toJson(),
"plans": List<dynamic>.from(plans.map((x) => x.toJson())),
};
}
class Plan {
String id;
String description;
int usdPrice;
Map<String, dynamic> price;
Map<String, dynamic> expectedMonthlyPrice;
bool bestValue;
// Original (pre-discount) prices. Only present when a discount (affiliate
// code) is applied; null otherwise.
int? originalUsdPrice;
int? originalUsdPrice1Y;
int? originalUsdPrice2Y;
Map<String, dynamic>? originalPrice;
Map<String, dynamic>? originalExpectedMonthlyPrice;
Plan({
required this.id,
required this.description,
required this.usdPrice,
required this.price,
required this.expectedMonthlyPrice,
this.bestValue = false,
this.originalUsdPrice,
this.originalUsdPrice1Y,
this.originalUsdPrice2Y,
this.originalPrice,
this.originalExpectedMonthlyPrice,
});
factory Plan.fromJson(Map<String, dynamic> json) => Plan(
id: json["id"],
description: json["description"],
usdPrice: json["usdPrice"],
price: json["price"],
expectedMonthlyPrice: json["expectedMonthlyPrice"],
bestValue: json["bestValue"] ?? false,
originalUsdPrice: json["originalUsdPrice"],
originalUsdPrice1Y: json["originalUsdPrice1Y"],
originalUsdPrice2Y: json["originalUsdPrice2Y"],
originalPrice: json["originalPrice"] == null
? null
: Map<String, dynamic>.from(json["originalPrice"]),
originalExpectedMonthlyPrice: json["originalExpectedMonthlyPrice"] == null
? null
: Map<String, dynamic>.from(json["originalExpectedMonthlyPrice"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"description": description,
"usdPrice": usdPrice,
"price": price,
"expectedMonthlyPrice": expectedMonthlyPrice,
"bestValue": bestValue,
"originalUsdPrice": originalUsdPrice,
"originalUsdPrice1Y": originalUsdPrice1Y,
"originalUsdPrice2Y": originalUsdPrice2Y,
"originalPrice": originalPrice,
"originalExpectedMonthlyPrice": originalExpectedMonthlyPrice,
};
}
class Providers {
List<Android> android;
List<Android> desktop;
Providers({required this.android, required this.desktop});
factory Providers.fromJson(Map<String, dynamic> json) => Providers(
android: List<Android>.from(
json["android"].map((x) => Android.fromJson(x)),
),
desktop: List<Android>.from(
json["desktop"].map((x) => Android.fromJson(x)),
),
);
Map<String, dynamic> toJson() => {
"android": List<dynamic>.from(android.map((x) => x.toJson())),
"desktop": List<dynamic>.from(desktop.map((x) => x.toJson())),
};
}
class Android {
String method;
Provider providers;
Android({required this.method, required this.providers});
factory Android.fromJson(Map<String, dynamic> json) => Android(
method: json["method"],
providers: Provider.fromJson(json["provider"]),
);
Map<String, dynamic> toJson() => {
"method": method,
"provider": providers.toJson(),
};
}
class Provider {
String name;
ProviderData data;
List<String> icons;
bool supportSubscription;
Provider({
required this.name,
required this.icons,
required this.supportSubscription,
ProviderData? data,
}) : data = data ?? ProviderData();
factory Provider.fromJson(Map<String, dynamic> json) => Provider(
name: json["name"],
data: ProviderData.fromJson(json["data"]),
icons: List<String>.from(json["icons"].map((x) => x)),
supportSubscription: json["supportsSubscription"] ?? false,
);
Map<String, dynamic> toJson() => {
"name": name,
"data": data.toJson(),
"icons": List<dynamic>.from(icons.map((x) => x)),
"supportsSubscription": supportSubscription,
};
}
/// Provider-specific payload. Its keys vary by provider (Stripe sends
/// `pubKey`, shepherd sends nothing), so the raw map is kept alongside the
/// typed accessors for keys the app understands.
class ProviderData {
final Map<String, dynamic> raw;
ProviderData({this.raw = const {}});
/// Stripe publishable key for this provider, or null if absent/empty.
String? get pubKey {
final value = raw['pubKey'];
return value is String && value.isNotEmpty ? value : null;
}
factory ProviderData.fromJson(dynamic json) => ProviderData(
raw: json is Map ? Map<String, dynamic>.from(json) : const {},
);
Map<String, dynamic> toJson() => raw;
}