processPayment method

Future<MastercardPaymentResponse> processPayment({
  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>? shippingAddress,
  8. Map<String, dynamic>? customerInfo,
  9. String? description,
})

Process Direct Payment

Implementation

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

    final requestBody = {
      'apiOperation': 'PAY',
      'order': {
        'amount': amount.toString(),
        'currency': currency ?? config.currency,
        'id': orderId,
        'description': description ?? 'Payment via Lokotro Pay',
      },
      'sourceOfFunds': {
        'type': 'CARD',
        'provided': {
          'card': {
            'number': cardDetails.number,
            'securityCode': cardDetails.cvv,
            'expiry': {
              'month': cardDetails.expiryMonth,
              'year': cardDetails.expiryYear,
            },
          },
        },
      },
      'transaction': {
        'reference': transactionId,
      },
    };

    // Add authentication if provided (3DS)
    if (authenticationId != null) {
      requestBody['authentication'] = {
        'transactionId': authenticationId,
      };
    }

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

    // Add shipping address
    if (shippingAddress != null) {
      requestBody['shipping'] = shippingAddress;
    }

    // Add customer information
    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(
      'Payment processing failed: ${e.message}',
      statusCode: e.response?.statusCode,
      response: e.response?.data,
    );
  } catch (e) {
    throw MastercardException('Payment processing failed: $e');
  }
}