initiateAuthentication method

Future<MastercardAuthResponse> initiateAuthentication({
  1. required String orderId,
  2. required MastercardCardDetails cardDetails,
  3. required double amount,
  4. String? currency,
  5. Map<String, dynamic>? billingAddress,
  6. Map<String, dynamic>? shippingAddress,
})

Initiate 3DS Authentication

Implementation

Future<MastercardAuthResponse> initiateAuthentication({
  required String orderId,
  required MastercardCardDetails cardDetails,
  required double amount,
  String? currency,
  Map<String, dynamic>? billingAddress,
  Map<String, dynamic>? shippingAddress,
}) async {
  try {
    final authId = _uuid.v4();

    final requestBody = {
      'authentication': {
        'acceptVersions': '3DS1,3DS2',
        'channel': 'PAYER_BROWSER',
        'purpose': 'PAYMENT_TRANSACTION',
      },
      'correlationId': authId,
      'order': {
        'amount': amount.toString(),
        'currency': currency ?? config.currency,
        'id': orderId,
      },
      'sourceOfFunds': {
        'type': 'CARD',
        'provided': {
          'card': {
            'number': cardDetails.number,
            'securityCode': cardDetails.cvv,
            'expiry': {
              'month': cardDetails.expiryMonth,
              'year': cardDetails.expiryYear,
            },
          },
        },
      },
      'device': {
        'browser': await _getBrowserInfo(),
        'ipAddress': '127.0.0.1', // Should be actual IP
      },
      'merchant': {
        'name': config.merchantName,
        'url': config.merchantUrl,
      },
    };

    if (billingAddress != null) {
      (requestBody['order'] as Map<String, dynamic>)['billing'] = billingAddress;
    }

    if (shippingAddress != null) {
      (requestBody['order'] as Map<String, dynamic>)['shipping'] = shippingAddress;
    }

    final response = await _dio.post(
      '/authentication/$authId',
      data: requestBody,
    );

    return MastercardAuthResponse.fromJson(response.data);
  } on DioException catch (e) {
    throw MastercardException(
      'Authentication initiation failed: ${e.message}',
      statusCode: e.response?.statusCode,
      response: e.response?.data,
    );
  } catch (e) {
    throw MastercardException('Authentication initiation failed: $e');
  }
}