From 27cd50f3df413ec6f056e4200394e61450d05e99 Mon Sep 17 00:00:00 2001 From: Robert Schulze Dieckhoff Date: Tue, 10 Oct 2023 15:30:30 +0200 Subject: [PATCH 1/2] Improved payment result class --- example/lib/main.dart | 21 +++++++++++----- lib/src/adyen_checkout.dart | 40 +++++++++++++++++++++++------- lib/src/models/payment_result.dart | 40 ++++++++++++++++-------------- lib/src/utils/dto_mapper.dart | 34 ++++++++++++------------- 4 files changed, 85 insertions(+), 50 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index e9efbdc3..5efc4726 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -186,20 +186,29 @@ class _MyAppState extends State { ); } - _dialogBuilder(BuildContext context, PaymentResult paymentResult) { + String title = ""; String message = ""; - if (paymentResult.result != null) { - message = "Result code: ${paymentResult.result?.resultCode}"; - } else { - message = "Error: ${paymentResult.reason}"; + switch (paymentResult) { + case PaymentAdvancedFlowFinished(): + title = "Finished"; + message = "Result code: ${paymentResult.resultCode}"; + case PaymentSessionFinished(): + title = "Finished"; + message = "Result code: ${paymentResult.resultCode}"; + case PaymentCancelledByUser(): + title = "Cancelled by user"; + message = "Drop-in cancelled manually by user"; + case PaymentError(): + title = "Error occurred"; + message = "${paymentResult.reason}"; } return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( - title: Text(paymentResult.type.name), + title: Text(title), content: Text(message), actions: [ TextButton( diff --git a/lib/src/adyen_checkout.dart b/lib/src/adyen_checkout.dart index 251564d3..a3e8ea4c 100644 --- a/lib/src/adyen_checkout.dart +++ b/lib/src/adyen_checkout.dart @@ -97,13 +97,26 @@ class AdyenCheckout implements AdyenCheckoutInterface { } }); - return dropInSessionCompleter.future.then((value) { + return dropInSessionCompleter.future.then((paymentResultDTO) { AdyenCheckoutPlatformInterface.instance.cleanUpDropIn(); _resultApi.dropInSessionPlatformCommunicationStream.close(); - _adyenLogger.print("Drop-in session result type: ${value.type.name}"); _adyenLogger - .print("Drop-in session result code: ${value.result?.resultCode}"); - return value.fromDTO(); + .print("Drop-in session result type: ${paymentResultDTO.type.name}"); + _adyenLogger.print( + "Drop-in session result code: ${paymentResultDTO.result?.resultCode}"); + switch (paymentResultDTO.type) { + case PaymentResultEnum.cancelledByUser: + return PaymentCancelledByUser(); + case PaymentResultEnum.error: + return PaymentError(reason: paymentResultDTO.reason); + case PaymentResultEnum.finished: + return PaymentSessionFinished( + sessionId: paymentResultDTO.result?.sessionId ?? "", + sessionData: paymentResultDTO.result?.sessionData ?? "", + resultCode: paymentResultDTO.result?.resultCode ?? "", + order: paymentResultDTO.result?.order?.fromDTO(), + ); + } }); } @@ -168,14 +181,23 @@ class AdyenCheckout implements AdyenCheckoutInterface { } }); - return dropInAdvancedFlowCompleter.future.then((value) { + return dropInAdvancedFlowCompleter.future.then((paymentResultDTO) { AdyenCheckoutPlatformInterface.instance.cleanUpDropIn(); _resultApi.dropInAdvancedFlowPlatformCommunicationStream.close(); - _adyenLogger - .print("Drop-in advanced flow result type: ${value.type.name}"); _adyenLogger.print( - "Drop-in advanced flow result code: ${value.result?.resultCode}"); - return value.fromDTO(); + "Drop-in advanced flow result type: ${paymentResultDTO.type.name}"); + _adyenLogger.print( + "Drop-in advanced flow result code: ${paymentResultDTO.result?.resultCode}"); + switch (paymentResultDTO.type) { + case PaymentResultEnum.cancelledByUser: + return PaymentCancelledByUser(); + case PaymentResultEnum.error: + return PaymentError(reason: paymentResultDTO.reason); + case PaymentResultEnum.finished: + return PaymentAdvancedFlowFinished( + resultCode: paymentResultDTO.result?.resultCode ?? "", + ); + } }); } diff --git a/lib/src/models/payment_result.dart b/lib/src/models/payment_result.dart index a5b904fd..669a6e28 100644 --- a/lib/src/models/payment_result.dart +++ b/lib/src/models/payment_result.dart @@ -1,27 +1,31 @@ import 'package:adyen_checkout/adyen_checkout.dart'; -class PaymentResult { - final PaymentResultEnum type; - final String? reason; - final PaymentResultModel? result; +sealed class PaymentResult {} + +class PaymentAdvancedFlowFinished extends PaymentResult { + final String resultCode; - PaymentResult( - this.type, - this.reason, - this.result, - ); + PaymentAdvancedFlowFinished({required this.resultCode}); } -class PaymentResultModel { - final String? sessionId; - final String? sessionData; - final String? resultCode; +class PaymentSessionFinished extends PaymentResult { + final String sessionId; + final String sessionData; + final String resultCode; final OrderResponse? order; - PaymentResultModel( - this.sessionId, - this.sessionData, - this.resultCode, + PaymentSessionFinished({ + required this.sessionId, + required this.sessionData, + required this.resultCode, this.order, - ); + }); +} + +class PaymentCancelledByUser extends PaymentResult {} + +class PaymentError extends PaymentResult { + final String? reason; + + PaymentError({required this.reason}); } diff --git a/lib/src/utils/dto_mapper.dart b/lib/src/utils/dto_mapper.dart index 6e898ad8..721c12a6 100644 --- a/lib/src/utils/dto_mapper.dart +++ b/lib/src/utils/dto_mapper.dart @@ -68,23 +68,23 @@ extension AmountMapper on Amount { currency: currency, ); } - -extension PaymentResultMapper on PaymentResultDTO { - PaymentResult fromDTO() => PaymentResult( - type, - reason, - result?.fromDTO(), - ); -} - -extension PaymentResulModelMapper on PaymentResultModelDTO { - PaymentResultModel fromDTO() => PaymentResultModel( - sessionId, - sessionData, - resultCode, - order?.fromDTO(), - ); -} +// +// extension PaymentResultMapper on PaymentResultDTO { +// PaymentResult fromDTO() => PaymentResult( +// type, +// reason, +// result?.fromDTO(), +// ); +// } +// +// extension PaymentResulModelMapper on PaymentResultModelDTO { +// PaymentResultModel fromDTO() => PaymentResultModel( +// sessionId, +// sessionData, +// resultCode, +// order?.fromDTO(), +// ); +// } extension OrderResponseMapper on OrderResponseDTO { OrderResponse fromDTO() => OrderResponse( From 19032be2e7446ef17e2762470a1239518956a330 Mon Sep 17 00:00:00 2001 From: Robert Schulze Dieckhoff Date: Tue, 10 Oct 2023 16:55:28 +0200 Subject: [PATCH 2/2] Improved wording --- example/lib/main.dart | 2 +- lib/src/utils/dto_mapper.dart | 17 ----------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 5efc4726..3847f5c1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -198,7 +198,7 @@ class _MyAppState extends State { message = "Result code: ${paymentResult.resultCode}"; case PaymentCancelledByUser(): title = "Cancelled by user"; - message = "Drop-in cancelled manually by user"; + message = "Drop-in cancelled by user"; case PaymentError(): title = "Error occurred"; message = "${paymentResult.reason}"; diff --git a/lib/src/utils/dto_mapper.dart b/lib/src/utils/dto_mapper.dart index 721c12a6..eaa882e5 100644 --- a/lib/src/utils/dto_mapper.dart +++ b/lib/src/utils/dto_mapper.dart @@ -68,23 +68,6 @@ extension AmountMapper on Amount { currency: currency, ); } -// -// extension PaymentResultMapper on PaymentResultDTO { -// PaymentResult fromDTO() => PaymentResult( -// type, -// reason, -// result?.fromDTO(), -// ); -// } -// -// extension PaymentResulModelMapper on PaymentResultModelDTO { -// PaymentResultModel fromDTO() => PaymentResultModel( -// sessionId, -// sessionData, -// resultCode, -// order?.fromDTO(), -// ); -// } extension OrderResponseMapper on OrderResponseDTO { OrderResponse fromDTO() => OrderResponse(