fix cakepay text encoding (#1902)

* fix text encoding * fix initial encoding

Serhii committed Dec 28, 2024 at 08:06 UTC 4cdee649d1802c2316cdac5e1e7c55baa7fdf8c4
3 files changed +8 -32
lib/cake_pay/cake_pay_api.dart
+2 -1
@@ -230,6 +230,7 @@ class CakePayApi {
230
231 var headers = {
232 'accept': 'application/json; charset=UTF-8',
233 + 'Content-Type': 'application/json; charset=UTF-8',
234 'Authorization': 'Api-Key $apiKey',
235 };
236
@@ -240,7 +241,7 @@ class CakePayApi {
241 'Failed to fetch vendors: statusCode - ${response.statusCode}, queryParams -$queryParams, response - ${response.body}');
242 }
243
243 - final bodyJson = json.decode(response.body);
244 + final bodyJson = json.decode(utf8.decode(response.bodyBytes));
245
246 if (bodyJson is List<dynamic> && bodyJson.isEmpty) {
247 return [];
lib/cake_pay/cake_pay_card.dart
+5 -22
@@ -1,5 +1,3 @@
1 -import 'dart:convert';
2 -
1 import 'package:cake_wallet/entities/fiat_currency.dart';
2
3 class CakePayCard {
@@ -38,17 +36,11 @@ class CakePayCard {
36 });
37
38 factory CakePayCard.fromJson(Map<String, dynamic> json) {
41 - final name = stripHtmlIfNeeded(json['name'] as String? ?? '');
42 - final decodedName = fixEncoding(name);
39
40 + final name = stripHtmlIfNeeded(json['name'] as String? ?? '');
41 final description = stripHtmlIfNeeded(json['description'] as String? ?? '');
45 - final decodedDescription = fixEncoding(description);
46 -
42 final termsAndConditions = stripHtmlIfNeeded(json['terms_and_conditions'] as String? ?? '');
48 - final decodedTermsAndConditions = fixEncoding(termsAndConditions);
49 -
43 final howToUse = stripHtmlIfNeeded(json['how_to_use'] as String? ?? '');
51 - final decodedHowToUse = fixEncoding(howToUse);
44
45 final fiatCurrency = FiatCurrency.deserialize(raw: json['currency_code'] as String? ?? '');
46
@@ -59,10 +51,10 @@ class CakePayCard {
51
52 return CakePayCard(
53 id: json['id'] as int? ?? 0,
62 - name: decodedName,
63 - description: decodedDescription,
64 - termsAndConditions: decodedTermsAndConditions,
65 - howToUse: decodedHowToUse,
54 + name: name,
55 + description: description,
56 + termsAndConditions: termsAndConditions,
57 + howToUse: howToUse,
58 expiryAndValidity: json['expiry_and_validity'] as String?,
59 cardImageUrl: json['card_image_url'] as String?,
60 country: json['country'] as String?,
@@ -79,13 +71,4 @@ class CakePayCard {
71 static String stripHtmlIfNeeded(String text) {
72 return text.replaceAll(RegExp(r'<[^>]*>|&[^;]+;'), ' ');
73 }
82 -
83 - static String fixEncoding(String text) {
84 - try {
85 - final bytes = latin1.encode(text);
86 - return utf8.decode(bytes, allowMalformed: true);
87 - } catch (_) {
88 - return text;
89 - }
90 - }
74 }
lib/cake_pay/cake_pay_vendor.dart
+1 -9
@@ -1,5 +1,3 @@
1 -import 'dart:convert';
2 -
1 import 'cake_pay_card.dart';
2
3 class CakePayVendor {
@@ -21,7 +19,6 @@ class CakePayVendor {
19
20 factory CakePayVendor.fromJson(Map<String, dynamic> json, String country) {
21 final name = stripHtmlIfNeeded(json['name'] as String);
24 - final decodedName = fixEncoding(name);
22
23 var cardsJson = json['cards'] as List?;
24 CakePayCard? cardForVendor;
@@ -36,7 +33,7 @@ class CakePayVendor {
33
34 return CakePayVendor(
35 id: json['id'] as int,
39 - name: decodedName,
36 + name: name,
37 unavailable: json['unavailable'] as bool? ?? false,
38 cakeWarnings: json['cake_warnings'] as String?,
39 country: country,
@@ -47,9 +44,4 @@ class CakePayVendor {
44 static String stripHtmlIfNeeded(String text) {
45 return text.replaceAll(RegExp(r'<[^>]*>|&[^;]+;'), ' ');
46 }
50 -
51 - static String fixEncoding(String text) {
52 - final bytes = latin1.encode(text);
53 - return utf8.decode(bytes, allowMalformed: true);
54 - }
47 }