Skip to content

Commit

Permalink
refactor: reorganized android and ios mappings code
Browse files Browse the repository at this point in the history
  • Loading branch information
BreX900 committed Nov 19, 2023
1 parent d7e59bf commit 142c1f9
Show file tree
Hide file tree
Showing 26 changed files with 1,888 additions and 1,793 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import mek.stripeterminal.api.TerminalExceptionCodeApi
import mek.stripeterminal.api.TerminalHandlersApi
import mek.stripeterminal.api.TerminalPlatformApi
import mek.stripeterminal.api.TippingConfigurationApi
import mek.stripeterminal.api.toApi
import mek.stripeterminal.api.toHost
import mek.stripeterminal.api.toPlatformError
import mek.stripeterminal.mappings.toApi
import mek.stripeterminal.mappings.toHost
import mek.stripeterminal.mappings.toPlatformError
import mek.stripeterminal.plugin.DiscoverReadersSubject
import mek.stripeterminal.plugin.ReaderDelegatePlugin
import mek.stripeterminal.plugin.ReaderReconnectionListenerPlugin
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package mek.stripeterminal.mappings

import com.stripe.stripeterminal.external.models.CardDetails
import com.stripe.stripeterminal.external.models.CardNetworks
import com.stripe.stripeterminal.external.models.CardPresentCaptureMethod
import com.stripe.stripeterminal.external.models.CardPresentDetails
import com.stripe.stripeterminal.external.models.CardPresentParameters
import com.stripe.stripeterminal.external.models.CardPresentRoutingOptionParameters
import com.stripe.stripeterminal.external.models.IncrementalAuthorizationStatus
import com.stripe.stripeterminal.external.models.ReceiptDetails
import com.stripe.stripeterminal.external.models.RoutingPriority
import mek.stripeterminal.api.CardBrandApi
import mek.stripeterminal.api.CardDetailsApi
import mek.stripeterminal.api.cardBrandToApi
import mek.stripeterminal.api.fundingToApi
import mek.stripeterminal.api.CardFundingTypeApi
import mek.stripeterminal.api.CardNetworksApi
import mek.stripeterminal.api.CardPresentCaptureMethodApi
import mek.stripeterminal.api.CardPresentDetailsApi
import mek.stripeterminal.api.CardPresentParametersApi
import mek.stripeterminal.api.CardPresentRoutingApi
import mek.stripeterminal.api.IncrementalAuthorizationStatusApi
import mek.stripeterminal.api.ReceiptDetailsApi

fun CardDetails.toApi(): CardDetailsApi {
return CardDetailsApi(
Expand All @@ -15,3 +30,99 @@ fun CardDetails.toApi(): CardDetailsApi {
last4 = last4,
)
}

fun cardBrandToApi(value: String?): CardBrandApi? {
return when (value) {
"amex" -> CardBrandApi.AMEX
"diners" -> CardBrandApi.DINERS_CLUB
"discover" -> CardBrandApi.DISCOVER
"jcb" -> CardBrandApi.JCB
"mastercard" -> CardBrandApi.MASTER_CARD
"unionpay" -> CardBrandApi.UNION_PAY
"visa" -> CardBrandApi.VISA
"unknown" -> null
else -> null
}
}

fun fundingToApi(value: String?): CardFundingTypeApi? {
return when (value) {
"credit" -> CardFundingTypeApi.CREDIT
"debit" -> CardFundingTypeApi.DEBIT
"prepaid" -> CardFundingTypeApi.PREPAID
"unknown" -> null
else -> null
}
}

fun CardPresentDetails.toApi(): CardPresentDetailsApi {
return CardPresentDetailsApi(
brand = cardBrandToApi(brand),
country = country,
expMonth = expMonth.toLong(),
expYear = expYear.toLong(),
funding = fundingToApi(funding),
last4 = last4,
cardholderName = cardholderName,
generatedCard = generatedCard,
receipt = receiptDetails?.toApi(),
emvAuthData = emvAuthData,
networks = networks?.toApi(),
incrementalAuthorizationStatus = incrementalAuthorizationStatus.toApi(),
)
}

fun ReceiptDetails.toApi(): ReceiptDetailsApi {
return ReceiptDetailsApi(
accountType = accountType,
applicationPreferredName = applicationPreferredName!!,
authorizationCode = authorizationCode,
authorizationResponseCode = authorizationResponseCode!!,
applicationCryptogram = applicationCryptogram!!,
dedicatedFileName = dedicatedFileName!!,
transactionStatusInformation = tsi!!,
terminalVerificationResults = tvr!!,
)
}

fun CardNetworks.toApi(): CardNetworksApi {
return CardNetworksApi(
available =
available.map {
cardBrandToApi(it)!!
},
preferred = preferred,
)
}

fun IncrementalAuthorizationStatus.toApi(): IncrementalAuthorizationStatusApi? {
return when (this) {
IncrementalAuthorizationStatus.NOT_SUPPORTED -> IncrementalAuthorizationStatusApi.NOT_SUPPORTED
IncrementalAuthorizationStatus.SUPPORTED -> IncrementalAuthorizationStatusApi.SUPPORTED
IncrementalAuthorizationStatus.UNKNOWN -> null
}
}

// PARAMS

fun CardPresentParametersApi.toHost(): CardPresentParameters {
val b = CardPresentParameters.Builder()
captureMethod?.let { b.setCaptureMethod(it.toHost()) }
requestExtendedAuthorization?.let { b.setRequestExtendedAuthorization(it) }
requestIncrementalAuthorizationSupport?.let { b.setRequestIncrementalAuthorizationSupport(it) }
requestedPriority?.let { b.setRouting(CardPresentRoutingOptionParameters(it.toHost())) }
return b.build()
}

fun CardPresentCaptureMethodApi.toHost(): CardPresentCaptureMethod {
return when (this) {
CardPresentCaptureMethodApi.MANUAL_PREFERRED -> CardPresentCaptureMethod.ManualPreferred
}
}

fun CardPresentRoutingApi.toHost(): RoutingPriority {
return when (this) {
CardPresentRoutingApi.DOMESTIC -> RoutingPriority.DOMESTIC
CardPresentRoutingApi.INTERNATIONAL -> RoutingPriority.INTERNATIONAL
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package mek.stripeterminal.mappings

import com.stripe.stripeterminal.external.models.Charge
import com.stripe.stripeterminal.external.models.PaymentMethodDetails
import mek.stripeterminal.api.ChargeApi
import mek.stripeterminal.api.ChargeStatusApi
import mek.stripeterminal.api.PaymentMethodDetailsApi
import mek.stripeterminal.api.toApi
import mek.stripeterminal.toHashMap

Expand All @@ -26,3 +28,10 @@ fun Charge.toApi(): ChargeApi {
authorizationCode = authorizationCode,
)
}

fun PaymentMethodDetails.toApi(): PaymentMethodDetailsApi {
return PaymentMethodDetailsApi(
cardPresent = cardPresentDetails?.toApi(),
interacPresent = interacPresentDetails?.toApi(),
)
}
Loading

0 comments on commit 142c1f9

Please sign in to comment.