processPaymentWith3DS method

Future<LokotroPayOnResponse> processPaymentWith3DS({
  1. required MastercardPaymentRequest paymentRequest,
})

Process payment with 3DS authentication

Implementation

Future<LokotroPayOnResponse> processPaymentWith3DS({
  required MastercardPaymentRequest paymentRequest,
}) async {
  try {
    final orderId = paymentRequest.orderId;

    // Step 1: Initiate 3DS Authentication
    final authResponse = await _authService.initiateAuthentication(
      orderId: orderId,
      cardDetails: paymentRequest.cardDetails,
      amount: paymentRequest.amount,
      currency: paymentRequest.currency,
      billingAddress: paymentRequest.billingAddress?.toJson(),
    );

    // Step 2: Handle 3DS redirect if required
    String? authenticationId;
    if (authResponse.requiresRedirect) {
      // In a real app, you would show a WebView with the redirect URL
      // For now, we'll simulate successful authentication
      authenticationId = authResponse.authenticationId;

      // Wait for authentication completion (in real app, this would be callback-driven)
      await _waitForAuthenticationCompletion(authenticationId!);
    }

    // Step 3: Process payment with authentication
    final paymentResponse = await _paymentService.processPayment(
      orderId: orderId,
      cardDetails: paymentRequest.cardDetails,
      amount: paymentRequest.amount,
      currency: paymentRequest.currency,
      authenticationId: authenticationId,
      billingAddress: paymentRequest.billingAddress?.toJson(),
      customerInfo: paymentRequest.customer?.toJson(),
      description: paymentRequest.description,
    );

    // Step 4: Convert to Lokotro response format
    return _convertToLokotroResponse(paymentResponse);

  } on MastercardException catch (e) {
    return LokotroPayOnResponse(
      title: 'Payment Failed',
      message: e.errorMessage,
      paymentStatus: LokotroPaymentStatus.failed,
      apiResponseCode: LokotroPayApiResponseCode.lok001,
      amount: paymentRequest.amount,
      currency: paymentRequest.currency,
      systemRef: _uuid.v4(),
      customRef: paymentRequest.orderId,
      timestamp: DateTime.now(),
      transactionId: null,
    );
  } catch (e) {
    return LokotroPayOnResponse(
      title: 'System Error',
      message: 'Payment processing failed: $e',
      paymentStatus: LokotroPaymentStatus.failed,
      apiResponseCode: LokotroPayApiResponseCode.lok007,
      amount: paymentRequest.amount,
      currency: paymentRequest.currency,
      systemRef: _uuid.v4(),
      customRef: paymentRequest.orderId,
      timestamp: DateTime.now(),
      transactionId: null,
    );
  }
}