From b1c9be637f407d1f131d1ad269f6e0dbf01b59ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rafael=20x=C9=B1r?= Date: Fri, 27 Dec 2024 00:01:41 -0300 Subject: [PATCH] fix: wrong card for vendor country (#1905) * fix: wrong card for vendor country * Update lib/cake_pay/cake_pay_vendor.dart --------- Co-authored-by: Omar Hatem --- lib/cake_pay/cake_pay_api.dart | 4 ++-- lib/cake_pay/cake_pay_card.dart | 8 ++++++-- lib/cake_pay/cake_pay_service.dart | 2 +- lib/cake_pay/cake_pay_vendor.dart | 18 +++++++++++------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/cake_pay/cake_pay_api.dart b/lib/cake_pay/cake_pay_api.dart index f9aa2f0f1..68aba3f3e 100644 --- a/lib/cake_pay/cake_pay_api.dart +++ b/lib/cake_pay/cake_pay_api.dart @@ -204,8 +204,8 @@ class CakePayApi { /// Get Vendors Future> getVendors({ required String apiKey, + required String country, int? page, - String? country, String? countryCode, String? search, List? vendorIds, @@ -247,7 +247,7 @@ class CakePayApi { } return (bodyJson['results'] as List) - .map((e) => CakePayVendor.fromJson(e as Map)) + .map((e) => CakePayVendor.fromJson(e as Map, country)) .toList(); } } diff --git a/lib/cake_pay/cake_pay_card.dart b/lib/cake_pay/cake_pay_card.dart index 26fa0c50b..d3f07e409 100644 --- a/lib/cake_pay/cake_pay_card.dart +++ b/lib/cake_pay/cake_pay_card.dart @@ -81,7 +81,11 @@ class CakePayCard { } static String fixEncoding(String text) { - final bytes = latin1.encode(text); - return utf8.decode(bytes, allowMalformed: true); + try { + final bytes = latin1.encode(text); + return utf8.decode(bytes, allowMalformed: true); + } catch (_) { + return text; + } } } diff --git a/lib/cake_pay/cake_pay_service.dart b/lib/cake_pay/cake_pay_service.dart index 768588775..9ba65df9a 100644 --- a/lib/cake_pay/cake_pay_service.dart +++ b/lib/cake_pay/cake_pay_service.dart @@ -29,8 +29,8 @@ class CakePayService { /// Get Vendors Future> getVendors({ + required String country, int? page, - String? country, String? countryCode, String? search, List? vendorIds, diff --git a/lib/cake_pay/cake_pay_vendor.dart b/lib/cake_pay/cake_pay_vendor.dart index c947fa882..564896654 100644 --- a/lib/cake_pay/cake_pay_vendor.dart +++ b/lib/cake_pay/cake_pay_vendor.dart @@ -7,7 +7,7 @@ class CakePayVendor { final String name; final bool unavailable; final String? cakeWarnings; - final List countries; + final String country; final CakePayCard? card; CakePayVendor({ @@ -15,19 +15,23 @@ class CakePayVendor { required this.name, required this.unavailable, this.cakeWarnings, - required this.countries, + required this.country, this.card, }); - factory CakePayVendor.fromJson(Map json) { + factory CakePayVendor.fromJson(Map json, String country) { final name = stripHtmlIfNeeded(json['name'] as String); final decodedName = fixEncoding(name); var cardsJson = json['cards'] as List?; - CakePayCard? firstCard; + CakePayCard? cardForVendor; if (cardsJson != null && cardsJson.isNotEmpty) { - firstCard = CakePayCard.fromJson(cardsJson.first as Map); + try { + cardForVendor = CakePayCard.fromJson(cardsJson + .where((element) => element['country'] == country) + .first as Map); + } catch (_) {} } return CakePayVendor( @@ -35,8 +39,8 @@ class CakePayVendor { name: decodedName, unavailable: json['unavailable'] as bool? ?? false, cakeWarnings: json['cake_warnings'] as String?, - countries: List.from(json['countries'] as List? ?? []), - card: firstCard, + country: country, + card: cardForVendor, ); }