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 <omarh.ismail1@gmail.com>
This commit is contained in:
rafael xɱr 2024-12-27 00:01:41 -03:00 committed by GitHub
parent 542920a512
commit b1c9be637f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 12 deletions

View file

@ -204,8 +204,8 @@ class CakePayApi {
/// Get Vendors
Future<List<CakePayVendor>> getVendors({
required String apiKey,
required String country,
int? page,
String? country,
String? countryCode,
String? search,
List<String>? vendorIds,
@ -247,7 +247,7 @@ class CakePayApi {
}
return (bodyJson['results'] as List)
.map((e) => CakePayVendor.fromJson(e as Map<String, dynamic>))
.map((e) => CakePayVendor.fromJson(e as Map<String, dynamic>, country))
.toList();
}
}

View file

@ -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;
}
}
}

View file

@ -29,8 +29,8 @@ class CakePayService {
/// Get Vendors
Future<List<CakePayVendor>> getVendors({
required String country,
int? page,
String? country,
String? countryCode,
String? search,
List<String>? vendorIds,

View file

@ -7,7 +7,7 @@ class CakePayVendor {
final String name;
final bool unavailable;
final String? cakeWarnings;
final List<String> 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<String, dynamic> json) {
factory CakePayVendor.fromJson(Map<String, dynamic> 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<String, dynamic>);
try {
cardForVendor = CakePayCard.fromJson(cardsJson
.where((element) => element['country'] == country)
.first as Map<String, dynamic>);
} 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<String>.from(json['countries'] as List? ?? []),
card: firstCard,
country: country,
card: cardForVendor,
);
}