createPaymentLink method
Future<PaymentLinkResponse>
createPaymentLink({
- required PaymentLinkRequest request,
- String? imagePath,
Create a payment link
Implementation
Future<PaymentLinkResponse> createPaymentLink({
required PaymentLinkRequest request,
String? imagePath,
}) async {
if (_authToken == null) {
throw const PaymentInitializationException('Authentication token not available. Please authenticate first.');
}
try {
// Prepare form data
final formData = request.toFormData();
// Add image file if provided
Map<String, dynamic>? files;
if (imagePath != null && imagePath.isNotEmpty) {
final file = File(imagePath);
if (await file.exists()) {
files = {
'payment_link_image': file,
};
}
}
log('Creating payment link with data: $formData');
final response = await _apiService.postMultipart<Map<String, dynamic>>(
ApiConstants.paymentLinks,
fields: formData,
files: files,
);
log('Payment link response: $response');
if (response.statusCode != null && response.statusCode! >= 200 && response.statusCode! < 300) {
return PaymentLinkResponse.fromJson(response.data!);
} else {
throw const PaymentLinkException('Failed to create payment link');
}
} catch (e) {
if (e is PaymobException) rethrow;
log('Error creating payment link: $e');
throw const PaymentLinkException('Failed to create payment link');
}
}