Skip to content

Commit

Permalink
Refactored android methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zfurtak committed Dec 19, 2024
1 parent e5c69e9 commit 05149c5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 85 deletions.
2 changes: 0 additions & 2 deletions modules/react-native-wallet/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ def getExtOrIntegerDefault(name) {
}

android {
if (supportsNamespace()) {
namespace "com.wallet"

sourceSets {
main {
manifest.srcFile "src/main/AndroidManifestNew.xml"
}
}
}

compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wallet

import android.R.attr.data
import android.app.Activity
import android.app.Activity.RESULT_CANCELED
import android.app.Activity.RESULT_OK
Expand Down Expand Up @@ -51,57 +50,28 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall
activity: Activity?, requestCode: Int, resultCode: Int, intent: Intent?
) {
if (requestCode == REQUEST_CREATE_WALLET) {
pendingCreateWalletPromise?.let {
if (resultCode == RESULT_OK) {
it.resolve(true);
return
}
it.resolve(false);
}
pendingCreateWalletPromise?.resolve(resultCode == RESULT_OK)
pendingCreateWalletPromise = null
} else if (requestCode == REQUEST_CODE_PUSH_TOKENIZE) {
if (resultCode == RESULT_OK) {
val tokenId: String = intent?.getStringExtra(TapAndPay.EXTRA_ISSUER_TOKEN_ID).toString()
sendEvent(context, "onCardActivated", OnCardActivatedEvent("active", tokenId).toMap())
return
intent?.let{
val tokenId = it.getStringExtra(TapAndPay.EXTRA_ISSUER_TOKEN_ID).toString()
sendEvent(context, "onCardActivated", OnCardActivatedEvent("active", tokenId).toMap())
}
} else if (resultCode == RESULT_CANCELED) {
sendEvent(context, "onCardActivated", OnCardActivatedEvent("cancelled", null).toMap())
return
}
}
return
}

override fun onNewIntent(p0: Intent?) {}
})
}
private fun sendEvent(reactContext: ReactContext, eventName: String, params: WritableMap?) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, params)
}

private fun getCardNetwork(network: String): Int {
return when (network.uppercase(Locale.getDefault())) {
TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA
TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD
else -> 1000
}
}

private fun getTokenServiceProvider(network: String): Int {
return when (network.uppercase(Locale.getDefault())) {
TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA
TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD
else -> 1000
}
}

@ReactMethod
override fun checkWalletAvailability(promise: Promise) {
val localPromise = PromiseImpl({ _ ->
promise.resolve(true)
}, { e ->
}, { _ ->
pendingCreateWalletPromise = promise
tapAndPayClient!!.createWallet(currentActivity!!, REQUEST_CREATE_WALLET)
})
Expand All @@ -118,35 +88,23 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall
platform = "android", deviceID = hardwareId.await(), walletAccountID = walletId.await()
)

val walletDataJson = JSONObject().apply {
put("platform", walletData.platform)
put("deviceID", walletData.deviceID)
put("walletAccountID", walletData.walletAccountID)
val walletDataMap: WritableMap = Arguments.createMap().apply {
putString("platform", walletData.platform)
putString("deviceID", walletData.deviceID)
putString("walletAccountID", walletData.walletAccountID)
}

promise.resolve(walletDataJson.toString())
promise.resolve(walletDataMap)
} catch (e: Exception) {
promise.reject("Error", "Failed to retrieve IDs: ${e.localizedMessage}")
}
}
}

private fun getCardStatusCode(code: Int): Int {
return when (code) {
TapAndPay.TOKEN_STATE_ACTIVE -> CardStatus.ACTIVE.code
TapAndPay.TOKEN_STATE_PENDING -> CardStatus.PENDING.code
TapAndPay.TOKEN_STATE_SUSPENDED -> CardStatus.SUSPENDED.code
TapAndPay.TOKEN_STATE_NEEDS_IDENTITY_VERIFICATION -> CardStatus.REQUIRE_IDENTITY_VERIFICATION.code
TapAndPay.TOKEN_STATE_FELICA_PENDING_PROVISIONING -> CardStatus.PENDING.code
else -> CardStatus.NOT_FOUND_IN_WALLET.code
}
}


@ReactMethod
override fun getCardStatus(last4Digits: String, promise: Promise) {
if (!ensureTapAndPayClientInitialized()) {
promise.reject("Initialization error", "TapAndPay client initialization failed")
if (!ensureTapAndPayClientInitialized(promise)) {
return
}

Expand All @@ -156,13 +114,12 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall
promise.reject("error", "no tokens available")
return@addOnCompleteListener
}
val token = task.result.find { it.fpanLastFour == last4Digits }
token?.let {
task.result.find { it.fpanLastFour == last4Digits }?.let {
Log.i("getCardStatus", "Card Token State: ${it.tokenState}")
promise.resolve(
getCardStatusCode(token.tokenState)
getCardStatusCode(it.tokenState)
)
} ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code)
} ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code)
}
.addOnFailureListener { e -> promise.reject("getCardStatus function failed", e) }
.addOnCanceledListener {
Expand All @@ -175,8 +132,7 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall

@ReactMethod
override fun getCardTokenStatus(tsp: String, tokenRefId: String, promise: Promise) {
if (!ensureTapAndPayClientInitialized()) {
promise.reject("Initialization error", "TapAndPay client initialization failed")
if (!ensureTapAndPayClientInitialized(promise)) {
return
}

Expand All @@ -186,12 +142,11 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall
promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code)
return@addOnCompleteListener
}
val token = task.result
token?.let {
task.result?.let {
promise.resolve(
getCardStatusCode(token.tokenState)
getCardStatusCode(it.tokenState)
)
} ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code)
} ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code)
}
.addOnFailureListener { e -> promise.reject("getCardStatus function failed", e) }
.addOnCanceledListener {
Expand All @@ -207,7 +162,7 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall
override fun addCardToWallet(
data: ReadableMap, promise: Promise
) {
if (!ensureTapAndPayClientInitialized()) return
if (!ensureTapAndPayClientInitialized(promise)) return

try {
val cardData = data.toCardData() ?: return promise.reject("Reject: ", "Insufficient data")
Expand All @@ -227,63 +182,92 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall
.setUserAddress(cardData.userAddress)
.build()

tapAndPayClient?.pushTokenize(
tapAndPayClient!!.pushTokenize(
currentActivity!!, pushTokenizeRequest, REQUEST_CODE_PUSH_TOKENIZE
)

tapAndPayClient?.registerDataChangedListener {
Log.i("DUPA", "TEST")
}
} catch (e: java.lang.Exception) {
promise.reject(e)
}
}

private fun getWalletId(promise: Promise) {
if (!ensureTapAndPayClientInitialized()) {
promise.reject("Initialization error", "TapAndPay client initialization failed")
if (!ensureTapAndPayClientInitialized(promise)) {
return
}
tapAndPayClient?.activeWalletId?.addOnCompleteListener { task ->
tapAndPayClient!!.activeWalletId.addOnCompleteListener { task ->
if (task.isSuccessful) {
val walletId = task.result
if (walletId != null) {
promise.resolve(walletId)
}
}
}?.addOnFailureListener { e ->
}.addOnFailureListener { e ->
promise.reject("Wallet id retrieval failed", e)
}?.addOnCanceledListener {
}.addOnCanceledListener {
promise.reject(
"Reject: ", "Wallet id retrieval canceled"
)
}
}

private fun getHardwareId(promise: Promise) {
if (!ensureTapAndPayClientInitialized()) {
promise.reject("Initialization error", "TapAndPay client initialization failed")
if (!ensureTapAndPayClientInitialized(promise)) {
return
}
tapAndPayClient?.stableHardwareId?.addOnCompleteListener { task ->
tapAndPayClient!!.stableHardwareId.addOnCompleteListener { task ->
if (task.isSuccessful) {
val hardwareId = task.result
promise.resolve(hardwareId)
}
}?.addOnFailureListener { e ->
}.addOnFailureListener { e ->
promise.reject("Stable hardware id retrieval failed", e)
}?.addOnCanceledListener {
}.addOnCanceledListener {
promise.reject(
"Reject: ", "Stable hardware id retrieval canceled"
)
}
}

private fun ensureTapAndPayClientInitialized(): Boolean {
private fun sendEvent(reactContext: ReactContext, eventName: String, params: WritableMap?) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, params)
}

private fun getCardNetwork(network: String): Int {
return when (network.uppercase(Locale.getDefault())) {
TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA
TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD
else -> 1000
}
}

private fun getTokenServiceProvider(network: String): Int {
return when (network.uppercase(Locale.getDefault())) {
TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA
TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD
else -> 1000
}
}

private fun getCardStatusCode(code: Int): Int {
return when (code) {
TapAndPay.TOKEN_STATE_ACTIVE -> CardStatus.ACTIVE.code
TapAndPay.TOKEN_STATE_PENDING -> CardStatus.PENDING.code
TapAndPay.TOKEN_STATE_SUSPENDED -> CardStatus.SUSPENDED.code
TapAndPay.TOKEN_STATE_NEEDS_IDENTITY_VERIFICATION -> CardStatus.REQUIRE_IDENTITY_VERIFICATION.code
TapAndPay.TOKEN_STATE_FELICA_PENDING_PROVISIONING -> CardStatus.PENDING.code
else -> CardStatus.NOT_FOUND_IN_WALLET.code
}
}

private fun ensureTapAndPayClientInitialized(promise: Promise): Boolean {
if (tapAndPayClient == null && currentActivity != null) {
tapAndPayClient = TapAndPay.getClient(currentActivity!!)
}
if (tapAndPayClient == null) {
promise.reject("Initialization error", "TapAndPay client initialization failed")
return false
}
return true
Expand Down
4 changes: 2 additions & 2 deletions modules/react-native-wallet/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion modules/react-native-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
"languages": "kotlin-objc",
"version": "0.43.0"
}
}
}

0 comments on commit 05149c5

Please sign in to comment.