run method
Future<String>
run({
- required BuildContext context,
- required bool isDev,
- required InitiateIvorypayTransaction data,
- required dynamic onError()?,
- required dynamic onSuccess()?,
- required dynamic onLoading()?,
Implementation
Future<String> run({
required BuildContext context,
required bool isDev,
required InitiateIvorypayTransaction data,
required Function(bool, Object)? onError,
required Function(VerifyTransactionRes)? onSuccess,
required Function(bool)? onLoading,
}) async {
final baseUrl = isDev ? Constants.devURL : Constants.prodURL;
final paymentLinkBaseUrl =
isDev ? Constants.paymentLinkDevURL : Constants.paymentLinkProdURL;
onLoading!(true);
try {
final response = await http.post(
Uri.parse(baseUrl),
body: json.encode({
"baseFiat": data.baseFiat,
"amount": data.amount,
"crypto": data.crypto,
"email": data.email,
"metadata": null
}),
headers: {
'Content-Type': ' application/json',
'Authorization': data.authorization ?? ''
},
);
if (response.statusCode == 201) {
final data = TransactionResponseDto.fromJson(
jsonDecode(response.body),
);
/// The below code is creating a string variable called [link[ that
/// concatenates a base payment link URL with a reference ID obtained from
/// [data.data[. The [?.[ operator is used to safely access the [reference[
/// property of [data.data[ without causing a null reference exception if
/// [data.data[ is null.
final link = "$paymentLinkBaseUrl/${data.data?.reference}";
await launchUrl(Uri.parse(link), mode: LaunchMode.inAppWebView);
ref = data.data?.reference ?? '';
return link;
} else {
onError!(true, response.body);
return response.body.toString();
}
} catch (e) {
onError!(true, e);
return e.toString();
} finally {
onLoading(false);
}
}