2022-07-28 17:03:16 +00:00
|
|
|
import 'package:cake_wallet/ionia/ionia_merchant.dart';
|
|
|
|
import 'package:cake_wallet/ionia/ionia_order.dart';
|
|
|
|
import 'package:cake_wallet/ionia/ionia_virtual_card.dart';
|
|
|
|
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';
|
|
|
|
import 'package:cake_wallet/ionia/ionia_category.dart';
|
2022-08-31 16:37:43 +00:00
|
|
|
import 'package:platform_device_id/platform_device_id.dart';
|
2022-07-28 17:03:16 +00:00
|
|
|
|
|
|
|
class IoniaService {
|
|
|
|
IoniaService(this.secureStorage, this.ioniaApi);
|
|
|
|
|
|
|
|
static const ioniaEmailStorageKey = 'ionia_email';
|
|
|
|
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);
|
|
|
|
await secureStorage.write(key: ioniaUsernameStorageKey, value: username);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify email
|
|
|
|
|
|
|
|
Future<void> verifyEmail(String code) async {
|
2022-10-12 17:09:57 +00:00
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final email = (await secureStorage.read(key: ioniaEmailStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
final credentials = await ioniaApi.verifyEmail(email: email, username: username, code: code, clientId: clientId);
|
|
|
|
await secureStorage.write(key: ioniaPasswordStorageKey, value: credentials.password);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getUserEmail() async {
|
2022-10-12 17:09:57 +00:00
|
|
|
return (await secureStorage.read(key: ioniaEmailStorageKey))!;
|
2022-07-28 17:03:16 +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 {
|
2022-10-12 17:09:57 +00:00
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.createCard(username: username, password: password, clientId: clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get virtual card
|
|
|
|
|
|
|
|
Future<IoniaVirtualCard> getCard() async {
|
2022-10-12 17:09:57 +00:00
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.getCards(username: username, password: password, clientId: clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Merchants
|
|
|
|
|
|
|
|
Future<List<IoniaMerchant>> getMerchants() async {
|
2022-10-12 17:09:57 +00:00
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.getMerchants(username: username, password: password, clientId: clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Merchants By Filter
|
|
|
|
|
|
|
|
Future<List<IoniaMerchant>> getMerchantsByFilter({
|
2022-10-12 17:09:57 +00:00
|
|
|
String? search,
|
|
|
|
List<IoniaCategory>? categories,
|
2022-07-28 17:03:16 +00:00
|
|
|
int merchantFilterType = 0}) async {
|
2022-10-12 17:09:57 +00:00
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.getMerchantsByFilter(
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
clientId: clientId,
|
|
|
|
search: search,
|
|
|
|
categories: categories,
|
|
|
|
merchantFilterType: merchantFilterType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purchase Gift Card
|
|
|
|
|
|
|
|
Future<IoniaOrder> purchaseGiftCard({
|
2022-10-12 17:09:57 +00:00
|
|
|
required String merchId,
|
|
|
|
required double amount,
|
|
|
|
required String currency}) async {
|
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-08-31 16:37:43 +00:00
|
|
|
final deviceId = await PlatformDeviceId.getDeviceId;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.purchaseGiftCard(
|
2022-10-12 17:09:57 +00:00
|
|
|
requestedUUID: deviceId!,
|
2022-07-28 17:03:16 +00:00
|
|
|
merchId: merchId,
|
|
|
|
amount: amount,
|
|
|
|
currency: currency,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
clientId: clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Current User Gift Card Summaries
|
|
|
|
|
|
|
|
Future<List<IoniaGiftCard>> getCurrentUserGiftCardSummaries() async {
|
2022-10-12 17:09:57 +00:00
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.getCurrentUserGiftCardSummaries(username: username, password: password, clientId: clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Charge Gift Card
|
|
|
|
|
|
|
|
Future<void> chargeGiftCard({
|
2022-10-12 17:09:57 +00:00
|
|
|
required int giftCardId,
|
|
|
|
required double amount}) async {
|
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
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
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<IoniaGiftCard> getGiftCard({required int id}) async {
|
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.getGiftCard(username: username, password: password, clientId: clientId,id: id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payment Status
|
|
|
|
|
|
|
|
Future<int> getPaymentStatus({
|
2022-10-12 17:09:57 +00:00
|
|
|
required String orderId,
|
|
|
|
required String paymentId}) async {
|
|
|
|
final username = (await secureStorage.read(key: ioniaUsernameStorageKey))!;
|
|
|
|
final password = (await secureStorage.read(key: ioniaPasswordStorageKey))!;
|
2022-07-28 17:03:16 +00:00
|
|
|
return ioniaApi.getPaymentStatus(username: username, password: password, clientId: clientId, orderId: orderId, paymentId: paymentId);
|
|
|
|
}
|
|
|
|
}
|