|
@@ -1,3 +1,6 @@
|
|
|
+import 'dart:io';
|
|
|
+
|
|
|
+import 'package:dio/dio.dart';
|
|
|
import 'package:logging/logging.dart';
|
|
|
import 'package:photos/core/configuration.dart';
|
|
|
import 'package:photos/core/network.dart';
|
|
@@ -13,6 +16,7 @@ class BillingService {
|
|
|
|
|
|
final _logger = Logger("BillingService");
|
|
|
final _dio = Network.instance.getDio();
|
|
|
+ final _config = Configuration.instance;
|
|
|
|
|
|
SharedPreferences _prefs;
|
|
|
Future<List<BillingPlan>> _future;
|
|
@@ -24,7 +28,7 @@ class BillingService {
|
|
|
Future<List<BillingPlan>> getBillingPlans() {
|
|
|
if (_future == null) {
|
|
|
_future = _dio
|
|
|
- .get(Configuration.instance.getHttpEndpoint() + "/billing/plans")
|
|
|
+ .get(_config.getHttpEndpoint() + "/billing/plans")
|
|
|
.then((response) {
|
|
|
final plans = List<BillingPlan>();
|
|
|
for (final plan in response.data["plans"]) {
|
|
@@ -36,6 +40,30 @@ class BillingService {
|
|
|
return _future;
|
|
|
}
|
|
|
|
|
|
+ Future<Subscription> loadSubscription(String verificationData) async {
|
|
|
+ return _dio
|
|
|
+ .get(
|
|
|
+ _config.getHttpEndpoint() + "/billing/subscription",
|
|
|
+ queryParameters: {
|
|
|
+ "client": Platform.isAndroid ? "android" : "ios",
|
|
|
+ "verificationData": verificationData,
|
|
|
+ },
|
|
|
+ options: Options(
|
|
|
+ headers: {
|
|
|
+ "X-Auth-Token": _config.getToken(),
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ .then((response) {
|
|
|
+ if (response == null || response.statusCode != 200) {
|
|
|
+ throw Exception(response);
|
|
|
+ }
|
|
|
+ final subscription = Subscription.fromMap(response.data["subscription"]);
|
|
|
+ setSubscription(subscription);
|
|
|
+ return subscription;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
// TODO: Fetch new subscription once the current one has expired?
|
|
|
Subscription getSubscription() {
|
|
|
final jsonValue = _prefs.getString(subscriptionKey);
|