CW-1139-Migrate-CakePay-mobile-to-use-the-new-backend-APIs (#2416)

* migrate to new backend API

* hide value_type filter

* Improve vendor card parsing

* Update country.dart

* Update lib/cake_pay/src/models/cake_pay_card.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* Update lib/cake_pay/src/models/cake_pay_card.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* address review comments

* fix min limit value

* Update lib/view_model/cake_pay/cake_pay_buy_card_view_model.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* refactor denomination handling and min value logic

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
Serhii
2025-08-13 09:07:30 +03:00
committed by GitHub
co-authored by Omar Hatem
parent 75b811f9de
commit 328422cfb5
13 changed files with 283 additions and 216 deletions
@@ -19,16 +19,21 @@ abstract class CakePayBuyCardViewModelBase with Store {
CakePayBuyCardViewModelBase(
{required this.vendor, required CakePayService cakePayService, required this.sendViewModel})
: _cakePayService = cakePayService, walletType = sendViewModel.walletType,
amount = vendor.card!.denominations.isNotEmpty
? double.parse(vendor.card!.denominations.first)
amount = vendor.card!.denominationItems.isNotEmpty
? vendor.card!.denominationItems.first.value
: 0,
quantity = 1,
min = double.parse(vendor.card!.minValue ?? '0'),
max = double.parse(vendor.card!.maxValue ?? '0'),
card = vendor.card! {
card = vendor.card!,
min = _toDouble(vendor.card!.minValue) ?? 0,
max = _toDouble(vendor.card!.maxValue) ?? 0 {
selectedPaymentMethod = availableMethods.isNotEmpty ? availableMethods.first : null;
}
static double? _toDouble(String? value) {
if (value == null || value.isEmpty) return null;
return double.tryParse(value.replaceAll(',', '.'));
}
final CakePayVendor vendor;
final SendViewModel sendViewModel;
final CakePayService _cakePayService;
@@ -44,10 +49,13 @@ abstract class CakePayBuyCardViewModelBase with Store {
bool confirmsNoVpn = false;
bool confirmsVoidedRefund = false;
bool confirmsTermsAgreed = false;
(String, int?) selectedDenomination = ('', null);
String simulatedResponse = '';
bool get isDenominationSelected => card.denominations.isNotEmpty;
bool get isDenominationSelected =>
card.denominationItems.isNotEmpty &&
card.denominationItems.any((item) => item.value == amount);
Future<bool> get isUserLogged async => await _cakePayService.isLogged();
@@ -80,7 +88,10 @@ abstract class CakePayBuyCardViewModelBase with Store {
double get totalAmount => amount * quantity;
@computed
bool get isSimulating => isSimulatingFlow && FeatureFlag.hasDevOptions;
bool get isSimulating =>
isSimulatingFlow &&
FeatureFlag.hasDevOptions &&
FeatureFlag.isCakePayPurchaseSimulationEnabled;
@computed
List<CakePayPaymentMethod> get availableMethods {
@@ -116,7 +127,6 @@ abstract class CakePayBuyCardViewModelBase with Store {
CryptoPaymentData? getPaymentDataFor(CakePayPaymentMethod? method) {
if (order == null || method == null) return null;
final data = switch (method) {
CakePayPaymentMethod.BTC => order?.paymentData.btc,
CakePayPaymentMethod.XMR => order?.paymentData.xmr,
@@ -149,8 +159,8 @@ abstract class CakePayBuyCardViewModelBase with Store {
}
try {
order = await _cakePayService.createOrder(
cardId: card.id,
price: amount.toString(),
cardId: isDenominationSelected ? selectedDenomination.$2 ?? card.id : card.id,
price: isDenominationSelected ? selectedDenomination.$1 : amount.toString(),
quantity: quantity,
confirmsNoVpn: confirmsNoVpn,
confirmsVoidedRefund: confirmsVoidedRefund,
@@ -192,7 +202,6 @@ abstract class CakePayBuyCardViewModelBase with Store {
try {
simulatedResponse = await _cakePayService.simulatePayment(orderId: order!.orderId);
sendViewModel.state = TransactionCommitted();
} catch (e) {
sendViewModel.state = FailureState(
sendViewModel.translateErrorMessage(e, walletType, sendViewModel.wallet.currency));
@@ -231,8 +240,7 @@ abstract class CakePayBuyCardViewModelBase with Store {
final hours = duration.inHours;
final minutes = duration.inMinutes.remainder(60);
final seconds = duration.inSeconds.remainder(60);
return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds
.toString().padLeft(2, '0')}';
return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
}
void disposeExpirationTimer() {
@@ -22,7 +22,7 @@ abstract class CakePayCardsListViewModelBase with Store {
required this.settingsStore,
}) : cakePayVendors = [],
userCards = [],
availableCountries = [],
availableCountries = ObservableList<Country>.of(Country.allForCakePay),
page = 1,
displayPrepaidCards = true,
displayGiftCards = true,
@@ -33,8 +33,7 @@ abstract class CakePayCardsListViewModelBase with Store {
createCardState = CakePayCreateCardState(),
userCardState = UserCakePayCardsStateInitial(),
searchString = '',
searchMyCardsString = '',
CakePayVendorList = <CakePayVendor>[] {
searchMyCardsString = '' {
checkAuth();
initialization();
}
@@ -47,7 +46,6 @@ abstract class CakePayCardsListViewModelBase with Store {
}
void initialization() async {
await getCountries();
getVendors();
getUserCards();
}
@@ -55,8 +53,6 @@ abstract class CakePayCardsListViewModelBase with Store {
final CakePayService cakePayService;
final SettingsStore settingsStore;
List<CakePayVendor> CakePayVendorList;
Map<String, List<FilterItem>> get createFilterItems => {
'Card Type': [
FilterItem(
@@ -68,16 +64,17 @@ abstract class CakePayCardsListViewModelBase with Store {
caption: S.current.gift_cards,
onChanged: toggleGiftCards),
],
S.current.value_type: [
FilterItem(
value: () => displayDenominationsCards,
caption: S.current.denominations,
onChanged: toggleDenominationsCards),
FilterItem(
value: () => displayCustomValueCards,
caption: S.current.custom_value,
onChanged: toggleCustomValueCards),
],
// Uncomment if will be added to backend
// S.current.value_type: [
// FilterItem(
// value: () => displayDenominationsCards,
// caption: S.current.denominations,
// onChanged: toggleDenominationsCards),
// FilterItem(
// value: () => displayCustomValueCards,
// caption: S.current.custom_value,
// onChanged: toggleCustomValueCards),
// ],
};
String searchString;
@@ -165,14 +162,6 @@ abstract class CakePayCardsListViewModelBase with Store {
displayCustomValueCards != _initialDisplayCustomValueCards;
}
Future<void> getCountries() async {
try {
availableCountries = await cakePayService.getCountries();
} catch (e) {
printV(e);
}
}
Future<void> getUserCards() async {
//Dummy user cards // TODO: fetch from API
userCardState = UserCakePayCardsStateFetching();
@@ -195,9 +184,7 @@ abstract class CakePayCardsListViewModelBase with Store {
userCardState = UserCakePayCardsStateNoCards();
}
} catch (e) {
userCardState = UserCakePayCardsStateFailure(
error: e.toString(),
);
userCardState = UserCakePayCardsStateFailure(error: e.toString());
}
}
@@ -220,15 +207,17 @@ abstract class CakePayCardsListViewModelBase with Store {
try {
searchString = text ?? '';
var newVendors = await cakePayService.getVendors(
country: Country.getCakePayName(selectedCountry),
countryCode: selectedCountry.countryCode,
page: currentPage ?? page,
search: searchString,
giftCards: displayGiftCards,
prepaidCards: displayPrepaidCards,
custom: displayCustomValueCards,
onDemand: displayDenominationsCards);
prepaidCards: displayPrepaidCards);
cakePayVendors = CakePayVendorList = newVendors;
cakePayVendors = newVendors.where((vendor) {
if (vendor.card == null) return false;
if (vendor.available != true) return false;
return true;
}).toList();
} catch (e) {
printV(e);
}
@@ -245,15 +234,17 @@ abstract class CakePayCardsListViewModelBase with Store {
page++;
try {
var newVendors = await cakePayService.getVendors(
country: Country.getCakePayName(selectedCountry),
countryCode: selectedCountry.countryCode,
page: page,
search: searchString,
giftCards: displayGiftCards,
prepaidCards: displayPrepaidCards,
custom: displayCustomValueCards,
onDemand: displayDenominationsCards);
prepaidCards: displayPrepaidCards);
cakePayVendors.addAll(newVendors);
cakePayVendors.addAll(newVendors.where((vendor) {
if (vendor.card == null) return false;
if (vendor.available != true) return false;
return true;
}).toList());
} catch (error) {
if (error.toString().contains('detail":"Invalid page."')) {
hasMoreDataToFetch = false;