printData method
Implementation
@override
Future<List<Map>> printData({required IfoodPagoPrintPayload payload}) async {
try {
final prefs = await SharedPreferences.getInstance();
String accessToken = prefs.getString(IfoodPagoKeysStorage.TOKEN) ?? '';
String createAt = prefs.getString(IfoodPagoKeysStorage.TOKEN_CREATED_AT) ?? '';
DateTime? createAtDate = DateTime.tryParse(createAt);
DateTime now = DateTime.now();
Duration? difference;
if (createAtDate != null) {
difference = now.difference(createAtDate);
}
if (difference != null && difference.inHours > 23) {
await prefs.remove(IfoodPagoKeysStorage.TOKEN);
await prefs.remove(IfoodPagoKeysStorage.TOKEN_CREATED_AT);
accessToken = '';
createAt = '';
}
if (accessToken.isEmpty && createAt.isEmpty) {
final response = await methodChannel.invokeMethod<Map>('requestPrintToken', {'integrationApp': payload.integrationApp});
if (response is! Map) {
throw IfoodPagoPrintException(message: 'invalid response');
}
if (response['code'] != IfoodPagoStatusDeeplink.SUCCESS.name) {
throw IfoodPagoPrintException(message: response['message']);
}
if (response['data'] is! Map) {
throw IfoodPagoPrintException(message: 'invalid response data');
}
final authResponse = IfoodPagoAuthResponse.fromJson(Map<String, dynamic>.from(response['data']));
accessToken = authResponse.hash;
createAt = authResponse.createAt;
prefs.setString(IfoodPagoKeysStorage.TOKEN, accessToken);
prefs.setString(IfoodPagoKeysStorage.TOKEN_CREATED_AT, createAt);
}
final listaImageBase64 = await methodChannel.invokeMethod<Map>('generateImageBase64', {
'printable_content': payload.toJson()['printable_content'],
'groupAll': payload.groupAll,
});
if (listaImageBase64 is! Map) {
throw IfoodPagoPrintException(message: 'invalid listaImageBase64');
}
if (listaImageBase64['code'] != IfoodPagoStatusDeeplink.SUCCESS.name) {
throw IfoodPagoPrintException(message: listaImageBase64['message']);
}
if (listaImageBase64['data'] is! List) {
throw IfoodPagoPrintException(message: 'invalid response data');
}
List<Map> imageBase64List = [];
for (var imageBase64 in listaImageBase64['data']) {
log(imageBase64.toString());
final response = await http.post(
Uri.parse("https://movilepay-api.ifood.com.br/ifoodpay/mobile/api/v1/print/file"),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({"authorizationHash": accessToken, "contentBase64": imageBase64['imageBase64']}),
);
if (response.statusCode == 200) {
imageBase64List.add({'imageBase64': imageBase64['imageBase64']});
} else {
imageBase64List.add({'messageError': 'Failed to print: ${response.reasonPhrase}', 'imageBase64': imageBase64['imageBase64']});
}
}
return imageBase64List;
} on IfoodPagoPrintException catch (e) {
throw IfoodPagoPrintException(message: e.message);
} on PlatformException catch (e) {
throw IfoodPagoPrintException(message: e.message ?? 'PlatformException');
} catch (e) {
throw IfoodPagoPrintException(message: "Print Error: $e");
}
}