Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create token from bank account #591

Merged
merged 7 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -> null
}
}

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)
souhe marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 11 additions & 0 deletions ios/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ class Mappers {
return nil
}

class func mapToBankAccountHolderType(_ type: String?) -> STPBankAccountHolderType? {
if let type = type {
switch type {
case "Company": return STPBankAccountHolderType.company
case "Individual": return STPBankAccountHolderType.individual
default: return nil
}
}
return nil
}

class func mapFromBankAccountStatus(_ status: STPBankAccountStatus?) -> String? {
if let status = status {
switch status {
Expand Down
56 changes: 51 additions & 5 deletions ios/StripeSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -532,19 +532,65 @@ class StripeSdk: RCTEventEmitter, STPApplePayContextDelegate, STPBankSelectionVi
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
let address = params["address"] as? NSDictionary
guard let type = params["type"] as? String else {
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, "type parameter is required"))
return
}

if let type = params["type"] as? String {
if (type != "Card") {
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, type + " type is not supported yet"))
}
// TODO: Consider moving this to its own class when more types are supported.
switch type {
case "BankAccount":
createTokenFromBankAccount(params: params, resolver: resolve, rejecter: reject)
case "Card":
createTokenFromCard(params: params, resolver: resolve, rejecter: reject)
default:
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, type + " type is not supported yet"))
}
}

func createTokenFromBankAccount(
params: NSDictionary,
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
let accountHolderName = params["accountHolderName"] as? String
let accountHolderType = params["accountHolderType"] as? String
let accountNumber = params["accountNumber"] as? String
let country = params["country"] as? String
let currency = params["currency"] as? String
let routingNumber = params["routingNumber"] as? String

let bankAccountParams = STPBankAccountParams()
bankAccountParams.accountHolderName = accountHolderName
bankAccountParams.accountNumber = accountNumber
bankAccountParams.country = country
bankAccountParams.currency = currency
bankAccountParams.routingNumber = routingNumber

if let holderType = Mappers.mapToBankAccountHolderType(accountHolderType) {
bankAccountParams.accountHolderType = holderType

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charliecruzan-stripe .accountHolderType defaults to .individual on iOS. What does it default to on Android? Do we want consistent defaults across for both platforms?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in bf363b8

}

STPAPIClient.shared.createToken(withBankAccount: bankAccountParams) { token, error in
if let token = token {
resolve(Mappers.createResult("token", Mappers.mapFromToken(token: token)))
} else {
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, error?.localizedDescription))
}
}
}

func createTokenFromCard(
params: NSDictionary,
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
guard let cardParams = cardFieldView?.cardParams ?? cardFormView?.cardParams else {
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, "Card details not complete"))
return
}

let address = params["address"] as? NSDictionary
let cardSourceParams = STPCardParams()
cardSourceParams.number = cardParams.number
cardSourceParams.cvc = cardParams.cvc
Expand Down
4 changes: 2 additions & 2 deletions src/NativeStripeSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import type {
InitPaymentSheetResult,
PresentPaymentSheetResult,
ConfirmPaymentSheetPaymentResult,
Card,
ApplePayResult,
CreateTokenResult,
GooglePayInitResult,
PayWithGooglePayResult,
CreateGooglePayPaymentMethodResult,
GooglePay,
OpenApplePaySetupResult,
CreateTokenParams,
} from './types';

type NativeStripeSdkType = {
Expand Down Expand Up @@ -65,7 +65,7 @@ type NativeStripeSdkType = {
confirmPaymentSheetPayment(): Promise<ConfirmPaymentSheetPaymentResult>;
createTokenForCVCUpdate(cvc: string): Promise<CreateTokenForCVCUpdateResult>;
handleURLCallback(url: string): Promise<boolean>;
createToken(params: Card.CreateTokenParams): Promise<CreateTokenResult>;
createToken(params: CreateTokenParams): Promise<CreateTokenResult>;
initGooglePay(params: GooglePay.InitParams): Promise<GooglePayInitResult>;
presentGooglePay(
params: GooglePay.PresentGooglePayParams
Expand Down
4 changes: 2 additions & 2 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
GooglePay,
CreateGooglePayPaymentMethodResult,
OpenApplePaySetupResult,
CreateTokenParams,
} from './types';
import type { Card } from './types/Card';

const APPLE_PAY_NOT_SUPPORTED_MESSAGE =
'Apple pay is not supported on this device';
Expand Down Expand Up @@ -55,7 +55,7 @@ export const createPaymentMethod = async (
};

export const createToken = async (
params: Card.CreateTokenParams
params: CreateTokenParams
): Promise<CreateTokenResult> => {
try {
const { token, error } = await NativeStripeSdk.createToken(params);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useStripe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import type {
ConfirmPaymentSheetPaymentResult,
ConfirmSetupIntent,
CreateTokenResult,
Card,
PayWithGooglePayResult,
GooglePayInitResult,
GooglePay,
CreateGooglePayPaymentMethodResult,
OpenApplePaySetupResult,
CreateTokenParams,
} from '../types';
import { useCallback, useEffect, useState } from 'react';
import { isiOS } from '../helpers';
Expand Down Expand Up @@ -78,7 +78,7 @@ export function useStripe() {
);

const _createToken = useCallback(
async (params: Card.CreateTokenParams): Promise<CreateTokenResult> => {
async (params: CreateTokenParams): Promise<CreateTokenResult> => {
return createToken(params);
},
[]
Expand Down
6 changes: 0 additions & 6 deletions src/types/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,4 @@ export namespace Card {
postalCode?: string;
state?: string;
}

export interface CreateTokenParams {
type: 'Account' | 'BankAccount' | 'Card' | 'CvcUpdate' | 'Person' | 'Pii';
address?: Address;
name?: string;
}
}
22 changes: 22 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,25 @@ export type OpenApplePaySetupResult =
| {
error: StripeError<ApplePayError>;
};

export type CreateTokenParams =
| CreateTokenCardParams
| CreateTokenBankAccountParams;

export type CreateTokenCardParams = {
type: 'Card';
address?: Card.Address;
name?: string;
};

export type BankAcccountHolderType = 'Company' | 'Individual';

export type CreateTokenBankAccountParams = {
type: 'BankAccount';
accountHolderName?: string;
accountHolderType?: BankAcccountHolderType;
accountNumber: string;
country: string;
currency: string;
routingNumber?: string;
};