Skip to content

Commit

Permalink
Added error handling for advanced flow callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-SD committed Oct 10, 2023
1 parent c26109d commit 30f0fce
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adyen.adyen_checkout.dropInAdvancedFlow

import DeletedStoredPaymentMethodResultDTO
import DropInErrorDTO
import DropInResultDTO
import DropInResultType
import android.content.Intent
Expand Down Expand Up @@ -132,7 +133,7 @@ class AdvancedFlowDropInService : DropInService(), LifecycleOwner {
)

DropInResultType.ERROR -> DropInServiceResult.Error(
errorDialog = null,
errorDialog = buildErrorDialog(dropInResultDTO.error),
reason = dropInResultDTO.error?.reason,
dismissDropIn = dropInResultDTO.error?.dismissDropIn ?: false
)
Expand All @@ -158,6 +159,14 @@ class AdvancedFlowDropInService : DropInService(), LifecycleOwner {
}
}

private fun buildErrorDialog(dropInError: DropInErrorDTO?): ErrorDialog? {
return if (dropInError?.dismissDropIn == true) {
null
} else {
ErrorDialog(message = dropInError?.errorMessage)
}
}

override fun onBind(intent: Intent?): IBinder {
dispatcher.onServicePreSuperOnBind()
return super.onBind(intent)
Expand Down
6 changes: 3 additions & 3 deletions example/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class Config {
//Environment constants
static const String merchantAccount = "TestMerchantCheckout";
static const String merchantName = "Test Merchant";
static const String countryCode = "US";
static const String shopperLocale = "us_US";
static const String countryCode = "NL";
static const String shopperLocale = "nl_NL";
static const String shopperReference = "Test reference";
static const Environment environment = Environment.test;
static const String baseUrl = "checkout-test.adyen.com";
static const String apiVersion = "v70";
static const String iOSReturnUrl = "ui-host://payments";

//Example data
static Amount amount = Amount(currency: "USD", value: 2100);
static Amount amount = Amount(currency: "EUR", value: 2100);
}
41 changes: 27 additions & 14 deletions ios/Classes/CheckoutPlatformApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,14 @@ class CheckoutPlatformApi: CheckoutPlatformInterface {
}

private func handleDropInResult(dropInResult: DropInResultDTO) {
do {
switch dropInResult.dropInResultType {
case .finished:
onDropInResultFinished(dropInResult: dropInResult)
case .action:
try onDropInResultAction(dropInResult: dropInResult)
onDropInResultAction(dropInResult: dropInResult)
case .error:
onDropInResultError(dropInResult: dropInResult)
}
} catch {
let paymentResult = PaymentResultDTO(type: PaymentResultEnum.error, reason: error.localizedDescription)
checkoutFlutterApi.onDropInAdvancedFlowPlatformCommunication(platformCommunicationModel: PlatformCommunicationModel(type: PlatformCommunicationType.result, paymentResult: paymentResult), completion: { _ in })
finalize(false, "\(error.localizedDescription)")
}
}

private func onDropInResultFinished(dropInResult: DropInResultDTO) {
Expand All @@ -204,16 +198,35 @@ class CheckoutPlatformApi: CheckoutPlatformInterface {
}
}

private func onDropInResultAction(dropInResult: DropInResultDTO) throws {
let jsonData = try JSONSerialization.data(withJSONObject: dropInResult.actionResponse as Any, options: [])
let result = try JSONDecoder().decode(Action.self, from: jsonData)
dropInComponent?.handle(result)
private func onDropInResultAction(dropInResult: DropInResultDTO) {
do {
let jsonData = try JSONSerialization.data(withJSONObject: dropInResult.actionResponse as Any, options: [])
let result = try JSONDecoder().decode(Action.self, from: jsonData)
dropInComponent?.handle(result)
} catch {
let paymentResult = PaymentResultDTO(type: PaymentResultEnum.error, reason: error.localizedDescription)
checkoutFlutterApi.onDropInAdvancedFlowPlatformCommunication(platformCommunicationModel: PlatformCommunicationModel(type: PlatformCommunicationType.result, paymentResult: paymentResult), completion: { _ in })
finalize(false, "\(error.localizedDescription)")
}
}

private func onDropInResultError(dropInResult: DropInResultDTO) {
let paymentResult = PaymentResultDTO(type: PaymentResultEnum.error, reason: dropInResult.error?.errorMessage)
checkoutFlutterApi.onDropInAdvancedFlowPlatformCommunication(platformCommunicationModel: PlatformCommunicationModel(type: PlatformCommunicationType.result, paymentResult: paymentResult), completion: { _ in })
finalize(false, dropInResult.error?.errorMessage ?? "")
dropInComponent?.stopLoading()

if (dropInResult.error?.dismissDropIn == true) {
let paymentResult = PaymentResultDTO(type: PaymentResultEnum.error, reason: dropInResult.error?.errorMessage)
checkoutFlutterApi.onDropInAdvancedFlowPlatformCommunication(platformCommunicationModel: PlatformCommunicationModel(type: PlatformCommunicationType.result, paymentResult: paymentResult), completion: { _ in })
finalize(false, dropInResult.error?.errorMessage ?? "")
} else {
dropInComponent?.finalizeIfNeeded(with: false, completion: {})
let localizationParameters = (dropInComponent as? Localizable)?.localizationParameters
let title = localizedString(.errorTitle, localizationParameters)
let alertController = UIAlertController(title: title,
message: dropInResult.error?.errorMessage,
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: localizedString(.dismissButton, localizationParameters), style: .cancel))
viewController?.adyen.topPresenter.present(alertController, animated: true)
}
}

private func finalize(_ success: Bool, _: String) {
Expand Down
16 changes: 10 additions & 6 deletions lib/src/adyen_checkout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ class AdyenCheckout implements AdyenCheckoutInterface {
DropInResultDTO dropInResult = _mapToDropInResult(paymentsResult);
AdyenCheckoutPlatformInterface.instance.onPaymentsResult(dropInResult);
} catch (error) {
_adyenLogger.print(error.toString());
String errorMessage = error.toString();
_adyenLogger.print("Failure in postPayments, $errorMessage");
AdyenCheckoutPlatformInterface.instance.onPaymentsResult(DropInResultDTO(
dropInResultType: DropInResultType.error,
error: DropInErrorDTO(
reason: "Failure executing postPayments, ${error.toString()}",
dismissDropIn: true,
errorMessage: errorMessage,
reason: "Failure in postPayments, $errorMessage",
dismissDropIn: false,
),
));
}
Expand All @@ -226,13 +228,15 @@ class AdyenCheckout implements AdyenCheckoutInterface {
AdyenCheckoutPlatformInterface.instance
.onPaymentsDetailsResult(dropInResult);
} catch (error) {
_adyenLogger.print(error.toString());
String errorMessage = error.toString();
_adyenLogger.print("Failure in postPaymentsDetails, $errorMessage");
AdyenCheckoutPlatformInterface.instance
.onPaymentsDetailsResult(DropInResultDTO(
dropInResultType: DropInResultType.error,
error: DropInErrorDTO(
reason: "Failure executing postPaymentsDetails, ${error.toString()}",
dismissDropIn: true,
errorMessage: errorMessage,
reason: "Failure in postPaymentsDetails, $errorMessage}",
dismissDropIn: false,
),
));
}
Expand Down

0 comments on commit 30f0fce

Please sign in to comment.