cake_wallet/lib/ionia/ionia_service.dart

172 lines
6 KiB
Dart
Raw Normal View History

2022-06-20 19:27:28 +00:00
import 'package:cake_wallet/ionia/ionia_merchant.dart';
import 'package:cake_wallet/ionia/ionia_order.dart';
2022-05-30 13:34:21 +00:00
import 'package:cake_wallet/ionia/ionia_virtual_card.dart';
2022-06-20 19:27:28 +00:00
import 'package:flutter/foundation.dart';
2022-05-30 13:34:21 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/ionia/ionia_api.dart';
import 'package:cake_wallet/ionia/ionia_gift_card.dart';
2022-06-29 13:00:57 +00:00
import 'package:cake_wallet/ionia/ionia_category.dart';
2022-05-30 13:34:21 +00:00
class IoniaService {
IoniaService(this.secureStorage, this.ioniaApi);
static const ioniaEmailStorageKey = 'ionia_email';
2022-05-30 13:34:21 +00:00
static const ioniaUsernameStorageKey = 'ionia_username';
static const ioniaPasswordStorageKey = 'ionia_password';
static String get clientId => secrets.ioniaClientId;
final FlutterSecureStorage secureStorage;
final IoniaApi ioniaApi;
// Create user
Future<void> createUser(String email) async {
final username = await ioniaApi.createUser(email, clientId: clientId);
await secureStorage.write(key: ioniaEmailStorageKey, value: email);
2022-05-30 13:34:21 +00:00
await secureStorage.write(key: ioniaUsernameStorageKey, value: username);
}
// Verify email
Future<void> verifyEmail(String code) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
2022-07-27 12:16:42 +00:00
final email = await secureStorage.read(key: ioniaEmailStorageKey);
final credentials = await ioniaApi.verifyEmail(email: email, username: username, code: code, clientId: clientId);
2022-05-30 13:34:21 +00:00
await secureStorage.write(key: ioniaPasswordStorageKey, value: credentials.password);
2022-07-27 12:16:42 +00:00
await secureStorage.write(key: ioniaUsernameStorageKey, value: credentials.username);
}
// Sign In
Future<void> signIn(String email) async {
final username = await ioniaApi.signIn(email, clientId: clientId);
await secureStorage.write(key: ioniaEmailStorageKey, value: email);
await secureStorage.write(key: ioniaUsernameStorageKey, value: username);
2022-05-30 13:34:21 +00:00
}
Future<String> getUserEmail() async {
return secureStorage.read(key: ioniaEmailStorageKey);
}
2022-05-30 13:34:21 +00:00
// Check is user logined
Future<bool> isLogined() async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey) ?? '';
final password = await secureStorage.read(key: ioniaPasswordStorageKey) ?? '';
return username.isNotEmpty && password.isNotEmpty;
}
// Logout
Future<void> logout() async {
await secureStorage.delete(key: ioniaUsernameStorageKey);
await secureStorage.delete(key: ioniaPasswordStorageKey);
}
// Create virtual card
Future<IoniaVirtualCard> createCard() async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.createCard(username: username, password: password, clientId: clientId);
}
// Get virtual card
Future<IoniaVirtualCard> getCard() async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getCards(username: username, password: password, clientId: clientId);
}
2022-06-20 19:27:28 +00:00
// Get Merchants
Future<List<IoniaMerchant>> getMerchants() async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getMerchants(username: username, password: password, clientId: clientId);
}
// Get Merchants By Filter
Future<List<IoniaMerchant>> getMerchantsByFilter({
String search,
2022-06-29 13:00:57 +00:00
List<IoniaCategory> categories,
int merchantFilterType = 0}) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getMerchantsByFilter(
username: username,
password: password,
clientId: clientId,
search: search,
categories: categories,
merchantFilterType: merchantFilterType);
}
2022-06-20 19:27:28 +00:00
// Purchase Gift Card
Future<IoniaOrder> purchaseGiftCard({
@required String merchId,
@required double amount,
@required String currency}) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.purchaseGiftCard(
merchId: merchId,
amount: amount,
currency: currency,
username: username,
password: password,
clientId: clientId);
}
// Get Current User Gift Card Summaries
Future<List<IoniaGiftCard>> getCurrentUserGiftCardSummaries() async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getCurrentUserGiftCardSummaries(username: username, password: password, clientId: clientId);
}
2022-07-14 16:57:40 +00:00
// Charge Gift Card
Future<void> chargeGiftCard({
@required int giftCardId,
@required double amount}) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
await ioniaApi.chargeGiftCard(
username: username,
password: password,
clientId: clientId,
giftCardId: giftCardId,
amount: amount);
}
// Redeem
Future<void> redeem(IoniaGiftCard giftCard) async {
await chargeGiftCard(giftCardId: giftCard.id, amount: giftCard.remainingAmount);
}
// Get Gift Card
Future<IoniaGiftCard> getGiftCard({@required int id}) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getGiftCard(username: username, password: password, clientId: clientId,id: id);
}
2022-07-18 15:23:53 +00:00
// Payment Status
Future<int> getPaymentStatus({
@required String orderId,
@required String paymentId}) async {
final username = await secureStorage.read(key: ioniaUsernameStorageKey);
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getPaymentStatus(username: username, password: password, clientId: clientId, orderId: orderId, paymentId: paymentId);
}
2022-05-30 13:34:21 +00:00
}