Skip to content

Commit

Permalink
chore: add SessionCheckoutService and minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
descorp committed Jan 2, 2024
1 parent 2898fe6 commit f4cb062
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<service android:name="com.adyenreactnativesdk.component.dropin.AdyenCheckoutService" android:exported="false" />
<service android:name="com.adyenreactnativesdk.component.dropin.AdvancedCheckoutService" android:exported="false" />
<service android:name="com.adyenreactnativesdk.component.dropin.SessionCheckoutService" android:exported="false" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.adyenreactnativesdk.component

import androidx.lifecycle.lifecycleScope
import com.adyen.checkout.components.core.internal.Configuration
import com.adyen.checkout.dropin.DropInConfiguration
import com.adyen.checkout.redirect.RedirectComponent
Expand All @@ -9,9 +10,7 @@ import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class SessionHelperModule(context: ReactApplicationContext?) : BaseModule(context) {
Expand Down Expand Up @@ -44,8 +43,7 @@ class SessionHelperModule(context: ReactApplicationContext?) : BaseModule(contex
configurationJSON: ReadableMap,
promise: Promise
) {
val myPluginScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
myPluginScope.launch {
appCompatActivity.lifecycleScope.launch(Dispatchers.IO) {
super.createSessionAsync(sessionModelJSON, configurationJSON, promise)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.adyen.checkout.action.core.internal.ActionHandlingComponent
import com.adyen.checkout.components.core.PaymentComponentData
import com.adyen.checkout.components.core.PaymentComponentState
Expand Down Expand Up @@ -46,9 +47,6 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -193,7 +191,7 @@ abstract class BaseModule(context: ReactApplicationContext?) : ReactContextBaseJ
val httpClient = HttpClientFactory.getHttpClient(environment)
val sessionService = SessionService(httpClient)

CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
appCompatActivity.lifecycleScope
.launch {
session?.let {
val request = SessionDetailsRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ sealed class ModuleException(code: String, message: String, cause: Throwable? =
code = "session",
message = "Something went wrong while starting session"
)

class NoActivity : ModuleException(
code = "noActivity",
message = "Launcher not registered. Please call AdyenCheckout.setLauncherActivity() on MainActivity.onCreate()"
)

class WrongFlow : ModuleException(
code = "noActivity",
message = "ViewModel callback is inconsistent"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.adyenreactnativesdk.component.CheckoutProxy.ModuleEventListener
import com.facebook.react.bridge.ReadableMap
import org.json.JSONObject

open class AdyenCheckoutService : DropInService(), ModuleEventListener {
open class AdvancedCheckoutService : DropInService(), ModuleEventListener {

override fun onCreate() {
super.onCreate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ class DropInModule(context: ReactApplicationContext?) : BaseModule(context),
it,
session,
dropInConfiguration,
SessionCheckoutService::class.java
)
}
} ?: throw ModuleException.NoActivity()
} else {

AdyenCheckout.dropInLauncher?.let {
startPayment(
reactApplicationContext,
it,
paymentMethodsResponse,
dropInConfiguration,
AdyenCheckoutService::class.java
AdvancedCheckoutService::class.java
)
}
} ?: throw ModuleException.NoActivity()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.adyenreactnativesdk.component.dropin

import com.adyen.checkout.dropin.SessionDropInService

class SessionCheckoutService() : SessionDropInService() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GooglePayFragment(
configuration,
it
)
}) ?: throw ModuleException.Unknown("ViewModel callback is inconsistent")
}) ?: throw ModuleException.WrongFlow()

this.component = component
AdyenCheckout.setIntentHandler(component)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class InstantFragment(
configuration,
it
)
}) ?: throw ModuleException.Unknown("ViewModel callback is inconsistent")
}) ?: throw ModuleException.WrongFlow()

this.component = component
AdyenCheckout.setIntentHandler(component)
Expand Down

0 comments on commit f4cb062

Please sign in to comment.