Skip to content

Commit

Permalink
Merge pull request #361 from Adyen/feature/AddBinLookupToDropIn
Browse files Browse the repository at this point in the history
Add bin lookup to drop-in
  • Loading branch information
Robert-SD authored Mar 5, 2025
2 parents 50e2202 + 1e4def1 commit f2902f4
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 119 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### New

- Added bin lookup callbacks for Drop-In.
- Set minimum SDK version to Flutter 3.16/Dart 3.2
- Android Components/Drop-in
version: [5.9.0](https://docs.adyen.com/online-payments/release-notes/?title%5B0%5D=Android+Components%2FDrop-in#releaseNote=2025-01-17-android-componentsdrop-in-5.9.0).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,24 @@ import com.adyen.checkout.flutter.utils.ConfigurationMapper.mapToOrderResponseMo
import com.adyen.checkout.sessions.core.CheckoutSession
import com.adyen.checkout.sessions.core.SessionSetupResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class DropInPlatformApi(
internal class DropInPlatformApi(
private val checkoutFlutter: CheckoutFlutterInterface,
private val activity: FragmentActivity,
private val sessionHolder: SessionHolder,
) : DropInPlatformInterface {
lateinit var dropInSessionLauncher: ActivityResultLauncher<SessionDropInResultContractParams>
lateinit var dropInAdvancedFlowLauncher: ActivityResultLauncher<DropInResultContractParams>
private var dropInPlatformMessengerJob: Job? = null

companion object {
val dropInMessageFlow = MutableSharedFlow<CheckoutEvent>()
}

override fun showDropInSession(dropInConfigurationDTO: DropInConfigurationDTO) {
setStoredPaymentMethodDeletionObserver()
Expand All @@ -66,6 +73,11 @@ class DropInPlatformApi(
dropInConfigurationDTO.environment.mapToEnvironment(),
dropInConfigurationDTO.clientKey
)

dropInPlatformMessengerJob?.cancel()
dropInPlatformMessengerJob =
activity.lifecycleScope.launch { dropInMessageFlow.collect { event -> checkoutFlutter.send(event) {} } }

DropIn.startPayment(
activity.applicationContext,
dropInSessionLauncher,
Expand All @@ -84,6 +96,11 @@ class DropInPlatformApi(
setBalanceCheckPlatformMessengerObserver()
setOrderRequestPlatformMessengerObserver()
setOrderCancelPlatformMessengerObserver()

dropInPlatformMessengerJob?.cancel()
dropInPlatformMessengerJob =
activity.lifecycleScope.launch { dropInMessageFlow.collect { event -> checkoutFlutter.send(event) {} } }

activity.lifecycleScope.launch(Dispatchers.IO) {
val paymentMethodsApiResponse =
PaymentMethodsApiResponse.SERIALIZER.deserialize(
Expand Down Expand Up @@ -138,6 +155,8 @@ class DropInPlatformApi(
}

override fun cleanUpDropIn() {
dropInPlatformMessengerJob?.cancel()

DropInServiceResultMessenger.instance().removeObservers(activity)
DropInAdditionalDetailsPlatformMessenger.instance().removeObservers(activity)
DropInPaymentMethodDeletionPlatformMessenger.instance().removeObservers(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.os.IBinder
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ServiceLifecycleDispatcher
import androidx.lifecycle.lifecycleScope
import com.adyen.checkout.card.BinLookupData
import com.adyen.checkout.components.core.ActionComponentData
import com.adyen.checkout.components.core.BalanceResult
import com.adyen.checkout.components.core.Order
Expand All @@ -20,15 +22,20 @@ import com.adyen.checkout.dropin.DropInServiceResult
import com.adyen.checkout.dropin.ErrorDialog
import com.adyen.checkout.dropin.OrderDropInServiceResult
import com.adyen.checkout.dropin.RecurringDropInServiceResult
import com.adyen.checkout.flutter.dropIn.DropInPlatformApi
import com.adyen.checkout.flutter.dropIn.model.DropInStoredPaymentMethodDeletionModel
import com.adyen.checkout.flutter.dropIn.model.DropInType
import com.adyen.checkout.flutter.generated.BinLookupDataDTO
import com.adyen.checkout.flutter.generated.DeletedStoredPaymentMethodResultDTO
import com.adyen.checkout.flutter.generated.ErrorDTO
import com.adyen.checkout.flutter.generated.OrderCancelResultDTO
import com.adyen.checkout.flutter.generated.PaymentEventDTO
import com.adyen.checkout.flutter.generated.PaymentEventType
import com.adyen.checkout.flutter.generated.CheckoutEvent
import com.adyen.checkout.flutter.generated.CheckoutEventType
import com.adyen.checkout.flutter.utils.Constants
import com.adyen.checkout.googlepay.GooglePayComponentState
import kotlinx.coroutines.launch
import org.json.JSONObject

class AdvancedDropInService : DropInService(), LifecycleOwner {
Expand Down Expand Up @@ -98,6 +105,29 @@ class AdvancedDropInService : DropInService(), LifecycleOwner {
}
}

override fun onBinLookup(data: List<BinLookupData>) {
lifecycleScope.launch {
val binLookupDataDtoList = data.map { BinLookupDataDTO(it.brand) }
val checkoutEvent =
CheckoutEvent(
CheckoutEventType.BIN_LOOKUP,
binLookupDataDtoList
)
DropInPlatformApi.dropInMessageFlow.emit(checkoutEvent)
}
}

override fun onBinValue(binValue: String) {
lifecycleScope.launch {
val checkoutEvent =
CheckoutEvent(
CheckoutEventType.BIN_VALUE,
binValue
)
DropInPlatformApi.dropInMessageFlow.emit(checkoutEvent)
}
}

private fun onPaymentComponentState(state: PaymentComponentState<*>) {
try {
setAdvancedFlowDropInServiceObserver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import android.os.IBinder
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ServiceLifecycleDispatcher
import androidx.lifecycle.lifecycleScope
import com.adyen.checkout.card.BinLookupData
import com.adyen.checkout.components.core.StoredPaymentMethod
import com.adyen.checkout.dropin.ErrorDialog
import com.adyen.checkout.dropin.RecurringDropInServiceResult
import com.adyen.checkout.dropin.SessionDropInService
import com.adyen.checkout.flutter.dropIn.DropInPlatformApi
import com.adyen.checkout.flutter.dropIn.advanced.DropInPaymentMethodDeletionPlatformMessenger
import com.adyen.checkout.flutter.dropIn.advanced.DropInPaymentMethodDeletionResultMessenger
import com.adyen.checkout.flutter.dropIn.model.DropInStoredPaymentMethodDeletionModel
import com.adyen.checkout.flutter.dropIn.model.DropInType
import com.adyen.checkout.flutter.generated.BinLookupDataDTO
import com.adyen.checkout.flutter.generated.DeletedStoredPaymentMethodResultDTO
import com.adyen.checkout.flutter.generated.CheckoutEvent
import com.adyen.checkout.flutter.generated.CheckoutEventType
import kotlinx.coroutines.launch

class SessionDropInService : SessionDropInService(), LifecycleOwner {
private val dispatcher = ServiceLifecycleDispatcher(this)
Expand All @@ -32,6 +39,25 @@ class SessionDropInService : SessionDropInService(), LifecycleOwner {
}
}

override fun onBinLookup(data: List<BinLookupData>) {
lifecycleScope.launch {
val binLookupDataDtoList = data.map { BinLookupDataDTO(it.brand) }
val checkoutEvent =
CheckoutEvent(
CheckoutEventType.BIN_LOOKUP,
binLookupDataDtoList
)
DropInPlatformApi.dropInMessageFlow.emit(checkoutEvent)
}
}

override fun onBinValue(binValue: String) {
lifecycleScope.launch {
val platformCommunicationModel = CheckoutEvent(CheckoutEventType.BIN_VALUE, binValue)
DropInPlatformApi.dropInMessageFlow.emit(platformCommunicationModel)
}
}

private fun setStoredPaymentMethodDeletionObserver() {
DropInPaymentMethodDeletionResultMessenger.instance().removeObservers(this)
DropInPaymentMethodDeletionResultMessenger.instance().observe(this) { message ->
Expand Down
Loading

0 comments on commit f2902f4

Please sign in to comment.