createRazorpayOrder method
Implementation
Future<String?> createRazorpayOrder(
double amount, String custEmail, String key, String secret) async {
try {
log('Generating order');
Dio dioClient = Dio();
String credentials = '$key:$secret';
String encodedCredentials = base64.encode(utf8.encode(credentials));
log('Encoded credentials: $encodedCredentials');
dioClient.options.headers['Authorization'] = 'Basic $encodedCredentials';
dioClient.options.headers['Content-Type'] = 'application/json';
Map<String, dynamic> data = {
"amount":
(amount * 100).toInt(),
"currency": "INR",
"receipt": "receipt#1",
"notes": {"key1": "value3", "key2": "value2"}
};
log('Request data: ${json.encode(data)}');
Response response = await dioClient
.post('https://api.razorpay.com/v1/orders', data: json.encode(data));
log('Response status: ${response.statusCode}');
log('Response data: ${response.data}');
if (response.statusCode == 200) {
var orderId = response.data['id'];
log('Order created successfully: $orderId');
return orderId;
} else {
log('Failed to create order: ${response.statusCode}');
return null;
}
} catch (e) {
log('Error creating order: $e');
return null;
}
}