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 2 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 -> BankAccountTokenParams.Type.Individual
souhe marked this conversation as resolved.
Show resolved Hide resolved
}
}

internal fun mapFromBankAccountStatus(status: BankAccount.Status?): String {
return when (status) {
BankAccount.Status.Errored -> "Errored"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 +404,50 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ

@ReactMethod
fun createToken(params: ReadableMap, promise: Promise) {
val type = getValOr(params, "type", null)?.let {
if (it != "Card") {
val type = getValOr(params, "type", null)
souhe marked this conversation as resolved.
Show resolved Hide resolved
type?.let {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use:
when(type){ "BankAccount" -> {...} null -> {...} }

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good idea. I just refactored this function into a big when expression

if (it != "Card" && it != "BankAccount") {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), "$it type is not supported yet"))
return
}
}
val address = getMapOrNull(params, "address")

// TODO: create a service for creating tokens from a difference sources.
souhe marked this conversation as resolved.
Show resolved Hide resolved
if (type == "BankAccount") {
val accountHolderName = getValOr(params, "accountHolderName")
val accountHolderType = getValOr(params, "accountHolderType")
val accountNumber = getValOr(params, "accountNumber")
val country = getValOr(params, "country")
val currency = getValOr(params, "currency")
souhe marked this conversation as resolved.
Show resolved Hide resolved
val routingNumber = getValOr(params, "routingNumber")

val bankAccountParams = BankAccountTokenParams(
country = country!!,
currency = currency!!,
accountNumber = accountNumber!!,
accountHolderName = accountHolderName,
routingNumber = routingNumber,
accountHolderType = mapToBankAccountType(accountHolderType)
)

runBlocking {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this done on a background thread?

Copy link
Collaborator

Choose a reason for hiding this comment

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

As I just take it over from Arek I'm not sure I get the comment @michelleb-stripe. runBlocking is needed here because without it we need an error Suspend function 'createBankAccountToken' should be called only from a coroutine or another suspend function

Copy link
Contributor

Choose a reason for hiding this comment

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

@souhe When the ReactMethod is called is it being called from a UI thread. The reason the routine is marked as suspend is to help indicate that it should not be called from a UI thread.

try {
souhe marked this conversation as resolved.
Show resolved Hide resolved
val token = stripe.createBankAccountToken(bankAccountParams, null, stripeAccountId)
promise.resolve(createResult("token", mapFromToken(token)))
} catch (e: Exception) {
promise.resolve(createError(CreateTokenErrorType.Failed.toString(), e.message))
}
}
return
}

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 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
42 changes: 38 additions & 4 deletions ios/StripeSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -532,19 +532,53 @@ 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 (type != "Card" && type != "BankAccount") {
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, type + " type is not supported yet"))
return
}

if let type = params["type"] as? String {
if (type != "Card") {
resolve(Errors.createError(CreateTokenErrorType.Failed.rawValue, type + " type is not supported yet"))
// TODO: create a service for creating tokens from a difference sources.
if (type == "BankAccount") {
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
}

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))
}
}
return
}


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 BankAcccountHolerType = 'company' | 'individual';
souhe marked this conversation as resolved.
Show resolved Hide resolved

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