Skip to content

Commit

Permalink
Support collect inputs (#633)
Browse files Browse the repository at this point in the history
* support collect inputs.

* fix lint and update jest snapshot.

* revert wildcard imports & lint.

* optimize input definitions & typo.

---------

Co-authored-by: lam161006 <[email protected]>
  • Loading branch information
ianlin-bbpos and lam161006 authored Mar 19, 2024
1 parent e4f4efc commit ff71b70
Show file tree
Hide file tree
Showing 35 changed files with 1,062 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ enum class DiscoveryMethod {
INTERNET,
LOCAL_MOBILE,
HANDOFF,
USB,
USB
}
9 changes: 6 additions & 3 deletions android/src/main/java/com/stripeterminalreactnative/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import kotlin.jvm.Throws
internal fun createError(throwable: Throwable): ReadableMap = nativeMapOf { putError(throwable) }

internal fun WritableMap.putError(throwable: Throwable): ReadableMap = apply {
putMap("error", nativeMapOf {
putErrorContents(throwable)
})
putMap(
"error",
nativeMapOf {
putErrorContents(throwable)
}
)
}

private fun WritableMap.putErrorContents(throwable: Throwable?) {
Expand Down
76 changes: 69 additions & 7 deletions android/src/main/java/com/stripeterminalreactnative/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,51 @@ import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeArray
import com.stripe.stripeterminal.external.CollectInputs
import com.stripe.stripeterminal.external.models.Address
import com.stripe.stripeterminal.external.models.CardDetails
import com.stripe.stripeterminal.external.models.CardPresentDetails
import com.stripe.stripeterminal.external.models.CartLineItem
import com.stripe.stripeterminal.external.models.Charge
import com.stripe.stripeterminal.external.models.CollectInputsResult
import com.stripe.stripeterminal.external.models.ConnectionStatus
import com.stripe.stripeterminal.external.models.DeviceType
import com.stripe.stripeterminal.external.models.DisconnectReason
import com.stripe.stripeterminal.external.models.EmailResult
import com.stripe.stripeterminal.external.models.Location
import com.stripe.stripeterminal.external.models.LocationStatus
import com.stripe.stripeterminal.external.models.NetworkStatus
import com.stripe.stripeterminal.external.models.NumericResult
import com.stripe.stripeterminal.external.models.OfflineStatus
import com.stripe.stripeterminal.external.models.PaymentIntent
import com.stripe.stripeterminal.external.models.PaymentIntentStatus
import com.stripe.stripeterminal.external.models.PaymentMethod
import com.stripe.stripeterminal.external.models.PaymentMethodDetails
import com.stripe.stripeterminal.external.models.PaymentMethodType
import com.stripe.stripeterminal.external.models.PaymentStatus
import com.stripe.stripeterminal.external.models.PhoneResult
import com.stripe.stripeterminal.external.models.Reader
import com.stripe.stripeterminal.external.models.ReaderAccessibility
import com.stripe.stripeterminal.external.models.ReaderDisplayMessage
import com.stripe.stripeterminal.external.models.ReaderEvent
import com.stripe.stripeterminal.external.models.ReaderInputOptions
import com.stripe.stripeterminal.external.models.ReaderInputOptions.ReaderInputOption
import com.stripe.stripeterminal.external.models.ReaderAccessibility
import com.stripe.stripeterminal.external.models.ReaderSettings
import com.stripe.stripeterminal.external.models.ReaderSoftwareUpdate
import com.stripe.stripeterminal.external.models.ReaderTextToSpeechStatus
import com.stripe.stripeterminal.external.models.ReceiptDetails
import com.stripe.stripeterminal.external.models.Refund
import com.stripe.stripeterminal.external.models.SelectionResult
import com.stripe.stripeterminal.external.models.SetupAttempt
import com.stripe.stripeterminal.external.models.SetupAttemptStatus
import com.stripe.stripeterminal.external.models.SetupIntent
import com.stripe.stripeterminal.external.models.SetupIntentCardPresentDetails
import com.stripe.stripeterminal.external.models.SetupIntentPaymentMethodDetails
import com.stripe.stripeterminal.external.models.SetupIntentStatus
import com.stripe.stripeterminal.external.models.SetupIntentUsage
import com.stripe.stripeterminal.external.models.SignatureResult
import com.stripe.stripeterminal.external.models.SimulateReaderUpdate
import com.stripe.stripeterminal.external.models.TextResult
import com.stripe.stripeterminal.external.models.Wallet
import com.stripe.stripeterminal.log.LogLevel

Expand Down Expand Up @@ -425,11 +433,14 @@ internal fun mapFromRefund(refund: Refund): ReadableMap = nativeMapOf {
putString("failureBalanceTransaction", refund.failureBalanceTransaction)
putString("failureReason", refund.failureReason)
putString("id", refund.id)
putMap("metadata", nativeMapOf {
refund.metadata?.map {
putString(it.key, it.value)
putMap(
"metadata",
nativeMapOf {
refund.metadata?.map {
putString(it.key, it.value)
}
}
})
)
putString("paymentIntentId", refund.paymentIntentId)
putMap("paymentMethodDetails", mapFromPaymentMethodDetails(refund.paymentMethodDetails))
putString("reason", refund.reason)
Expand Down Expand Up @@ -500,7 +511,10 @@ private fun mapFromCardPresentDetails(cardPresentDetails: CardPresentDetails?):
putString("network", cardPresentDetails?.network)
putString("description", cardPresentDetails?.description)
putMap("wallet", mapFromWallet(cardPresentDetails?.wallet))
putArray("preferredLocales", convertListToReadableArray(cardPresentDetails?.preferredLocales))
putArray(
"preferredLocales",
convertListToReadableArray(cardPresentDetails?.preferredLocales)
)
}

internal fun mapFromWallet(wallet: Wallet?): ReadableMap =
Expand All @@ -509,7 +523,9 @@ internal fun mapFromWallet(wallet: Wallet?): ReadableMap =
}

private fun convertListToReadableArray(list: List<String>?): ReadableArray? {
return list?.let { WritableNativeArray().apply { for (item in list) { pushString(item) } } } ?: null
return list?.let {
WritableNativeArray().apply { for (item in list) { pushString(item) } }
} ?: null
}

fun mapFromReceiptDetails(receiptDetails: ReceiptDetails?): ReadableMap =
Expand Down Expand Up @@ -598,3 +614,49 @@ internal fun mapFromReaderSettings(settings: ReaderSettings): ReadableMap {
}
}
}

@OptIn(CollectInputs::class)
fun mapFromCollectInputsResults(results: List<CollectInputsResult>): ReadableArray {
return nativeArrayOf {
results.forEach {
when (it) {
is EmailResult -> pushMap(
nativeMapOf {
putBoolean("skipped", it.skipped)
putString("email", it.email)
}
)
is NumericResult -> pushMap(
nativeMapOf {
putBoolean("skipped", it.skipped)
putString("numericString", it.numericString)
}
)
is PhoneResult -> pushMap(
nativeMapOf {
putBoolean("skipped", it.skipped)
putString("phone", it.phone)
}
)
is SelectionResult -> pushMap(
nativeMapOf {
putBoolean("skipped", it.skipped)
putString("selection", it.selection)
}
)
is SignatureResult -> pushMap(
nativeMapOf {
putBoolean("skipped", it.skipped)
putString("signatureSvg", it.signatureSvg)
}
)
is TextResult -> pushMap(
nativeMapOf {
putBoolean("skipped", it.skipped)
putString("text", it.text)
}
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ internal object ReactExtensions {

fun ReactApplicationContext.sendEvent(
eventName: String,
resultBuilder: (WritableMap.() -> Unit)? = null,
resultBuilder: (WritableMap.() -> Unit)? = null
) {
getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, resultBuilder?.let {
nativeMapOf {
it()
.emit(
eventName,
resultBuilder?.let {
nativeMapOf {
it()
}
}
})
)
}
}
Loading

0 comments on commit ff71b70

Please sign in to comment.