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>

rafael xɱr committed Dec 27, 2024 at 00:01 UTC b1c9be637f407d1f131d1ad269f6e0dbf01b59ca
4 files changed +20 -12
lib/cake_pay/cake_pay_api.dart
+2 -2
@@ -204,8 +204,8 @@ class CakePayApi {
204 /// Get Vendors
205 Future<List<CakePayVendor>> getVendors({
206 required String apiKey,
207 + required String country,
208 int? page,
208 - String? country,
209 String? countryCode,
210 String? search,
211 List<String>? vendorIds,
@@ -247,7 +247,7 @@ class CakePayApi {
247 }
248
249 return (bodyJson['results'] as List)
250 - .map((e) => CakePayVendor.fromJson(e as Map<String, dynamic>))
250 + .map((e) => CakePayVendor.fromJson(e as Map<String, dynamic>, country))
251 .toList();
252 }
253 }
lib/cake_pay/cake_pay_card.dart
+6 -2
@@ -81,7 +81,11 @@ class CakePayCard {
81 }
82
83 static String fixEncoding(String text) {
84 - final bytes = latin1.encode(text);
85 - return utf8.decode(bytes, allowMalformed: true);
84 + try {
85 + final bytes = latin1.encode(text);
86 + return utf8.decode(bytes, allowMalformed: true);
87 + } catch (_) {
88 + return text;
89 + }
90 }
91 }
lib/cake_pay/cake_pay_service.dart
+1 -1
@@ -29,8 +29,8 @@ class CakePayService {
29
30 /// Get Vendors
31 Future<List<CakePayVendor>> getVendors({
32 + required String country,
33 int? page,
33 - String? country,
34 String? countryCode,
35 String? search,
36 List<String>? vendorIds,
lib/cake_pay/cake_pay_vendor.dart
+11 -7
@@ -7,7 +7,7 @@ class CakePayVendor {
7 final String name;
8 final bool unavailable;
9 final String? cakeWarnings;
10 - final List<String> countries;
10 + final String country;
11 final CakePayCard? card;
12
13 CakePayVendor({
@@ -15,19 +15,23 @@ class CakePayVendor {
15 required this.name,
16 required this.unavailable,
17 this.cakeWarnings,
18 - required this.countries,
18 + required this.country,
19 this.card,
20 });
21
22 - factory CakePayVendor.fromJson(Map<String, dynamic> json) {
22 + factory CakePayVendor.fromJson(Map<String, dynamic> json, String country) {
23 final name = stripHtmlIfNeeded(json['name'] as String);
24 final decodedName = fixEncoding(name);
25
26 var cardsJson = json['cards'] as List?;
27 - CakePayCard? firstCard;
27 + CakePayCard? cardForVendor;
28
29 if (cardsJson != null && cardsJson.isNotEmpty) {
30 - firstCard = CakePayCard.fromJson(cardsJson.first as Map<String, dynamic>);
30 + try {
31 + cardForVendor = CakePayCard.fromJson(cardsJson
32 + .where((element) => element['country'] == country)
33 + .first as Map<String, dynamic>);
34 + } catch (_) {}
35 }
36
37 return CakePayVendor(
@@ -35,8 +39,8 @@ class CakePayVendor {
39 name: decodedName,
40 unavailable: json['unavailable'] as bool? ?? false,
41 cakeWarnings: json['cake_warnings'] as String?,
38 - countries: List<String>.from(json['countries'] as List? ?? []),
39 - card: firstCard,
42 + country: country,
43 + card: cardForVendor,
44 );
45 }
46