refundPayment method
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');
}
}