startCheckout method
First this create a Preference in mercadopago endpoint if something goes wrong it will throw a MpException else it will start the MobileCheckout screen using the SDK for android.
Implementation
Future<CheckoutResult> startCheckout(
Preference pref,
) async {
try {
if (Platform.isIOS) {
throw Exception(
'IOS platform is not supported by this package see https://github.com/gonzalogauto/mpcheckout/issues/17',
);
}
final url = Uri.parse('$_apiUrl/checkout/preferences');
final headers = {
'access_token': accesToken,
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': 'Bearer $accesToken',
};
final body = json.encode(pref.toJson());
final response = await http.post(url, headers: headers, body: body);
final responseBody = json.decode(response.body) as Map<String, dynamic>;
///Handle response with errors
if (response.statusCode != 200 && response.statusCode != 201) {
final message = response.reasonPhrase;
throw MpException.preference(
message: 'Ha ocurrido un error al crear la preferencia: $message',
);
}
final preferenceResponse = PreferenceResponse.fromJson(responseBody);
final result = await _channel.invokeMapMethod<String, dynamic>(
'startCheckout',
{
'publicKey': publicKey,
'preferenceId': preferenceResponse.id,
},
);
if (result == null) throw Exception('startCheckout result is null');
return CheckoutResult.fromJson(result);
} on Exception {
rethrow;
} catch (exception, stackTrace) {
throw MpException.unknown(
exception: exception,
stackTrace: stackTrace,
);
}
}