Skip to content

Commit

Permalink
Improved error handling in case callbacks fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-SD committed Oct 9, 2023
1 parent 70f0514 commit c26109d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
95 changes: 64 additions & 31 deletions lib/src/adyen_checkout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,33 +186,56 @@ class AdyenCheckout implements AdyenCheckoutInterface {
dropInAdvancedFlowCompleter.complete(event.paymentResult);
}

Future<void> _handleAdditionalDetails(
Future<void> _handlePaymentComponent(
PlatformCommunicationModel event,
Future<DropInOutcome> Function(String additionalDetails)
postPaymentsDetails,
Future<DropInOutcome> Function(String paymentComponentJson) postPayments,
) async {
if (event.data == null) {
throw Exception("Additional data is not provided.");
}
try {
if (event.data == null) {
throw Exception("Payment data is not provided.");
}

final DropInOutcome paymentsDetailsResult =
await postPaymentsDetails(event.data!);
DropInResultDTO dropInResult = _mapToDropInResult(paymentsDetailsResult);
AdyenCheckoutPlatformInterface.instance
.onPaymentsDetailsResult(dropInResult);
final DropInOutcome paymentsResult = await postPayments(event.data!);
DropInResultDTO dropInResult = _mapToDropInResult(paymentsResult);
AdyenCheckoutPlatformInterface.instance.onPaymentsResult(dropInResult);
} catch (error) {
_adyenLogger.print(error.toString());
AdyenCheckoutPlatformInterface.instance.onPaymentsResult(DropInResultDTO(
dropInResultType: DropInResultType.error,
error: DropInErrorDTO(
reason: "Failure executing postPayments, ${error.toString()}",
dismissDropIn: true,
),
));
}
}

Future<void> _handlePaymentComponent(
Future<void> _handleAdditionalDetails(
PlatformCommunicationModel event,
Future<DropInOutcome> Function(String paymentComponentJson) postPayments,
Future<DropInOutcome> Function(String additionalDetails)
postPaymentsDetails,
) async {
if (event.data == null) {
throw Exception("Payment data is not provided.");
}
try {
if (event.data == null) {
throw Exception("Additional data is not provided.");
}

final DropInOutcome paymentsResult = await postPayments(event.data!);
DropInResultDTO dropInResult = _mapToDropInResult(paymentsResult);
AdyenCheckoutPlatformInterface.instance.onPaymentsResult(dropInResult);
final DropInOutcome paymentsDetailsResult =
await postPaymentsDetails(event.data!);
DropInResultDTO dropInResult = _mapToDropInResult(paymentsDetailsResult);
AdyenCheckoutPlatformInterface.instance
.onPaymentsDetailsResult(dropInResult);
} catch (error) {
_adyenLogger.print(error.toString());
AdyenCheckoutPlatformInterface.instance
.onPaymentsDetailsResult(DropInResultDTO(
dropInResultType: DropInResultType.error,
error: DropInErrorDTO(
reason: "Failure executing postPaymentsDetails, ${error.toString()}",
dismissDropIn: true,
),
));
}
}

DropInResultDTO _mapToDropInResult(DropInOutcome dropInOutcome) {
Expand Down Expand Up @@ -242,18 +265,28 @@ class AdyenCheckout implements AdyenCheckoutInterface {
PlatformCommunicationModel event,
StoredPaymentMethodConfiguration? storedPaymentMethodConfiguration,
) async {
if (storedPaymentMethodConfiguration != null &&
storedPaymentMethodConfiguration.deleteStoredPaymentMethodCallback !=
null &&
event.data != null) {
final storedPaymentMethodId = event.data;
final result = await storedPaymentMethodConfiguration
.deleteStoredPaymentMethodCallback!(storedPaymentMethodId!);
AdyenCheckoutPlatformInterface.instance.onDeleteStoredPaymentMethodResult(
DeletedStoredPaymentMethodResultDTO(
storedPaymentMethodId: storedPaymentMethodId,
isSuccessfullyRemoved: result,
));
final String? storedPaymentMethodId = event.data;
final deletionCallback =
storedPaymentMethodConfiguration?.deleteStoredPaymentMethodCallback;

if (storedPaymentMethodId != null && deletionCallback != null) {
try {
final bool result = await deletionCallback(storedPaymentMethodId);
AdyenCheckoutPlatformInterface.instance
.onDeleteStoredPaymentMethodResult(
DeletedStoredPaymentMethodResultDTO(
storedPaymentMethodId: storedPaymentMethodId,
isSuccessfullyRemoved: result,
));
} catch (error) {
_adyenLogger.print(error.toString());
AdyenCheckoutPlatformInterface.instance
.onDeleteStoredPaymentMethodResult(
DeletedStoredPaymentMethodResultDTO(
storedPaymentMethodId: storedPaymentMethodId,
isSuccessfullyRemoved: false,
));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/logging/adyen_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AdyenLogger {

void print(String message) {
if (kDebugMode && _loggingEnabled) {
debugPrint(message);
debugPrint("AdyenCheckout: $message");
}
}
}

0 comments on commit c26109d

Please sign in to comment.