Add redeem for ionia gift cards

This commit is contained in:
M 2022-07-14 17:57:40 +01:00
parent 2a0206c9cd
commit abb6ff4933
8 changed files with 204 additions and 24 deletions

View file

@ -145,6 +145,7 @@ import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/src/screens/dashboard/widgets/address_page.dart';
import 'package:cake_wallet/ionia/ionia_token_service.dart';
import 'package:cake_wallet/anypay/anypay_api.dart';
import 'package:cake_wallet/view_model/ionia/ionia_gift_card_details_view_model.dart';
final getIt = GetIt.instance;
@ -713,8 +714,14 @@ Future setup(
return IoniaBuyGiftCardDetailPage(getIt.get<IoniaMerchPurchaseViewModel>(param1: amount, param2: merchant));
});
getIt.registerFactoryParam<IoniaGiftCardDetailsViewModel, IoniaGiftCard, void>((IoniaGiftCard giftCard, _) {
return IoniaGiftCardDetailsViewModel(
ioniaService: getIt.get<IoniaService>(),
giftCard: giftCard);
});
getIt.registerFactoryParam<IoniaGiftCardDetailPage, IoniaGiftCard, void>((IoniaGiftCard giftCard, _) {
return IoniaGiftCardDetailPage(giftCard);
return IoniaGiftCardDetailPage(getIt.get<IoniaGiftCardDetailsViewModel>(param1: giftCard));
});
getIt.registerFactoryParam<IoniaCustomTipPage, List, void>((List args, _) {

View file

@ -19,6 +19,8 @@ class IoniaApi {
static final getMerchantsByFilterUrl = Uri.https(baseUri, '/$pathPrefix/GetMerchantsByFilter');
static final getPurchaseMerchantsUrl = Uri.https(baseUri, '/$pathPrefix/PurchaseGiftCard');
static final getCurrentUserGiftCardSummariesUrl = Uri.https(baseUri, '/$pathPrefix/GetCurrentUserGiftCardSummaries');
static final changeGiftCardUrl = Uri.https(baseUri, '/$pathPrefix/ChargeGiftCard');
static final getGiftCardUrl = Uri.https(baseUri, '/$pathPrefix/GetGiftCard');
// Create user
@ -274,4 +276,83 @@ class IoniaApi {
return IoniaGiftCard.fromJsonMap(element);
}).toList();
}
// Charge Gift Card
Future<void> chargeGiftCard({
@required String username,
@required String password,
@required String clientId,
@required int giftCardId,
@required double amount}) async {
final headers = <String, String>{
'clientId': clientId,
'username': username,
'password': password,
'Content-Type': 'application/json'};
final body = <String, dynamic>{
'Id': giftCardId,
'Amount': amount};
final response = await post(
changeGiftCardUrl,
headers: headers,
body: json.encode(body));
if (response.statusCode != 200) {
throw Exception('Failed to update Gift Card with ID ${giftCardId};Incorrect response status: ${response.statusCode};');
}
final decodedBody = json.decode(response.body) as Map<String, dynamic>;
final isSuccessful = decodedBody['Successful'] as bool ?? false;
if (!isSuccessful) {
final data = decodedBody['Data'] as Map<String, dynamic>;
final msg = data['Message'] as String ?? '';
if (msg.isNotEmpty) {
throw Exception(msg);
}
throw Exception('Failed to update Gift Card with ID ${giftCardId};');
}
}
// Get Gift Card
Future<IoniaGiftCard> getGiftCard({
@required String username,
@required String password,
@required String clientId,
@required int id}) async {
final headers = <String, String>{
'clientId': clientId,
'username': username,
'password': password,
'Content-Type': 'application/json'};
final body = <String, dynamic>{'Id': id};
final response = await post(
getGiftCardUrl,
headers: headers,
body: json.encode(body));
if (response.statusCode != 200) {
throw Exception('Failed to get Gift Card with ID ${id};Incorrect response status: ${response.statusCode};');
}
final decodedBody = json.decode(response.body) as Map<String, dynamic>;
final isSuccessful = decodedBody['Successful'] as bool ?? false;
if (!isSuccessful) {
final msg = decodedBody['ErrorMessage'] as String ?? '';
if (msg.isNotEmpty) {
throw Exception(msg);
}
throw Exception('Failed to get Gift Card with ID ${id};');
}
final data = decodedBody['Data'] as Map<String, dynamic>;
return IoniaGiftCard.fromJsonMap(data);
}
}

View file

@ -33,6 +33,7 @@ class IoniaGiftCard {
systemName: element['SystemName'] as String,
barcodeUrl: element['BarcodeUrl'] as String,
cardNumber: element['CardNumber'] as String,
cardPin: element['CardPin'] as String,
tip: element['Tip'] as double,
purchaseAmount: element['PurchaseAmount'] as double,
actualAmount: element['ActualAmount'] as double,

View file

@ -120,4 +120,33 @@ class IoniaService {
final password = await secureStorage.read(key: ioniaPasswordStorageKey);
return ioniaApi.getCurrentUserGiftCardSummaries(username: username, password: password, clientId: clientId);
}
// 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);
}
}

View file

@ -1,20 +1,25 @@
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/ionia/ionia_gift_card.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/screens/ionia/widgets/ionia_tile.dart';
import 'package:cake_wallet/src/screens/ionia/widgets/text_icon_button.dart';
import 'package:cake_wallet/src/widgets/alert_background.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/src/widgets/primary_button.dart';
import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
import 'package:cake_wallet/typography.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/view_model/ionia/ionia_gift_card_details_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
class IoniaGiftCardDetailPage extends BasePage {
IoniaGiftCardDetailPage(this.merchant);
IoniaGiftCardDetailPage(this.viewModel);
final IoniaGiftCard merchant;
final IoniaGiftCardDetailsViewModel viewModel;
@override
Widget leading(BuildContext context) {
@ -48,56 +53,78 @@ class IoniaGiftCardDetailPage extends BasePage {
@override
Widget middle(BuildContext context) {
return Text(
merchant.legalName,
viewModel.giftCard.legalName,
style: textLargeSemiBold(color: Theme.of(context).accentTextTheme.display4.backgroundColor),
);
}
@override
Widget body(BuildContext context) {
reaction((_) => viewModel.redeemState, (ExecutionState state) {
if (state is FailureState) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: S.of(context).error,
alertContent: state.error,
buttonText: S.of(context).ok,
buttonAction: () => Navigator.of(context).pop());
});
});
}
});
return ScrollableWithBottomSection(
contentPadding: EdgeInsets.all(24),
content: Column(
children: [
if (merchant.barcodeUrl != null && merchant.barcodeUrl.isNotEmpty)
if (viewModel.giftCard.barcodeUrl != null && viewModel.giftCard.barcodeUrl.isNotEmpty)
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 24.0,
vertical: 24,
),
child: SizedBox(height: 96, width: double.infinity, child: Image.network(merchant.barcodeUrl)),
child: SizedBox(height: 96, width: double.infinity, child: Image.network(viewModel.giftCard.barcodeUrl)),
),
SizedBox(height: 24),
IoniaTile(
title: S.of(context).gift_card_number,
subTitle: merchant.cardNumber,
subTitle: viewModel.giftCard.cardNumber,
),
Divider(height: 30),
IoniaTile(
title: S.of(context).pin_number,
subTitle: merchant.cardPin ?? '',
subTitle: viewModel.giftCard.cardPin ?? '',
),
Divider(height: 30),
IoniaTile(
title: S.of(context).amount,
subTitle: merchant.remainingAmount.toString() ?? '0',
),
Observer(builder: (_) =>
IoniaTile(
title: S.of(context).amount,
subTitle: viewModel.giftCard.remainingAmount.toString() ?? '0',
)),
Divider(height: 50),
TextIconButton(
label: S.of(context).how_to_use_card,
onTap: () => _showHowToUseCard(context, merchant),
onTap: () => _showHowToUseCard(context, viewModel.giftCard),
),
],
),
bottomSection: Padding(
padding: EdgeInsets.only(bottom: 12),
child: LoadingPrimaryButton(
isLoading: false,
onPressed: () {},
text: S.of(context).mark_as_redeemed,
color: Theme.of(context).accentTextTheme.body2.color,
textColor: Colors.white,
)),
child: Observer(builder: (_) {
if (!viewModel.giftCard.isEmpty) {
return LoadingPrimaryButton(
isLoading: viewModel.redeemState is IsExecutingState,
onPressed: () => viewModel.redeem(),
text: S.of(context).mark_as_redeemed,
color: Theme.of(context).accentTextTheme.body2.color,
textColor: Colors.white);
}
return Container();
})),
);
}

View file

@ -24,13 +24,13 @@ abstract class IoniaAccountViewModelBase with Store {
List<IoniaGiftCard> merchs;
@computed
int get countOfMerch => merchs.where((merch) => merch.isActive).length;
int get countOfMerch => merchs.where((merch) => !merch.isEmpty).length;
@computed
List<IoniaGiftCard> get activeMechs => merchs.where((merch) => merch.isActive).toList();
List<IoniaGiftCard> get activeMechs => merchs.where((merch) => !merch.isEmpty).toList();
@computed
List<IoniaGiftCard> get redeemedMerchs => merchs.where((merch) => !merch.isActive).toList();
List<IoniaGiftCard> get redeemedMerchs => merchs.where((merch) => merch.isEmpty).toList();
@action
void logout() {

View file

@ -0,0 +1,35 @@
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/ionia/ionia_service.dart';
import 'package:cake_wallet/ionia/ionia_gift_card.dart';
import 'package:mobx/mobx.dart';
part 'ionia_gift_card_details_view_model.g.dart';
class IoniaGiftCardDetailsViewModel = IoniaGiftCardDetailsViewModelBase with _$IoniaGiftCardDetailsViewModel;
abstract class IoniaGiftCardDetailsViewModelBase with Store {
IoniaGiftCardDetailsViewModelBase({this.ioniaService, this.giftCard}) {
redeemState = InitialExecutionState();
}
final IoniaService ioniaService;
@observable
IoniaGiftCard giftCard;
@observable
ExecutionState redeemState;
@action
Future<void> redeem() async {
try {
redeemState = InitialExecutionState();
await ioniaService.redeem(giftCard);
giftCard = await ioniaService.getGiftCard(id: giftCard.id);
redeemState = ExecutedSuccessfullyState();
} catch(e) {
redeemState = FailureState(e.toString());
}
}
}

View file

@ -620,5 +620,5 @@
"awaiting_payment_confirmation": "Awaiting payment confirmation",
"transaction_sent_notice": "If the screen doesnt proceed after 1 minute, check a block explorer and your email.",
"agree": "Agree",
"in_store": "In Store",
"in_store": "In Store"
}