createPaymentIntention method

Future<PaymentIntentionResponse> createPaymentIntention({
  1. required PaymentIntentionRequest request,
  2. required String secretKey,
})

Create Payment Intention for Unified Checkout

Implementation

Future<PaymentIntentionResponse> createPaymentIntention({
  required PaymentIntentionRequest request,
  required String secretKey,
}) async {
  try {
    log('Creating payment intention with data: ${request.toJson()}');

    final response = await _apiService.post<Map<String, dynamic>>(
      ApiConstants.paymentIntention,
      data: request.toJson(),
      options: Options(
        headers: {
          'Authorization': 'Token $secretKey',
          'Content-Type': 'application/json',
        },
      ),
    );

    log('Payment intention response: $response');

    if (response.statusCode != null && response.statusCode! >= 200 && response.statusCode! < 300) {
      return PaymentIntentionResponse.fromJson(response.data!);
    } else {
      throw const PaymentIntentionException('Failed to create payment intention');
    }
  } catch (e) {
    if (e is PaymobException) rethrow;
    log('Error creating payment intention: $e');
    throw const PaymentIntentionException('Failed to create payment intention');
  }
}