Skip to content

Commit

Permalink
Merge pull request #28 from Adyen/feature/add_ktlint
Browse files Browse the repository at this point in the history
Feature/add ktlint
  • Loading branch information
Robert-SD authored Sep 15, 2023
2 parents 3900515 + d3491e5 commit 7af8c96
Show file tree
Hide file tree
Showing 14 changed files with 794 additions and 139 deletions.
619 changes: 619 additions & 0 deletions android/.editorconfig

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ allprojects {

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply from: 'ktlint.gradle'

android {
namespace = "com.adyen.adyen_checkout"
Expand Down Expand Up @@ -64,4 +65,4 @@ android {
}
}
}
}
}
28 changes: 28 additions & 0 deletions android/ktlint.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
configurations {
ktlint
}

dependencies {
ktlint "com.pinterest:ktlint:0.50.0"
}

tasks.register('ktlintCheck', JavaExec) {
description = "Check Kotlin code style."
group = "verification"

classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "src/**/main/**/*.kt", "--reporter=plain", "--reporter=checkstyle,output=${project.buildDir}/reports/klint/klint-results.xml"
}

check.dependsOn ktlintCheck

tasks.register('ktlintFormat', JavaExec) {
description = "Fix Kotlin code style deviations."
group = "formatting"

classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "-F", "src/main/**/*.kt"
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class AdyenCheckoutPlugin : FlutterPlugin, ActivityAware {

override fun onDetachedFromActivityForConfigChanges() = teardown()

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) =
setupActivity(binding)
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) = setupActivity(
binding
)

override fun onDetachedFromActivity() = teardown()

Expand All @@ -69,10 +70,9 @@ class AdyenCheckoutPlugin : FlutterPlugin, ActivityAware {
Lifecycle.Event.ON_CREATE -> {
checkoutPlatformApi?.dropInSessionLauncher =
DropIn.registerForDropInResult(fragmentActivity, sessionDropInCallback())
checkoutPlatformApi?.dropInAdvancedFlowLauncher =
DropIn.registerForDropInResult(
fragmentActivity, dropInAdvancedFlowCallback
)
checkoutPlatformApi?.dropInAdvancedFlowLauncher = DropIn.registerForDropInResult(
fragmentActivity, dropInAdvancedFlowCallback
)
}

else -> {}
Expand All @@ -91,11 +91,13 @@ class AdyenCheckoutPlugin : FlutterPlugin, ActivityAware {
)

is SessionDropInResult.Error -> PaymentResult(
PaymentResultEnum.ERROR, reason = sessionDropInResult.reason
PaymentResultEnum.ERROR,
reason = sessionDropInResult.reason
)

is SessionDropInResult.Finished -> PaymentResult(
PaymentResultEnum.FINISHED, result = with(sessionDropInResult.result) {
PaymentResultEnum.FINISHED,
result = with(sessionDropInResult.result) {
PaymentResultModel(
sessionId,
sessionData,
Expand All @@ -119,18 +121,22 @@ class AdyenCheckoutPlugin : FlutterPlugin, ActivityAware {
)

is DropInResult.Error -> PaymentResult(
PaymentResultEnum.ERROR, reason = dropInAdvancedFlowResult.reason
PaymentResultEnum.ERROR,
reason = dropInAdvancedFlowResult.reason
)

is DropInResult.Finished -> PaymentResult(
PaymentResultEnum.FINISHED, result = PaymentResultModel(
PaymentResultEnum.FINISHED,
result = PaymentResultModel(
resultCode = dropInAdvancedFlowResult.result
)
)
}

val model = PlatformCommunicationModel(
PlatformCommunicationType.RESULT, data = "", paymentResult = mappedResult
PlatformCommunicationType.RESULT,
data = "",
paymentResult = mappedResult
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
}
Expand Down
221 changes: 111 additions & 110 deletions android/src/main/kotlin/com/adyen/adyen_checkout/CheckoutPlatformApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,138 +35,139 @@ import org.json.JSONObject
@Suppress("NAME_SHADOWING")
class CheckoutPlatformApi(private val checkoutFlutterApi: CheckoutFlutterApi?) :
CheckoutPlatformInterface {
lateinit var activity: FragmentActivity
lateinit var dropInSessionLauncher: ActivityResultLauncher<SessionDropInResultContractParams>
lateinit var dropInAdvancedFlowLauncher: ActivityResultLauncher<DropInResultContractParams>
lateinit var activity: FragmentActivity
lateinit var dropInSessionLauncher:
ActivityResultLauncher<SessionDropInResultContractParams>
lateinit var dropInAdvancedFlowLauncher: ActivityResultLauncher<DropInResultContractParams>

override fun getPlatformVersion(callback: (Result<String>) -> Unit) {
callback.invoke(Result.success("Android ${android.os.Build.VERSION.RELEASE}"))
}
override fun getPlatformVersion(callback: (Result<String>) -> Unit) {
callback.invoke(Result.success("Android ${android.os.Build.VERSION.RELEASE}"))
}

override fun getReturnUrl(callback: (Result<String>) -> Unit) {
callback(Result.success(RedirectComponent.getReturnUrl(activity.applicationContext)))
}
override fun getReturnUrl(callback: (Result<String>) -> Unit) {
callback(Result.success(RedirectComponent.getReturnUrl(activity.applicationContext)))
}

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

}

override fun startDropInAdvancedFlowPayment(
dropInConfiguration: DropInConfiguration,
paymentMethodsResponse: String,
) {
checkForFlutterFragmentActivity()
setAdvancedFlowDropInServiceObserver()
activity.lifecycleScope.launch(Dispatchers.IO) {
val paymentMethodsApiResponse = PaymentMethodsApiResponse.SERIALIZER.deserialize(
JSONObject(paymentMethodsResponse)
)
val paymentMethodsWithoutGiftCards =
removeGiftCardPaymentMethods(paymentMethodsApiResponse)
val dropInConfiguration =
dropInConfiguration.mapToDropInConfiguration(activity.applicationContext)
withContext(Dispatchers.Main) {
DropIn.startPayment(
activity.applicationContext,
dropInAdvancedFlowLauncher,
paymentMethodsWithoutGiftCards,
dropInConfiguration,
AdvancedFlowDropInService::class.java,
override fun startDropInAdvancedFlowPayment(
dropInConfiguration: DropInConfiguration,
paymentMethodsResponse: String,
) {
checkForFlutterFragmentActivity()
setAdvancedFlowDropInServiceObserver()
activity.lifecycleScope.launch(Dispatchers.IO) {
val paymentMethodsApiResponse = PaymentMethodsApiResponse.SERIALIZER.deserialize(
JSONObject(paymentMethodsResponse),
)
val paymentMethodsWithoutGiftCards =
removeGiftCardPaymentMethods(paymentMethodsApiResponse)
val dropInConfiguration =
dropInConfiguration.mapToDropInConfiguration(activity.applicationContext)
withContext(Dispatchers.Main) {
DropIn.startPayment(
activity.applicationContext,
dropInAdvancedFlowLauncher,
paymentMethodsWithoutGiftCards,
dropInConfiguration,
AdvancedFlowDropInService::class.java,
)
}
}
}
}

override fun onPaymentsResult(paymentsResult: DropInResult) {
if (paymentsResult.dropInResultType == DropInResultType.ACTION) {
setAdvanceFlowDropInAdditionalDetailsMessengerObserver()
}
override fun onPaymentsResult(paymentsResult: DropInResult) {
if (paymentsResult.dropInResultType == DropInResultType.ACTION) {
setAdvanceFlowDropInAdditionalDetailsMessengerObserver()
}

DropInPaymentResultMessenger.sendResult(paymentsResult)
}
DropInPaymentResultMessenger.sendResult(paymentsResult)
}

override fun onPaymentsDetailsResult(paymentsDetailsResult: DropInResult) {
DropInAdditionalDetailsResultMessenger.sendResult(paymentsDetailsResult)
}
override fun onPaymentsDetailsResult(paymentsDetailsResult: DropInResult) {
DropInAdditionalDetailsResultMessenger.sendResult(paymentsDetailsResult)
}

private suspend fun createCheckoutSession(
sessionModel: com.adyen.checkout.sessions.core.SessionModel,
dropInConfiguration: com.adyen.checkout.dropin.DropInConfiguration
): CheckoutSession {
val checkoutSessionResult =
CheckoutSessionProvider.createSession(sessionModel, dropInConfiguration)
return when (checkoutSessionResult) {
is CheckoutSessionResult.Success -> checkoutSessionResult.checkoutSession
is CheckoutSessionResult.Error -> throw checkoutSessionResult.exception
private suspend fun createCheckoutSession(
sessionModel: com.adyen.checkout.sessions.core.SessionModel,
dropInConfiguration: com.adyen.checkout.dropin.DropInConfiguration,
): CheckoutSession {
val checkoutSessionResult =
CheckoutSessionProvider.createSession(sessionModel, dropInConfiguration)
return when (checkoutSessionResult) {
is CheckoutSessionResult.Success -> checkoutSessionResult.checkoutSession
is CheckoutSessionResult.Error -> throw checkoutSessionResult.exception
}
}
}

private fun setAdvancedFlowDropInServiceObserver() {
DropInServiceResultMessenger.instance().removeObservers(activity)
DropInServiceResultMessenger.instance().observe(activity) { message ->
if (message.hasBeenHandled()) {
return@observe
}

private fun setAdvancedFlowDropInServiceObserver() {
DropInServiceResultMessenger.instance().removeObservers(activity)
DropInServiceResultMessenger.instance().observe(activity) { message ->
if (message.hasBeenHandled()) {
return@observe
val model = PlatformCommunicationModel(
PlatformCommunicationType.PAYMENTCOMPONENT,
data = message.contentIfNotHandled.toString(),
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
}

val model = PlatformCommunicationModel(
PlatformCommunicationType.PAYMENTCOMPONENT,
data = message.contentIfNotHandled.toString()
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
}
}

private fun setAdvanceFlowDropInAdditionalDetailsMessengerObserver() {
DropInAdditionalDetailsPlatformMessenger.instance().removeObservers(activity)
DropInAdditionalDetailsPlatformMessenger.instance().observe(activity) { message ->
if (message.hasBeenHandled()) {
return@observe
}
private fun setAdvanceFlowDropInAdditionalDetailsMessengerObserver() {
DropInAdditionalDetailsPlatformMessenger.instance().removeObservers(activity)
DropInAdditionalDetailsPlatformMessenger.instance().observe(activity) { message ->
if (message.hasBeenHandled()) {
return@observe
}

val model = PlatformCommunicationModel(
PlatformCommunicationType.ADDITIONALDETAILS,
data = message.contentIfNotHandled.toString()
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
val model = PlatformCommunicationModel(
PlatformCommunicationType.ADDITIONALDETAILS,
data = message.contentIfNotHandled.toString(),
)
checkoutFlutterApi?.onDropInAdvancedFlowPlatformCommunication(model) {}
}
}
}

private fun checkForFlutterFragmentActivity() {
if (!this::activity.isInitialized) {
throw Exception(WRONG_FLUTTER_ACTIVITY_USAGE_ERROR_MESSAGE)
private fun checkForFlutterFragmentActivity() {
if (!this::activity.isInitialized) {
throw Exception(WRONG_FLUTTER_ACTIVITY_USAGE_ERROR_MESSAGE)
}
}
}

//Gift cards will be supported in a later version
private fun removeGiftCardPaymentMethods(paymentMethodsResponse: PaymentMethodsApiResponse): PaymentMethodsApiResponse {
val giftCardTypeIdentifier = "giftcard"
val storedPaymentMethods =
paymentMethodsResponse.storedPaymentMethods?.filterNot { it.type == giftCardTypeIdentifier }
val paymentMethods =
paymentMethodsResponse.paymentMethods?.filterNot { it.type == giftCardTypeIdentifier }

return PaymentMethodsApiResponse(
storedPaymentMethods = storedPaymentMethods,
paymentMethods = paymentMethods
)
// Gift cards will be supported in a later version
private fun removeGiftCardPaymentMethods(
paymentMethodsResponse: PaymentMethodsApiResponse
): PaymentMethodsApiResponse {
val giftCardTypeIdentifier = "giftcard"
val storedPaymentMethods =
paymentMethodsResponse.storedPaymentMethods?.filterNot { it.type == giftCardTypeIdentifier }
val paymentMethods =
paymentMethodsResponse.paymentMethods?.filterNot { it.type == giftCardTypeIdentifier }

return PaymentMethodsApiResponse(
storedPaymentMethods = storedPaymentMethods,
paymentMethods = paymentMethods
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v11.0.0), do not edit directly.
// Autogenerated from Pigeon (v11.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon


Expand Down
Loading

0 comments on commit 7af8c96

Please sign in to comment.