getCheckoutUrl method
Get checkout URL for a payment transaction
This method initializes a transaction with Paystack and returns the checkout URL that can be opened in a webview for payment processing.
Implementation
@override
Future<String> getCheckoutUrl(PaymentRequest request) async {
if (_publicKey == null) {
throw PaystackError(
message: 'Paystack not initialized. Call initialize() first.',
);
}
request.validate();
try {
// Initialize transaction with Paystack API to get checkout URL
final url = Uri.parse('https://api.paystack.co/transaction/initialize');
final response = await http.post(
url,
headers: {
'Authorization': 'Bearer $_publicKey',
'Content-Type': 'application/json',
},
body: json.encode({
'amount': request.amount,
'email': request.email,
'currency': request.currency.name.toUpperCase(),
'reference': request.reference ?? _generateReference(),
'callback_url': request.callbackUrl,
'metadata': request.metadata,
}),
);
if (response.statusCode == 200) {
final data = json.decode(response.body);
if (data['status'] == true) {
return data['data']['authorization_url'] as String;
} else {
throw PaystackError.fromApiResponse(data);
}
} else {
throw PaystackError(
message: 'HTTP ${response.statusCode}: ${response.body}',
);
}
} catch (e) {
if (e is PaystackError) rethrow;
throw PaystackError(message: 'Failed to get checkout URL: $e');
}
}