Skip to content

Commit

Permalink
feat: create token from bank account (#591)
Browse files Browse the repository at this point in the history
* chore: create token from bank account

* chre: create token from bankAccount android

* fix: improve error catching in createToken

* refactor: creatToken refactoring

* fix: correct casing for BankAcccountHolderType

* chore: cleanup implementations

* fix: default to 'Individual' bank account type

Co-authored-by: Arkadiusz Kubaczkowski <arek.kubaczkowski@callstak.com>
Co-authored-by: souhe <jakub.klobus@outlook.com>
Co-authored-by: Charles Cruzan <charliecruzan@stripe.com>
  • Loading branch information
4 people authored Feb 9, 2022
1 parent b6bfec8 commit eeeb998
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 112 deletions.
8 changes: 8 additions & 0 deletions android/src/main/java/com/reactnativestripesdk/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ internal fun mapFromBankAccountType(type: BankAccount.Type?): String {
}
}

internal fun mapToBankAccountType(type: String?): BankAccountTokenParams.Type {
return when (type) {
"Company" -> BankAccountTokenParams.Type.Company
"Individual" -> BankAccountTokenParams.Type.Individual
else -> BankAccountTokenParams.Type.Individual
}
}

internal fun mapFromBankAccountStatus(status: BankAccount.Status?): String {
return when (status) {
BankAccount.Status.Errored -> "Errored"
Expand Down
63 changes: 51 additions & 12 deletions android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher
import com.stripe.android.model.*
import com.stripe.android.paymentsheet.PaymentSheetResult
import com.stripe.android.view.AddPaymentMethodActivityStarter
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*

@ReactModule(name = StripeSdkModule.NAME)
class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
Expand Down Expand Up @@ -404,21 +403,60 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ

@ReactMethod
fun createToken(params: ReadableMap, promise: Promise) {
val type = getValOr(params, "type", null)?.let {
if (it != "Card") {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "$it type is not supported yet"))
return
val type = getValOr(params, "type", null)
if (type == null) {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "type parameter is required"))
return
}

when (type) {
"BankAccount" -> {
createTokenFromBankAccount(params, promise)
}
"Card" -> {
createTokenFromCard(params, promise)
}
else -> {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "$type type is not supported yet"))
}
}
val address = getMapOrNull(params, "address")
}

val cardParamsMap = (cardFieldView?.cardParams ?: cardFormView?.cardParams)?.toParamMap() ?: run {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "Card details not complete"))
return
private fun createTokenFromBankAccount(params: ReadableMap, promise: Promise) {
val accountHolderName = getValOr(params, "accountHolderName")
val accountHolderType = getValOr(params, "accountHolderType")
val accountNumber = getValOr(params, "accountNumber", null)
val country = getValOr(params, "country", null)
val currency = getValOr(params, "currency", null)
val routingNumber = getValOr(params, "routingNumber")

runCatching {
val bankAccountParams = BankAccountTokenParams(
country = country!!,
currency = currency!!,
accountNumber = accountNumber!!,
accountHolderName = accountHolderName,
routingNumber = routingNumber,
accountHolderType = mapToBankAccountType(accountHolderType)
)
CoroutineScope(Dispatchers.IO).launch {
val token = stripe.createBankAccountToken(bankAccountParams, null, stripeAccountId)
promise.resolve(createResult("token", mapFromToken(token)))
}
}.onFailure {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), it.message))
}
}

val cardAddress = cardFieldView?.cardAddress ?: cardFormView?.cardAddress
private fun createTokenFromCard(params: ReadableMap, promise: Promise) {
val cardParamsMap = (cardFieldView?.cardParams ?: cardFormView?.cardParams)?.toParamMap()
?: run {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "Card details not complete"))
return
}

val cardAddress = cardFieldView?.cardAddress ?: cardFormView?.cardAddress
val address = getMapOrNull(params, "address")
val cardParams = CardParams(
number = cardParamsMap["number"] as String,
expMonth = cardParamsMap["exp_month"] as Int,
Expand All @@ -427,7 +465,8 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
address = mapToAddress(address, cardAddress),
name = getValOr(params, "name", null)
)
runBlocking {

CoroutineScope(Dispatchers.IO).launch {
try {
val token = stripe.createCardToken(
cardParams = cardParams,
Expand Down
Loading

0 comments on commit eeeb998

Please sign in to comment.