initializePayment method
Initialize payment with Flutterwave
Implementation
Future<Map<String, dynamic>> initializePayment({
required double amount,
required String email,
required String currency,
required String redirectUrl,
required String? phoneNumber,
required String customerName,
required String title,
required String description,
}) async {
const String baseUrl = "https://api.flutterwave.com/v3";
final String reference = generateReference();
Map<String, dynamic> data = {
"payment_options": "card,banktransfer",
"amount": amount,
"email": email,
"tx_ref": reference,
"currency": currency,
"redirect_url": redirectUrl,
"customer": {"email": email, "name": customerName},
"customizations": {"title": title, "description": description}
};
if (phoneNumber != null) {
data["customer"]["phone_number"] = phoneNumber;
}
final response = await http.post(
Uri.parse("$baseUrl/payments"),
headers: {
"Authorization": "Bearer $secretKey",
"Content-Type": "application/json",
},
body: jsonEncode(data),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception("Failed to initialize payment: ${response.body}");
}
}