Created
April 24, 2023 22:47
-
-
Save filpgame/111f87411e8b8f205b55a155f62d16fa to your computer and use it in GitHub Desktop.
Class structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionController(context: Context, private val tapSession: TapSession): SessionControllerInterface { | |
private val activationRepository = ActivationRepository(context) | |
private val logger = LoggerFactory.getLogger(TAG) | |
internal var activationData: ActivationData? = null | |
override suspend fun activate( | |
clientId: String, | |
clientSecret: String | |
): StoneStatus { | |
logger.trace("activate()") | |
val response = activationRepository.activate( | |
tapSession.vendorName, | |
clientId, | |
clientSecret | |
) | |
return try { | |
if (response.isSuccess) { | |
val data = response.getOrThrow() | |
activationData = data | |
return tapSession.activate(data) | |
} else { | |
logger.error("Request failed: ", response.exceptionOrNull()) | |
return ActivationErrorFamily.FAILED_ACTIVATE_TERMINAL | |
} | |
} catch (e: Throwable) { | |
logger.error("Failed to activate", e) | |
ActivationErrorFamily.FAILED_ACTIVATE_TERMINAL | |
}.also { | |
logger.trace("activate = {}", it) | |
} | |
} | |
override fun deactivate() { | |
logger.trace("deactivate()") | |
return tapSession.closeSession().also { logger.trace("deactivate") } | |
} | |
companion object { | |
const val TAG = "SessionController" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stone.tapsdklib | |
import android.annotation.SuppressLint | |
import android.content.Context | |
import co.stone.commons.NotInitializedException | |
import co.stone.commons.api.TapSdkInterface | |
import co.stone.commons.api.SessionControllerInterface | |
import co.stone.commons.api.TransactionControllerInterface | |
import com.stone.tappcilib.PciLib | |
import com.stone.tappcilib.SessionController | |
import com.stone.tappcilib.TransactionController | |
import com.stone.tappcimpp.TapPciMpp | |
import org.slf4j.LoggerFactory | |
@SuppressLint("StaticFieldLeak") | |
object TapSdk : TapSdkInterface { | |
private const val TAG = "TapSdk" | |
private val logger = LoggerFactory.getLogger(TAG) | |
private lateinit var pciLib: PciLib | |
private lateinit var transactionController: TransactionControllerInterface | |
private lateinit var sessionController: SessionControllerInterface | |
private fun selectPciLib(context: Context): PciLib { | |
logger.trace("selectPciLib(context = {})", context) | |
return TapPciMpp(context).also { //TODO: Add some factory to select dynamically which provider | |
logger.trace("selectPciLib = {}", it) | |
} | |
} | |
override fun transactionController(): TransactionControllerInterface { | |
logger.trace("transactionController()") | |
if (::pciLib.isInitialized.not()) throw NotInitializedException() | |
return transactionController.also { logger.trace("transactionController = {}", it) } | |
} | |
override fun sessionController(): SessionControllerInterface { | |
logger.trace("sessionController()") | |
if (::pciLib.isInitialized.not()) throw NotInitializedException() | |
return sessionController.also { logger.trace("sessionController = {}", it) } | |
} | |
internal fun initialize(context: Context) { | |
logger.trace("initialize(context = {})", context) | |
pciLib = selectPciLib(context) | |
pciLib.initialize(BuildConfig.VERSION_NAME) | |
sessionController = SessionController(context, pciLib.provideTapSession()) | |
transactionController = TransactionController(pciLib.provideTapTransaction()) | |
logger.trace("initialize") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TransactionController(private val tapTransaction: TapTransaction) : TransactionControllerInterface { | |
override suspend fun openTransactionSession(): StoneStatus { | |
return tapTransaction.openSession() | |
} | |
override fun startTransaction( | |
amount: Int, | |
paymentMethod: PaymentMethod, | |
installments: Int, | |
installmentsType: InstallmentsType, | |
operationType: OperationType, | |
softDescriptor: String, | |
): Flow<TransactionEvent> { | |
val installmentsTypeValue = if (installments > 1) installmentsType else InstallmentsType.NONE | |
return tapTransaction.startTransaction(amount, paymentMethod, installments, installmentsTypeValue, operationType, softDescriptor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment