authorizePayment method

Future<MastercardPaymentResponse> authorizePayment({
  1. required String orderId,
  2. required MastercardCardDetails cardDetails,
  3. required double amount,
  4. String? currency,
  5. String? authenticationId,
  6. Map<String, dynamic>? billingAddress,
  7. Map<String, dynamic>? customerInfo,
})

Authorize Payment (without capture)

Implementation

Future<MastercardPaymentResponse> authorizePayment({
  required String orderId,
  required MastercardCardDetails cardDetails,
  required double amount,
  String? currency,
  String? authenticationId,
  Map<String, dynamic>? billingAddress,
  Map<String, dynamic>? customerInfo,
}) async {
  try {
    final transactionId = _uuid.v4();

    final requestBody = {
      'apiOperation': 'AUTHORIZE',
      '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,
            },
          },
        },
      },
      'transaction': {
        'reference': transactionId,
      },
    };

    if (authenticationId != null) {
      requestBody['authentication'] = {
        'transactionId': authenticationId,
      };
    }

    if (billingAddress != null) {
      requestBody['billing'] = billingAddress;
    }

    if (customerInfo != null) {
      requestBody['customer'] = customerInfo;
    }

    final response = await _dio.put(
      '/order/$orderId/transaction/$transactionId',
      data: requestBody,
    );

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