chargeCard static method
Implementation
static Future<APIResponse> chargeCard({
required PaymentInfo paymentInfo,
}) async {
APIResponse apiResponse;
//Preparing request payload
final Map<String, dynamic> formDataMap = {
"email": paymentInfo.email,
"amount": paymentInfo.amount,
"currency": paymentInfo.currency,
"reference": paymentInfo.reference,
"metadata": paymentInfo.metadata,
"card": {
"number": paymentInfo.paymentCard!.number,
"cvv": paymentInfo.paymentCard!.cvv,
"expiry_month": paymentInfo.paymentCard!.month,
"expiry_year": paymentInfo.paymentCard!.year
},
};
try {
var response = await dio.post(
APIs.chargeUrl,
data: formDataMap,
options: Options(
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ${paymentInfo.secretKey}',
},
),
);
if (response.statusCode == 200) {
//format the response data for system to continue operating
apiResponse = APIResponse.fromObject(response.data);
} else {
var errorMessage = "Request Failed. Please try again later";
try {
//alert the user with the message from the server
errorMessage =
response.data["data"]["message"] ?? response.data["message"];
} catch (error) {
if (kDebugMode) {
print("Error Data Getting Failed:: $error");
}
}
throw errorMessage;
}
} catch (error) {
DioError dioError = error as DioError;
var errorMessage = "Request Failed. Please try again later";
try {
//alert the user with the message from the server
// final jsonObject = convert.jsonDecode(error.message);
if (dioError.response?.data["data"] != null) {
errorMessage = dioError.response?.data["data"]["message"];
} else {
errorMessage = dioError.response?.data["message"];
}
} catch (error) {
if (kDebugMode) {
print("Error Data Getting Failed:: $error");
}
}
throw errorMessage;
}
return apiResponse;
}