refundPayment method

Future<MastercardPaymentResponse> refundPayment({
  1. required String orderId,
  2. required String originalTransactionId,
  3. required double amount,
  4. String? currency,
  5. String? reason,
})

Refund payment

Implementation

Future<MastercardPaymentResponse> refundPayment({
  required String orderId,
  required String originalTransactionId,
  required double amount,
  String? currency,
  String? reason,
}) async {
  try {
    final transactionId = _uuid.v4();

    final requestBody = {
      'apiOperation': 'REFUND',
      'transaction': {
        'amount': amount.toString(),
        'currency': currency ?? config.currency,
        'reference': transactionId,
      },
    };

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

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

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