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/foundation.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'; import 'package:platform_device_id/platform_device_id.dart'; 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 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 verifyEmail(String code) async { final username = await secureStorage.read(key: ioniaUsernameStorageKey); final email = await secureStorage.read(key: ioniaEmailStorageKey); 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 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 getUserEmail() async { return secureStorage.read(key: ioniaEmailStorageKey); } // Check is user logined Future isLogined() async { final username = await secureStorage.read(key: ioniaUsernameStorageKey) ?? ''; final password = await secureStorage.read(key: ioniaPasswordStorageKey) ?? ''; return username.isNotEmpty && password.isNotEmpty; } // Logout Future logout() async { await secureStorage.delete(key: ioniaUsernameStorageKey); await secureStorage.delete(key: ioniaPasswordStorageKey); } // Create virtual card Future 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 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); } // Get Merchants Future> 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> getMerchantsByFilter({ String search, List 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); } // Purchase Gift Card Future 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); final deviceId = await PlatformDeviceId.getDeviceId; return ioniaApi.purchaseGiftCard( requestedUUID: deviceId, merchId: merchId, amount: amount, currency: currency, username: username, password: password, clientId: clientId); } // Get Current User Gift Card Summaries Future> 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); } // Charge Gift Card Future 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 redeem(IoniaGiftCard giftCard) async { await chargeGiftCard(giftCardId: giftCard.id, amount: giftCard.remainingAmount); } // Get Gift Card Future 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); } // Payment Status Future 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); } }