Skip to content

Commit

Permalink
Added apple pay configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-SD committed Sep 15, 2023
1 parent 306dfa5 commit a4d4a79
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.json.JSONObject

@Suppress("NAME_SHADOWING")
class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
CheckoutPlatformInterface {
CheckoutPlatformInterface {
lateinit var activity: FragmentActivity
lateinit var dropInSessionLauncher: ActivityResultLauncher<SessionDropInResultContractParams>
lateinit var dropInAdvancedFlowLauncher: ActivityResultLauncher<DropInResultContractParams>
Expand All @@ -48,48 +48,48 @@ class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
}

override fun startDropInSessionPayment(
dropInConfiguration: DropInConfigurationDTO,
session: Session,
dropInConfigurationDTO: DropInConfigurationDTO,
session: Session,
) {
checkForFlutterFragmentActivity()
activity.lifecycleScope.launch(Dispatchers.IO) {
val sessionModel = session.mapToSession()
val dropInConfiguration =
dropInConfiguration.mapToDropInConfiguration(activity.applicationContext)
dropInConfigurationDTO.mapToDropInConfiguration(activity.applicationContext)
val checkoutSession = createCheckoutSession(sessionModel, dropInConfiguration)
withContext(Dispatchers.Main) {
DropIn.startPayment(
activity.applicationContext,
dropInSessionLauncher,
checkoutSession,
dropInConfiguration
activity.applicationContext,
dropInSessionLauncher,
checkoutSession,
dropInConfiguration
)
}
}

}

override fun startDropInAdvancedFlowPayment(
dropInConfiguration: DropInConfigurationDTO,
paymentMethodsResponse: String,
dropInConfigurationDTO: DropInConfigurationDTO,
paymentMethodsResponse: String,
) {
checkForFlutterFragmentActivity()
setAdvancedFlowDropInServiceObserver()
activity.lifecycleScope.launch(Dispatchers.IO) {
val paymentMethodsApiResponse = PaymentMethodsApiResponse.SERIALIZER.deserialize(
JSONObject(paymentMethodsResponse)
JSONObject(paymentMethodsResponse)
)
val paymentMethodsWithoutGiftCards =
removeGiftCardPaymentMethods(paymentMethodsApiResponse)
removeGiftCardPaymentMethods(paymentMethodsApiResponse)
val dropInConfiguration =
dropInConfiguration.mapToDropInConfiguration(activity.applicationContext)
dropInConfigurationDTO.mapToDropInConfiguration(activity.applicationContext)
withContext(Dispatchers.Main) {
DropIn.startPayment(
activity.applicationContext,
dropInAdvancedFlowLauncher,
paymentMethodsWithoutGiftCards,
dropInConfiguration,
AdvancedFlowDropInService::class.java,
activity.applicationContext,
dropInAdvancedFlowLauncher,
paymentMethodsWithoutGiftCards,
dropInConfiguration,
AdvancedFlowDropInService::class.java,
)
}
}
Expand All @@ -108,11 +108,11 @@ class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
}

private suspend fun createCheckoutSession(
sessionModel: com.adyen.checkout.sessions.core.SessionModel,
dropInConfiguration: com.adyen.checkout.dropin.DropInConfiguration
sessionModel: com.adyen.checkout.sessions.core.SessionModel,
dropInConfiguration: com.adyen.checkout.dropin.DropInConfiguration
): CheckoutSession {
val checkoutSessionResult =
CheckoutSessionProvider.createSession(sessionModel, dropInConfiguration)
CheckoutSessionProvider.createSession(sessionModel, dropInConfiguration)
return when (checkoutSessionResult) {
is CheckoutSessionResult.Success -> checkoutSessionResult.checkoutSession
is CheckoutSessionResult.Error -> throw checkoutSessionResult.exception
Expand All @@ -128,8 +128,8 @@ class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
}

val model = PlatformCommunicationModel(
PlatformCommunicationType.PAYMENTCOMPONENT,
data = message.contentIfNotHandled.toString()
PlatformCommunicationType.PAYMENTCOMPONENT,
data = message.contentIfNotHandled.toString()
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
}
Expand All @@ -143,8 +143,8 @@ class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
}

val model = PlatformCommunicationModel(
PlatformCommunicationType.ADDITIONALDETAILS,
data = message.contentIfNotHandled.toString()
PlatformCommunicationType.ADDITIONALDETAILS,
data = message.contentIfNotHandled.toString()
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
}
Expand All @@ -160,13 +160,13 @@ class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
private fun removeGiftCardPaymentMethods(paymentMethodsResponse: PaymentMethodsApiResponse): PaymentMethodsApiResponse {
val giftCardTypeIdentifier = "giftcard"
val storedPaymentMethods =
paymentMethodsResponse.storedPaymentMethods?.filterNot { it.type == giftCardTypeIdentifier }
paymentMethodsResponse.storedPaymentMethods?.filterNot { it.type == giftCardTypeIdentifier }
val paymentMethods =
paymentMethodsResponse.paymentMethods?.filterNot { it.type == giftCardTypeIdentifier }
paymentMethodsResponse.paymentMethods?.filterNot { it.type == giftCardTypeIdentifier }

return PaymentMethodsApiResponse(
storedPaymentMethods = storedPaymentMethods,
paymentMethods = paymentMethods
storedPaymentMethods = storedPaymentMethods,
paymentMethods = paymentMethods
)
}
}
95 changes: 67 additions & 28 deletions android/src/main/kotlin/com/adyen/adyen_checkout/PlatformApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ data class DropInConfigurationDTO (
val clientKey: String,
val countryCode: String,
val amount: Amount,
val analyticsOptions: AnalyticsOptions? = null,
val analyticsOptionsDTO: AnalyticsOptionsDTO? = null,
val showPreselectedStoredPaymentMethod: Boolean? = null,
val skipListWhenSinglePaymentMethod: Boolean? = null,
val cardsConfiguration: CardsConfigurationDTO? = null
val cardsConfigurationDTO: CardsConfigurationDTO? = null,
val applePayConfigurationDTO: ApplePayConfigurationDTO? = null

) {
companion object {
Expand All @@ -168,15 +169,18 @@ data class DropInConfigurationDTO (
val clientKey = list[1] as String
val countryCode = list[2] as String
val amount = Amount.fromList(list[3] as List<Any?>)
val analyticsOptions: AnalyticsOptions? = (list[4] as List<Any?>?)?.let {
AnalyticsOptions.fromList(it)
val analyticsOptionsDTO: AnalyticsOptionsDTO? = (list[4] as List<Any?>?)?.let {
AnalyticsOptionsDTO.fromList(it)
}
val showPreselectedStoredPaymentMethod = list[5] as Boolean?
val skipListWhenSinglePaymentMethod = list[6] as Boolean?
val cardsConfiguration: CardsConfigurationDTO? = (list[7] as List<Any?>?)?.let {
val cardsConfigurationDTO: CardsConfigurationDTO? = (list[7] as List<Any?>?)?.let {
CardsConfigurationDTO.fromList(it)
}
return DropInConfigurationDTO(environment, clientKey, countryCode, amount, analyticsOptions, showPreselectedStoredPaymentMethod, skipListWhenSinglePaymentMethod, cardsConfiguration)
val applePayConfigurationDTO: ApplePayConfigurationDTO? = (list[8] as List<Any?>?)?.let {
ApplePayConfigurationDTO.fromList(it)
}
return DropInConfigurationDTO(environment, clientKey, countryCode, amount, analyticsOptionsDTO, showPreselectedStoredPaymentMethod, skipListWhenSinglePaymentMethod, cardsConfigurationDTO, applePayConfigurationDTO)
}
}
fun toList(): List<Any?> {
Expand All @@ -185,10 +189,11 @@ data class DropInConfigurationDTO (
clientKey,
countryCode,
amount.toList(),
analyticsOptions?.toList(),
analyticsOptionsDTO?.toList(),
showPreselectedStoredPaymentMethod,
skipListWhenSinglePaymentMethod,
cardsConfiguration?.toList(),
cardsConfigurationDTO?.toList(),
applePayConfigurationDTO?.toList(),
)
}
}
Expand Down Expand Up @@ -234,17 +239,17 @@ data class CardsConfigurationDTO (
}

/** Generated class from Pigeon that represents data sent in messages. */
data class AnalyticsOptions (
data class AnalyticsOptionsDTO (
val enabled: Boolean? = null,
val payload: String? = null

) {
companion object {
@Suppress("UNCHECKED_CAST")
fun fromList(list: List<Any?>): AnalyticsOptions {
fun fromList(list: List<Any?>): AnalyticsOptionsDTO {
val enabled = list[0] as Boolean?
val payload = list[1] as String?
return AnalyticsOptions(enabled, payload)
return AnalyticsOptionsDTO(enabled, payload)
}
}
fun toList(): List<Any?> {
Expand All @@ -255,6 +260,31 @@ data class AnalyticsOptions (
}
}

/** Generated class from Pigeon that represents data sent in messages. */
data class ApplePayConfigurationDTO (
val merchantId: String,
val merchantName: String,
val allowOnboarding: Boolean

) {
companion object {
@Suppress("UNCHECKED_CAST")
fun fromList(list: List<Any?>): ApplePayConfigurationDTO {
val merchantId = list[0] as String
val merchantName = list[1] as String
val allowOnboarding = list[2] as Boolean
return ApplePayConfigurationDTO(merchantId, merchantName, allowOnboarding)
}
}
fun toList(): List<Any?> {
return listOf<Any?>(
merchantId,
merchantName,
allowOnboarding,
)
}
}

/** Generated class from Pigeon that represents data sent in messages. */
data class PaymentResult (
val type: PaymentResultEnum,
Expand Down Expand Up @@ -437,30 +467,35 @@ private object CheckoutPlatformInterfaceCodec : StandardMessageCodec() {
}
129.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
AnalyticsOptions.fromList(it)
AnalyticsOptionsDTO.fromList(it)
}
}
130.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
CardsConfigurationDTO.fromList(it)
ApplePayConfigurationDTO.fromList(it)
}
}
131.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
DropInConfigurationDTO.fromList(it)
CardsConfigurationDTO.fromList(it)
}
}
132.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
DropInError.fromList(it)
DropInConfigurationDTO.fromList(it)
}
}
133.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
DropInResult.fromList(it)
DropInError.fromList(it)
}
}
134.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
DropInResult.fromList(it)
}
}
135.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
Session.fromList(it)
}
Expand All @@ -474,30 +509,34 @@ private object CheckoutPlatformInterfaceCodec : StandardMessageCodec() {
stream.write(128)
writeValue(stream, value.toList())
}
is AnalyticsOptions -> {
is AnalyticsOptionsDTO -> {
stream.write(129)
writeValue(stream, value.toList())
}
is CardsConfigurationDTO -> {
is ApplePayConfigurationDTO -> {
stream.write(130)
writeValue(stream, value.toList())
}
is DropInConfigurationDTO -> {
is CardsConfigurationDTO -> {
stream.write(131)
writeValue(stream, value.toList())
}
is DropInError -> {
is DropInConfigurationDTO -> {
stream.write(132)
writeValue(stream, value.toList())
}
is DropInResult -> {
is DropInError -> {
stream.write(133)
writeValue(stream, value.toList())
}
is Session -> {
is DropInResult -> {
stream.write(134)
writeValue(stream, value.toList())
}
is Session -> {
stream.write(135)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
Expand All @@ -507,8 +546,8 @@ private object CheckoutPlatformInterfaceCodec : StandardMessageCodec() {
interface CheckoutPlatformInterface {
fun getPlatformVersion(callback: (Result<String>) -> Unit)
fun getReturnUrl(callback: (Result<String>) -> Unit)
fun startDropInSessionPayment(dropInConfiguration: DropInConfigurationDTO, session: Session)
fun startDropInAdvancedFlowPayment(dropInConfiguration: DropInConfigurationDTO, paymentMethodsResponse: String)
fun startDropInSessionPayment(dropInConfigurationDTO: DropInConfigurationDTO, session: Session)
fun startDropInAdvancedFlowPayment(dropInConfigurationDTO: DropInConfigurationDTO, paymentMethodsResponse: String)
fun onPaymentsResult(paymentsResult: DropInResult)
fun onPaymentsDetailsResult(paymentsDetailsResult: DropInResult)

Expand Down Expand Up @@ -561,11 +600,11 @@ interface CheckoutPlatformInterface {
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val dropInConfigurationArg = args[0] as DropInConfigurationDTO
val dropInConfigurationDTOArg = args[0] as DropInConfigurationDTO
val sessionArg = args[1] as Session
var wrapped: List<Any?>
try {
api.startDropInSessionPayment(dropInConfigurationArg, sessionArg)
api.startDropInSessionPayment(dropInConfigurationDTOArg, sessionArg)
wrapped = listOf<Any?>(null)
} catch (exception: Throwable) {
wrapped = wrapError(exception)
Expand All @@ -581,11 +620,11 @@ interface CheckoutPlatformInterface {
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val dropInConfigurationArg = args[0] as DropInConfigurationDTO
val dropInConfigurationDTOArg = args[0] as DropInConfigurationDTO
val paymentMethodsResponseArg = args[1] as String
var wrapped: List<Any?>
try {
api.startDropInAdvancedFlowPayment(dropInConfigurationArg, paymentMethodsResponseArg)
api.startDropInAdvancedFlowPayment(dropInConfigurationDTOArg, paymentMethodsResponseArg)
wrapped = listOf<Any?>(null)
} catch (exception: Throwable) {
wrapped = wrapError(exception)
Expand Down
18 changes: 9 additions & 9 deletions android/src/main/kotlin/com/adyen/adyen_checkout/utils/Mapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ object Mapper {
environment = environment,
clientKey = this.clientKey
)
.setShowStorePaymentField(cardsConfiguration?.showStorePaymentField ?: false)
.setAddressConfiguration(cardsConfiguration?.addressMode?.mapToAddressConfiguration()
.setShowStorePaymentField(cardsConfigurationDTO?.showStorePaymentField ?: false)
.setAddressConfiguration(cardsConfigurationDTO?.addressMode?.mapToAddressConfiguration()
?: AddressConfiguration.None)
.setShowStorePaymentField(cardsConfiguration?.showStorePaymentField ?: false)
.setHideCvcStoredCard(cardsConfiguration?.hideCvcStoredCard ?: false)
.setHideCvc(cardsConfiguration?.hideCvc ?: false)
.setKcpAuthVisibility(determineKcpAuthVisibility(cardsConfiguration?.kcpVisible))
.setSocialSecurityNumberVisibility(determineSocialSecurityNumberVisibility(cardsConfiguration?.socialSecurityVisible))
.setSupportedCardTypes(*mapToSupportedCardTypes(cardsConfiguration?.supportedCardTypes))
.setHolderNameRequired(cardsConfiguration?.holderNameRequired ?: false)
.setShowStorePaymentField(cardsConfigurationDTO?.showStorePaymentField ?: false)
.setHideCvcStoredCard(cardsConfigurationDTO?.hideCvcStoredCard ?: false)
.setHideCvc(cardsConfigurationDTO?.hideCvc ?: false)
.setKcpAuthVisibility(determineKcpAuthVisibility(cardsConfigurationDTO?.kcpVisible))
.setSocialSecurityNumberVisibility(determineSocialSecurityNumberVisibility(cardsConfigurationDTO?.socialSecurityVisible))
.setSupportedCardTypes(*mapToSupportedCardTypes(cardsConfigurationDTO?.supportedCardTypes))
.setHolderNameRequired(cardsConfigurationDTO?.holderNameRequired ?: false)
.build()
val amount = this.amount.mapToAmount()
return com.adyen.checkout.dropin.DropInConfiguration.Builder(
Expand Down
Loading

0 comments on commit a4d4a79

Please sign in to comment.